1 package DBIx::Class::Storage::DBI::Pg;
6 use DBD::Pg qw(:pg_types);
8 use base qw/DBIx::Class::Storage::DBI::MultiColumnIn/;
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 with_deferred_fk_checks {
17 my ($self, $sub) = @_;
19 $self->dbh->do('SET CONSTRAINTS ALL DEFERRED');
23 sub _dbh_last_insert_id {
24 my ($self, $dbh, $seq) = @_;
25 $dbh->last_insert_id(undef, undef, undef, undef, {sequence => $seq});
29 my ($self,$source,$col) = @_;
30 my $seq = ($source->column_info($col)->{sequence} ||= $self->get_autoinc_seq($source,$col));
31 $self->throw_exception("could not fetch primary key for " . $source->name . ", could not "
32 . "get autoinc sequence for $col (check that table and column specifications are correct "
33 . "and in the correct case)") unless defined $seq;
34 $self->dbh_do('_dbh_last_insert_id', $seq);
37 sub _dbh_get_autoinc_seq {
38 my ($self, $dbh, $schema, $table, @pri) = @_;
40 while (my $col = shift @pri) {
41 my $info = $dbh->column_info(undef,$schema,$table,$col)->fetchrow_hashref;
42 if(defined $info->{COLUMN_DEF} and
43 $info->{COLUMN_DEF} =~ /^nextval\(+'([^']+)'::(?:text|regclass)\)/) {
45 # may need to strip quotes -- see if this works
46 return $seq =~ /\./ ? $seq : $info->{TABLE_SCHEM} . "." . $seq;
53 my ($self,$source,$col) = @_;
55 my @pri = $source->primary_columns;
56 my ($schema,$table) = $source->name =~ /^(.+)\.(.+)$/ ? ($1,$2)
57 : (undef,$source->name);
59 $self->dbh_do('_dbh_get_autoinc_seq', $schema, $table, @pri);
66 sub datetime_parser_type { return "DateTime::Format::Pg"; }
68 sub bind_attribute_by_data_type {
69 my ($self,$data_type) = @_;
71 my $bind_attributes = {
72 bytea => { pg_type => DBD::Pg::PG_BYTEA },
73 blob => { pg_type => DBD::Pg::PG_BYTEA },
76 if( defined $bind_attributes->{$data_type} ) {
77 return $bind_attributes->{$data_type};
85 my ( $self, $type, $seq ) = @_;
86 my ($id) = $self->dbh->selectrow_array("SELECT nextval('${seq}')");
91 my ($self, $name) = @_;
93 $self->dbh->pg_savepoint($name);
97 my ($self, $name) = @_;
99 $self->dbh->pg_release($name);
103 my ($self, $name) = @_;
105 $self->dbh->pg_rollback_to($name);
112 DBIx::Class::Storage::DBI::Pg - Automatic primary key class for PostgreSQL
116 # In your table classes
117 __PACKAGE__->load_components(qw/PK::Auto Core/);
118 __PACKAGE__->set_primary_key('id');
119 __PACKAGE__->sequence('mysequence');
123 This class implements autoincrements for PostgreSQL.
127 Marcus Ramberg <m.ramberg@cpan.org>
131 You may distribute this code under the same terms as Perl itself.