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