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->_get_dbh->do('SET CONSTRAINTS ALL DEFERRED');
23 my ($self,$source,$col) = @_;
24 my $seq = ($source->column_info($col)->{sequence} ||= $self->get_autoinc_seq($source,$col));
25 $self->throw_exception("could not fetch primary key for " . $source->name . ", could not "
26 . "get autoinc sequence for $col (check that table and column specifications are correct "
27 . "and in the correct case)") unless defined $seq;
29 $self->_dbh_last_insert_id ($self->_dbh, $seq);
32 # there seems to be absolutely no reason to have this as a separate method,
33 # but leaving intact in case someone is already overriding it
34 sub _dbh_last_insert_id {
35 my ($self, $dbh, $seq) = @_;
36 $dbh->last_insert_id(undef, undef, undef, undef, {sequence => $seq});
40 sub _get_pg_search_path {
42 # cache the search path as ['schema','schema',...] in the storage
44 $self->{_pg_search_path} ||= do {
46 my ($sp_string) = $dbh->selectrow_array('SHOW search_path');
47 while( $sp_string =~ s/("[^"]+"|[^,]+),?// ) {
48 unless( defined $1 and length $1 ) {
49 $self->throw_exception("search path sanity check failed: '$1'")
51 push @search_path, $1;
57 sub _dbh_get_autoinc_seq {
58 my ($self, $dbh, $schema, $table, @pri) = @_;
60 # get the list of postgres schemas to search. if we have a schema
61 # specified, use that. otherwise, use the search path
63 if( defined $schema and length $schema ) {
64 @search_path = ( $schema );
66 @search_path = @{ $self->_get_pg_search_path($dbh) };
69 foreach my $search_schema (@search_path) {
70 foreach my $col (@pri) {
71 my $info = $dbh->column_info(undef,$search_schema,$table,$col)->fetchrow_hashref;
73 # if we get here, we have definitely found the right
75 if( defined $info->{COLUMN_DEF} and
77 =~ /^nextval\(+'([^']+)'::(?:text|regclass)\)/i
80 return $seq =~ /\./ ? $seq : $info->{TABLE_SCHEM} . "." . $seq;
82 # we have found the column, but cannot figure out
93 my ($self,$source,$col) = @_;
95 my @pri = $source->primary_columns;
98 my $table = $source->name;
100 if (ref $table eq 'SCALAR') {
103 elsif ($table =~ /^(.+)\.(.+)$/) {
104 ($schema, $table) = ($1, $2);
107 $self->dbh_do('_dbh_get_autoinc_seq', $schema, $table, @pri);
114 sub datetime_parser_type { return "DateTime::Format::Pg"; }
116 sub bind_attribute_by_data_type {
117 my ($self,$data_type) = @_;
119 my $bind_attributes = {
120 bytea => { pg_type => DBD::Pg::PG_BYTEA },
121 blob => { pg_type => DBD::Pg::PG_BYTEA },
124 if( defined $bind_attributes->{$data_type} ) {
125 return $bind_attributes->{$data_type};
132 sub _sequence_fetch {
133 my ( $self, $type, $seq ) = @_;
134 my ($id) = $self->_get_dbh->selectrow_array("SELECT nextval('${seq}')");
139 my ($self, $name) = @_;
141 $self->_get_dbh->pg_savepoint($name);
145 my ($self, $name) = @_;
147 $self->_get_dbh->pg_release($name);
151 my ($self, $name) = @_;
153 $self->_get_dbh->pg_rollback_to($name);
160 DBIx::Class::Storage::DBI::Pg - Automatic primary key class for PostgreSQL
164 # In your table classes
165 __PACKAGE__->load_components(qw/PK::Auto Core/);
166 __PACKAGE__->set_primary_key('id');
167 __PACKAGE__->sequence('mysequence');
171 This class implements autoincrements for PostgreSQL.
173 =head1 POSTGRESQL SCHEMA SUPPORT
175 This supports multiple PostgreSQL schemas, with one caveat: for
176 performance reasons, the schema search path is queried the first time it is
177 needed and CACHED for subsequent uses.
179 For this reason, you should do any necessary manipulation of the
180 PostgreSQL search path BEFORE instantiating your schema object, or as
181 part of the on_connect_do option to connect(), for example:
183 my $schema = My::Schema->connect
186 [ 'SET search_path TO myschema, foo, public' ],
192 See L<DBIx::Class/CONTRIBUTORS>
196 You may distribute this code under the same terms as Perl itself.