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