On Mon, Jun 23, 2003 at 05:16:51PM +0100, Dominic Mitchell wrote: > >Note that this is quite safe and the value returned is the current value > >for YOUR session only; so no need to worry about someone else inserting > >something or other such race conditions. :) > > Doesn't that depend on whether you're in a transaction or not? I would have > thought that you're liable to have it updated if you've got AutoCommit > turned on.
No, it's still correct even outside of sequences. the current value returned is always the value for your session. > I don't know this, I'm just making assumptions about how sequences work. :-) tut tut ;) > ># Returns the last ID resulting from an INSERT command > >sub last_inserted_id > >{ > > my $self = shift; > > my $table = shift; #should be safe, but check anyway > > if ($table =~ /(\w[\w\d\_]+)/) { > > $table = $1; > > } > > else { > > die("LBHF.pm/last_inserted_id: Invalid table name: $table\n"); > > } > > my $query = $self->{db}->prepare("SELECT currval('" . $table . > > "_id_seq')"); $query->execute > > Hmmm, big assumptions about sequence names here. I merely point this > out because I'm continually getting annoyed at postgres for truncating > min to 32 chars. This is because I include the table name in my primary > key column name though... True, but (currently) that is the way Postgresql generates sequence names from serials. Using this assumption works well enough for my internal code. tjc