Merge 'trunk' into 'pg_unqualified_schema'
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / Pg.pm
CommitLineData
843f8ecd 1package DBIx::Class::Storage::DBI::Pg;
2
3use strict;
4use warnings;
5
4ce3b851 6use base qw/DBIx::Class::Storage::DBI::MultiColumnIn/;
ac45262b 7use mro 'c3';
843f8ecd 8
ac45262b 9use DBD::Pg qw(:pg_types);
843f8ecd 10
ac45262b 11# Ask for a DBD::Pg with array support
c70c716e 12warn "DBD::Pg 2.9.2 or greater is strongly recommended\n"
ac45262b 13 if ($DBD::Pg::VERSION < 2.009002); # pg uses (used?) version::qv()
17614944 14
e96a93df 15sub with_deferred_fk_checks {
16 my ($self, $sub) = @_;
17
9ae966b9 18 $self->_get_dbh->do('SET CONSTRAINTS ALL DEFERRED');
e96a93df 19 $sub->();
20}
21
843f8ecd 22sub last_insert_id {
6ea2d01b 23 my ($self,$source,@cols) = @_;
24
2d424996 25 my @values;
26
27 for my $col (@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 "
30 . $source->name
31 . ".$col, please consider adding a "
32 . "schema-qualified sequence to its column info"
33 );
34
35 push @values, $self->_dbh_last_insert_id ($self->_dbh, $seq);
36 }
37
38 return @values;
0680ac39 39}
40
2d424996 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
9a0b7b26 43sub _dbh_last_insert_id {
2d424996 44 my ($self, $dbh, $seq) = @_;
45 $dbh->last_insert_id(undef, undef, undef, undef, {sequence => $seq});
46}
9a0b7b26 47
48
2d424996 49sub _dbh_get_autoinc_seq {
50 my ($self, $dbh, $source, $col) = @_;
0063119f 51
2d424996 52 my $schema;
53 my $table = $source->name;
0063119f 54
2d424996 55 # deref table name if it needs it
56 $table = $$table
57 if ref $table eq 'SCALAR';
0063119f 58
2d424996 59 # parse out schema name if present
60 if( $table =~ /^(.+)\.(.+)$/ ) {
61 ( $schema, $table ) = ( $1, $2 );
62 }
0680ac39 63
2d424996 64 # get the column default using a Postgres-specific pg_catalog query
65 my $seq_expr = $self->_dbh_get_column_default( $dbh, $schema, $table, $col );
66
67 # if no default value is set on the column, or if we can't parse the
68 # default value as a sequence, throw.
5861223d 69 unless ( defined $seq_expr and $seq_expr =~ /^nextval\(+'([^']+)'::(?:text|regclass)\)/i ) {
2d424996 70 $seq_expr = '' unless defined $seq_expr;
71 $schema = "$schema." if defined $schema && length $schema;
72 $self->throw_exception( "no sequence found for $schema$table.$col, check table definition, "
73 . "or explicitly set the 'sequence' for this column in the "
74 . $source->source_name
75 . " class"
76 );
777f7527 77 }
2d424996 78
79 return $1;
777f7527 80}
81
2d424996 82# custom method for fetching column default, since column_info has a
83# bug with older versions of DBD::Pg
84sub _dbh_get_column_default {
85 my ( $self, $dbh, $schema, $table, $col ) = @_;
86
87 # Build and execute a query into the pg_catalog to find the Pg
88 # expression for the default value for this column in this table.
89 # If the table name is schema-qualified, query using that specific
90 # schema name.
91
92 # Otherwise, find the table in the standard Postgres way, using the
93 # search path. This is done with the pg_catalog.pg_table_is_visible
94 # function, which returns true if a given table is 'visible',
95 # meaning the first table of that name to be found in the search
96 # path.
97
98 # I *think* we can be assured that this query will always find the
99 # correct column according to standard Postgres semantics.
100 #
101 # -- rbuels
102
103 my $sqlmaker = $self->sql_maker;
104 local $sqlmaker->{bindtype} = 'normal';
105
106 my ($where, @bind) = $sqlmaker->where ({
107 'a.attnum' => {'>', 0},
108 'c.relname' => $table,
109 'a.attname' => $col,
110 -not_bool => 'a.attisdropped',
111 (defined $schema && length $schema)
112 ? ( 'n.nspname' => $schema )
113 : ( -bool => \'pg_catalog.pg_table_is_visible(c.oid)' )
114 });
115
116 my ($seq_expr) = $dbh->selectrow_array(<<EOS,undef,@bind);
117
118SELECT
119 (SELECT pg_catalog.pg_get_expr(d.adbin, d.adrelid)
120 FROM pg_catalog.pg_attrdef d
121 WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef)
122FROM pg_catalog.pg_class c
123 LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
124 JOIN pg_catalog.pg_attribute a ON a.attrelid = c.oid
125$where
126
127EOS
128
129 return $seq_expr;
130}
131
132
4f533e8c 133sub sqlt_type {
134 return 'PostgreSQL';
135}
136
45fa8288 137sub datetime_parser_type { return "DateTime::Format::Pg"; }
138
a71859b4 139sub bind_attribute_by_data_type {
140 my ($self,$data_type) = @_;
141
142 my $bind_attributes = {
eda28767 143 bytea => { pg_type => DBD::Pg::PG_BYTEA },
5ba88f68 144 blob => { pg_type => DBD::Pg::PG_BYTEA },
a71859b4 145 };
d4daee7b 146
a71859b4 147 if( defined $bind_attributes->{$data_type} ) {
9fdf90df 148 return $bind_attributes->{$data_type};
a71859b4 149 }
150 else {
151 return;
152 }
153}
154
f04b2839 155sub _sequence_fetch {
156 my ( $self, $type, $seq ) = @_;
9ae966b9 157 my ($id) = $self->_get_dbh->selectrow_array("SELECT nextval('${seq}')");
f04b2839 158 return $id;
159}
160
adb3554a 161sub _svp_begin {
eeb8cfeb 162 my ($self, $name) = @_;
adb3554a 163
9ae966b9 164 $self->_get_dbh->pg_savepoint($name);
adb3554a 165}
166
167sub _svp_release {
eeb8cfeb 168 my ($self, $name) = @_;
adb3554a 169
9ae966b9 170 $self->_get_dbh->pg_release($name);
adb3554a 171}
172
173sub _svp_rollback {
eeb8cfeb 174 my ($self, $name) = @_;
adb3554a 175
9ae966b9 176 $self->_get_dbh->pg_rollback_to($name);
adb3554a 177}
178
843f8ecd 1791;
180
fd159e2a 181__END__
182
75d07914 183=head1 NAME
843f8ecd 184
185DBIx::Class::Storage::DBI::Pg - Automatic primary key class for PostgreSQL
186
187=head1 SYNOPSIS
188
189 # In your table classes
190 __PACKAGE__->load_components(qw/PK::Auto Core/);
191 __PACKAGE__->set_primary_key('id');
192 __PACKAGE__->sequence('mysequence');
193
194=head1 DESCRIPTION
195
196This class implements autoincrements for PostgreSQL.
197
7c0176a1 198=head1 POSTGRESQL SCHEMA SUPPORT
199
4f609014 200This driver supports multiple PostgreSQL schemas, with one caveat: for
6ff1d58c 201performance reasons, data about the search path, sequence names, and
202so forth is queried as needed and CACHED for subsequent uses.
7c0176a1 203
4f609014 204For this reason, once your schema is instantiated, you should not
205change the PostgreSQL schema search path for that schema's database
206connection. If you do, Bad Things may happen.
207
208You should do any necessary manipulation of the search path BEFORE
209instantiating your schema object, or as part of the on_connect_do
210option to connect(), for example:
7c0176a1 211
212 my $schema = My::Schema->connect
213 ( $dsn,$user,$pass,
214 { on_connect_do =>
215 [ 'SET search_path TO myschema, foo, public' ],
216 },
217 );
218
7ff926e6 219=head1 AUTHORS
7c0176a1 220
7ff926e6 221See L<DBIx::Class/CONTRIBUTORS>
843f8ecd 222
223=head1 LICENSE
224
225You may distribute this code under the same terms as Perl itself.
226
227=cut