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