--- PApp-SQL/SQL.pm 2000/11/01 03:22:07 1.2 +++ PApp-SQL/SQL.pm 2000/11/05 01:54:38 1.3 @@ -188,9 +188,24 @@ =cut -# uncodumented, since unportable (only works with DBH even!). yet it is exported (aaargh!) -sub sql_insertid { - $DBH->{mysql_insertid}; +=item $lastid = sql_insertid $sth + +Returns the last automatically created key value (e.g. for mysql +AUTO_INCREMENT or sybase IDENTITY fields). It must be executed directly +after executing the insert statement that created it. + +=cut + +sub sql_insertid($) { + my $sth = shift or die "sql_insertid requires a statement handle"; + my $dbh = $sth->{Database}; + my $driver = $dbh->{Driver}{Name}; + + $driver eq "mysql" and return $sth->{mysql_insertid}; + $driver eq "Sybase" and return sql_fetch($dbh, 'SELECT @@IDENTITY'); + $driver eq "Informix" and return $sth->{ix_sqlerrd}[1]; + + die "sql_insertid does not spport the dbd driver '$driver', please see PApp::SQL::sql_insertid"; } =item [old-size] = cachesize [new-size]