try to fix PK::Auto::Pg for sequences with quoted names
[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 {
b6b65a3e 11 my $self = shift;
5d567302 12 $self->get_autoinc_seq unless $self->{_autoinc_seq};
66d9ef6b 13 $self->result_source->storage->dbh->last_insert_id(undef,undef,undef,undef,
5d567302 14 {sequence=>$self->{_autoinc_seq}});
15}
16
17sub get_autoinc_seq {
b6b65a3e 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
47d2f099 25 my @pri = $self->primary_columns;
fe2b6875 26 my $dbh = $self->result_source->storage->dbh;
c5b7d799 27 my ($schema,$table) = $self->table =~ /^(.+)\.(.+)$/ ? ($1,$2) : (undef,$self->table);
b6b65a3e 28 while (my $col = shift @pri) {
c5b7d799 29 my $info = $dbh->column_info(undef,$schema,$table,$col)->fetchrow_arrayref;
b6b65a3e 30 if (defined $info->[12] and $info->[12] =~
10f7b30b 31 /^nextval\('([^']+)'::(?:text|regclass)\)/)
b6b65a3e 32 {
33 $self->{_autoinc_seq} = $1;
10f7b30b 34 #$self->{_autoinc_seq} =~ s/"//g;
b6b65a3e 35 last;
36 }
37 }
5d567302 38}
39
401;
41
42=head1 NAME
43
eb49d4e3 44DBIx::Class::PK::Auto::Pg - Automatic primary key class for PostgreSQL
5d567302 45
46=head1 SYNOPSIS
47
eb49d4e3 48 # In your table classes
49 __PACKAGE__->load_components(qw/PK::Auto::Pg Core/);
50 __PACKAGE__->set_primary_key('id');
6718c5f0 51
5d567302 52=head1 DESCRIPTION
53
eb49d4e3 54This class implements autoincrements for PostgreSQL.
5d567302 55
56=head1 AUTHORS
57
d00123a3 58Marcus Ramberg <m.ramberg@cpan.org>
5d567302 59
60=head1 LICENSE
61
62You may distribute this code under the same terms as Perl itself.
63
64=cut