Merge 'trunk' into 'DBIx-Class-current'
[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 {
ca48cd7d 11 my ($self,$source,$col) = @_;
12 my $seq = ($source->column_info($col)->{sequence} ||= $self->get_autoinc_seq($source,$col));
13 $self->_dbh->last_insert_id(undef,undef,undef,undef, {sequence => $seq});
843f8ecd 14}
15
16sub get_autoinc_seq {
ca48cd7d 17 my ($self,$source,$col) = @_;
18
843f8ecd 19 my @pri = $source->primary_columns;
20 my $dbh = $self->_dbh;
34470972 21 my ($schema,$table) = $source->name =~ /^(.+)\.(.+)$/ ? ($1,$2)
22 : (undef,$source->name);
843f8ecd 23 while (my $col = shift @pri) {
4d272ce5 24 my $info = $dbh->column_info(undef,$schema,$table,$col)->fetchrow_hashref;
25 if (defined $info->{COLUMN_DEF} and $info->{COLUMN_DEF} =~
32da1042 26 /^nextval\(+'([^']+)'::(?:text|regclass)\)/)
843f8ecd 27 {
4d272ce5 28 my $seq = $1;
29 return $seq =~ /\./ ? $seq : $info->{TABLE_SCHEM} . "." . $seq; # may need to strip quotes -- see if this works
75d07914 30 }
843f8ecd 31 }
32}
33
4f533e8c 34sub sqlt_type {
35 return 'PostgreSQL';
36}
37
843f8ecd 381;
39
75d07914 40=head1 NAME
843f8ecd 41
42DBIx::Class::Storage::DBI::Pg - Automatic primary key class for PostgreSQL
43
44=head1 SYNOPSIS
45
46 # In your table classes
47 __PACKAGE__->load_components(qw/PK::Auto Core/);
48 __PACKAGE__->set_primary_key('id');
49 __PACKAGE__->sequence('mysequence');
50
51=head1 DESCRIPTION
52
53This class implements autoincrements for PostgreSQL.
54
55=head1 AUTHORS
56
57Marcus Ramberg <m.ramberg@cpan.org>
58
59=head1 LICENSE
60
61You may distribute this code under the same terms as Perl itself.
62
63=cut