blib/lib/DBIx/QuickORM/Dialect.pm

filter: Statementstmt 95.1% 195 / 205 Branchbran 84.6% 44 / 52 Conditioncond 76.9% 30 / 39 MC/DCmcdc 45.4% 10 / 22 Subroutinesub 97.7% 44 / 45 Podpod 100% 29 / 29 totaltotal 89.8% 352 / 392 CC 96 SCAR 15.5
CC
96
Cov
91%
_null_socket
11.6
_null_socket
11.6
_add_fks
9.0
CRAP 4.7
« blib/lib/DBIx/QuickORM/Type/UUID.pm ↑ Summary blib/lib/DBIx/QuickORM/JoinRow.pm »
1 package DBIx::QuickORM::Dialect;
2 268 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 268 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 268 our $VERSION = '0.000031';
6  
7 268 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 268 ▶use DBI();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 268 ▶use POSIX();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 268 ▶use Object::HashBase qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
12 <server_version
13 268 };
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 268 my %DRIVERS = (
97 Pg => 'PostgreSQL',
98 MariaDB => 'MySQL',
99 SQLite => 'SQLite',
100 DuckDB => 'DuckDB',
101 );
102  
103 310 ▶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)))) 308 2 310
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 63 ▶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
5940
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 309 ▶sub dialect_for_dbh { $_[0] }
Subroutine dialect_for_dbh: called CC=1 SCAR=0.0
Pod documented
182 4163 ▶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 108 ▶sub supports_isolation { 0 }
Subroutine supports_isolation: called CC=1 SCAR=0.0
Pod documented
185 164 ▶sub supports_returning_insert { 1 }
Subroutine supports_returning_insert: called CC=1 SCAR=0.0
Pod documented
186 86 ▶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 34 ▶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 114 ▶sub begin_sql {
Subroutine begin_sql: called CC=2 SCAR=6.9
Pod documented
212 114 my $self = shift;
213 114 my ($level) = @_;
214 114 ▶ croak "The " . $self->name . " dialect cannot set a transaction isolation level" if $level;
Branch: 1 branch
BranchTrueFalseTotal
if $level 6 108 114
215 108 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 3921 ▶sub quote_identifier {
Subroutine quote_identifier: called CC=1 SCAR=0.0
Pod documented
231 3921 my $self = shift;
232 3921 my ($name) = @_;
233  
234 3921 my $q = $self->quote_char;
235 3921 $name =~ s/\Q$q\E/$q$q/g;
236 3921 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 8 ▶sub release_inherited {
Subroutine release_inherited: called CC=1 SCAR=0.0
Pod documented
273 8 my $self = shift;
274 8 my ($dbh) = @_;
275 8 $dbh->{InactiveDestroy} = 1;
276 8 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 29 ▶sub supports_async { 0 }
Subroutine supports_async: called CC=1 SCAR=0.0
Pod documented
304  
305 =pod
306  
307 =over 4
308  
309 =item $dialect->text_from_db(@values)
310  
311 =item $dialect->text_to_db(@values)
312  
313 Convert text in place, as it is fetched from the database and before it is
314 sent, for drivers that do not exchange character strings themselves. Both do
315 nothing by default.
316  
317 Callers pass only text, never values of binary fields.
318  
319 =back
320  
321 =cut
322  
323 5154 ▶sub text_from_db { }
Subroutine text_from_db: called CC=1 SCAR=0.0
Pod documented
324 1116 ▶sub text_to_db { }
Subroutine text_to_db: called CC=1 SCAR=0.0
Pod documented
325  
326 =pod
327  
328 =over 4
329  
330 =item $attrs = $dialect->binary_bind_attrs
331  
332 The C<bind_param> attributes for a binary value. C<< {TYPE => SQL_BLOB} >> by
333 default.
334  
335 =back
336  
337 =cut
338  
339 45 ▶sub binary_bind_attrs { {TYPE => DBI::SQL_BLOB()} }
Subroutine binary_bind_attrs: called CC=1 SCAR=0.0
Pod documented
340  
341 =pod
342  
343 =over 4
344  
345 =item $attrs = $dialect->bind_attrs(\%field)
346  
347 The C<bind_param> attributes for a value going to the field (a map field
348 record, or undef): C<binary_bind_attrs> for a field of C<binary> affinity,
349 otherwise undef.
350  
351 =back
352  
353 =cut
354  
355 1425 ▶sub bind_attrs {
Subroutine bind_attrs: called CC=4 SCAR=13.9
Pod documented
356 1425 my $self = shift;
357 1425 my ($field) = @_;
358 1425 ▶ return undef unless $field && ($field->{affinity} // '') eq 'binary';
Branch: 1 branch
BranchTrueFalseTotal
unless $field and ($field->{"affinity"} // "") eq "binary" 1381 44 1425
Condition80%4/5
$field and ($field->{"affinity"} // "") eq "binary"
!ll&&!rl&&r
1138044
$field->{"affinity"} // ""
l!l
14240
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
359 44 return $self->binary_bind_attrs;
360 }
361  
362 =pod
363  
364 =over 4
365  
366 =item $id = $dialect->last_insert_id($dbh, $table, $field)
367  
368 The value the database generated for the field by the last insert.
369  
370 =back
371  
372 =cut
373  
374 2 ▶sub last_insert_id {
Subroutine last_insert_id: called CC=1 SCAR=0.0
Pod documented
375 2 my $self = shift;
376 2 my ($dbh, $table, $field) = @_;
377 2 return $dbh->last_insert_id(undef, undef, $table, $field);
378 }
379  
380 =pod
381  
382 =over 4
383  
384 =item \%kinds = $dialect->native_error_kinds
385  
386 Native error codes mapped to error kinds. Empty by default.
387  
388 =back
389  
390 =cut
391  
392 268 my %STATE_KIND = (
393 '40001' => 'serialization_failure',
394 '40P01' => 'deadlock',
395 '55P03' => 'lock_timeout',
396 '23505' => 'unique_violation',
397 '23503' => 'foreign_key_violation',
398 '23502' => 'not_null_violation',
399 '23514' => 'check_violation',
400 );
401  
402 11 ▶sub native_error_kinds { {} }
Subroutine native_error_kinds: called CC=1 SCAR=0.0
Pod documented
403  
404 =pod
405  
406 =over 4
407  
408 =item $kind = $dialect->error_kind($native, $state, $errstr)
409  
410 The kind of a database error (see L<DBIx::QuickORM::Exception::DB>), or undef.
411  
412 The native code is looked up in C<native_error_kinds> first, then the
413 SQLSTATE. C<$errstr> is unused here. The SQLSTATEs known:
414  
415 =for highlighter language=text
416  
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 =back
426  
427 =cut
428  
429 63 ▶sub error_kind {
Subroutine error_kind: called CC=4 SCAR=13.9
Pod documented
430 63 my $self = shift;
431 63 my ($native, $state) = @_;
432  
433 63 ▶ my $kind = defined($native) ? $self->native_error_kinds->{$native} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $native ? : 62 1 63
434 63 ▶ $kind //= $STATE_KIND{$state} if defined $state;
Branch: 1 branch
BranchTrueFalseTotal
if defined $state 62 1 63
Condition100%3/3
$kind //= $STATE_KIND{$state}
l!l&&r!l&&!r
4499
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
435 63 return $kind;
436 }
437  
438 =pod
439  
440 =over 4
441  
442 =item \%tables = $dialect->scan($dbh)
443  
444 Scans the tables of the connection's namespace and returns them as plain map
445 data:
446  
447 =for highlighter language=perl
448  
449 {
450 users => {
451 fields => {
452 id => {
453 table => 'users',
454 name => 'id',
455 affinity => 'numeric',
456 db_type => 'integer',
457 type => 'integer',
458 },
459 ...
460 },
461 pk => ['id'], # key order, or undef when there is none
462 unique => [['email'], ['a', 'b']], # each sorted, and the primary key is not repeated
463 fk => {orgs => [{org_id => 'id'}]},
464 },
465 }
466  
467 Views, temporary tables, and system tables are left out. So are partial and
468 expression unique indexes, since they do not make their columns unique across
469 all rows.
470  
471 The scan runs one query per kind of metadata (DuckDB two for unique keys).
472 Each dialect provides the SQL through these methods, each given the handle
473 but C<unique_sql>:
474  
475 =over 4
476  
477 =item C<tables_sql>: C<(table)> rows.
478  
479 =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>).
480  
481 =item C<pk_sql>: C<(table, field)> rows in key order.
482  
483 =item C<unique_sql>: C<(table, key, field)> rows, with an undef field for a key that does not cover whole columns.
484  
485 =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.
486  
487 =back
488  
489 =back
490  
491 =cut
492  
493 286 ▶sub scan {
Subroutine scan: called CC=6 SCAR=17.9
Pod documented
494 286 my $self = shift;
495 286 my ($dbh) = @_;
496  
497 286 my %tables = map { $_->[0] => {fields => {}, pk => undef, unique => [], fk => {}} } @{$self->_rows($dbh, $self->tables_sql($dbh))};
498  
499 286 for my $row (@{$self->_rows($dbh, $self->fields_sql($dbh))}) {
500 1942 my ($table, $field, @types) = @$row;
501 1942 ▶ my $t = $tables{$table} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $t = $tables{$table} or next 1941 1 1942
502 1941 $t->{fields}->{$field} = {%{$self->field_facts(@types)}, table => $table, name => $field};
503 }
504  
505 286 for my $row (@{$self->_rows($dbh, $self->pk_sql($dbh))}) {
506 654 my ($table, $field) = @$row;
507 654 ▶ my $t = $tables{$table} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $t = $tables{$table} or next 654 0 654
508 654 ▶ push @{$t->{pk} //= []} => $field;
Condition100%2/2
$t->{"pk"} //= []
l!l
95559
MC/DC100%1/1
$t->{"pk"} //= []
$t->{"pk"}
Truth table100%2/2
A
A= $t->{"pk"}
Aresult
11
00
509 }
510  
511 286 $self->_add_unique(\%tables, $self->_keys_by_table($self->_unique_rows($dbh)));
512 286 $self->_add_fks(\%tables, $self->_keys_by_table($self->_rows($dbh, $self->fk_sql($dbh))));
513  
514 286 return \%tables;
515 }
516  
517 =pod
518  
519 =over 4
520  
521 =item \%field = $dialect->field_facts($db_type, $base_db_type, $db_fills, $db_refuses_value)
522  
523 The map's facts about a field:
524  
525 =over 4
526  
527 =item C<db_type>: as given.
528  
529 =item C<type>: the lowercased base name, without sizes, array markers, C<unsigned>, or C<zerofill>.
530  
531 =item C<size>: the number the first parentheses start with, when they do.
532  
533 =item C<affinity>: from C<$base_db_type> when given, otherwise from C<$db_type>.
534  
535 =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.
536  
537 =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.
538  
539 =back
540  
541 =back
542  
543 =cut
544  
545 1967 ▶sub field_facts {
Subroutine field_facts: called CC=5 SCAR=16.1
Pod documented
546 1967 my $self = shift;
547 1967 my ($db_type, $base, $fills, $refuses) = @_;
548  
549 1967 my ($type, $size) = $self->_base_type($db_type);
550 1967 ▶ my %field = (db_type => $db_type, type => $type, affinity => $self->affinity(defined($base) ? $base : $db_type));
Branch: 1 branch
BranchTrueFalseTotal
defined $base ? : 1 1966 1967
551 1967 ▶ $field{size} = $size if defined $size;
Branch: 1 branch
BranchTrueFalseTotal
if defined $size 409 1558 1967
552 1967 ▶ $field{db_fills} = 1 if $fills;
Branch: 1 branch
BranchTrueFalseTotal
if $fills 429 1538 1967
553 1967 ▶ $field{db_refuses_value} = 1 if $refuses;
Branch: 1 branch
BranchTrueFalseTotal
if $refuses 6 1961 1967
554  
555 1967 return \%field;
556 }
557  
558 my %AFFINITY = (
559 4824 (map { $_ => 'string' } qw/bit char character string text bpchar clob nchar nvarchar enum set json jsonb money uuid xml citext name/),
560 804 (map { $_ => 'string' } 'character varying', 'national character', 'national character varying'),
561 2144 (map { $_ => 'string' } qw/date datetime interval time timestamp timestamptz timetz year/),
562 1072 (map { $_ => 'string' } 'time with time zone', 'time without time zone', 'timestamp with time zone', 'timestamp without time zone'),
563 804 (map { $_ => 'binary' } qw/binary blob bytea/),
564 6164 (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/),
565 268 (map { $_ => 'numeric' } 'double precision'),
566 268 (map { $_ => 'boolean' } qw/bool boolean/),
567 );
568  
569 =pod
570  
571 =over 4
572  
573 =item $affinity = $dialect->affinity($db_type)
574  
575 C<string>, C<numeric>, C<binary>, or C<boolean>.
576  
577 Dates, times, JSON, and UUIDs are C<string>, and so is any type this does not
578 know. C<bit> is C<string>: PostgreSQL returns bit strings and MySQL raw
579 bytes, neither of which compares as a number.
580  
581 =back
582  
583 =cut
584  
585 26 ▶sub affinity {
Subroutine affinity: called CC=2 SCAR=6.9
Pod documented
586 26 my $self = shift;
587 26 my ($db_type) = @_;
588 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
589 }
590  
591 =head1 PRIVATE METHODS
592  
593 =over 4
594  
595 =item $version = $class->_server_version($dbh)
596  
597 The server's version for C<server_version>. By default C<SELECT version()>,
598 through C<_version_number>.
599  
600 =item $version = $class->_version_number($string)
601  
602 The first C<major.minor[.patch]> in the string as
603 C<major * 1_000_000 + minor * 1_000 + patch>, or undef when it has none, so
604 an unreadable version string does not refuse the connection.
605  
606 =back
607  
608 =cut
609  
610 1 ▶sub _server_version {
Subroutine _server_version: called CC=1 SCAR=0.0
611 1 my $class = shift;
612 1 my ($dbh) = @_;
613 1 my ($version) = $dbh->selectrow_array('SELECT version()');
614 1 return $class->_version_number($version);
615 }
616  
617 317 ▶sub _version_number {
Subroutine _version_number: called CC=4 SCAR=13.9
618 317 my $class = shift;
619 317 my ($string) = @_;
620 317 ▶ my ($major, $minor, $patch) = ($string // '') =~ m/(\d+)\.(\d+)(?:\.(\d+))?/ or return undef;
Branch: 1 branch
BranchTrueFalseTotal
() = ($string // "") =~ /(\d+)\.(\d+)(?:\.(\d+))?/ or return undef 314 3 317
Condition100%2/2
$string // ""
l!l
3161
MC/DC100%1/1
$string // ""
$string
Truth table100%2/2
A
A= $string
Aresult
11
00
621 314 ▶ return $major * 1_000_000 + $minor * 1_000 + ($patch // 0);
Condition100%2/2
$patch // 0
l!l
3131
MC/DC100%1/1
$patch // 0
$patch
Truth table100%2/2
A
A= $patch
Aresult
11
00
622 }
623  
624 =pod
625  
626 =over 4
627  
628 =item $dialect->_null_socket($fd)
629  
630 Points the file descriptor at F</dev/null>, so nothing this process does with
631 an inherited socket reaches the parent's session. Does nothing for undef or a
632 negative descriptor.
633  
634 =back
635  
636 =cut
637  
638 1 ▶sub _null_socket {
Subroutine _null_socket: called CC=5 SCAR=24.5
639 1 my $self = shift;
640 1 my ($fd) = @_;
641 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
642 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
643 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
644 }
645  
646 =pod
647  
648 =over 4
649  
650 =item $affinity = $dialect->_known_affinity($db_type)
651  
652 The affinity of a known type, ignoring case, sizes, and a C<tiny>, C<small>,
653 C<medium>, C<big>, C<long>, C<var>, or C<u> prefix, or undef for any other.
654  
655 =back
656  
657 =cut
658  
659 207 ▶sub _known_affinity {
Subroutine _known_affinity: called CC=4 SCAR=13.9
660 207 my $self = shift;
661 207 my ($db_type) = @_;
662  
663 207 my ($type) = $self->_base_type($db_type);
664 207 ▶ return $AFFINITY{$type} if $AFFINITY{$type};
Branch: 1 branch
BranchTrueFalseTotal
if $AFFINITY{$type} 198 9 207
665 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
666 3 return undef;
667 }
668  
669 =pod
670  
671 =over 4
672  
673 =item \@rows = $dialect->_rows($dbh, $sql)
674  
675 Every row of a scan query, as arrayrefs, each value passed through
676 C<text_from_db>.
677  
678 =back
679  
680 =cut
681  
682 1431 ▶sub _rows {
Subroutine _rows: called CC=2 SCAR=6.9
683 1431 my $self = shift;
684 1431 my ($dbh, $sql) = @_;
685 1431 my $rows = $dbh->selectall_arrayref($sql);
686 1431 $self->text_from_db(@$_) for @$rows;
687 1431 return $rows;
688 }
689  
690 =pod
691  
692 =over 4
693  
694 =item ($type, $size) = $dialect->_base_type($db_type)
695  
696 The C<type> and C<size> of C<field_facts>.
697  
698 =back
699  
700 =cut
701  
702 2174 ▶sub _base_type {
Subroutine _base_type: called CC=3 SCAR=11.0
703 2174 my $self = shift;
704 2174 my ($db_type) = @_;
705  
706 2174 my $type = lc($db_type);
707 2174 my ($size) = $type =~ m/^[^(]*\(\s*(\d+)/;
708 2174 ▶ 1 while $type =~ s/\([^()]*\)//g;
Branch: 1 branch
BranchTrueFalseTotal
$type =~ s/\([^()]*\)//g and do { '???' } 449 2174 2623
709 2174 $type =~ s/\[\d*\]//g;
710 2174 $type =~ s/\b(?:unsigned|zerofill)\b//g;
711 2174 $type =~ s/\s+/ /g;
712 2174 $type =~ s/^ | $//g;
713  
714 2174 ▶ return ($type, defined($size) ? $size + 0 : undef);
Branch: 1 branch
BranchTrueFalseTotal
defined $size ? : 443 1731 2174
715 }
716  
717 =pod
718  
719 =over 4
720  
721 =item \@rows = $dialect->_unique_rows($dbh)
722  
723 The C<(table, key, field)> rows of the unique keys: C<unique_sql>'s rows by
724 default.
725  
726 =back
727  
728 =cut
729  
730 286 ▶sub _unique_rows {
Subroutine _unique_rows: called CC=1 SCAR=0.0
731 286 my $self = shift;
732 286 my ($dbh) = @_;
733 286 return $self->_rows($dbh, $self->unique_sql);
734 }
735  
736 =pod
737  
738 =over 4
739  
740 =item \%keys = $dialect->_keys_by_table(\@rows)
741  
742 Groups C<(table, key, ...)> rows as C<< {table => {key => [[...], ...]}} >>,
743 keeping row order within each key.
744  
745 =back
746  
747 =cut
748  
749 575 ▶sub _keys_by_table {
Subroutine _keys_by_table: called CC=2 SCAR=6.9
750 575 my $self = shift;
751 575 my ($rows) = @_;
752  
753 575 my %keys;
754 575 for my $row (@$rows) {
755 495 my ($table, $key, @rest) = @$row;
756 495 push @{$keys{$table}->{$key}} => \@rest;
757 }
758  
759 575 return \%keys;
760 }
761  
762 =pod
763  
764 =over 4
765  
766 =item $dialect->_add_unique(\%tables, \%keys)
767  
768 =item $dialect->_add_fks(\%tables, \%keys)
769  
770 Turn grouped C<unique_sql> and C<fk_sql> rows into each table's C<unique> and
771 C<fk>, sorted and without duplicates.
772  
773 A unique key with an undef field, or a field the table does not have, is left
774 out. A foreign key with undef other fields is used only when the other table
775 was scanned and its primary key has as many fields. It then points at that
776 key.
777  
778 =back
779  
780 =cut
781  
782 287 ▶sub _add_unique {
Subroutine _add_unique: called CC=8 SCAR=20.8
783 287 my $self = shift;
784 287 my ($tables, $keys) = @_;
785  
786 287 for my $table (keys %$keys) {
787 245 ▶ my $t = $tables->{$table} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $t = $tables->{$table} or next 244 1 245
788 244 ▶ my %seen = $t->{pk} ? (join("\0" => sort @{$t->{pk}}) => 1) : ();
Branch: 1 branch
BranchTrueFalseTotal
$t->{'pk'} ? : 244 0 244
789  
790 244 my @unique;
791 244 for my $rows (values %{$keys->{$table}}) {
792 258 my @fields = map { $_->[0] } @$rows;
793 258 ▶ next if grep { !defined($_) || !$t->{fields}->{$_} } @fields;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not defined $_ && $t->{"fields"}{$_};} @fields 5 253 258
Condition100%3/3
defined $_ && $t->{'fields'}{$_}
!ll&&!rl&&r
41357
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
794 253 @fields = sort @fields;
795 253 ▶ push @unique => \@fields unless $seen{join "\0" => @fields}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $seen{join "\0", @fields}++ 102 151 253
796 }
797  
798 244 $t->{unique} = [sort { join("\0" => @$a) cmp join("\0" => @$b) } @unique];
799 }
800 }
801  
802 287 ▶sub _add_fks {
Subroutine _add_fks: called CC=9 SCAR=22.0
803 287 my $self = shift;
804 287 my ($tables, $keys) = @_;
805  
806 287 for my $table (keys %$keys) {
807 114 ▶ my $t = $tables->{$table} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $t = $tables->{$table} or next 114 0 114
808  
809 114 my %links;
810 114 for my $rows (values %{$keys->{$table}}) {
811 127 my $other = $rows->[0]->[1];
812  
813 # An undef other field means the other table's primary key, in key order.
814 # A key on a table with none is unusable.
815 127 ▶ my $other_pk = $tables->{$other} ? $tables->{$other}->{pk} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$tables->{$other} ? : 126 1 127
816 127 my $usable = !grep { !defined $_->[2] } @$rows;
817 127 ▶ $usable ||= $other_pk && @$other_pk == @$rows;
Condition83%5/6
$usable ||= $other_pk && @$other_pk == @$rows
l!l&&r!l&&!r
12331
$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
818 127 ▶ next unless $usable;
Branch: 1 branch
BranchTrueFalseTotal
unless $usable 1 126 127
819  
820 126 ▶ my %link = map { $rows->[$_]->[0] => $rows->[$_]->[2] // $other_pk->[$_] } 0 .. $#$rows;
Condition66%2/3
$rows->[$_][2] // $other_pk->[$_]
l!l&&r!l&&!r
12340
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
821 126 $links{$other}->{join "\0" => map { "$_\0$link{$_}" } sort keys %link} = \%link;
822 }
823  
824 114 $t->{fk} = {map { my $l = $links{$_}; ($_ => [@{$l}{sort keys %$l}]) } keys %links};
825 }
826 }
827  
828 268 1;
829  
830 __END__
« blib/lib/DBIx/QuickORM/Type/UUID.pm ↑ Summary blib/lib/DBIx/QuickORM/JoinRow.pm »