… | |
… | |
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 | |
… | |
… | |
232 | |
241 | |
233 | =cut |
242 | =cut |
234 | |
243 | |
235 | =item reinitialize [not exported] |
244 | =item reinitialize [not exported] |
236 | |
245 | |
237 | Clears any internal caches (statement cache, database handle cache). |
246 | Clears any internal caches (statement cache, database handle |
|
|
247 | cache). Should be called after C<fork> and other accidents that invalidate |
|
|
248 | database handles. |
238 | |
249 | |
239 | =cut |
250 | =cut |
240 | |
251 | |
241 | sub reinitialize { |
252 | sub reinitialize { |
242 | cachesize cachesize 0; |
253 | cachesize cachesize 0; |
… | |
… | |
310 | |
321 | |
311 | =cut |
322 | =cut |
312 | |
323 | |
313 | sub dsn($) { |
324 | sub dsn($) { |
314 | my $self = shift; |
325 | my $self = shift; |
315 | $self->[1][1]; |
326 | (split /\x00/, $self->[0])[1]; |
316 | } |
327 | } |
317 | |
328 | |
318 | =back |
329 | =back |
319 | |
330 | |
320 | =cut |
331 | =cut |