1 package DBIx::Class::Storage::DBI::Pg;
6 use base qw/DBIx::Class::Storage::DBI::MultiColumnIn/;
9 use DBD::Pg qw(:pg_types);
11 # Ask for a DBD::Pg with array support
12 warn "DBD::Pg 2.9.2 or greater is strongly recommended\n"
13 if ($DBD::Pg::VERSION < 2.009002); # pg uses (used?) version::qv()
15 sub with_deferred_fk_checks {
16 my ($self, $sub) = @_;
18 $self->dbh->do('SET CONSTRAINTS ALL DEFERRED');
22 sub _dbh_last_insert_id {
23 my ($self, $dbh, $seq) = @_;
24 $dbh->last_insert_id(undef, undef, undef, undef, {sequence => $seq});
28 my ($self,$source,$col) = @_;
29 my $seq = ($source->column_info($col)->{sequence} ||= $self->get_autoinc_seq($source,$col));
30 $self->throw_exception("could not fetch primary key for " . $source->name . ", could not "
31 . "get autoinc sequence for $col (check that table and column specifications are correct "
32 . "and in the correct case)") unless defined $seq;
33 $self->dbh_do('_dbh_last_insert_id', $seq);
36 sub _dbh_get_autoinc_seq {
37 my ($self, $dbh, $schema, $table, @pri) = @_;
39 # get the list of postgres schemas to search. if we have a schema
40 # specified, use that. otherwise, use the search path
42 if( defined $schema and length $schema ) {
43 @search_path = ( $schema );
45 my ($search_path) = $dbh->selectrow_array('SHOW search_path');
46 while( $search_path =~ s/([^,]+),?// ) {
47 unless( defined $1 and length $1 ) {
48 $self->throw_exception("search path sanity check failed: '$1'")
50 push @search_path, $1;
54 foreach my $search_schema (@search_path) {
55 foreach my $col (@pri) {
56 my $info = $dbh->column_info(undef,$search_schema,$table,$col)->fetchrow_hashref;
58 # if we get here, we have definitely found the right
60 if( defined $info->{COLUMN_DEF} and
62 =~ /^nextval\(+'([^']+)'::(?:text|regclass)\)/i
65 return $seq =~ /\./ ? $seq : $info->{TABLE_SCHEM} . "." . $seq;
67 # we have found the column, but cannot figure out
78 my ($self,$source,$col) = @_;
80 my @pri = $source->primary_columns;
81 my ($schema,$table) = $source->name =~ /^(.+)\.(.+)$/ ? ($1,$2)
82 : (undef,$source->name);
84 $self->dbh_do('_dbh_get_autoinc_seq', $schema, $table, @pri);
91 sub datetime_parser_type { return "DateTime::Format::Pg"; }
93 sub bind_attribute_by_data_type {
94 my ($self,$data_type) = @_;
96 my $bind_attributes = {
97 bytea => { pg_type => DBD::Pg::PG_BYTEA },
98 blob => { pg_type => DBD::Pg::PG_BYTEA },
101 if( defined $bind_attributes->{$data_type} ) {
102 return $bind_attributes->{$data_type};
109 sub _sequence_fetch {
110 my ( $self, $type, $seq ) = @_;
111 my ($id) = $self->dbh->selectrow_array("SELECT nextval('${seq}')");
116 my ($self, $name) = @_;
118 $self->dbh->pg_savepoint($name);
122 my ($self, $name) = @_;
124 $self->dbh->pg_release($name);
128 my ($self, $name) = @_;
130 $self->dbh->pg_rollback_to($name);
137 DBIx::Class::Storage::DBI::Pg - Automatic primary key class for PostgreSQL
141 # In your table classes
142 __PACKAGE__->load_components(qw/PK::Auto Core/);
143 __PACKAGE__->set_primary_key('id');
144 __PACKAGE__->sequence('mysequence');
148 This class implements autoincrements for PostgreSQL.
152 Marcus Ramberg <m.ramberg@cpan.org>
156 You may distribute this code under the same terms as Perl itself.