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');
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 while (my $col = shift @pri) {
40 my $info = $dbh->column_info(undef,$schema,$table,$col)->fetchrow_hashref;
41 if(defined $info->{COLUMN_DEF} and
42 $info->{COLUMN_DEF} =~ /^nextval\(+'([^']+)'::(?:text|regclass)\)/) {
44 # may need to strip quotes -- see if this works
45 return $seq =~ /\./ ? $seq : $info->{TABLE_SCHEM} . "." . $seq;
52 my ($self,$source,$col) = @_;
54 my @pri = $source->primary_columns;
57 my $table = $source->name;
59 if (ref $table eq 'SCALAR') {
62 elsif ($table =~ /^(.+)\.(.+)$/) {
63 ($schema, $table) = ($1, $2);
66 $self->dbh_do('_dbh_get_autoinc_seq', $schema, $table, @pri);
73 sub datetime_parser_type { return "DateTime::Format::Pg"; }
75 sub bind_attribute_by_data_type {
76 my ($self,$data_type) = @_;
78 my $bind_attributes = {
79 bytea => { pg_type => DBD::Pg::PG_BYTEA },
80 blob => { pg_type => DBD::Pg::PG_BYTEA },
83 if( defined $bind_attributes->{$data_type} ) {
84 return $bind_attributes->{$data_type};
92 my ( $self, $type, $seq ) = @_;
93 my ($id) = $self->_get_dbh->selectrow_array("SELECT nextval('${seq}')");
98 my ($self, $name) = @_;
100 $self->_get_dbh->pg_savepoint($name);
104 my ($self, $name) = @_;
106 $self->_get_dbh->pg_release($name);
110 my ($self, $name) = @_;
112 $self->_get_dbh->pg_rollback_to($name);
119 DBIx::Class::Storage::DBI::Pg - Automatic primary key class for PostgreSQL
123 # In your table classes
124 __PACKAGE__->load_components(qw/PK::Auto Core/);
125 __PACKAGE__->set_primary_key('id');
126 __PACKAGE__->sequence('mysequence');
130 This class implements autoincrements for PostgreSQL.
134 Marcus Ramberg <m.ramberg@cpan.org>
138 You may distribute this code under the same terms as Perl itself.