blib/lib/DBIx/QuickORM/Dialect.pm

filter: Statementstmt 93.3% 195 / 209 Branchbran 84.6% 44 / 52 Conditioncond 73.1% 30 / 41 MC/DCmcdc 43.4% 10 / 23 Subroutinesub 95.6% 44 / 46 Podpod 100% 30 / 30 totaltotal 88.0% 353 / 401 CC 98 SCAR 15.6
CC
98
Cov
89%
_null_socket
11.6
_null_socket
11.6
_add_fks
9.0
CRAP 4.8
« blib/lib/DBIx/QuickORM/Type/UUID.pm ↑ Summary blib/lib/DBIx/QuickORM/JoinRow.pm »
1 package DBIx::QuickORM::Dialect;
2 314 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 314 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 314 our $VERSION = '0.000032';
6  
7 314 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 314 ▶use DBI();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 314 ▶use POSIX();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 314 ▶use Object::HashBase qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
12 <server_version
13 314 };
14  
15 =pod
16  
17 =encoding UTF-8
18  
19 =head1 NAME
20  
21 DBIx::QuickORM::Dialect - Base for the per-database facts and session setup.
22  
23 =head1 DESCRIPTION
24  
25 A dialect holds what the ORM needs to know about one database: quoting,
26 savepoints, C<RETURNING>, bind limits, error codes, session setup, and how to
27 scan the schema. Each supported database has one under
28 C<DBIx::QuickORM::Dialect::>. C<for_dbh> picks the one for a handle.
29  
30 C<for_dbh> returns a dialect object for one connection. Its only state is the
31 server's version (C<server_version>), read once at connect, so a method can
32 depend on it. Methods that need no server fact also work on the class.
33  
34 This base class holds the defaults most databases share: double-quoted
35 identifiers, savepoints, C<RETURNING> on every statement, and
36 C<current_schema()> as the namespace. A subclass overrides only what differs.
37  
38 Every dialect provides C<init_session($dbh, $time_zone)>, which turns on
39 UTF-8 for the session and sets the session time zone where the database has
40 one. C<$time_zone> is a zone name such as C<UTC> or C<America/New_York>,
41 passed to the database as given, except that MySQL and MariaDB get C<UTC> as
42 C<+00:00>.
43  
44 =head1 SYNOPSIS
45  
46 my $dialect = DBIx::QuickORM::Dialect->for_dbh($dbh);
47 $dialect->init_session($dbh, 'UTC');
48  
49 my $modern = $dialect->server_version >= 110000; # PostgreSQL 11 or later
50  
51 my $sql = 'SELECT * FROM ' . $dialect->quote_identifier('users');
52  
53 =head1 ATTRIBUTES
54  
55 =over 4
56  
57 =item $version = $dialect->server_version
58  
59 The server's version as one integer, so versions compare with C<< >= >>:
60  
61 =over 4
62  
63 =item *
64  
65 PostgreSQL: its own number, C<pg_server_version> (C<110000> is 11.0, C<90603>
66 is 9.6.3).
67  
68 =item *
69  
70 MySQL, MariaDB, SQLite, DuckDB: C<major * 1_000_000 + minor * 1_000 + patch>,
71 from the server's version string (C<8.0.36> is C<8000036>).
72  
73 =back
74  
75 Undef when the server's version string cannot be read.
76  
77 =back
78  
79 =head1 PUBLIC METHODS
80  
81 =over 4
82  
83 =item $dialect = DBIx::QuickORM::Dialect->for_dbh($dbh)
84  
85 The dialect object for a connected handle, chosen by driver name, with the
86 server's version read from the handle.
87  
88 The chosen class may refine the choice from the server (C<dialect_for_dbh>).
89 L<DBD::MariaDB> gives the MariaDB dialect for a MariaDB server, the MySQL one
90 otherwise. An unsupported driver croaks, naming the supported ones.
91  
92 =back
93  
94 =cut
95  
96 314 my %DRIVERS = (
97 Pg => 'PostgreSQL',
98 MariaDB => 'MySQL',
99 SQLite => 'SQLite',
100 DuckDB => 'DuckDB',
101 );
102  
103 369 ▶sub for_dbh {
Subroutine for_dbh: called CC=2 SCAR=9.2
Pod documented
104 0 ▶ my $class = shift;
Subroutine for_dbh: not called CC=2 SCAR=9.2
Pod documented
105 0 my ($dbh) = @_;
106  
107 0 my $driver = $dbh->{Driver}->{Name};
108 0 ▶ my $name = $DRIVERS{$driver} or croak "Unsupported database driver 'DBD::$driver', supported drivers are: " . join(', ' => map { "DBD::$_" } sort keys %DRIVERS);
Branch: 2 branches
BranchTrueFalseTotal
my $name = $DRIVERS{$driver} or croak("Unsupported database driver 'DBD::${driver}', supported drivers are: " . join(", ", map({"DBD::$_";} sort(keys %DRIVERS)))) 367 2 369
my $name = $DRIVERS{$driver} or croak("Unsupported database driver 'DBD::${driver}', supported drivers are: " . join(", ", map({"DBD::$_";} sort(keys %DRIVERS)))) 0 0 0
109  
110 0 require "DBIx/QuickORM/Dialect/$name.pm";
111 0 my $dialect = "DBIx::QuickORM::Dialect::$name"->dialect_for_dbh($dbh);
112 0 return $dialect->new(server_version => $dialect->_server_version($dbh));
113 }
114  
115 =pod
116  
117 =over 4
118  
119 =item $name = $dialect->name
120  
121 The dialect's short name, the last part of its class, such as C<PostgreSQL>,
122 for messages.
123  
124 =item $class = $dialect->dialect_for_dbh($dbh)
125  
126 The dialect to use for this handle: the dialect itself, unless a subclass
127 looks at the server.
128  
129 =item $char = $dialect->quote_char
130  
131 The character that quotes identifiers. C<"> by default.
132  
133 =item $bool = $dialect->supports_savepoints
134  
135 Whether the database has savepoints, so transactions can nest. True by
136 default.
137  
138 =item $bool = $dialect->supports_isolation
139  
140 Whether C<begin_sql> can set a transaction isolation level. False by default
141 (SQLite, DuckDB). Where it is true, a transaction states a level every time it
142 opens (see L<DBIx::QuickORM::Manual::Transactions/ISOLATION>).
143  
144 =item $bool = $dialect->supports_returning_insert
145  
146 =item $bool = $dialect->supports_returning_update
147  
148 =item $bool = $dialect->supports_returning_delete
149  
150 Whether C<INSERT>, C<UPDATE>, and C<DELETE> take a C<RETURNING> clause. True
151 by default.
152  
153 =item $bool = $dialect->having_uses_alias
154  
155 Whether a C<having> naming a computed column sends that name to the database.
156 False by default: the SQL builder writes the column's SQL in its place, which
157 works everywhere (PostgreSQL rejects a select alias in C<HAVING>, and SQLite
158 lets a table column of the same name win over it).
159  
160 =item $n = $dialect->bind_limit
161  
162 The most binds one statement may hold. 999 by default, the lowest limit of
163 the supported databases (SQLite before 3.32.0).
164  
165 =item ($parser, $formatter, \%instants) = $dialect->datetime_format
166  
167 The L<DateTime::Format> classes that parse the database's date and timestamp
168 values and format values for it. L<DateTime::Format::Pg> for both by default.
169  
170 A formatter of undef means L<DBIx::QuickORM::Type::DateTime> formats values
171 itself.
172  
173 C<%instants> holds the base types without a zone in their name that still
174 store an instant, such as C<< {timestamp => 1} >>. None by default.
175  
176 =back
177  
178 =cut
179  
180 64 ▶sub name { ((ref($_[0]) || $_[0]) =~ m/(\w+)$/)[0] }
Subroutine name: called CC=2 SCAR=7.2
Pod documented
Condition66%2/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
6040
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table66%2/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
181 368 ▶sub dialect_for_dbh { $_[0] }
Subroutine dialect_for_dbh: called CC=1 SCAR=0.0
Pod documented
182 4756 ▶sub quote_char { '"' }
Subroutine quote_char: called CC=1 SCAR=0.0
Pod documented
183 48 ▶sub supports_savepoints { 1 }
Subroutine supports_savepoints: called CC=1 SCAR=0.0
Pod documented
184 118 ▶sub supports_isolation { 0 }
Subroutine supports_isolation: called CC=1 SCAR=0.0
Pod documented
185 195 ▶sub supports_returning_insert { 1 }
Subroutine supports_returning_insert: called CC=1 SCAR=0.0
Pod documented
186 116 ▶sub supports_returning_update { 1 }
Subroutine supports_returning_update: called CC=1 SCAR=0.0
Pod documented
187 17 ▶sub supports_returning_delete { 1 }
Subroutine supports_returning_delete: called CC=1 SCAR=0.0
Pod documented
188 15 ▶sub having_uses_alias { 0 }
Subroutine having_uses_alias: called CC=1 SCAR=0.0
Pod documented
189 72 ▶sub bind_limit { 999 }
Subroutine bind_limit: called CC=1 SCAR=0.0
Pod documented
190 1 ▶sub datetime_format { ('DateTime::Format::Pg', 'DateTime::Format::Pg') }
Subroutine datetime_format: called CC=1 SCAR=0.0
Pod documented
191  
192 =pod
193  
194 =over 4
195  
196 =item @sql = $dialect->begin_sql
197  
198 =item @sql = $dialect->begin_sql($level)
199  
200 The statements that open a root transaction: C<BEGIN> by default.
201  
202 C<$level> is an upper-case isolation level, such as C<SERIALIZABLE>. The
203 Connection passes one for every root transaction where the dialect
204 C<supports_isolation>, and none for C<< isolation => 'server' >>. The base
205 croaks when one is given: the database has no level to set (SQLite, DuckDB).
206  
207 =back
208  
209 =cut
210  
211 124 ▶sub begin_sql {
Subroutine begin_sql: called CC=2 SCAR=6.9
Pod documented
212 124 my $self = shift;
213 124 my ($level) = @_;
214 124 ▶ croak "The " . $self->name . " dialect cannot set a transaction isolation level" if $level;
Branch: 1 branch
BranchTrueFalseTotal
if $level 6 118 124
215 118 return ('BEGIN');
216 }
217  
218 =pod
219  
220 =over 4
221  
222 =item $sql = $dialect->quote_identifier($name)
223  
224 C<$name> quoted with C<quote_char>, doubling any quote character inside it.
225  
226 =back
227  
228 =cut
229  
230 4470 ▶sub quote_identifier {
Subroutine quote_identifier: called CC=1 SCAR=0.0
Pod documented
231 4470 my $self = shift;
232 4470 my ($name) = @_;
233  
234 4470 my $q = $self->quote_char;
235 4470 $name =~ s/\Q$q\E/$q$q/g;
236 4470 return "$q$name$q";
237 }
238  
239 =pod
240  
241 =over 4
242  
243 =item $ns = $dialect->namespace($dbh)
244  
245 The namespace the connection works in, or undef when it has none.
246 C<current_schema()> by default.
247  
248 =back
249  
250 =cut
251  
252 1 ▶sub namespace {
Subroutine namespace: called CC=1 SCAR=0.0
Pod documented
253 1 my $self = shift;
254 1 my ($dbh) = @_;
255 1 my ($ns) = $dbh->selectrow_array('SELECT current_schema()');
256 1 return $ns;
257 }
258  
259 =pod
260  
261 =over 4
262  
263 =item $dialect->release_inherited($dbh)
264  
265 Lets go of a handle a forked child inherited without closing the parent's
266 session. Sets C<InactiveDestroy> by default.
267  
268 =back
269  
270 =cut
271  
272 10 ▶sub release_inherited {
Subroutine release_inherited: called CC=1 SCAR=0.0
Pod documented
273 10 my $self = shift;
274 10 my ($dbh) = @_;
275 10 $dbh->{InactiveDestroy} = 1;
276 10 return;
277 }
278  
279 =pod
280  
281 =over 4
282  
283 =item $bool = $dialect->supports_second_connection($dbh)
284  
285 Whether another connection, from this process or a forked child, reaches the
286 same data as this handle. True by default.
287  
288 =item $bool = $dialect->supports_async
289  
290 Whether a query can run on the handle without waiting for it. False by
291 default.
292  
293 A dialect that says true also provides C<async_prepare_attrs> (the C<prepare>
294 attributes for such a statement, which is executed once),
295 C<async_ready($dbh, $sth)>, C<async_result($dbh, $sth)> (waits for the
296 result), and C<cancel_async($dbh, $sth)>.
297  
298 =back
299  
300 =cut
301  
302 1 ▶sub supports_second_connection { 1 }
Subroutine supports_second_connection: called CC=1 SCAR=0.0
Pod documented
303  
304 =pod
305  
306 =over 4
307  
308 =item $key = $dialect->scan_cache_key($dbh, $namespace)
309  
310 Which database a scan of this handle describes, as a string, or undef when no
311 other connection could see the same tables. The Connection keys its scan cache
312 by it (see L<DBIx::QuickORM::Connection/"The scan cache">).
313  
314 By default it joins the driver, the DSN (C<< $dbh->{Name} >>, holding the host,
315 port or socket, and database), the user name, and the namespace. A dialect
316 adds whatever else changes what its scan sees.
317  
318 =back
319  
320 =cut
321  
322 0 ▶sub scan_cache_key {
Subroutine scan_cache_key: not called CC=2 SCAR=17.9
Pod documented
323 0 my $self = shift;
324 0 my ($dbh, $namespace) = @_;
325 0 ▶ return join "\0" => map { $_ // '' } $dbh->{Driver}->{Name}, $dbh->{Name}, $dbh->{Username}, $namespace;
Condition0%0/2
$_ // ""
l!l
00
MC/DC0%0/1
$_ // ""
$_
Truth table0%0/2
A
A= $_
Aresult
11
00
326 }
327 30 ▶sub supports_async { 0 }
Subroutine supports_async: called CC=1 SCAR=0.0
Pod documented
328  
329 =pod
330  
331 =over 4
332  
333 =item $dialect->text_from_db(@values)
334  
335 =item $dialect->text_to_db(@values)
336  
337 Convert text in place, as it is fetched from the database and before it is
338 sent, for drivers that do not exchange character strings themselves. Both do
339 nothing by default.
340  
341 Callers pass only text, never values of binary fields.
342  
343 =back
344  
345 =cut
346  
347 4287 ▶sub text_from_db { }
Subroutine text_from_db: called CC=1 SCAR=0.0
Pod documented
348 1329 ▶sub text_to_db { }
Subroutine text_to_db: called CC=1 SCAR=0.0
Pod documented
349  
350 =pod
351  
352 =over 4
353  
354 =item $attrs = $dialect->binary_bind_attrs
355  
356 The C<bind_param> attributes for a binary value. C<< {TYPE => SQL_BLOB} >> by
357 default.
358  
359 =back
360  
361 =cut
362  
363 49 ▶sub binary_bind_attrs { {TYPE => DBI::SQL_BLOB()} }
Subroutine binary_bind_attrs: called CC=1 SCAR=0.0
Pod documented
364  
365 =pod
366  
367 =over 4
368  
369 =item $attrs = $dialect->bind_attrs(\%field)
370  
371 The C<bind_param> attributes for a value going to the field (a map field
372 record, or undef): C<binary_bind_attrs> for a field of C<binary> affinity,
373 otherwise undef.
374  
375 =back
376  
377 =cut
378  
379 1585 ▶sub bind_attrs {
Subroutine bind_attrs: called CC=4 SCAR=13.9
Pod documented
380 1585 my $self = shift;
381 1585 my ($field) = @_;
382 1585 ▶ return undef unless $field && ($field->{affinity} // '') eq 'binary';
Branch: 1 branch
BranchTrueFalseTotal
unless $field and ($field->{"affinity"} // "") eq "binary" 1537 48 1585
Condition80%4/5
$field and ($field->{"affinity"} // "") eq "binary"
!ll&&!rl&&r
2153548
$field->{"affinity"} // ""
l!l
15830
MC/DC50%1/2
$field and ($field->{"affinity"} // "") eq "binary"
$field $field->{"affinity"}
Truth table66%2/3
A and B
A= $fieldB= $field->{"affinity"}
ABresult
0X0
100
111
383 48 return $self->binary_bind_attrs;
384 }
385  
386 =pod
387  
388 =over 4
389  
390 =item $id = $dialect->last_insert_id($dbh, $table, $field)
391  
392 The value the database generated for the field by the last insert.
393  
394 =back
395  
396 =cut
397  
398 2 ▶sub last_insert_id {
Subroutine last_insert_id: called CC=1 SCAR=0.0
Pod documented
399 2 my $self = shift;
400 2 my ($dbh, $table, $field) = @_;
401 2 return $dbh->last_insert_id(undef, undef, $table, $field);
402 }
403  
404 =pod
405  
406 =over 4
407  
408 =item \%kinds = $dialect->native_error_kinds
409  
410 Native error codes mapped to error kinds. Empty by default.
411  
412 =back
413  
414 =cut
415  
416 314 my %STATE_KIND = (
417 '40001' => 'serialization_failure',
418 '40P01' => 'deadlock',
419 '55P03' => 'lock_timeout',
420 '23505' => 'unique_violation',
421 '23503' => 'foreign_key_violation',
422 '23502' => 'not_null_violation',
423 '23514' => 'check_violation',
424 );
425  
426 11 ▶sub native_error_kinds { {} }
Subroutine native_error_kinds: called CC=1 SCAR=0.0
Pod documented
427  
428 =pod
429  
430 =over 4
431  
432 =item $kind = $dialect->error_kind($native, $state, $errstr)
433  
434 The kind of a database error (see L<DBIx::QuickORM::Exception::DB>), or undef.
435  
436 The native code is looked up in C<native_error_kinds> first, then the
437 SQLSTATE. C<$errstr> is unused here. The SQLSTATEs known:
438  
439 =for highlighter language=text
440  
441 40001 serialization_failure
442 40P01 deadlock
443 55P03 lock_timeout
444 23505 unique_violation
445 23503 foreign_key_violation
446 23502 not_null_violation
447 23514 check_violation
448  
449 =back
450  
451 =cut
452  
453 64 ▶sub error_kind {
Subroutine error_kind: called CC=4 SCAR=13.9
Pod documented
454 64 my $self = shift;
455 64 my ($native, $state) = @_;
456  
457 64 ▶ my $kind = defined($native) ? $self->native_error_kinds->{$native} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $native ? : 63 1 64
458 64 ▶ $kind //= $STATE_KIND{$state} if defined $state;
Branch: 1 branch
BranchTrueFalseTotal
if defined $state 63 1 64
Condition100%3/3
$kind //= $STATE_KIND{$state}
l!l&&r!l&&!r
44910
MC/DC100%2/2
$kind //= $STATE_KIND{$state}
$kind $STATE_KIND{$state}
Truth table100%3/3
A //= B
A= $kindB= $STATE_KIND{$state}
ABresult
1X1
011
000
459 64 return $kind;
460 }
461  
462 =pod
463  
464 =over 4
465  
466 =item \%tables = $dialect->scan($dbh)
467  
468 Scans the tables of the connection's namespace and returns them as plain map
469 data:
470  
471 =for highlighter language=perl
472  
473 {
474 users => {
475 fields => {
476 id => {
477 table => 'users',
478 name => 'id',
479 affinity => 'numeric',
480 db_type => 'integer',
481 type => 'integer',
482 },
483 ...
484 },
485 pk => ['id'], # key order, or undef when there is none
486 unique => [['email'], ['a', 'b']], # each sorted, and the primary key is not repeated
487 fk => {orgs => [{org_id => 'id'}]},
488 },
489 }
490  
491 Views, temporary tables, and system tables are left out. So are partial and
492 expression unique indexes, since they do not make their columns unique across
493 all rows.
494  
495 The scan runs one query per kind of metadata (DuckDB two for unique keys).
496 Each dialect provides the SQL through these methods, each given the handle
497 but C<unique_sql>:
498  
499 =over 4
500  
501 =item C<tables_sql>: C<(table)> rows.
502  
503 =item C<fields_sql>: C<(table, field, db_type, base_db_type, db_fills, db_refuses_value)> rows. C<base_db_type> is the type a user-defined type stands for, or undef. The last two are true or undef (see C<field_facts>).
504  
505 =item C<pk_sql>: C<(table, field)> rows in key order.
506  
507 =item C<unique_sql>: C<(table, key, field)> rows, with an undef field for a key that does not cover whole columns.
508  
509 =item C<fk_sql>: C<(table, key, field, other_table, other_field)> rows in key order, with an undef other field for a key on the other table's primary key.
510  
511 =back
512  
513 =back
514  
515 =cut
516  
517 237 ▶sub scan {
Subroutine scan: called CC=6 SCAR=17.9
Pod documented
518 237 my $self = shift;
519 237 my ($dbh) = @_;
520  
521 237 my %tables = map { $_->[0] => {fields => {}, pk => undef, unique => [], fk => {}} } @{$self->_rows($dbh, $self->tables_sql($dbh))};
522  
523 237 for my $row (@{$self->_rows($dbh, $self->fields_sql($dbh))}) {
524 1834 my ($table, $field, @types) = @$row;
525 1834 ▶ my $t = $tables{$table} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $t = $tables{$table} or next 1833 1 1834
526 1833 $t->{fields}->{$field} = {%{$self->field_facts(@types)}, table => $table, name => $field};
527 }
528  
529 237 for my $row (@{$self->_rows($dbh, $self->pk_sql($dbh))}) {
530 618 my ($table, $field) = @$row;
531 618 ▶ my $t = $tables{$table} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $t = $tables{$table} or next 618 0 618
532 618 ▶ push @{$t->{pk} //= []} => $field;
Condition100%2/2
$t->{"pk"} //= []
l!l
91527
MC/DC100%1/1
$t->{"pk"} //= []
$t->{"pk"}
Truth table100%2/2
A
A= $t->{"pk"}
Aresult
11
00
533 }
534  
535 237 $self->_add_unique(\%tables, $self->_keys_by_table($self->_unique_rows($dbh)));
536 237 $self->_add_fks(\%tables, $self->_keys_by_table($self->_rows($dbh, $self->fk_sql($dbh))));
537  
538 237 return \%tables;
539 }
540  
541 =pod
542  
543 =over 4
544  
545 =item \%field = $dialect->field_facts($db_type, $base_db_type, $db_fills, $db_refuses_value)
546  
547 The map's facts about a field:
548  
549 =over 4
550  
551 =item C<db_type>: as given.
552  
553 =item C<type>: the lowercased base name, without sizes, array markers, C<unsigned>, or C<zerofill>.
554  
555 =item C<size>: the number the first parentheses start with, when they do.
556  
557 =item C<affinity>: from C<$base_db_type> when given, otherwise from C<$db_type>.
558  
559 =item C<db_fills>: 1 when the database fills the column when an insert leaves it out (a default, identity, serial, auto-increment, rowid alias, or generated column). Absent otherwise.
560  
561 =item C<db_refuses_value>: 1 when the database rejects an explicit value for the column (PostgreSQL C<GENERATED ALWAYS AS IDENTITY>, generated columns). Absent otherwise.
562  
563 =back
564  
565 =back
566  
567 =cut
568  
569 1859 ▶sub field_facts {
Subroutine field_facts: called CC=5 SCAR=16.1
Pod documented
570 1859 my $self = shift;
571 1859 my ($db_type, $base, $fills, $refuses) = @_;
572  
573 1859 my ($type, $size) = $self->_base_type($db_type);
574 1859 ▶ my %field = (db_type => $db_type, type => $type, affinity => $self->affinity(defined($base) ? $base : $db_type));
Branch: 1 branch
BranchTrueFalseTotal
defined $base ? : 1 1858 1859
575 1859 ▶ $field{size} = $size if defined $size;
Branch: 1 branch
BranchTrueFalseTotal
if defined $size 344 1515 1859
576 1859 ▶ $field{db_fills} = 1 if $fills;
Branch: 1 branch
BranchTrueFalseTotal
if $fills 400 1459 1859
577 1859 ▶ $field{db_refuses_value} = 1 if $refuses;
Branch: 1 branch
BranchTrueFalseTotal
if $refuses 6 1853 1859
578  
579 1859 return \%field;
580 }
581  
582 my %AFFINITY = (
583 5652 (map { $_ => 'string' } qw/bit char character string text bpchar clob nchar nvarchar enum set json jsonb money uuid xml citext name/),
584 942 (map { $_ => 'string' } 'character varying', 'national character', 'national character varying'),
585 2512 (map { $_ => 'string' } qw/date datetime interval time timestamp timestamptz timetz year/),
586 1256 (map { $_ => 'string' } 'time with time zone', 'time without time zone', 'timestamp with time zone', 'timestamp without time zone'),
587 942 (map { $_ => 'binary' } qw/binary blob bytea/),
588 7222 (map { $_ => 'numeric' } qw/dec decimal double float float4 float8 hugeint int int2 int4 int8 integer tinyint smallint mediumint bigint number numeric real serial serial4 serial8 uhugeint/),
589 314 (map { $_ => 'numeric' } 'double precision'),
590 314 (map { $_ => 'boolean' } qw/bool boolean/),
591 );
592  
593 =pod
594  
595 =over 4
596  
597 =item $affinity = $dialect->affinity($db_type)
598  
599 C<string>, C<numeric>, C<binary>, or C<boolean>.
600  
601 Dates, times, JSON, and UUIDs are C<string>, and so is any type this does not
602 know. C<bit> is C<string>: PostgreSQL returns bit strings and MySQL raw
603 bytes, neither of which compares as a number.
604  
605 =back
606  
607 =cut
608  
609 26 ▶sub affinity {
Subroutine affinity: called CC=2 SCAR=6.9
Pod documented
610 26 my $self = shift;
611 26 my ($db_type) = @_;
612 26 ▶ return $self->_known_affinity($db_type) // 'string';
Condition100%2/2
$self->_known_affinity($db_type) // "string"
l!l
242
MC/DC100%1/1
$self->_known_affinity($db_type) // "string"
$self->_known_affinity($db_type)
Truth table100%2/2
A
A= $self->_known_affinity($db_type)
Aresult
11
00
613 }
614  
615 =head1 PRIVATE METHODS
616  
617 =over 4
618  
619 =item $version = $class->_server_version($dbh)
620  
621 The server's version for C<server_version>. By default C<SELECT version()>,
622 through C<_version_number>.
623  
624 =item $version = $class->_version_number($string)
625  
626 The first C<major.minor[.patch]> in the string as
627 C<major * 1_000_000 + minor * 1_000 + patch>, or undef when it has none, so
628 an unreadable version string does not refuse the connection.
629  
630 =back
631  
632 =cut
633  
634 1 ▶sub _server_version {
Subroutine _server_version: called CC=1 SCAR=0.0
635 1 my $class = shift;
636 1 my ($dbh) = @_;
637 1 my ($version) = $dbh->selectrow_array('SELECT version()');
638 1 return $class->_version_number($version);
639 }
640  
641 376 ▶sub _version_number {
Subroutine _version_number: called CC=4 SCAR=13.9
642 376 my $class = shift;
643 376 my ($string) = @_;
644 376 ▶ my ($major, $minor, $patch) = ($string // '') =~ m/(\d+)\.(\d+)(?:\.(\d+))?/ or return undef;
Branch: 1 branch
BranchTrueFalseTotal
() = ($string // "") =~ /(\d+)\.(\d+)(?:\.(\d+))?/ or return undef 373 3 376
Condition100%2/2
$string // ""
l!l
3751
MC/DC100%1/1
$string // ""
$string
Truth table100%2/2
A
A= $string
Aresult
11
00
645 373 ▶ return $major * 1_000_000 + $minor * 1_000 + ($patch // 0);
Condition100%2/2
$patch // 0
l!l
3721
MC/DC100%1/1
$patch // 0
$patch
Truth table100%2/2
A
A= $patch
Aresult
11
00
646 }
647  
648 =pod
649  
650 =over 4
651  
652 =item $dialect->_null_socket($fd)
653  
654 Points the file descriptor at F</dev/null>, so nothing this process does with
655 an inherited socket reaches the parent's session. Does nothing for undef or a
656 negative descriptor.
657  
658 =back
659  
660 =cut
661  
662 1 ▶sub _null_socket {
Subroutine _null_socket: called CC=5 SCAR=24.5
663 1 my $self = shift;
664 1 my ($fd) = @_;
665 1 ▶ return unless defined($fd) && $fd >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fd and $fd >= 0 1 0 1
Condition33%1/3
defined $fd and $fd >= 0
!ll&&!rl&&r
100
MC/DC0%0/2
defined $fd and $fd >= 0
defined $fd $fd >= 0
Truth table33%1/3
A and B
A= defined $fdB= $fd >= 0
ABresult
0X0
100
111
666 0 ▶ open(my $null, '+<', '/dev/null') or die "Could not open /dev/null: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $null, "+<", "/dev/null" or die "Could not open /dev/null: $!" 0 0 0
667 0 ▶ POSIX::dup2(fileno($null), $fd) // die "Could not redirect the inherited database socket: $!";
Condition0%0/2
POSIX::dup2(fileno $null, $fd) // die("Could not redirect the inherited database socket: $!")
l!l
00
MC/DC0%0/1
POSIX::dup2(fileno $null, $fd) // die("Could not redirect the inherited database socket: $!")
POSIX::dup2(fileno $null, $fd)
Truth table0%0/2
A
A= POSIX::dup2(fileno $null, $fd)
Aresult
11
00
668 }
669  
670 =pod
671  
672 =over 4
673  
674 =item $affinity = $dialect->_known_affinity($db_type)
675  
676 The affinity of a known type, ignoring case, sizes, and a C<tiny>, C<small>,
677 C<medium>, C<big>, C<long>, C<var>, or C<u> prefix, or undef for any other.
678  
679 =back
680  
681 =cut
682  
683 192 ▶sub _known_affinity {
Subroutine _known_affinity: called CC=4 SCAR=13.9
684 192 my $self = shift;
685 192 my ($db_type) = @_;
686  
687 192 my ($type) = $self->_base_type($db_type);
688 192 ▶ return $AFFINITY{$type} if $AFFINITY{$type};
Branch: 1 branch
BranchTrueFalseTotal
if $AFFINITY{$type} 183 9 192
689 9 ▶ return $AFFINITY{$1} if $type =~ m/^(?:tiny|small|medium|big|long|var|u)(.+)$/ && $AFFINITY{$1};
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /^(?:tiny|small|medium|big|long|var|u)(.+)$/ and $AFFINITY{$1} 6 3 9
Condition66%2/3
$type =~ /^(?:tiny|small|medium|big|long|var|u)(.+)$/ and $AFFINITY{$1}
!ll&&!rl&&r
306
MC/DC50%1/2
$type =~ /^(?:tiny|small|medium|big|long|var|u)(.+)$/ and $AFFINITY{$1}
$type =~ /^(?:tiny|small|medium|big|long|var|u)(.+)$/ $AFFINITY{$1}
Truth table66%2/3
A and B
A= $type =~ /^(?:tiny|small|medium|big|long|var|u)(.+)$/B= $AFFINITY{$1}
ABresult
0X0
100
111
690 3 return undef;
691 }
692  
693 =pod
694  
695 =over 4
696  
697 =item \@rows = $dialect->_rows($dbh, $sql)
698  
699 Every row of a scan query, as arrayrefs, each value passed through
700 C<text_from_db>.
701  
702 =back
703  
704 =cut
705  
706 1186 ▶sub _rows {
Subroutine _rows: called CC=2 SCAR=6.9
707 1186 my $self = shift;
708 1186 my ($dbh, $sql) = @_;
709 1186 my $rows = $dbh->selectall_arrayref($sql);
710 1186 $self->text_from_db(@$_) for @$rows;
711 1186 return $rows;
712 }
713  
714 =pod
715  
716 =over 4
717  
718 =item ($type, $size) = $dialect->_base_type($db_type)
719  
720 The C<type> and C<size> of C<field_facts>.
721  
722 =back
723  
724 =cut
725  
726 2053 ▶sub _base_type {
Subroutine _base_type: called CC=3 SCAR=11.0
727 2053 my $self = shift;
728 2053 my ($db_type) = @_;
729  
730 2053 my $type = lc($db_type);
731 2053 my ($size) = $type =~ m/^[^(]*\(\s*(\d+)/;
732 2053 ▶ 1 while $type =~ s/\([^()]*\)//g;
Branch: 1 branch
BranchTrueFalseTotal
$type =~ s/\([^()]*\)//g and do { '???' } 379 2053 2432
733 2053 $type =~ s/\[\d*\]//g;
734 2053 $type =~ s/\b(?:unsigned|zerofill)\b//g;
735 2053 $type =~ s/\s+/ /g;
736 2053 $type =~ s/^ | $//g;
737  
738 2053 ▶ return ($type, defined($size) ? $size + 0 : undef);
Branch: 1 branch
BranchTrueFalseTotal
defined $size ? : 373 1680 2053
739 }
740  
741 =pod
742  
743 =over 4
744  
745 =item \@rows = $dialect->_unique_rows($dbh)
746  
747 The C<(table, key, field)> rows of the unique keys: C<unique_sql>'s rows by
748 default.
749  
750 =back
751  
752 =cut
753  
754 237 ▶sub _unique_rows {
Subroutine _unique_rows: called CC=1 SCAR=0.0
755 237 my $self = shift;
756 237 my ($dbh) = @_;
757 237 return $self->_rows($dbh, $self->unique_sql);
758 }
759  
760 =pod
761  
762 =over 4
763  
764 =item \%keys = $dialect->_keys_by_table(\@rows)
765  
766 Groups C<(table, key, ...)> rows as C<< {table => {key => [[...], ...]}} >>,
767 keeping row order within each key.
768  
769 =back
770  
771 =cut
772  
773 477 ▶sub _keys_by_table {
Subroutine _keys_by_table: called CC=2 SCAR=6.9
774 477 my $self = shift;
775 477 my ($rows) = @_;
776  
777 477 my %keys;
778 477 for my $row (@$rows) {
779 466 my ($table, $key, @rest) = @$row;
780 466 push @{$keys{$table}->{$key}} => \@rest;
781 }
782  
783 477 return \%keys;
784 }
785  
786 =pod
787  
788 =over 4
789  
790 =item $dialect->_add_unique(\%tables, \%keys)
791  
792 =item $dialect->_add_fks(\%tables, \%keys)
793  
794 Turn grouped C<unique_sql> and C<fk_sql> rows into each table's C<unique> and
795 C<fk>, sorted and without duplicates.
796  
797 A unique key with an undef field, or a field the table does not have, is left
798 out. A foreign key with undef other fields is used only when the other table
799 was scanned and its primary key has as many fields. It then points at that
800 key.
801  
802 =back
803  
804 =cut
805  
806 238 ▶sub _add_unique {
Subroutine _add_unique: called CC=8 SCAR=20.8
807 238 my $self = shift;
808 238 my ($tables, $keys) = @_;
809  
810 238 for my $table (keys %$keys) {
811 226 ▶ my $t = $tables->{$table} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $t = $tables->{$table} or next 225 1 226
812 225 ▶ my %seen = $t->{pk} ? (join("\0" => sort @{$t->{pk}}) => 1) : ();
Branch: 1 branch
BranchTrueFalseTotal
$t->{'pk'} ? : 225 0 225
813  
814 225 my @unique;
815 225 for my $rows (values %{$keys->{$table}}) {
816 239 my @fields = map { $_->[0] } @$rows;
817 239 ▶ next if grep { !defined($_) || !$t->{fields}->{$_} } @fields;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not defined $_ && $t->{"fields"}{$_};} @fields 5 234 239
Condition100%3/3
defined $_ && $t->{'fields'}{$_}
!ll&&!rl&&r
41334
MC/DC100%2/2
defined $_ && $t->{'fields'}{$_}
defined $_ $t->{'fields'}{$_}
Truth table100%3/3
A && B
A= defined $_B= $t->{'fields'}{$_}
ABresult
0X0
100
111
818 234 @fields = sort @fields;
819 234 ▶ push @unique => \@fields unless $seen{join "\0" => @fields}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $seen{join "\0", @fields}++ 94 140 234
820 }
821  
822 225 $t->{unique} = [sort { join("\0" => @$a) cmp join("\0" => @$b) } @unique];
823 }
824 }
825  
826 238 ▶sub _add_fks {
Subroutine _add_fks: called CC=9 SCAR=22.0
827 238 my $self = shift;
828 238 my ($tables, $keys) = @_;
829  
830 238 for my $table (keys %$keys) {
831 110 ▶ my $t = $tables->{$table} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $t = $tables->{$table} or next 110 0 110
832  
833 110 my %links;
834 110 for my $rows (values %{$keys->{$table}}) {
835 121 my $other = $rows->[0]->[1];
836  
837 # An undef other field means the other table's primary key, in key order.
838 # A key on a table with none is unusable.
839 121 ▶ my $other_pk = $tables->{$other} ? $tables->{$other}->{pk} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$tables->{$other} ? : 120 1 121
840 121 my $usable = !grep { !defined $_->[2] } @$rows;
841 121 ▶ $usable ||= $other_pk && @$other_pk == @$rows;
Condition83%5/6
$usable ||= $other_pk && @$other_pk == @$rows
l!l&&r!l&&!r
11731
$other_pk && @$other_pk == @$rows
!ll&&!rl&&r
103
MC/DC0%0/3
$usable ||= $other_pk && @$other_pk == @$rows
$usable $other_pk @$other_pk == @$rows
Truth table0%0/4
A ||= B && C
A= $usableB= $other_pkC= @$other_pk == @$rows
ABCresult
1XX1
0111
00X0
0100
842 121 ▶ next unless $usable;
Branch: 1 branch
BranchTrueFalseTotal
unless $usable 1 120 121
843  
844 120 ▶ my %link = map { $rows->[$_]->[0] => $rows->[$_]->[2] // $other_pk->[$_] } 0 .. $#$rows;
Condition66%2/3
$rows->[$_][2] // $other_pk->[$_]
l!l&&r!l&&!r
11740
MC/DC0%0/2
$rows->[$_][2] // $other_pk->[$_]
$rows->[$_][2] $other_pk->[$_]
Truth table66%2/3
A // B
A= $rows->[$_][2]B= $other_pk->[$_]
ABresult
1X1
011
000
845 120 $links{$other}->{join "\0" => map { "$_\0$link{$_}" } sort keys %link} = \%link;
846 }
847  
848 110 $t->{fk} = {map { my $l = $links{$_}; ($_ => [@{$l}{sort keys %$l}]) } keys %links};
849 }
850 }
851  
852 314 1;
853  
854 __END__
« blib/lib/DBIx/QuickORM/Type/UUID.pm ↑ Summary blib/lib/DBIx/QuickORM/JoinRow.pm »