remove debug warning from last commit
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / Pg.pm
1 package DBIx::Class::Storage::DBI::Pg;
2
3 use strict;
4 use warnings;
5
6 use base qw/DBIx::Class::Storage::DBI/;
7
8 # __PACKAGE__->load_components(qw/PK::Auto/);
9
10 sub last_insert_id {
11   my ($self, $source) = @_;
12   $self->get_autoinc_seq($source) unless $source->{_autoinc_seq};
13   $self->_dbh->last_insert_id(undef,undef,undef,undef,
14     {sequence => $source->{_autoinc_seq}});
15 }
16
17 sub get_autoinc_seq {
18   my ($self,$source) = @_;
19   
20   # return the user-defined sequence if known
21   my $result_class = $source->result_class;
22   if ($result_class->can('sequence') and $result_class->sequence) {
23     return ($source->{_autoinc_seq} = $result_class->sequence);      
24   }
25   
26   my @pri = $source->primary_columns;
27   my $dbh = $self->_dbh;
28   my ($schema,$table) = $source->name =~ /^(.+)\.(.+)$/ ? ($1,$2)
29     : (undef,$source->name);
30   while (my $col = shift @pri) {
31     my $info = $dbh->column_info(undef,$schema,$table,$col)->fetchrow_arrayref;
32     if (defined $info->[12] and $info->[12] =~ 
33       /^nextval\('"?([^"']+)"?'::(?:text|regclass)\)/)
34     {
35       $source->{_autoinc_seq} = $1;
36       last;
37     } 
38   }
39 }
40
41 1;
42
43 =head1 NAME 
44
45 DBIx::Class::Storage::DBI::Pg - Automatic primary key class for PostgreSQL
46
47 =head1 SYNOPSIS
48
49   # In your table classes
50   __PACKAGE__->load_components(qw/PK::Auto Core/);
51   __PACKAGE__->set_primary_key('id');
52   __PACKAGE__->sequence('mysequence');
53
54 =head1 DESCRIPTION
55
56 This class implements autoincrements for PostgreSQL.
57
58 =head1 AUTHORS
59
60 Marcus Ramberg <m.ramberg@cpan.org>
61
62 =head1 LICENSE
63
64 You may distribute this code under the same terms as Perl itself.
65
66 =cut