… | |
… | |
195 | |
195 | |
196 | =cut |
196 | =cut |
197 | |
197 | |
198 | =item $lastid = sql_insertid $sth |
198 | =item $lastid = sql_insertid $sth |
199 | |
199 | |
200 | Returns the last automatically created key value (e.g. for mysql |
200 | Returns the last automatically created key value. It must be executed |
201 | AUTO_INCREMENT or sybase IDENTITY fields). It must be executed directly |
|
|
202 | after executing the insert statement that created it. |
201 | directly after executing the insert statement that created it. This is |
|
|
202 | what is actually returned for various databases. If your database is |
|
|
203 | missing, please send me an e-mail on how to implement this ;) |
|
|
204 | |
|
|
205 | mysql: first C<AUTO_INCREMENT> column set to NULL |
|
|
206 | postgres: C<oid> column (is there a way to get the last SERIAL?) |
|
|
207 | sybase: C<IDENTITY> column of the last insert (slow) |
|
|
208 | informix: C<SERIAL> or C<SERIAL8> column of the last insert |
|
|
209 | |
|
|
210 | Except for sybase, this does not require a server access. |
203 | |
211 | |
204 | =cut |
212 | =cut |
205 | |
213 | |
206 | sub sql_insertid($) { |
214 | sub sql_insertid($) { |
207 | my $sth = shift or die "sql_insertid requires a statement handle"; |
215 | my $sth = shift or die "sql_insertid requires a statement handle"; |
208 | my $dbh = $sth->{Database}; |
216 | my $dbh = $sth->{Database}; |
209 | my $driver = $dbh->{Driver}{Name}; |
217 | my $driver = $dbh->{Driver}{Name}; |
210 | |
218 | |
211 | $driver eq "mysql" and return $sth->{mysql_insertid}; |
219 | $driver eq "mysql" and return $sth->{mysql_insertid}; |
|
|
220 | $driver eq "Pg" and return $sth->{pg_oid_status}; |
212 | $driver eq "Sybase" and return sql_fetch($dbh, 'SELECT @@IDENTITY'); |
221 | $driver eq "Sybase" and return sql_fetch($dbh, 'SELECT @@IDENTITY'); |
213 | $driver eq "Informix" and return $sth->{ix_sqlerrd}[1]; |
222 | $driver eq "Informix" and return $sth->{ix_sqlerrd}[1]; |
214 | |
223 | |
215 | die "sql_insertid does not spport the dbd driver '$driver', please see PApp::SQL::sql_insertid"; |
224 | die "sql_insertid does not spport the dbd driver '$driver', please see PApp::SQL::sql_insertid"; |
216 | } |
225 | } |
217 | |
226 | |