SPOPS::Key::DBI::Identity - Retrieve IDENTITY values from a supported DBI database


SPOPS documentation Contained in the SPOPS distribution.

Index


Code Index:

NAME

Top

SPOPS::Key::DBI::Identity -- Retrieve IDENTITY values from a supported DBI database

SYNOPSIS

Top

 # In your SPOPS configuration
 $spops  = {
   'myspops' => {
       'isa' => [ qw/ SPOPS::Key::DBI::Identity  SPOPS::DBI / ],
       ...
   },
 };

DESCRIPTION

Top

This class enables a just-created object to the IDENTITY value returned by its last insert. Of course, this only works if you have an IDENTITY field in your table, such as:

 CREATE TABLE my_table (
   id    NUMERIC( 8, 0 ) IDENTITY NOT NULL,
   ...
 )

This method is typically used in Sybase and Microsoft SQL Server databases. The client library (Open Client, FreeTDS, ODBC) should not make a difference to this module since we perform a SELECT statement to retrieve the value rather than relying on a property of the database/statement handle.

METHODS

Top

post_fetch_id()

Retrieve the IDENTITY value after inserting a row.

BUGS

Top

None known.

TO DO

Top

Nothing known.

SEE ALSO

Top

DBD::Sybase

DBI

COPYRIGHT

Top

AUTHORS

Top

Chris Winters <chris@cwinters.com>

See the SPOPS module for the full author list.


SPOPS documentation Contained in the SPOPS distribution.

package SPOPS::Key::DBI::Identity;

# $Id: Identity.pm,v 3.4 2004/06/02 00:48:23 lachoy Exp $

use strict;
use Log::Log4perl qw( get_logger );
use SPOPS;

my $log = get_logger();

$SPOPS::Key::DBI::Identity::VERSION  = sprintf("%d.%02d", q$Revision: 3.4 $ =~ /(\d+)\.(\d+)/);

# Ensure only POST_fetch_id used

sub pre_fetch_id  { return undef }


# Retrieve the IDENTITY value

sub post_fetch_id {
    my ( $self, $p ) = @_;
    eval { $p->{statement}->finish };
    my $sql = 'SELECT @@IDENTITY';
    my ( $sth );
    eval {
        $sth = $p->{db}->prepare( $sql );
        $sth->execute;
    };
    if ( $@ ) {
        SPOPS::Exception::DBI->throw( "Cannot retrieve \@\@IDENTITY value: $@",
                                      { sql => $sql, action => 'post_fetch_id' } );
    }
    my $row = $sth->fetchrow_arrayref;
    $log->is_info &&
        $log->info( "Found inserted ID ($row->[0])" );
    return $row->[0];
}

1;

__END__