SPOPS::Key::DBI::HandleField - Retrieve an auto-increment value from a DBI statement or database handle


SPOPS documentation  | view source Contained in the SPOPS distribution.

Index


NAME

Top

SPOPS::Key::DBI::HandleField -- Retrieve an auto-increment value from a DBI statement or database handle

SYNOPSIS

Top

 # 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

Top

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

Top

post_fetch_id()

Retrieve the just-inserted value from a key in the handle, as described above.

BUGS

Top

None known.

TO DO

Top

Nothing known.

SEE ALSO

Top

DBD::mysql

DBI

COPYRIGHT

Top

AUTHORS

Top

Chris Winters <chris@cwinters.com>


SPOPS documentation  | view source Contained in the SPOPS distribution.