end0tknr's kipple - web写経開発

太宰府天満宮の狛犬って、妙にカワイイ

Data::Uniqidによるunique idの生成

これまで掲示板の記事idのようにユニークなidを取得したい場合、postgresを使用したアプリケーションでは、bigserial型を使用していました。

CREATE TABLE input_item_edit (
edit_id      bigserial   primary key,  --記事id
content      text,                     --記事
update_user  varchar(32),              --更新者
update_time  timestamp                 --更新日日時
);
sub get_new_edit_id {
    my ($self) = @_;
    my $sql = "select nextval('input_item_edit_edit_id_seq')";
    my $sth = $self->{dbh}->prepare($sql);
    $sth->execute();
    return $sth->fetchrow_array();
}

ところがData::Uniqidというモジュールを発見しました。
cpanのdescriptionによれば、作成されるunique idのタイプは次の通り。

  suinqid
    genrates a very short id valid only 
    for the localhost and with a liftime of 1 day  
  uniqid
    generates a short id valid on the local host 
  luniqid 
    generates a long id valid everywhere and ever

このうち luniqid では Sys::Hostnameのgethostbyname(hostname()) を使用している為、私のcolinux 環境では、make testで失敗しました。

Data::Uniqidであれば、dbがpostgres以外にも使えるので、今後は使ってもいいかもしれませんね。