1package SPOPS::Key::DBI::Identity;
2
3# $Id: Identity.pm,v 3.4 2004/06/02 00:48:23 lachoy Exp $
4
5use strict;
6use Log::Log4perl qw( get_logger );
7use SPOPS;
8
9my $log = get_logger();
10
11$SPOPS::Key::DBI::Identity::VERSION  = sprintf("%d.%02d", q$Revision: 3.4 $ =~ /(\d+)\.(\d+)/);
12
13# Ensure only POST_fetch_id used
14
15sub pre_fetch_id  { return undef }
16
17
18# Retrieve the IDENTITY value
19
20sub post_fetch_id {
21    my ( $self, $p ) = @_;
22    eval { $p->{statement}->finish };
23    my $sql = 'SELECT @@IDENTITY';
24    my ( $sth );
25    eval {
26        $sth = $p->{db}->prepare( $sql );
27        $sth->execute;
28    };
29    if ( $@ ) {
30        SPOPS::Exception::DBI->throw( "Cannot retrieve \@\@IDENTITY value: $@",
31                                      { sql => $sql, action => 'post_fetch_id' } );
32    }
33    my $row = $sth->fetchrow_arrayref;
34    $log->is_info &&
35        $log->info( "Found inserted ID ($row->[0])" );
36    return $row->[0];
37}
38
391;
40
41__END__
42
43=head1 NAME
44
45SPOPS::Key::DBI::Identity -- Retrieve IDENTITY values from a supported DBI database
46
47=head1 SYNOPSIS
48
49 # In your SPOPS configuration
50 $spops  = {
51   'myspops' => {
52       'isa' => [ qw/ SPOPS::Key::DBI::Identity  SPOPS::DBI / ],
53       ...
54   },
55 };
56
57=head1 DESCRIPTION
58
59This class enables a just-created object to the IDENTITY value
60returned by its last insert. Of course, this only works if you have an
61IDENTITY field in your table, such as:
62
63 CREATE TABLE my_table (
64   id    NUMERIC( 8, 0 ) IDENTITY NOT NULL,
65   ...
66 )
67
68This method is typically used in Sybase and Microsoft SQL Server
69databases. The client library (Open Client, FreeTDS, ODBC) should not
70make a difference to this module since we perform a SELECT statement
71to retrieve the value rather than relying on a property of the
72database/statement handle.
73
74=head1 METHODS
75
76B<post_fetch_id()>
77
78Retrieve the IDENTITY value after inserting a row.
79
80=head1 BUGS
81
82None known.
83
84=head1 TO DO
85
86Nothing known.
87
88=head1 SEE ALSO
89
90L<DBD::Sybase|DBD::Sybase>
91
92L<DBI|DBI>
93
94=head1 COPYRIGHT
95
96Copyright (c) 2001-2004 intes.net, inc.. All rights reserved.
97
98This library is free software; you can redistribute it and/or modify
99it under the same terms as Perl itself.
100
101=head1 AUTHORS
102
103Chris Winters  E<lt>chris@cwinters.comE<gt>
104
105See the L<SPOPS|SPOPS> module for the full author list.
106