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