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

filter: Statementstmt 68.1% 30 / 44 Branchbran 80.0% 8 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 50.0% 10 / 20 Podpod 100% 16 / 16 totaltotal 71.1% 64 / 90 CC 6 SCAR 7.7
CC
6
Cov
70%
release_inherited
6.0
fields_sql
3.0
begin_sql
2.0
CRAP 2.2
« blib/lib/DBIx/QuickORM/Type/JSON.pm ↑ Summary blib/lib/DBIx/QuickORM/Role/Type.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.000031';
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 $attrs = $dialect->binary_bind_attrs
84  
85 C<< {pg_type => PG_BYTEA} >>.
86  
87 =back
88  
89 =cut
90  
91 0 ▶sub binary_bind_attrs {
Subroutine binary_bind_attrs: not called CC=1 SCAR=6.9
Pod documented
92 0 require DBD::Pg;
93 0 return {pg_type => DBD::Pg::PG_BYTEA()};
94 }
95  
96 =pod
97  
98 =over 4
99  
100 =item $dialect->release_inherited($dbh)
101  
102 Points the child's copy of the handle's socket at F</dev/null> when the handle
103 is active, then sets C<InactiveDestroy>.
104  
105 DBD::Pg can still read the socket from a child: freeing a handle with an
106 async query pending, in the wrong order at exit, waits for the query's
107 result, taking it from the parent.
108  
109 =back
110  
111 =cut
112  
113 0 ▶sub release_inherited {
Subroutine release_inherited: not called CC=2 SCAR=17.9
Pod documented
114 0 my $self = shift;
115 0 my ($dbh) = @_;
116 0 ▶ $self->_null_socket($dbh->{pg_socket}) if $dbh->{Active};
Branch: 1 branch
BranchTrueFalseTotal
if $dbh->{"Active"} 0 0 0
117 0 return $self->SUPER::release_inherited($dbh);
118 }
119  
120 =pod
121  
122 =over 4
123  
124 =item $dialect->init_session($dbh, $time_zone)
125  
126 Sets C<client_encoding> to C<UTF8> and turns on C<pg_enable_utf8>, then
127 C<SET TIME ZONE> and C<DateStyle> C<ISO, YMD>, so dates and times come back in
128 the form L<DateTime::Format::Pg> reads.
129  
130 =back
131  
132 =cut
133  
134 1 ▶sub init_session {
Subroutine init_session: called CC=1 SCAR=0.0
Pod documented
135 1 my $self = shift;
136 1 my ($dbh, $time_zone) = @_;
137  
138 1 $dbh->do("SET client_encoding TO 'UTF8'");
139 1 $dbh->{pg_enable_utf8} = 1;
140  
141 1 $dbh->do('SET TIME ZONE ' . $dbh->quote($time_zone));
142 1 return $dbh->do("SET DateStyle TO 'ISO, YMD'");
143 }
144  
145 =pod
146  
147 =over 4
148  
149 =item $sql = $dialect->tables_sql
150  
151 =item $sql = $dialect->fields_sql
152  
153 =item $sql = $dialect->pk_sql
154  
155 =item $sql = $dialect->unique_sql
156  
157 =item $sql = $dialect->fk_sql
158  
159 The scan queries (see L<C<scan>|DBIx::QuickORM::Dialect/"PUBLIC METHODS">),
160 over the system catalogs for ordinary and partitioned tables in
161 C<current_schema()>.
162  
163 A field whose type is a domain also reports the domain's base type.
164  
165 A column the database fills has a default (C<serial> included), a domain
166 default, or an identity. One that refuses values is C<GENERATED ALWAYS AS
167 IDENTITY> or generated. Identity needs PostgreSQL 10, generated columns 12.
168  
169 Unique
170 keys come from unique indexes, without C<INCLUDE> columns (PostgreSQL 11 and
171 later, by C<server_version>). A foreign key to a
172 table in another schema names that table without its schema.
173  
174 =back
175  
176 =cut
177  
178 2 my $IN_SCHEMA = <<'EOT';
179 JOIN pg_namespace n ON n.oid = c.relnamespace AND n.nspname = current_schema()
180 EOT
181  
182 0 ▶sub tables_sql {
Subroutine tables_sql: not called CC=1 SCAR=6.9
Pod documented
183 0 return <<" EOT";
184 SELECT c.relname
185 FROM pg_class c
186 $IN_SCHEMA
187 WHERE c.relkind IN ('r', 'p')
188 EOT
189 }
190  
191 # attidentity is PostgreSQL 10 and later, attgenerated 12 and later.
192 4 ▶sub fields_sql {
Subroutine fields_sql: called CC=3 SCAR=11.0
Pod documented
193 4 my $self = shift;
194 4 my $v = $self->server_version;
195  
196 4 ▶ my ($identity, $always) = $v >= 100000 ? ("a.attidentity <> ''", "a.attidentity = 'a'") : ('FALSE', 'FALSE');
Branch: 1 branch
BranchTrueFalseTotal
$v >= 100000 ? : 3 1 4
197 4 ▶ my $generated = $v >= 120000 ? "a.attgenerated <> ''" : 'FALSE';
Branch: 1 branch
BranchTrueFalseTotal
$v >= 120000 ? : 1 3 4
198  
199 4 return <<" EOT";
200 SELECT c.relname, a.attname, format_type(a.atttypid, a.atttypmod),
201 CASE WHEN t.typtype = 'd' THEN format_type(t.typbasetype, t.typtypmod) END,
202 CASE WHEN a.atthasdef OR t.typdefault IS NOT NULL OR $identity THEN 1 END,
203 CASE WHEN $always OR $generated THEN 1 END
204 FROM pg_attribute a
205 JOIN pg_class c ON c.oid = a.attrelid
206 $IN_SCHEMA
207 JOIN pg_type t ON t.oid = a.atttypid
208 WHERE c.relkind IN ('r', 'p')
209 AND a.attnum > 0
210 AND NOT a.attisdropped
211 EOT
212 }
213  
214 0 ▶sub pk_sql {
Subroutine pk_sql: not called CC=1 SCAR=6.9
Pod documented
215 0 return <<" EOT";
216 SELECT c.relname, a.attname
217 FROM pg_constraint con
218 JOIN pg_class c ON c.oid = con.conrelid
219 $IN_SCHEMA
220 JOIN generate_subscripts(con.conkey, 1) s(i) ON TRUE
221 JOIN pg_attribute a ON a.attrelid = con.conrelid AND a.attnum = con.conkey[s.i]
222 WHERE con.contype = 'p'
223 ORDER BY c.relname, s.i
224 EOT
225 }
226  
227 # indnkeyatts (PostgreSQL 11) leaves out INCLUDE columns. Older servers have none.
228 2 ▶sub unique_sql {
Subroutine unique_sql: called CC=2 SCAR=6.9
Pod documented
229 2 my $self = shift;
230 2 ▶ my $natts = $self->server_version >= 110000 ? 'indnkeyatts' : 'indnatts';
Branch: 1 branch
BranchTrueFalseTotal
$self->server_version >= 110000 ? : 1 1 2
231  
232 2 return <<" EOT";
233 SELECT c.relname, i.indexrelid, a.attname
234 FROM pg_index i
235 JOIN pg_class c ON c.oid = i.indrelid
236 $IN_SCHEMA
237 JOIN generate_series(0, i.$natts - 1) s(i) ON TRUE
238 JOIN pg_attribute a ON a.attrelid = i.indrelid AND a.attnum = i.indkey[s.i]
239 WHERE i.indisunique
240 AND NOT i.indisprimary
241 AND i.indpred IS NULL
242 AND i.indexprs IS NULL
243 EOT
244 }
245  
246 0 ▶sub fk_sql {
Subroutine fk_sql: not called CC=1 SCAR=6.9
Pod documented
247 0 return <<" EOT";
248 SELECT c.relname, con.oid, a.attname, oc.relname, oa.attname
249 FROM pg_constraint con
250 JOIN pg_class c ON c.oid = con.conrelid
251 $IN_SCHEMA
252 JOIN pg_class oc ON oc.oid = con.confrelid
253 JOIN generate_subscripts(con.conkey, 1) s(i) ON TRUE
254 JOIN pg_attribute a ON a.attrelid = con.conrelid AND a.attnum = con.conkey[s.i]
255 JOIN pg_attribute oa ON oa.attrelid = con.confrelid AND oa.attnum = con.confkey[s.i]
256 WHERE con.contype = 'f'
257 ORDER BY c.relname, con.oid, s.i
258 EOT
259 }
260  
261 =head1 PRIVATE METHODS
262  
263 =over 4
264  
265 =item $version = $class->_server_version($dbh)
266  
267 C<pg_server_version>, PostgreSQL's own number.
268  
269 =back
270  
271 =cut
272  
273 1 ▶sub _server_version { $_[1]->{pg_server_version} }
Subroutine _server_version: called CC=1 SCAR=0.0
274  
275 2 1;
276  
277 __END__
« blib/lib/DBIx/QuickORM/Type/JSON.pm ↑ Summary blib/lib/DBIx/QuickORM/Role/Type.pm »