added caching of pg search path in Pg storage object
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / Pg.pm
1 package DBIx::Class::Storage::DBI::Pg;
2
3 use strict;
4 use warnings;
5
6 use base qw/DBIx::Class::Storage::DBI::MultiColumnIn/;
7 use mro 'c3';
8
9 use DBD::Pg qw(:pg_types);
10
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()
14
15 sub with_deferred_fk_checks {
16   my ($self, $sub) = @_;
17
18   $self->dbh->do('SET CONSTRAINTS ALL DEFERRED');
19   $sub->();
20 }
21
22 sub _dbh_last_insert_id {
23   my ($self, $dbh, $seq) = @_;
24   $dbh->last_insert_id(undef, undef, undef, undef, {sequence => $seq});
25 }
26
27 sub last_insert_id {
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);
34 }
35
36 sub _get_pg_search_path {
37     my ($self,$dbh) = @_;
38     # cache the search path as ['schema','schema',...] in the storage
39     # obj
40     $self->{_pg_search_path} ||= do {
41         my @search_path;
42         my ($sp_string) = $dbh->selectrow_array('SHOW search_path');
43         while( $sp_string =~ s/("[^"]+"|[^,]+),?// ) {
44             unless( defined $1 and length $1 ) {
45                 $self->throw_exception("search path sanity check failed: '$1'")
46             }
47             push @search_path, $1;
48         }
49         \@search_path
50     };
51 }
52
53 sub _dbh_get_autoinc_seq {
54   my ($self, $dbh, $schema, $table, @pri) = @_;
55
56   # get the list of postgres schemas to search.  if we have a schema
57   # specified, use that.  otherwise, use the search path
58   my @search_path;
59   if( defined $schema and length $schema ) {
60       @search_path = ( $schema );
61   } else {
62       @search_path = @{ $self->_get_pg_search_path($dbh) };
63   }
64
65   foreach my $search_schema (@search_path) {
66       foreach my $col (@pri) {
67           my $info = $dbh->column_info(undef,$search_schema,$table,$col)->fetchrow_hashref;
68           if($info) {
69               # if we get here, we have definitely found the right
70               # column.
71               if( defined $info->{COLUMN_DEF} and
72                   $info->{COLUMN_DEF}
73                     =~ /^nextval\(+'([^']+)'::(?:text|regclass)\)/i
74                 ) {
75                   my $seq = $1;
76                   return $seq =~ /\./ ? $seq : $info->{TABLE_SCHEM} . "." . $seq;
77               } else {
78                   # we have found the column, but cannot figure out
79                   # the nextval seq
80                   return;
81               }
82           }
83       }
84   }
85   return;
86 }
87
88 sub get_autoinc_seq {
89   my ($self,$source,$col) = @_;
90
91   my @pri = $source->primary_columns;
92   my ($schema,$table) = $source->name =~ /^(.+)\.(.+)$/ ? ($1,$2)
93     : (undef,$source->name);
94
95   $self->dbh_do('_dbh_get_autoinc_seq', $schema, $table, @pri);
96 }
97
98 sub sqlt_type {
99   return 'PostgreSQL';
100 }
101
102 sub datetime_parser_type { return "DateTime::Format::Pg"; }
103
104 sub bind_attribute_by_data_type {
105   my ($self,$data_type) = @_;
106
107   my $bind_attributes = {
108     bytea => { pg_type => DBD::Pg::PG_BYTEA },
109     blob  => { pg_type => DBD::Pg::PG_BYTEA },
110   };
111
112   if( defined $bind_attributes->{$data_type} ) {
113     return $bind_attributes->{$data_type};
114   }
115   else {
116     return;
117   }
118 }
119
120 sub _sequence_fetch {
121   my ( $self, $type, $seq ) = @_;
122   my ($id) = $self->dbh->selectrow_array("SELECT nextval('${seq}')");
123   return $id;
124 }
125
126 sub _svp_begin {
127     my ($self, $name) = @_;
128
129     $self->dbh->pg_savepoint($name);
130 }
131
132 sub _svp_release {
133     my ($self, $name) = @_;
134
135     $self->dbh->pg_release($name);
136 }
137
138 sub _svp_rollback {
139     my ($self, $name) = @_;
140
141     $self->dbh->pg_rollback_to($name);
142 }
143
144 1;
145
146 =head1 NAME
147
148 DBIx::Class::Storage::DBI::Pg - Automatic primary key class for PostgreSQL
149
150 =head1 SYNOPSIS
151
152   # In your table classes
153   __PACKAGE__->load_components(qw/PK::Auto Core/);
154   __PACKAGE__->set_primary_key('id');
155   __PACKAGE__->sequence('mysequence');
156
157 =head1 DESCRIPTION
158
159 This class implements autoincrements for PostgreSQL.
160
161 =head1 AUTHORS
162
163 Marcus Ramberg <m.ramberg@cpan.org>
164
165 =head1 LICENSE
166
167 You may distribute this code under the same terms as Perl itself.
168
169 =cut