Added sequence method to PK::Auto
[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->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   return $self->sequence if ($self->sequence);
22   
23   my $dbh= $self->storage->dbh;
24     my $sth     = $dbh->column_info( undef, undef, $self->_table_name, '%');
25     while (my $foo = $sth->fetchrow_arrayref){
26       if(defined $foo->[12] && $foo->[12] =~ /^nextval/) {
27         ($self->{_autoinc_seq}) = $foo->[12] =~ 
28           m!^nextval\('"?([^"']+)"?'::text\)!;
29       }
30     }
31 }
32
33 1;
34
35 =head1 NAME 
36
37 DBIx::Class::PK::Auto::Pg- Automatic Primary Key class for Postgresql
38
39 =head1 SYNOPSIS
40
41 =head1 DESCRIPTION
42
43 This class implements autoincrements for Postgresql.
44
45 =head1 AUTHORS
46
47 Marcus Ramberg <m.ramberg@cpan.org>
48
49 =head1 LICENSE
50
51 You may distribute this code under the same terms as Perl itself.
52
53 =cut
54