blib/lib/DBIx/QuickORM/Dialect/PostgreSQL.pm

filter: Statementstmt 69.1% 56 / 81 Branchbran 79.1% 19 / 24 Conditioncond 50.0% 7 / 14 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 66.6% 20 / 30 Podpod 100% 24 / 24 totaltotal 70.5% 127 / 180 CC 20 SCAR 12.3
CC
20
Cov
69%
scan_cache_key
12.0
version_number
7.8
identity_sql
3.0
CRAP 3.4
« blib/lib/DBIx/QuickORM/Value.pm ↑ Summary blib/lib/DBIx/QuickORM/Connection/Offline.pm »
1 package DBIx::QuickORM::Dialect::PostgreSQL;
2 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 13 our $VERSION = '0.000033';
6  
7 13 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 13 ▶use parent 'DBIx::QuickORM::Dialect';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =pod
12  
13 =encoding UTF-8
14  
15 =head1 NAME
16  
17 DBIx::QuickORM::Dialect::PostgreSQL - PostgreSQL dialect.
18  
19 =head1 DESCRIPTION
20  
21 PostgreSQL through L<DBD::Pg>. It keeps the base defaults of
22 L<DBIx::QuickORM::Dialect> and adds a 65535 bind limit, async queries, and
23 C<bytea> binding. The namespace is C<current_schema()>.
24  
25 =head1 SYNOPSIS
26  
27 my $dialect = DBIx::QuickORM::Dialect->for_dbh($dbh); # this class for DBD::Pg
28  
29 =head1 PUBLIC METHODS
30  
31 =over 4
32  
33 =item $n = $dialect->bind_limit
34  
35 65535, the most parameters the wire protocol can number in one statement.
36  
37 =item $bool = $dialect->supports_isolation
38  
39 True.
40  
41 =item @sql = $dialect->begin_sql($level)
42  
43 C<BEGIN ISOLATION LEVEL $level> when given a level, otherwise C<BEGIN>.
44  
45 =item $bool = $dialect->supports_async
46  
47 True.
48  
49 =item $attrs = $dialect->async_prepare_attrs
50  
51 C<< {pg_async => PG_ASYNC} >>.
52  
53 Each async statement is prepared fresh and executed once: DBD::Pg 3.21.2 can
54 skip the execute of a re-executed server-prepared async statement, losing its
55 rows (L<https://github.com/bucardo/dbdpg/issues/217>).
56  
57 =item $bool = $dialect->async_ready($dbh, $sth)
58  
59 =item $dialect->async_result($dbh, $sth)
60  
61 =item $dialect->cancel_async($dbh, $sth)
62  
63 C<pg_ready>, C<pg_result>, and C<pg_cancel> on the database handle.
64  
65 Cancelling a query inside a transaction makes PostgreSQL abort that
66 transaction.
67  
68 =back
69  
70 =cut
71  
72 1 ▶sub supports_isolation { 1 }
Subroutine supports_isolation: called CC=1 SCAR=0.0
Pod documented
73 2 ▶sub begin_sql { $_[1] ? ("BEGIN ISOLATION LEVEL $_[1]") : ('BEGIN') }
Subroutine begin_sql: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 1 1 2
74 1 ▶sub bind_limit { 65535 }
Subroutine bind_limit: called CC=1 SCAR=0.0
Pod documented
75 0 ▶sub supports_async { 1 }
Subroutine supports_async: not called CC=1 SCAR=6.9
Pod documented
76 0 ▶sub async_prepare_attrs { {pg_async => DBD::Pg::PG_ASYNC()} }
Subroutine async_prepare_attrs: not called CC=1 SCAR=6.9
Pod documented
77 0 ▶sub async_ready { $_[1]->pg_ready }
Subroutine async_ready: not called CC=1 SCAR=6.9
Pod documented
78 0 ▶sub async_result { $_[1]->pg_result }
Subroutine async_result: not called CC=1 SCAR=6.9
Pod documented
79 0 ▶sub cancel_async { $_[1]->pg_cancel }
Subroutine cancel_async: not called CC=1 SCAR=6.9
Pod documented
80  
81 =pod
82  
83 =over 4
84  
85 =item $sql = $dialect->identity_sql($table, \%field, $inline_pk)
86  
87 C<GENERATED BY DEFAULT AS IDENTITY>. Croaks before PostgreSQL 10.
88  
89 =item $sql = $dialect->generated_sql($expression)
90  
91 As inherited. Croaks before PostgreSQL 12.
92  
93 =item $sql = $dialect->quote_binary($dbh, $bytes)
94  
95 A C<bytea> hex literal.
96  
97 =item $sql = $dialect->index_using_sql($type)
98  
99 C<USING $type>, as C<CREATE INDEX> takes it, or C<USING> and a reference's
100 SQL as is.
101  
102 =item $sql = $dialect->collate_sql($collation)
103  
104 C<COLLATE> and the name quoted as an identifier, as PostgreSQL names
105 collations (C<"C">, C<"en-US-x-icu">).
106  
107 =item $sql = $dialect->extension_sql($name)
108  
109 C<CREATE EXTENSION IF NOT EXISTS>, since some extensions come installed.
110  
111 =item $number = $dialect->version_number($string)
112  
113 PostgreSQL's own numbering, which changed with version 10.
114  
115 my $pg = DBIx::QuickORM::Dialect->for_name('postgresql');
116  
117 my $old = $pg->version_number('9.6.3'); # 90603
118 my $new = $pg->version_number('17.4'); # 170004
119  
120 =back
121  
122 =cut
123  
124 4 ▶sub identity_sql {
Subroutine identity_sql: called CC=3 SCAR=11.1
Pod documented
125 4 my $self = shift;
126 4 ▶ croak "Identity columns need PostgreSQL 10 or later" if ($self->server_version // 0) < 100000;
Branch: 1 branch
BranchTrueFalseTotal
if ($self->server_version // 0) < 100000 2 2 4
Condition50%1/2
$self->server_version // 0
l!l
40
MC/DC0%0/1
$self->server_version // 0
$self->server_version
Truth table50%1/2
A
A= $self->server_version
Aresult
11
00
127 2 return 'GENERATED BY DEFAULT AS IDENTITY';
128 }
129  
130 2 ▶sub generated_sql {
Subroutine generated_sql: called CC=3 SCAR=11.1
Pod documented
131 2 my $self = shift;
132 2 ▶ croak "Generated columns need PostgreSQL 12 or later" if ($self->server_version // 0) < 120000;
Branch: 1 branch
BranchTrueFalseTotal
if ($self->server_version // 0) < 120000 1 1 2
Condition50%1/2
$self->server_version // 0
l!l
20
MC/DC0%0/1
$self->server_version // 0
$self->server_version
Truth table50%1/2
A
A= $self->server_version
Aresult
11
00
133 1 return $self->SUPER::generated_sql(@_);
134 }
135  
136 2 ▶sub quote_binary { "'\\x" . unpack('H*', $_[2]) . "'::bytea" }
Subroutine quote_binary: called CC=1 SCAR=0.0
Pod documented
137 2 ▶sub index_using_sql { ref($_[1]) ? "USING ${$_[1]}" : "USING $_[1]" }
Subroutine index_using_sql: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 1 1 2
138 2 ▶sub collate_sql { "COLLATE " . $_[0]->quote_identifier($_[1]) }
Subroutine collate_sql: called CC=1 SCAR=0.0
Pod documented
139 2 ▶sub extension_sql { "CREATE EXTENSION IF NOT EXISTS " . $_[0]->quote_identifier($_[1]) }
Subroutine extension_sql: called CC=1 SCAR=0.0
Pod documented
140  
141 6 ▶sub version_number {
Subroutine version_number: called CC=7 SCAR=20.5
Pod documented
142 6 my $class = shift;
143 6 my ($string) = @_;
144 6 ▶ my ($major, $minor, $patch) = ($string // '') =~ m/^\s*(\d+)(?:\.(\d+))?(?:\.(\d+))?/ or return undef;
Branch: 1 branch
BranchTrueFalseTotal
() = ($string // "") =~ /^\s*(\d+)(?:\.(\d+))?(?:\.(\d+))?/ or return undef 6 0 6
Condition50%1/2
$string // ""
l!l
60
MC/DC0%0/1
$string // ""
$string
Truth table50%1/2
A
A= $string
Aresult
11
00
145 6 ▶ return $major >= 10 ? $major * 10_000 + ($minor // 0) : $major * 10_000 + ($minor // 0) * 100 + ($patch // 0);
Branch: 1 branch
BranchTrueFalseTotal
$major >= 10 ? : 1 5 6
Condition66%4/6
$minor // 0
l!l
10
$minor // 0
l!l
50
$patch // 0
l!l
23
MC/DC0%0/1
$minor // 0
$minor
MC/DC0%0/1
$minor // 0
$minor
MC/DC100%1/1
$patch // 0
$patch
Truth table50%1/2
A
A= $minor
Aresult
11
00
Truth table50%1/2
A
A= $minor
Aresult
11
00
Truth table100%2/2
A
A= $patch
Aresult
11
00
146 }
147  
148 =pod
149  
150 =over 4
151  
152 =item $key = $dialect->scan_cache_key($dbh, $namespace)
153  
154 The base key, plus what else decides what the scan sees: the host (or socket
155 directory), port, database, and user the handle resolved to, whatever the DSN
156 or the libpq environment said, the server address over TCP (C<PGHOSTADDR> can
157 pick another server under the same host name), the session's current user,
158 its search path, and C<quote_all_identifiers> (both decide how C<format_type>
159 names a type). It is undef, so nothing is cached, for a session that already
160 has temporary tables, which can shadow names on its search path.
161  
162 =back
163  
164 =cut
165  
166 0 ▶sub scan_cache_key {
Subroutine scan_cache_key: not called CC=3 SCAR=24.8
Pod documented
167 0 my $self = shift;
168 0 my ($dbh, $namespace) = @_;
169 0 my @resolved = map { $dbh->{$_} } qw/pg_host pg_port pg_db pg_user/;
170 0 my ($temp, @session) = $dbh->selectrow_array(q{SELECT pg_my_temp_schema(), inet_server_addr(), current_user, current_setting('search_path'), current_setting('quote_all_identifiers')});
171 0 ▶ return undef if $temp; # its temporary tables can shadow names on the search path for this session only
Branch: 1 branch
BranchTrueFalseTotal
if $temp 0 0 0
172 0 ▶ return join "\0" => map { $_ // '' } $self->SUPER::scan_cache_key($dbh, $namespace), @resolved, @session;
Condition0%0/2
$_ // ""
l!l
00
MC/DC0%0/1
$_ // ""
$_
Truth table0%0/2
A
A= $_
Aresult
11
00
173 }
174  
175 =pod
176  
177 =over 4
178  
179 =item $attrs = $dialect->binary_bind_attrs
180  
181 C<< {pg_type => PG_BYTEA} >>.
182  
183 =back
184  
185 =cut
186  
187 0 ▶sub binary_bind_attrs {
Subroutine binary_bind_attrs: not called CC=1 SCAR=6.9
Pod documented
188 0 require DBD::Pg;
189 0 return {pg_type => DBD::Pg::PG_BYTEA()};
190 }
191  
192 =pod
193  
194 =over 4
195  
196 =item $dialect->release_inherited($dbh)
197  
198 Points the child's copy of the handle's socket at F</dev/null> when the handle
199 is active, then sets C<InactiveDestroy>.
200  
201 DBD::Pg can still read the socket from a child: freeing a handle with an
202 async query pending, in the wrong order at exit, waits for the query's
203 result, taking it from the parent.
204  
205 =back
206  
207 =cut
208  
209 1 ▶sub release_inherited {
Subroutine release_inherited: called CC=2 SCAR=7.0
Pod documented
210 1 my $self = shift;
211 1 my ($dbh) = @_;
212 1 ▶ $self->_null_socket($dbh->{pg_socket}) if $dbh->{Active};
Branch: 1 branch
BranchTrueFalseTotal
if $dbh->{"Active"} 0 1 1
213 1 return $self->SUPER::release_inherited($dbh);
214 }
215  
216 =pod
217  
218 =over 4
219  
220 =item $dialect->init_session($dbh, $time_zone)
221  
222 Sets C<client_encoding> to C<UTF8> and turns on C<pg_enable_utf8>, then
223 C<SET TIME ZONE> and C<DateStyle> C<ISO, YMD>, so dates and times come back in
224 the form L<DateTime::Format::Pg> reads.
225  
226 =back
227  
228 =cut
229  
230 1 ▶sub init_session {
Subroutine init_session: called CC=1 SCAR=0.0
Pod documented
231 1 my $self = shift;
232 1 my ($dbh, $time_zone) = @_;
233  
234 1 $dbh->do("SET client_encoding TO 'UTF8'");
235 1 $dbh->{pg_enable_utf8} = 1;
236  
237 1 $dbh->do('SET TIME ZONE ' . $dbh->quote($time_zone));
238 1 return $dbh->do("SET DateStyle TO 'ISO, YMD'");
239 }
240  
241 =pod
242  
243 =over 4
244  
245 =item $sql = $dialect->tables_sql($dbh, $temp)
246  
247 =item $sql = $dialect->fields_sql($dbh, $temp)
248  
249 =item $sql = $dialect->pk_sql($dbh, $temp)
250  
251 =item $sql = $dialect->unique_sql($dbh, $temp)
252  
253 =item $sql = $dialect->fk_sql($dbh, $temp)
254  
255 The scan queries (see L<C<scan>|DBIx::QuickORM::Dialect/"PUBLIC METHODS">),
256 over the system catalogs for ordinary and partitioned tables, views, and
257 materialized views in C<current_schema()>, or for the one temporary object
258 named in the session's temporary schema.
259  
260 A field whose type is a domain also reports the domain's base type.
261  
262 A column the database fills has a default (C<serial> included), a domain
263 default, or an identity. One that refuses values is C<GENERATED ALWAYS AS
264 IDENTITY> or generated. Identity needs PostgreSQL 10, generated columns 12.
265  
266 Unique
267 keys come from unique indexes, without C<INCLUDE> columns (PostgreSQL 11 and
268 later, by C<server_version>). A foreign key to a
269 table in another schema names that table without its schema.
270  
271 =back
272  
273 =cut
274  
275 0 ▶sub tables_sql {
Subroutine tables_sql: not called CC=1 SCAR=6.9
Pod documented
276 0 my $self = shift;
277 0 my $scope = $self->_scope(@_);
278 0 return <<" EOT";
279 SELECT c.relname, CASE c.relkind WHEN 'v' THEN 'view' WHEN 'm' THEN 'materialized' END
280 FROM pg_class c
281 $scope
282 WHERE c.relkind IN ('r', 'p', 'v', 'm')
283 EOT
284 }
285  
286 # attidentity is PostgreSQL 10 and later, attgenerated 12 and later.
287 4 ▶sub fields_sql {
Subroutine fields_sql: called CC=3 SCAR=11.0
Pod documented
288 4 my $self = shift;
289 4 my $scope = $self->_scope(@_);
290 4 my $v = $self->server_version;
291  
292 4 ▶ my ($identity, $always) = $v >= 100000 ? ("a.attidentity <> ''", "a.attidentity = 'a'") : ('FALSE', 'FALSE');
Branch: 1 branch
BranchTrueFalseTotal
$v >= 100000 ? : 3 1 4
293 4 ▶ my $generated = $v >= 120000 ? "a.attgenerated <> ''" : 'FALSE';
Branch: 1 branch
BranchTrueFalseTotal
$v >= 120000 ? : 1 3 4
294  
295 4 return <<" EOT";
296 SELECT c.relname, a.attname, format_type(a.atttypid, a.atttypmod),
297 CASE WHEN t.typtype = 'd' THEN format_type(t.typbasetype, t.typtypmod) END,
298 CASE WHEN a.atthasdef OR t.typdefault IS NOT NULL OR $identity THEN 1 END,
299 CASE WHEN $always OR $generated THEN 1 END
300 FROM pg_attribute a
301 JOIN pg_class c ON c.oid = a.attrelid
302 $scope
303 JOIN pg_type t ON t.oid = a.atttypid
304 WHERE c.relkind IN ('r', 'p', 'v', 'm')
305 AND a.attnum > 0
306 AND NOT a.attisdropped
307 EOT
308 }
309  
310 0 ▶sub pk_sql {
Subroutine pk_sql: not called CC=1 SCAR=6.9
Pod documented
311 0 my $self = shift;
312 0 my $scope = $self->_scope(@_);
313 0 return <<" EOT";
314 SELECT c.relname, a.attname
315 FROM pg_constraint con
316 JOIN pg_class c ON c.oid = con.conrelid
317 $scope
318 JOIN generate_subscripts(con.conkey, 1) s(i) ON TRUE
319 JOIN pg_attribute a ON a.attrelid = con.conrelid AND a.attnum = con.conkey[s.i]
320 WHERE con.contype = 'p'
321 ORDER BY c.relname, s.i
322 EOT
323 }
324  
325 # indnkeyatts (PostgreSQL 11) leaves out INCLUDE columns. Older servers have none.
326 2 ▶sub unique_sql {
Subroutine unique_sql: called CC=2 SCAR=6.9
Pod documented
327 2 my $self = shift;
328 2 my $scope = $self->_scope(@_);
329 2 ▶ my $natts = $self->server_version >= 110000 ? 'indnkeyatts' : 'indnatts';
Branch: 1 branch
BranchTrueFalseTotal
$self->server_version >= 110000 ? : 1 1 2
330  
331 2 return <<" EOT";
332 SELECT c.relname, i.indexrelid, a.attname
333 FROM pg_index i
334 JOIN pg_class c ON c.oid = i.indrelid
335 $scope
336 JOIN generate_series(0, i.$natts - 1) s(i) ON TRUE
337 JOIN pg_attribute a ON a.attrelid = i.indrelid AND a.attnum = i.indkey[s.i]
338 WHERE i.indisunique
339 AND NOT i.indisprimary
340 AND i.indpred IS NULL
341 AND i.indexprs IS NULL
342 EOT
343 }
344  
345 0 ▶sub fk_sql {
Subroutine fk_sql: not called CC=1 SCAR=6.9
Pod documented
346 0 my $self = shift;
347 0 my $scope = $self->_scope(@_);
348 0 return <<" EOT";
349 SELECT c.relname, con.oid, a.attname, oc.relname, oa.attname
350 FROM pg_constraint con
351 JOIN pg_class c ON c.oid = con.conrelid
352 $scope
353 JOIN pg_class oc ON oc.oid = con.confrelid
354 JOIN generate_subscripts(con.conkey, 1) s(i) ON TRUE
355 JOIN pg_attribute a ON a.attrelid = con.conrelid AND a.attnum = con.conkey[s.i]
356 JOIN pg_attribute oa ON oa.attrelid = con.confrelid AND oa.attnum = con.confkey[s.i]
357 WHERE con.contype = 'f'
358 ORDER BY c.relname, con.oid, s.i
359 EOT
360 }
361  
362 =head1 PRIVATE METHODS
363  
364 =over 4
365  
366 =item $version = $class->_server_version($dbh)
367  
368 C<pg_server_version>, PostgreSQL's own number.
369  
370 =back
371  
372 =cut
373  
374 1 ▶sub _server_version { $_[1]->{pg_server_version} }
Subroutine _server_version: called CC=1 SCAR=0.0
375  
376 =pod
377  
378 =over 4
379  
380 =item $sql = $dialect->_scope($dbh, $temp)
381  
382 The scan queries' join to C<pg_namespace> for the relation C<c>: the current
383 schema, or with C<$temp> the session's temporary schema and that one name.
384  
385 =back
386  
387 =cut
388  
389 6 ▶sub _scope {
Subroutine _scope: called CC=2 SCAR=7.6
390 6 my $self = shift;
391 6 my ($dbh, $temp) = @_;
392 6 ▶ return " JOIN pg_namespace n ON n.oid = c.relnamespace AND n.nspname = current_schema()\n" unless defined $temp;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $temp 6 0 6
393 0 return " JOIN pg_namespace n ON n.oid = c.relnamespace AND n.oid = pg_my_temp_schema() AND c.relname = " . $dbh->quote($temp) . "\n";
394 }
395  
396 13 1;
397  
398 __END__
« blib/lib/DBIx/QuickORM/Value.pm ↑ Summary blib/lib/DBIx/QuickORM/Connection/Offline.pm »