Applied Brandon Black's patch to PK::Auto::Pg
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / PK / Auto / Pg.pm
CommitLineData
5d567302 1package DBIx::Class::PK::Auto::Pg;
2
3use strict;
4use warnings;
5
6use base qw/DBIx::Class/;
7
8__PACKAGE__->load_components(qw/PK::Auto/);
9
10sub 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
17sub get_autoinc_seq {
18 my $self=shift;
97cc0025 19
20 # return the user-defined sequence if known
5b34b2f9 21 if ($self->sequence) {
22 return $self->{_autoinc_seq} = $self->sequence;
23 }
97cc0025 24
5d567302 25 my $dbh= $self->storage->dbh;
26 my $sth = $dbh->column_info( undef, undef, $self->_table_name, '%');
27 while (my $foo = $sth->fetchrow_arrayref){
28 if(defined $foo->[12] && $foo->[12] =~ /^nextval/) {
29 ($self->{_autoinc_seq}) = $foo->[12] =~
4ef9789a 30 m!^nextval\('"?([^"']+)"?'::(?:text|regclass)\)!;
5d567302 31 }
32 }
33}
34
351;
36
37=head1 NAME
38
9d8ad326 39DBIx::Class::PK::Auto::Pg - Automatic Primary Key class for Postgresql
5d567302 40
41=head1 SYNOPSIS
42
43=head1 DESCRIPTION
44
d00123a3 45This class implements autoincrements for Postgresql.
5d567302 46
47=head1 AUTHORS
48
d00123a3 49Marcus Ramberg <m.ramberg@cpan.org>
5d567302 50
51=head1 LICENSE
52
53You may distribute this code under the same terms as Perl itself.
54
55=cut
56