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,@cols) = @_;
28 my $seq = ( $source->column_info($col)->{sequence} ||= $self->dbh_do('_dbh_get_autoinc_seq', $source, $col) )
29 or $self->throw_exception( "could not determine sequence for "
31 . ".$col, please consider adding a "
32 . "schema-qualified sequence to its column info"
35 push @values, $self->_dbh_last_insert_id ($self->_dbh, $seq);
41 # there seems to be absolutely no reason to have this as a separate method,
42 # but leaving intact in case someone is already overriding it
43 sub _dbh_last_insert_id {
44 my ($self, $dbh, $seq) = @_;
45 $dbh->last_insert_id(undef, undef, undef, undef, {sequence => $seq});
49 sub _dbh_get_autoinc_seq {
50 my ($self, $dbh, $source, $col) = @_;
53 my $table = $source->name;
55 # deref table name if it needs it
57 if ref $table eq 'SCALAR';
59 # parse out schema name if present
60 if( $table =~ /^(.+)\.(.+)$/ ) {
61 ( $schema, $table ) = ( $1, $2 );
64 # use DBD::Pg to fetch the column info if it is recent enough to
65 # work. otherwise, use custom SQL
66 my $seq_expr = $DBD::Pg::VERSION >= 2.015001
67 ? eval{ $dbh->column_info(undef,$schema,$table,$col)->fetchrow_hashref->{COLUMN_DEF} }
68 : $self->_dbh_get_column_default( $dbh, $schema, $table, $col );
70 # if no default value is set on the column, or if we can't parse the
71 # default value as a sequence, throw.
72 unless ( defined $seq_expr and $seq_expr =~ /^nextval\(+'([^']+)'::(?:text|regclass)\)/i ){
73 $seq_expr = '' unless defined $seq_expr;
74 $schema = "$schema." if defined $schema && length $schema;
75 $self->throw_exception( "no sequence found for $schema$table.$col, check table definition, "
76 . "or explicitly set the 'sequence' for this column in the "
77 . $source->source_name
85 # custom method for fetching column default, since column_info has a
86 # bug with older versions of DBD::Pg
87 sub _dbh_get_column_default {
88 my ( $self, $dbh, $schema, $table, $col ) = @_;
90 # Build and execute a query into the pg_catalog to find the Pg
91 # expression for the default value for this column in this table.
92 # If the table name is schema-qualified, query using that specific
95 # Otherwise, find the table in the standard Postgres way, using the
96 # search path. This is done with the pg_catalog.pg_table_is_visible
97 # function, which returns true if a given table is 'visible',
98 # meaning the first table of that name to be found in the search
101 # I *think* we can be assured that this query will always find the
102 # correct column according to standard Postgres semantics.
106 my $sqlmaker = $self->sql_maker;
107 local $sqlmaker->{bindtype} = 'normal';
109 my ($where, @bind) = $sqlmaker->where ({
110 'a.attnum' => {'>', 0},
111 'c.relname' => $table,
113 -not_bool => 'a.attisdropped',
114 (defined $schema && length $schema)
115 ? ( 'n.nspname' => $schema )
116 : ( -bool => \'pg_catalog.pg_table_is_visible(c.oid)' )
119 my ($seq_expr) = $dbh->selectrow_array(<<EOS,undef,@bind);
122 (SELECT pg_catalog.pg_get_expr(d.adbin, d.adrelid)
123 FROM pg_catalog.pg_attrdef d
124 WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef)
125 FROM pg_catalog.pg_class c
126 LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
127 JOIN pg_catalog.pg_attribute a ON a.attrelid = c.oid
140 sub datetime_parser_type { return "DateTime::Format::Pg"; }
142 sub bind_attribute_by_data_type {
143 my ($self,$data_type) = @_;
145 my $bind_attributes = {
146 bytea => { pg_type => DBD::Pg::PG_BYTEA },
147 blob => { pg_type => DBD::Pg::PG_BYTEA },
150 if( defined $bind_attributes->{$data_type} ) {
151 return $bind_attributes->{$data_type};
158 sub _sequence_fetch {
159 my ( $self, $type, $seq ) = @_;
160 my ($id) = $self->_get_dbh->selectrow_array("SELECT nextval('${seq}')");
165 my ($self, $name) = @_;
167 $self->_get_dbh->pg_savepoint($name);
171 my ($self, $name) = @_;
173 $self->_get_dbh->pg_release($name);
177 my ($self, $name) = @_;
179 $self->_get_dbh->pg_rollback_to($name);
188 DBIx::Class::Storage::DBI::Pg - Automatic primary key class for PostgreSQL
192 # In your table classes
193 __PACKAGE__->load_components(qw/PK::Auto Core/);
194 __PACKAGE__->set_primary_key('id');
195 __PACKAGE__->sequence('mysequence');
199 This class implements autoincrements for PostgreSQL.
201 =head1 POSTGRESQL SCHEMA SUPPORT
203 This driver supports multiple PostgreSQL schemas, with one caveat: for
204 performance reasons, data about the search path, sequence names, and
205 so forth is queried as needed and CACHED for subsequent uses.
207 For this reason, once your schema is instantiated, you should not
208 change the PostgreSQL schema search path for that schema's database
209 connection. If you do, Bad Things may happen.
211 You should do any necessary manipulation of the search path BEFORE
212 instantiating your schema object, or as part of the on_connect_do
213 option to connect(), for example:
215 my $schema = My::Schema->connect
218 [ 'SET search_path TO myschema, foo, public' ],
224 See L<DBIx::Class/CONTRIBUTORS>
228 You may distribute this code under the same terms as Perl itself.