PK::Auto fixes in branch
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / Pg.pm
CommitLineData
843f8ecd 1package DBIx::Class::Storage::DBI::Pg;
2
3use strict;
4use warnings;
5
6use base qw/DBIx::Class::Storage::DBI/;
7
8# __PACKAGE__->load_components(qw/PK::Auto/);
9
10sub last_insert_id {
11 my ($self, $source) = @_;
34470972 12 $self->get_autoinc_seq($source) unless $source->{_autoinc_seq};
843f8ecd 13 $self->_dbh->last_insert_id(undef,undef,undef,undef,
34470972 14 {sequence => $source->{_autoinc_seq}});
843f8ecd 15}
16
17sub get_autoinc_seq {
34470972 18 my ($self,$source) = @_;
843f8ecd 19
20 # return the user-defined sequence if known
34470972 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);
843f8ecd 24 }
25
26 my @pri = $source->primary_columns;
27 my $dbh = $self->_dbh;
34470972 28 my ($schema,$table) = $source->name =~ /^(.+)\.(.+)$/ ? ($1,$2)
29 : (undef,$source->name);
843f8ecd 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 {
34470972 35 warn "setting autoinc_seq on $source to $1";
36 $source->{_autoinc_seq} = $1;
843f8ecd 37 last;
38 }
39 }
40}
41
421;
43
44=head1 NAME
45
46DBIx::Class::Storage::DBI::Pg - Automatic primary key class for PostgreSQL
47
48=head1 SYNOPSIS
49
50 # In your table classes
51 __PACKAGE__->load_components(qw/PK::Auto Core/);
52 __PACKAGE__->set_primary_key('id');
53 __PACKAGE__->sequence('mysequence');
54
55=head1 DESCRIPTION
56
57This class implements autoincrements for PostgreSQL.
58
59=head1 AUTHORS
60
61Marcus Ramberg <m.ramberg@cpan.org>
62
63=head1 LICENSE
64
65You may distribute this code under the same terms as Perl itself.
66
67=cut