Shovelling PK::Auto stuff where it belongs..
[dbsrgits/DBIx-Class-Historic.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) = @_;
12 $self->get_autoinc_seq unless $self->{_autoinc_seq};
13 $self->_dbh->last_insert_id(undef,undef,undef,undef,
14 {sequence=>$self->{_autoinc_seq}});
15}
16
17sub get_autoinc_seq {
18 my $self = shift;
19
20 # return the user-defined sequence if known
21 if ($source->sequence) {
22 return $self->{_autoinc_seq} = $source->sequence;
23 }
24
25 my @pri = $source->primary_columns;
26 my $dbh = $self->_dbh;
27 my ($schema,$table) = $source->name =~ /^(.+)\.(.+)$/ ? ($1,$2) : (undef,$sou
28rce->table);
29 while (my $col = shift @pri) {
30 my $info = $dbh->column_info(undef,$schema,$table,$col)->fetchrow_arrayref;
31 if (defined $info->[12] and $info->[12] =~
32 /^nextval\('"?([^"']+)"?'::(?:text|regclass)\)/)
33 {
34 $self->{_autoinc_seq} = $1;
35 last;
36 }
37 }
38}
39
401;
41
42=head1 NAME
43
44DBIx::Class::Storage::DBI::Pg - Automatic primary key class for PostgreSQL
45
46=head1 SYNOPSIS
47
48 # In your table classes
49 __PACKAGE__->load_components(qw/PK::Auto Core/);
50 __PACKAGE__->set_primary_key('id');
51 __PACKAGE__->sequence('mysequence');
52
53=head1 DESCRIPTION
54
55This class implements autoincrements for PostgreSQL.
56
57=head1 AUTHORS
58
59Marcus Ramberg <m.ramberg@cpan.org>
60
61=head1 LICENSE
62
63You may distribute this code under the same terms as Perl itself.
64
65=cut