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) = @_;
25 return map $self->dbh_do('_dbh_last_insert_id', $source, $_ ), @cols;
28 sub _dbh_last_insert_id {
29 my ($self, $dbh, $source, $col ) = @_;
31 # if a sequence is defined, explicitly specify it to DBD::Pg's last_insert_id()
32 if( my $seq = $source->column_info($col)->{sequence} ) {
33 return $dbh->last_insert_id(undef, undef, undef, undef, {sequence => $seq});
36 # if not, parse out the schema and table names, pass them to
37 # DBD::Pg, and let it figure out (and cache) the sequence name
42 my $table = $source->name;
44 # deref table name if necessary
45 $table = $$table if ref $table eq 'SCALAR';
47 # parse out schema name if present
48 if ( $table =~ /^(.+)\.(.+)$/ ) {
49 ( $schema, $table ) = ( $1, $2 );
52 return $dbh->last_insert_id( undef, $schema, $table, undef );
60 sub datetime_parser_type { return "DateTime::Format::Pg"; }
62 sub bind_attribute_by_data_type {
63 my ($self,$data_type) = @_;
65 my $bind_attributes = {
66 bytea => { pg_type => DBD::Pg::PG_BYTEA },
67 blob => { pg_type => DBD::Pg::PG_BYTEA },
70 if( defined $bind_attributes->{$data_type} ) {
71 return $bind_attributes->{$data_type};
79 my ( $self, $type, $seq ) = @_;
80 my ($id) = $self->_get_dbh->selectrow_array("SELECT nextval('${seq}')");
85 my ($self, $name) = @_;
87 $self->_get_dbh->pg_savepoint($name);
91 my ($self, $name) = @_;
93 $self->_get_dbh->pg_release($name);
97 my ($self, $name) = @_;
99 $self->_get_dbh->pg_rollback_to($name);
108 DBIx::Class::Storage::DBI::Pg - Automatic primary key class for PostgreSQL
112 # In your table classes
113 __PACKAGE__->load_components(qw/PK::Auto Core/);
114 __PACKAGE__->set_primary_key('id');
115 __PACKAGE__->sequence('mysequence');
119 This class implements autoincrements for PostgreSQL.
121 =head1 POSTGRESQL SCHEMA SUPPORT
123 This driver supports multiple PostgreSQL schemas, with one caveat: for
124 performance reasons, data about the search path, sequence names, and
125 so forth is queried as needed and CACHED for subsequent uses.
127 For this reason, once your schema is instantiated, you should not
128 change the PostgreSQL schema search path for that schema's database
129 connection. If you do, Bad Things may happen.
131 You should do any necessary manipulation of the search path BEFORE
132 instantiating your schema object, or as part of the on_connect_do
133 option to connect(), for example:
135 my $schema = My::Schema->connect
138 [ 'SET search_path TO myschema, foo, public' ],
144 See L<DBIx::Class/CONTRIBUTORS>
148 You may distribute this code under the same terms as Perl itself.