NAME
SPOPS::Key::DBI::HandleField -- Retrieve an auto-increment value from a DBI statement or database handle
SYNOPSIS
# In your SPOPS configuration
$spops = { 'myspops' => { 'isa' => [ qw/ SPOPS::Key::DBI::HandleField SPOPS::DBI / ], 'handle_field' => 'mysql_insertid', ... }, };
# Note: Other classes (such as 'SPOPS::DBI::MySQL') use this class # without requiring you to specify the class or any of its # configuration information.
DESCRIPTION
This class simply reads an ID value from a statement or database handle using the specified key. The value will generally represent the unique ID of the row just inserted and was presumably retrieved by the DBD library, which made it available by a particular key.
Currently, this is only known to work with the MySQL database and DBD::mysql. MySQL supports auto-incrementing fields using the keyword 'AUTO_INCREMENT', such as:
CREATE TABLE mytable ( myid INT NOT NULL AUTO_INCREMENT, ... )
With every INSERT into this table, the database will provide a guaranteed-unique value for 'myid' if one is not specified in the INSERT. Rather than forcing you to run a SELECT against the table to find out the value of the unique key, the MySQL client libraries provide (and DBD::mysql supports) the value of the field for you.
With MySQL, this is available through the 'mysql_insertid' key of the DBI database handle. (It is also currently available via the statement handle using the same name, but this may go away in the future.)
So if you were using straight DBI methods, a simplified example of doing this same action would be (using MySQL):
my $dbh = DBI->connect( 'DBI:mysql:test', ... ); my $sql = "INSERT INTO mytable ( name ) VALUES ( 'european swallow' )"; my $rv = $dbh->do( $sql ); print "ID of just-inserted record: $dbh->{mysql_insertid}\n";
METHODS
post_fetch_id()
Retrieve the just-inserted value from a key in the handle, as described above.
BUGS
None known.
TO DO
Nothing known.
SEE ALSO
COPYRIGHT
Copyright (c) 2001-2004 intes.net, inc.. All rights reserved.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
AUTHORS
Chris Winters <chris@cwinters.com>
Generated from the SPOPS 0.87 source.