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

filter: Statementstmt 57.6% 30 / 52 Branchbran 66.6% 8 / 12 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 47.6% 10 / 21 Podpod 100% 17 / 17 totaltotal 61.9% 65 / 105 CC 8 SCAR 11.7
CC
8
Cov
58%
scan_cache_key
12.0
release_inherited
6.0
fields_sql
3.0
CRAP 3.2
« blib/lib/DBIx/QuickORM/Value.pm ↑ Summary blib/lib/DBIx/QuickORM/Dialect/SQLite.pm »
1 package DBIx::QuickORM::Dialect::PostgreSQL;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 our $VERSION = '0.000032';
6  
7 2 ▶use parent 'DBIx::QuickORM::Dialect';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =pod
10  
11 =encoding UTF-8
12  
13 =head1 NAME
14  
15 DBIx::QuickORM::Dialect::PostgreSQL - PostgreSQL dialect.
16  
17 =head1 DESCRIPTION
18  
19 PostgreSQL through L<DBD::Pg>. It keeps the base defaults of
20 L<DBIx::QuickORM::Dialect> and adds a 65535 bind limit, async queries, and
21 C<bytea> binding. The namespace is C<current_schema()>.
22  
23 =head1 SYNOPSIS
24  
25 my $dialect = DBIx::QuickORM::Dialect->for_dbh($dbh); # this class for DBD::Pg
26  
27 =head1 PUBLIC METHODS
28  
29 =over 4
30  
31 =item $n = $dialect->bind_limit
32  
33 65535, the most parameters the wire protocol can number in one statement.
34  
35 =item $bool = $dialect->supports_isolation
36  
37 True.
38  
39 =item @sql = $dialect->begin_sql($level)
40  
41 C<BEGIN ISOLATION LEVEL $level> when given a level, otherwise C<BEGIN>.
42  
43 =item $bool = $dialect->supports_async
44  
45 True.
46  
47 =item $attrs = $dialect->async_prepare_attrs
48  
49 C<< {pg_async => PG_ASYNC} >>.
50  
51 Each async statement is prepared fresh and executed once: DBD::Pg 3.21.2 can
52 skip the execute of a re-executed server-prepared async statement, losing its
53 rows (L<https://github.com/bucardo/dbdpg/issues/217>).
54  
55 =item $bool = $dialect->async_ready($dbh, $sth)
56  
57 =item $dialect->async_result($dbh, $sth)
58  
59 =item $dialect->cancel_async($dbh, $sth)
60  
61 C<pg_ready>, C<pg_result>, and C<pg_cancel> on the database handle.
62  
63 Cancelling a query inside a transaction makes PostgreSQL abort that
64 transaction.
65  
66 =back
67  
68 =cut
69  
70 1 ▶sub supports_isolation { 1 }
Subroutine supports_isolation: called CC=1 SCAR=0.0
Pod documented
71 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
72 1 ▶sub bind_limit { 65535 }
Subroutine bind_limit: called CC=1 SCAR=0.0
Pod documented
73 0 ▶sub supports_async { 1 }
Subroutine supports_async: not called CC=1 SCAR=6.9
Pod documented
74 0 ▶sub async_prepare_attrs { {pg_async => DBD::Pg::PG_ASYNC()} }
Subroutine async_prepare_attrs: not called CC=1 SCAR=6.9
Pod documented
75 0 ▶sub async_ready { $_[1]->pg_ready }
Subroutine async_ready: not called CC=1 SCAR=6.9
Pod documented
76 0 ▶sub async_result { $_[1]->pg_result }
Subroutine async_result: not called CC=1 SCAR=6.9
Pod documented
77 0 ▶sub cancel_async { $_[1]->pg_cancel }
Subroutine cancel_async: not called CC=1 SCAR=6.9
Pod documented
78  
79 =pod
80  
81 =over 4
82  
83 =item $key = $dialect->scan_cache_key($dbh, $namespace)
84  
85 The base key, plus what else decides what the scan sees: the host (or socket
86 directory), port, database, and user the handle resolved to, whatever the DSN
87 or the libpq environment said, the server address over TCP (C<PGHOSTADDR> can
88 pick another server under the same host name), the session's current user,
89 its search path, and C<quote_all_identifiers> (both decide how C<format_type>
90 names a type). It is undef, so nothing is cached, for a session that already
91 has temporary tables, which can shadow names on its search path.
92  
93 =back
94  
95 =cut
96  
97 0 ▶sub scan_cache_key {
Subroutine scan_cache_key: not called CC=3 SCAR=24.8
Pod documented
98 0 my $self = shift;
99 0 my ($dbh, $namespace) = @_;
100 0 my @resolved = map { $dbh->{$_} } qw/pg_host pg_port pg_db pg_user/;
101 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')});
102 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
103 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
104 }
105  
106 =pod
107  
108 =over 4
109  
110 =item $attrs = $dialect->binary_bind_attrs
111  
112 C<< {pg_type => PG_BYTEA} >>.
113  
114 =back
115  
116 =cut
117  
118 0 ▶sub binary_bind_attrs {
Subroutine binary_bind_attrs: not called CC=1 SCAR=6.9
Pod documented
119 0 require DBD::Pg;
120 0 return {pg_type => DBD::Pg::PG_BYTEA()};
121 }
122  
123 =pod
124  
125 =over 4
126  
127 =item $dialect->release_inherited($dbh)
128  
129 Points the child's copy of the handle's socket at F</dev/null> when the handle
130 is active, then sets C<InactiveDestroy>.
131  
132 DBD::Pg can still read the socket from a child: freeing a handle with an
133 async query pending, in the wrong order at exit, waits for the query's
134 result, taking it from the parent.
135  
136 =back
137  
138 =cut
139  
140 0 ▶sub release_inherited {
Subroutine release_inherited: not called CC=2 SCAR=17.9
Pod documented
141 0 my $self = shift;
142 0 my ($dbh) = @_;
143 0 ▶ $self->_null_socket($dbh->{pg_socket}) if $dbh->{Active};
Branch: 1 branch
BranchTrueFalseTotal
if $dbh->{"Active"} 0 0 0
144 0 return $self->SUPER::release_inherited($dbh);
145 }
146  
147 =pod
148  
149 =over 4
150  
151 =item $dialect->init_session($dbh, $time_zone)
152  
153 Sets C<client_encoding> to C<UTF8> and turns on C<pg_enable_utf8>, then
154 C<SET TIME ZONE> and C<DateStyle> C<ISO, YMD>, so dates and times come back in
155 the form L<DateTime::Format::Pg> reads.
156  
157 =back
158  
159 =cut
160  
161 1 ▶sub init_session {
Subroutine init_session: called CC=1 SCAR=0.0
Pod documented
162 1 my $self = shift;
163 1 my ($dbh, $time_zone) = @_;
164  
165 1 $dbh->do("SET client_encoding TO 'UTF8'");
166 1 $dbh->{pg_enable_utf8} = 1;
167  
168 1 $dbh->do('SET TIME ZONE ' . $dbh->quote($time_zone));
169 1 return $dbh->do("SET DateStyle TO 'ISO, YMD'");
170 }
171  
172 =pod
173  
174 =over 4
175  
176 =item $sql = $dialect->tables_sql
177  
178 =item $sql = $dialect->fields_sql
179  
180 =item $sql = $dialect->pk_sql
181  
182 =item $sql = $dialect->unique_sql
183  
184 =item $sql = $dialect->fk_sql
185  
186 The scan queries (see L<C<scan>|DBIx::QuickORM::Dialect/"PUBLIC METHODS">),
187 over the system catalogs for ordinary and partitioned tables in
188 C<current_schema()>.
189  
190 A field whose type is a domain also reports the domain's base type.
191  
192 A column the database fills has a default (C<serial> included), a domain
193 default, or an identity. One that refuses values is C<GENERATED ALWAYS AS
194 IDENTITY> or generated. Identity needs PostgreSQL 10, generated columns 12.
195  
196 Unique
197 keys come from unique indexes, without C<INCLUDE> columns (PostgreSQL 11 and
198 later, by C<server_version>). A foreign key to a
199 table in another schema names that table without its schema.
200  
201 =back
202  
203 =cut
204  
205 2 my $IN_SCHEMA = <<'EOT';
206 JOIN pg_namespace n ON n.oid = c.relnamespace AND n.nspname = current_schema()
207 EOT
208  
209 0 ▶sub tables_sql {
Subroutine tables_sql: not called CC=1 SCAR=6.9
Pod documented
210 0 return <<" EOT";
211 SELECT c.relname
212 FROM pg_class c
213 $IN_SCHEMA
214 WHERE c.relkind IN ('r', 'p')
215 EOT
216 }
217  
218 # attidentity is PostgreSQL 10 and later, attgenerated 12 and later.
219 4 ▶sub fields_sql {
Subroutine fields_sql: called CC=3 SCAR=11.0
Pod documented
220 4 my $self = shift;
221 4 my $v = $self->server_version;
222  
223 4 ▶ my ($identity, $always) = $v >= 100000 ? ("a.attidentity <> ''", "a.attidentity = 'a'") : ('FALSE', 'FALSE');
Branch: 1 branch
BranchTrueFalseTotal
$v >= 100000 ? : 3 1 4
224 4 ▶ my $generated = $v >= 120000 ? "a.attgenerated <> ''" : 'FALSE';
Branch: 1 branch
BranchTrueFalseTotal
$v >= 120000 ? : 1 3 4
225  
226 4 return <<" EOT";
227 SELECT c.relname, a.attname, format_type(a.atttypid, a.atttypmod),
228 CASE WHEN t.typtype = 'd' THEN format_type(t.typbasetype, t.typtypmod) END,
229 CASE WHEN a.atthasdef OR t.typdefault IS NOT NULL OR $identity THEN 1 END,
230 CASE WHEN $always OR $generated THEN 1 END
231 FROM pg_attribute a
232 JOIN pg_class c ON c.oid = a.attrelid
233 $IN_SCHEMA
234 JOIN pg_type t ON t.oid = a.atttypid
235 WHERE c.relkind IN ('r', 'p')
236 AND a.attnum > 0
237 AND NOT a.attisdropped
238 EOT
239 }
240  
241 0 ▶sub pk_sql {
Subroutine pk_sql: not called CC=1 SCAR=6.9
Pod documented
242 0 return <<" EOT";
243 SELECT c.relname, a.attname
244 FROM pg_constraint con
245 JOIN pg_class c ON c.oid = con.conrelid
246 $IN_SCHEMA
247 JOIN generate_subscripts(con.conkey, 1) s(i) ON TRUE
248 JOIN pg_attribute a ON a.attrelid = con.conrelid AND a.attnum = con.conkey[s.i]
249 WHERE con.contype = 'p'
250 ORDER BY c.relname, s.i
251 EOT
252 }
253  
254 # indnkeyatts (PostgreSQL 11) leaves out INCLUDE columns. Older servers have none.
255 2 ▶sub unique_sql {
Subroutine unique_sql: called CC=2 SCAR=6.9
Pod documented
256 2 my $self = shift;
257 2 ▶ my $natts = $self->server_version >= 110000 ? 'indnkeyatts' : 'indnatts';
Branch: 1 branch
BranchTrueFalseTotal
$self->server_version >= 110000 ? : 1 1 2
258  
259 2 return <<" EOT";
260 SELECT c.relname, i.indexrelid, a.attname
261 FROM pg_index i
262 JOIN pg_class c ON c.oid = i.indrelid
263 $IN_SCHEMA
264 JOIN generate_series(0, i.$natts - 1) s(i) ON TRUE
265 JOIN pg_attribute a ON a.attrelid = i.indrelid AND a.attnum = i.indkey[s.i]
266 WHERE i.indisunique
267 AND NOT i.indisprimary
268 AND i.indpred IS NULL
269 AND i.indexprs IS NULL
270 EOT
271 }
272  
273 0 ▶sub fk_sql {
Subroutine fk_sql: not called CC=1 SCAR=6.9
Pod documented
274 0 return <<" EOT";
275 SELECT c.relname, con.oid, a.attname, oc.relname, oa.attname
276 FROM pg_constraint con
277 JOIN pg_class c ON c.oid = con.conrelid
278 $IN_SCHEMA
279 JOIN pg_class oc ON oc.oid = con.confrelid
280 JOIN generate_subscripts(con.conkey, 1) s(i) ON TRUE
281 JOIN pg_attribute a ON a.attrelid = con.conrelid AND a.attnum = con.conkey[s.i]
282 JOIN pg_attribute oa ON oa.attrelid = con.confrelid AND oa.attnum = con.confkey[s.i]
283 WHERE con.contype = 'f'
284 ORDER BY c.relname, con.oid, s.i
285 EOT
286 }
287  
288 =head1 PRIVATE METHODS
289  
290 =over 4
291  
292 =item $version = $class->_server_version($dbh)
293  
294 C<pg_server_version>, PostgreSQL's own number.
295  
296 =back
297  
298 =cut
299  
300 1 ▶sub _server_version { $_[1]->{pg_server_version} }
Subroutine _server_version: called CC=1 SCAR=0.0
301  
302 2 1;
303  
304 __END__
« blib/lib/DBIx/QuickORM/Value.pm ↑ Summary blib/lib/DBIx/QuickORM/Dialect/SQLite.pm »