1 package DBIx::Class::Storage::DBI::Pg;
6 use DBD::Pg qw(:pg_types);
8 use base qw/DBIx::Class::Storage::DBI/;
10 # __PACKAGE__->load_components(qw/PK::Auto/);
12 # Warn about problematic versions of DBD::Pg
13 warn "DBD::Pg 1.49 is strongly recommended"
14 if ($DBD::Pg::VERSION < 1.49);
16 sub _dbh_last_insert_id {
17 my ($self, $dbh, $seq) = @_;
18 $dbh->last_insert_id(undef, undef, undef, undef, {sequence => $seq});
22 my ($self,$source,$col) = @_;
23 my $seq = ($source->column_info($col)->{sequence} ||= $self->get_autoinc_seq($source,$col));
24 $self->dbh_do($self->can('_dbh_last_insert_id'), $seq);
27 sub _dbh_get_autoinc_seq {
28 my ($self, $dbh, $schema, $table, @pri) = @_;
30 while (my $col = shift @pri) {
31 my $info = $dbh->column_info(undef,$schema,$table,$col)->fetchrow_hashref;
32 if(defined $info->{COLUMN_DEF} and
33 $info->{COLUMN_DEF} =~ /^nextval\(+'([^']+)'::(?:text|regclass)\)/) {
35 # may need to strip quotes -- see if this works
36 return $seq =~ /\./ ? $seq : $info->{TABLE_SCHEM} . "." . $seq;
43 my ($self,$source,$col) = @_;
45 my @pri = $source->primary_columns;
46 my ($schema,$table) = $source->name =~ /^(.+)\.(.+)$/ ? ($1,$2)
47 : (undef,$source->name);
49 $self->dbh_do($self->can('_dbh_get_autoinc_seq'), $schema, $table, @pri);
56 sub datetime_parser_type { return "DateTime::Format::Pg"; }
58 sub bind_attribute_by_data_type {
59 my ($self,$data_type) = @_;
61 my $bind_attributes = {
62 bytea => { pg_type => DBD::Pg::PG_BYTEA },
65 if( defined $bind_attributes->{$data_type} ) {
66 return $bind_attributes->{$data_type};
77 DBIx::Class::Storage::DBI::Pg - Automatic primary key class for PostgreSQL
81 # In your table classes
82 __PACKAGE__->load_components(qw/PK::Auto Core/);
83 __PACKAGE__->set_primary_key('id');
84 __PACKAGE__->sequence('mysequence');
88 This class implements autoincrements for PostgreSQL.
92 Marcus Ramberg <m.ramberg@cpan.org>
96 You may distribute this code under the same terms as Perl itself.