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