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

filter: Statementstmt 68.0% 96 / 141 Branchbran 54.3% 25 / 46 Conditioncond 25.8% 8 / 31 MC/DCmcdc 29.4% 5 / 17 Subroutinesub 70.4% 31 / 44 Podpod 100% 37 / 37 totaltotal 63.9% 202 / 316 CC 40 SCAR 38.7
CC
40
Cov
59%
scan_temp
240.0
END
27.7
_scan_key_columns
12.0
CRAP 48.0
↑ Summary blib/lib/DBIx/QuickORM/Map.pm »
1 package DBIx::QuickORM::Dialect::MySQL;
2 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 14 our $VERSION = '0.000033';
6  
7 14 ▶use DBI();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 14 ▶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::MySQL - MySQL dialect, and base for MariaDB.
18  
19 =head1 DESCRIPTION
20  
21 MySQL through L<DBD::MariaDB>, the only supported driver for the MySQL family.
22 Backtick quoting, no C<RETURNING>, native error codes, and async queries
23 through the driver. The namespace is C<DATABASE()>.
24  
25 Which dialect a handle gets is a server fact: a server whose version names
26 MariaDB gets L<DBIx::QuickORM::Dialect::MariaDB>. Any other server (MySQL
27 Community, Percona) gets this one.
28  
29 =head1 SYNOPSIS
30  
31 # this class or MariaDB for DBD::MariaDB
32 my $dialect = DBIx::QuickORM::Dialect->for_dbh($dbh);
33  
34 =head1 PUBLIC METHODS
35  
36 =over 4
37  
38 =item $char = $dialect->quote_char
39  
40 A backtick.
41  
42 =item $bool = $dialect->supports_returning_insert
43  
44 =item $bool = $dialect->supports_returning_update
45  
46 =item $bool = $dialect->supports_returning_delete
47  
48 All false: MySQL has no C<RETURNING>.
49  
50 =item $bool = $dialect->supports_isolation
51  
52 True.
53  
54 =item @sql = $dialect->begin_sql($level)
55  
56 C<SET TRANSACTION ISOLATION LEVEL $level> then C<BEGIN> when given a level,
57 otherwise C<BEGIN>. Without C<SESSION>, the level applies to the next
58 transaction only.
59  
60 =item $bool = $dialect->having_uses_alias
61  
62 True: the server resolves the alias in C<HAVING> and computes the column
63 once, but it computes a repeated expression a second time.
64  
65 =item $n = $dialect->bind_limit
66  
67 65535, the most placeholders a prepared statement may hold.
68  
69 =item ($parser, $formatter, \%instants) = $dialect->datetime_format
70  
71 L<DateTime::Format::MySQL> for both. C<TIMESTAMP> stores an instant: the
72 server converts it from and to the session time zone. C<DATETIME> does not.
73  
74 =item $bool = $dialect->supports_async
75  
76 True.
77  
78 =item $attrs = $dialect->async_prepare_attrs
79  
80 C<< {mariadb_async => 1} >>.
81  
82 =item $bool = $dialect->async_ready($dbh, $sth)
83  
84 =item $dialect->async_result($dbh, $sth)
85  
86 C<mariadb_async_ready> and C<mariadb_async_result> on the statement. The
87 C<mariadb_> prefix names the driver, but they work against every server of
88 the family.
89  
90 =item $dialect->cancel_async($dbh, $sth)
91  
92 Waits for the result and discards it: the driver cannot cancel a query on its
93 own connection.
94  
95 =item $bool = $dialect->supports_temp_views
96  
97 False: MySQL and MariaDB have no temporary views.
98  
99 =item $bool = $dialect->temp_ddl_transactional
100  
101 False: a rollback neither removes a temporary table created in the
102 transaction nor restores one dropped in it.
103  
104 =item $sql = $dialect->identity_sql($table, \%field, $inline_pk)
105  
106 C<AUTO_INCREMENT>. The column must be a key.
107  
108 =item @sql = $dialect->create_enum_sql($name, \@values)
109  
110 =item ($db_type, $check) = $dialect->enum_column($column, $enum, \@values, temp => $bool)
111  
112 No enum types: an enum column is C<ENUM('a', 'b')>, temporary or not, and
113 nothing is created for the enum.
114  
115 my @none = $dialect->create_enum_sql(post_status => ['draft', 'live']); # ()
116  
117 my ($type, $check) = $dialect->enum_column('status', 'post_status', ['draft', 'live']);
118 # "ENUM('draft', 'live')" and undef
119  
120  
121 =item @keys = $dialect->db_type_keys
122  
123 C<mysql>, then C<any_mysql>, then C<any>.
124  
125 =item $sql = $dialect->quote_literal($value)
126  
127 A string literal: the handle's C<quote> on a connected dialect, so it follows
128 the session's SQL mode, including C<NO_BACKSLASH_ESCAPES>.
129  
130 my $text = $dialect->quote_literal("it's C:\\temp"); # 'it\'s C:\\temp'
131  
132 A dialect with no live handle (one from C<for_name>, for C<build_sql>)
133 escapes as the default SQL mode does: C<\0>, C<\n>, C<\r>, C<\\>, C<\'>,
134 C<\">, and C<\Z>. SQL built that way is wrong on a server running
135 C<NO_BACKSLASH_ESCAPES>, which treats a backslash as an ordinary character.
136  
137 =item $sql = $dialect->quote_binary($dbh, $bytes)
138  
139 A hex literal, which every SQL mode reads the same way.
140  
141 my $literal = $dialect->quote_binary($dbh, "\xDE\xAD"); # X'dead'
142  
143 =item $sql = $dialect->index_using_sql($type)
144  
145 C<USING> and the type in upper case (C<BTREE>, C<HASH>), or a reference's
146 SQL as is, placed after the index name inside C<CREATE TEMPORARY TABLE>.
147  
148 my $using = $dialect->index_using_sql('hash'); # USING HASH
149  
150  
151 =back
152  
153 =cut
154  
155 570 ▶sub quote_char { '`' }
Subroutine quote_char: called CC=1 SCAR=0.0
Pod documented
156 1 ▶sub supports_returning_insert { 0 }
Subroutine supports_returning_insert: called CC=1 SCAR=0.0
Pod documented
157 2 ▶sub supports_returning_update { 0 }
Subroutine supports_returning_update: called CC=1 SCAR=0.0
Pod documented
158 1 ▶sub supports_returning_delete { 0 }
Subroutine supports_returning_delete: called CC=1 SCAR=0.0
Pod documented
159 1 ▶sub supports_isolation { 1 }
Subroutine supports_isolation: called CC=1 SCAR=0.0
Pod documented
160 2 ▶sub begin_sql { $_[1] ? ("SET TRANSACTION ISOLATION LEVEL $_[1]", 'BEGIN') : ('BEGIN') }
Subroutine begin_sql: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 1 1 2
161 1 ▶sub having_uses_alias { 1 }
Subroutine having_uses_alias: called CC=1 SCAR=0.0
Pod documented
162 1 ▶sub bind_limit { 65535 }
Subroutine bind_limit: called CC=1 SCAR=0.0
Pod documented
163 1 ▶sub datetime_format { ('DateTime::Format::MySQL', 'DateTime::Format::MySQL', {timestamp => 1}) }
Subroutine datetime_format: called CC=1 SCAR=0.0
Pod documented
164 0 ▶sub supports_async { 1 }
Subroutine supports_async: not called CC=1 SCAR=6.9
Pod documented
165 3 ▶sub supports_temp_views { 0 }
Subroutine supports_temp_views: called CC=1 SCAR=0.0
Pod documented
166 2 ▶sub temp_ddl_transactional { 0 }
Subroutine temp_ddl_transactional: called CC=1 SCAR=0.0
Pod documented
167 7 ▶sub db_type_keys { ('mysql', 'any_mysql', 'any') }
Subroutine db_type_keys: called CC=1 SCAR=0.0
Pod documented
168 1 ▶sub identity_sql { 'AUTO_INCREMENT' }
Subroutine identity_sql: called CC=1 SCAR=0.0
Pod documented
169 4 ▶sub index_using_sql { ref($_[1]) ? "USING ${$_[1]}" : "USING \U$_[1]" }
Subroutine index_using_sql: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 2 2 4
170 21 ▶sub create_enum_sql { () }
Subroutine create_enum_sql: called CC=1 SCAR=0.0
Pod documented
171 26 ▶sub enum_column { ('ENUM(' . join(', ' => map { $_[0]->quote_literal($_) } @{$_[3]}) . ')', undef) }
Subroutine enum_column: called CC=1 SCAR=0.0
Pod documented
172 14 my %ESCAPE = ("\0" => '\\0', "\n" => '\\n', "\r" => '\\r', "\\" => '\\\\', "'" => "\\'", '"' => '\\"', "\x1a" => '\\Z');
173  
174 # The handle's quote follows the session's sql_mode (NO_BACKSLASH_ESCAPES), so prefer it when there is one.
175 72 ▶sub quote_literal {
Subroutine quote_literal: called CC=5 SCAR=16.1
Pod documented
176 72 my $self = shift;
177 72 my ($value) = @_;
178 72 ▶ return 'NULL' unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 71 72
179 71 ▶ my $dbh = ref($self) && $self->dbh;
Condition100%3/3
ref $self && $self->dbh
!ll&&!rl&&r
1682
MC/DC100%2/2
ref $self && $self->dbh
ref $self $self->dbh
Truth table100%3/3
A && B
A= ref $selfB= $self->dbh
ABresult
0X0
100
111
180 71 ▶ return $dbh->quote($value) if $dbh && $dbh->{Active};
Branch: 1 branch
BranchTrueFalseTotal
if $dbh and $dbh->{"Active"} 1 70 71
Condition100%3/3
$dbh and $dbh->{"Active"}
!ll&&!rl&&r
6911
MC/DC100%2/2
$dbh and $dbh->{"Active"}
$dbh $dbh->{"Active"}
Truth table100%3/3
A and B
A= $dbhB= $dbh->{"Active"}
ABresult
0X0
100
111
181 70 return "'" . ($value =~ s/([\0\n\r\\'"\x1a])/$ESCAPE{$1}/gr) . "'";
182 }
183  
184 3 ▶sub quote_binary { "X'" . unpack('H*', $_[2]) . "'" }
Subroutine quote_binary: called CC=1 SCAR=0.0
Pod documented
185  
186 0 ▶sub async_prepare_attrs { {mariadb_async => 1} }
Subroutine async_prepare_attrs: not called CC=1 SCAR=6.9
Pod documented
187 0 ▶sub async_ready { $_[2]->mariadb_async_ready }
Subroutine async_ready: not called CC=1 SCAR=6.9
Pod documented
188 0 ▶sub async_result { $_[2]->mariadb_async_result }
Subroutine async_result: not called CC=1 SCAR=6.9
Pod documented
189 0 ▶sub cancel_async { $_[2]->mariadb_async_result }
Subroutine cancel_async: not called CC=1 SCAR=6.9
Pod documented
190  
191 =pod
192  
193 =over 4
194  
195 =item $class = DBIx::QuickORM::Dialect::MySQL->dialect_for_dbh($dbh)
196  
197 L<DBIx::QuickORM::Dialect::MariaDB> when C<SELECT version()> names MariaDB,
198 otherwise this class.
199  
200 my $class = DBIx::QuickORM::Dialect::MySQL->dialect_for_dbh($dbh);
201 # DBIx::QuickORM::Dialect::MariaDB on a MariaDB server
202  
203 =back
204  
205 =cut
206  
207 3 ▶sub dialect_for_dbh {
Subroutine dialect_for_dbh: called CC=2 SCAR=6.9
Pod documented
208 3 my $class = shift;
209 3 my ($dbh) = @_;
210  
211 3 my ($version) = $dbh->selectrow_array('SELECT version()');
212 3 ▶ return $class unless $version =~ m/mariadb/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $version =~ /mariadb/i 2 1 3
213  
214 1 require DBIx::QuickORM::Dialect::MariaDB;
215 1 return 'DBIx::QuickORM::Dialect::MariaDB';
216 }
217  
218 =pod
219  
220 =over 4
221  
222 =item \%kinds = $dialect->native_error_kinds
223  
224 The native codes, since MySQL reports a generic SQLSTATE for most errors:
225  
226 =for highlighter language=text
227  
228 1205 lock_timeout (lock wait timeout)
229 1213 deadlock
230 1062, 1169, 1586 unique_violation (duplicate key)
231 1216, 1217, 1451, 1452 foreign_key_violation (missing or still-referenced parent row)
232 1048, 1364 not_null_violation (NULL, or no value and no default)
233 3819 check_violation (failed CHECK)
234  
235 =back
236  
237 =cut
238  
239 my %NATIVE_KIND = (
240 1205 => 'lock_timeout',
241 1213 => 'deadlock',
242 3819 => 'check_violation',
243 42 (map { $_ => 'unique_violation' } 1062, 1169, 1586),
244 56 (map { $_ => 'foreign_key_violation' } 1216, 1217, 1451, 1452),
245 14 (map { $_ => 'not_null_violation' } 1048, 1364),
246 );
247  
248 23 ▶sub native_error_kinds { \%NATIVE_KIND }
Subroutine native_error_kinds: called CC=1 SCAR=0.0
Pod documented
249  
250 =pod
251  
252 =over 4
253  
254 =item $ns = $dialect->namespace($dbh)
255  
256 C<DATABASE()>, or undef when the connection has no database selected.
257  
258 =back
259  
260 =cut
261  
262 2 ▶sub namespace {
Subroutine namespace: called CC=1 SCAR=0.0
Pod documented
263 2 my $self = shift;
264 2 my ($dbh) = @_;
265 2 my ($ns) = $dbh->selectrow_array('SELECT DATABASE()');
266 2 return $ns;
267 }
268  
269 =pod
270  
271 =over 4
272  
273 =item $key = $dialect->scan_cache_key($dbh, $namespace)
274  
275 The base key, plus what else decides what the scan sees: the server's host
276 name, port, and socket (connection attributes outside the DSN can pick the
277 server), the account the session resolved to, its C<sql_select_limit> (which
278 truncates the catalog queries), its active roles where the
279 server has roles (they can change which tables the catalog shows), and, on
280 MySQL 8.0.30 and later, whether the catalog shows generated invisible primary
281 keys.
282  
283 =back
284  
285 =cut
286  
287 0 ▶sub scan_cache_key {
Subroutine scan_cache_key: not called CC=2 SCAR=17.9
Pod documented
288 0 my $self = shift;
289 0 my ($dbh, $namespace) = @_;
290 0 my @session = $dbh->selectrow_array('SELECT ' . join(', ' => '@@hostname', '@@port', '@@socket', 'CURRENT_USER()', '@@session.sql_select_limit', $self->_scan_key_columns));
291 0 ▶ return join "\0" => map { $_ // '' } $self->SUPER::scan_cache_key($dbh, $namespace), @session;
Condition0%0/2
$_ // ""
l!l
00
MC/DC0%0/1
$_ // ""
$_
Truth table0%0/2
A
A= $_
Aresult
11
00
292 }
293  
294 =pod
295  
296 =over 4
297  
298 =item @sql = $dialect->_scan_key_columns
299  
300 The server-specific session values C<scan_cache_key> adds: C<CURRENT_ROLE()>
301 from MySQL 8.0, and the generated invisible primary key setting from 8.0.30.
302  
303 =back
304  
305 =cut
306  
307 0 ▶sub _scan_key_columns {
Subroutine _scan_key_columns: not called CC=3 SCAR=24.8
308 0 my $self = shift;
309 0 my $version = $self->server_version;
310 return (
311 0 ▶ ($version >= 8_000_000 ? 'CURRENT_ROLE()' : ()),
Branch: 2 branches
BranchTrueFalseTotal
$version >= 8000000 ? : 0 0 0
$version >= 8000030 ? : 0 0 0
312 ($version >= 8_000_030 ? '@@session.show_gipk_in_create_table_and_information_schema' : ()),
313 );
314 }
315  
316 =pod
317  
318 =over 4
319  
320 =item $dialect->init_session($dbh, $time_zone)
321  
322 C<SET NAMES utf8mb4>, then C<SET time_zone>. Also records the pid that owns
323 the handle, for C<release_inherited>.
324  
325 C<UTC> is sent as C<+00:00>, which works without the server's time zone
326 tables. Any other zone name needs them.
327  
328 =back
329  
330 =cut
331  
332 2 ▶sub init_session {
Subroutine init_session: called CC=2 SCAR=6.9
Pod documented
333 2 my $self = shift;
334 2 my ($dbh, $time_zone) = @_;
335  
336 2 $dbh->do('SET NAMES utf8mb4');
337 2 $dbh->{private_DBIx_QuickORM_pid} = $$;
338  
339 2 ▶ $time_zone = '+00:00' if $time_zone eq 'UTC';
Branch: 1 branch
BranchTrueFalseTotal
if $time_zone eq "UTC" 1 1 2
340 2 return $dbh->do('SET time_zone = ' . $dbh->quote($time_zone));
341 }
342  
343 =pod
344  
345 =over 4
346  
347 =item $dialect->release_inherited($dbh)
348  
349 Points the child's copy of the handle's socket at F</dev/null>, then
350 disconnects, so the parent's session is untouched. Does nothing for an
351 inactive handle.
352  
353 DBD::MariaDB closes every handle it holds when a process exits,
354 C<InactiveDestroy> or not, so a forked child that exits normally would
355 otherwise end the parent's session. At exit this runs for every still-active
356 handle whose recorded pid is not this process.
357  
358 =back
359  
360 =cut
361  
362 2 ▶sub release_inherited {
Subroutine release_inherited: called CC=2 SCAR=6.9
Pod documented
363 2 my $self = shift;
364 2 my ($dbh) = @_;
365  
366 2 ▶ return unless $dbh->{Active};
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh->{"Active"} 1 1 2
367  
368 1 $self->_null_socket($dbh->{mariadb_sockfd});
369  
370 # Statements still open on the handle are the parent's.
371 1 $dbh->{Warn} = 0;
372 1 $dbh->disconnect;
373 1 return;
374 }
375  
376 =pod
377  
378 =over 4
379  
380 =item $sql = $dialect->tables_sql($dbh, $temp)
381  
382 =item $sql = $dialect->fields_sql($dbh, $temp)
383  
384 =item $sql = $dialect->pk_sql($dbh, $temp)
385  
386 =item $sql = $dialect->unique_sql($dbh, $temp)
387  
388 =item $sql = $dialect->fk_sql($dbh, $temp)
389  
390 The scan queries (see L<C<scan>|DBIx::QuickORM::Dialect/"PUBLIC METHODS">),
391 over C<information_schema> for base tables and views in C<DATABASE()>.
392 They take C<$dbh> and C<$temp> as every dialect's do, but use neither:
393 C<information_schema> does not list temporary tables, so C<scan_temp> reads
394 C<SHOW COLUMNS> and C<SHOW INDEX> instead.
395  
396 Field types are C<COLUMN_TYPE>.
397  
398 A column the database fills is auto-increment, generated, or has a default.
399 Generated columns refuse values.
400  
401 MariaDB reports the default of a nullable column without one as the text
402 C<NULL>, which is not a default. On MySQL that text is also how a string
403 default C<'NULL'> appears, so such a nullable column misses C<db_fills>.
404 Key columns are never nullable.
405  
406 A unique index over a column prefix or an
407 expression is left out.
408  
409 =back
410  
411 =cut
412  
413 0 ▶sub tables_sql {
Subroutine tables_sql: not called CC=1 SCAR=6.9
Pod documented
414 0 return <<" EOT";
415 SELECT TABLE_NAME, CASE TABLE_TYPE WHEN 'VIEW' THEN 'view' END
416 FROM information_schema.TABLES
417 WHERE TABLE_SCHEMA = DATABASE()
418 AND TABLE_TYPE IN ('BASE TABLE', 'VIEW')
419 EOT
420 }
421  
422 0 ▶sub fields_sql {
Subroutine fields_sql: not called CC=1 SCAR=6.9
Pod documented
423 0 return <<" EOT";
424 SELECT TABLE_NAME, COLUMN_NAME, COLUMN_TYPE, NULL,
425 CASE WHEN EXTRA LIKE '%auto_increment%'
426 OR COALESCE(GENERATION_EXPRESSION, '') <> ''
427 OR (COLUMN_DEFAULT IS NOT NULL AND NOT (COLUMN_DEFAULT = 'NULL' AND IS_NULLABLE = 'YES'))
428 THEN 1 END,
429 CASE WHEN COALESCE(GENERATION_EXPRESSION, '') <> '' THEN 1 END
430 FROM information_schema.COLUMNS
431 WHERE TABLE_SCHEMA = DATABASE()
432 EOT
433 }
434  
435 0 ▶sub pk_sql {
Subroutine pk_sql: not called CC=1 SCAR=6.9
Pod documented
436 0 return <<" EOT";
437 SELECT TABLE_NAME, COLUMN_NAME
438 FROM information_schema.STATISTICS
439 WHERE TABLE_SCHEMA = DATABASE()
440 AND INDEX_NAME = 'PRIMARY'
441 ORDER BY TABLE_NAME, SEQ_IN_INDEX
442 EOT
443 }
444  
445 0 ▶sub unique_sql {
Subroutine unique_sql: not called CC=1 SCAR=6.9
Pod documented
446 0 return <<" EOT";
447 SELECT TABLE_NAME, INDEX_NAME, CASE WHEN SUB_PART IS NULL THEN COLUMN_NAME END
448 FROM information_schema.STATISTICS
449 WHERE TABLE_SCHEMA = DATABASE()
450 AND NON_UNIQUE = 0
451 AND INDEX_NAME <> 'PRIMARY'
452 EOT
453 }
454  
455 0 ▶sub fk_sql {
Subroutine fk_sql: not called CC=1 SCAR=6.9
Pod documented
456 0 return <<" EOT";
457 SELECT TABLE_NAME, CONSTRAINT_NAME, COLUMN_NAME, REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME
458 FROM information_schema.KEY_COLUMN_USAGE
459 WHERE TABLE_SCHEMA = DATABASE()
460 AND REFERENCED_TABLE_NAME IS NOT NULL
461 ORDER BY TABLE_NAME, CONSTRAINT_NAME, ORDINAL_POSITION
462 EOT
463 }
464  
465 =pod
466  
467 =over 4
468  
469 =item @sql = $dialect->drop_sql($name, $kind, \%spec, temp => 1)
470  
471 C<DROP TEMPORARY TABLE>, which, unlike C<DROP TABLE>, does not commit the open
472 transaction.
473  
474 =back
475  
476 =cut
477  
478 2 ▶sub drop_sql {
Subroutine drop_sql: called CC=1 SCAR=0.0
Pod documented
479 2 my $self = shift;
480 2 my ($name, $kind, $spec, %opts) = @_;
481 2 $self->_only_temp(drop_sql => %opts);
482 1 return "DROP TEMPORARY TABLE " . $self->quote_identifier($name);
483 }
484  
485 =pod
486  
487 =over 4
488  
489 =item \%table = $dialect->scan_temp($dbh, $name)
490  
491 As inherited, from C<SHOW COLUMNS> and C<SHOW INDEX>, read with the same rules
492 as the scan queries. Those also find a permanent table, and croak for a
493 missing name, so pass only a temporary table created on this handle. A temporary table has no foreign keys here: the MySQL
494 family refuses them.
495  
496 =back
497  
498 =cut
499  
500 0 ▶sub scan_temp {
Subroutine scan_temp: not called CC=15 SCAR=54.8
Pod documented
501 0 my $self = shift;
502 0 my ($dbh, $name) = @_;
503 0 my $q = $self->quote_identifier($name);
504 0 my $t = {fields => {}, pk => undef, unique => [], fk => {}, temp => 1};
505  
506 0 for my $col (@{$self->_rows($dbh, "SHOW COLUMNS FROM $q")}) {
507 0 my ($field, $type, $null, undef, $default, $extra) = @$col;
508 0 ▶ my $generated = ($extra // '') =~ m/\b(?:VIRTUAL|STORED|PERSISTENT)\b/i ? 1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
($extra // '') =~ /\b(?:VIRTUAL|STORED|PERSISTENT)\b/i ? : 0 0 0
Condition0%0/2
$extra // ""
l!l
00
MC/DC0%0/1
$extra // ""
$extra
Truth table0%0/2
A
A= $extra
Aresult
11
00
509 0 ▶ my $fills = $generated || ($extra // '') =~ m/auto_increment/i ? 1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$generated || ($extra // '') =~ /auto_increment/i ? : 0 0 0
Condition0%0/5
$generated or ($extra // "") =~ /auto_increment/i
l!l&&r!l&&!r
000
$extra // ""
l!l
00
MC/DC0%0/2
$generated or ($extra // "") =~ /auto_increment/i
$generated $extra
Truth table0%0/3
A or B
A= $generatedB= $extra
ABresult
1X1
011
000
510 0 ▶ $fills = 1 if defined($default) && !($default eq 'NULL' && $null eq 'YES');
Branch: 1 branch
BranchTrueFalseTotal
if defined $default and not $default eq "NULL" && $null eq "YES" 0 0 0
Condition0%0/6
defined $default and not $default eq "NULL" && $null eq "YES"
!ll&&!rl&&r
000
$default eq 'NULL' && $null eq 'YES'
!ll&&!rl&&r
000
MC/DC0%0/3
defined $default and not $default eq "NULL" && $null eq "YES"
defined $default $default eq 'NULL' $null eq 'YES'
Truth table0%0/4
A and not(B && C)
A= defined $defaultB= $default eq 'NULL'C= $null eq 'YES'
ABCresult
0XX0
1110
10X1
1101
511 0 $t->{fields}->{$field} = {%{$self->field_facts($type, undef, $fills, $generated)}, table => $name, name => $field};
512 }
513  
514 0 my (@pk, @unique);
515 0 for my $index (@{$self->_rows($dbh, "SHOW INDEX FROM $q")}) {
516 0 my (undef, $non_unique, $key, $seq, $field, undef, undef, $sub_part) = @$index;
517 0 ▶ if ($key eq 'PRIMARY') { $pk[$seq - 1] = $field; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "PRIMARY") 0 0 0
518 0 ▶ push @unique => [$name, $key, defined($sub_part) ? undef : $field] unless $non_unique;
Branch: 2 branches
BranchTrueFalseTotal
unless $non_unique 0 0 0
defined $sub_part ? : 0 0 0
519 }
520 0 ▶ $t->{pk} = \@pk if @pk;
Branch: 1 branch
BranchTrueFalseTotal
if @pk 0 0 0
521 0 $self->_add_unique({$name => $t}, $self->_keys_by_table(\@unique));
522 0 return $t;
523 }
524  
525 =pod
526  
527 =over 4
528  
529 =item \%field = $dialect->field_facts($db_type)
530  
531 As inherited, but a type whose name ends in C<int> has no C<size>:
532 C<int(11)> is a display width.
533  
534 =for highlighter language=perl
535  
536 my $count = $dialect->field_facts('int(11) unsigned'); # no size
537 my $title = $dialect->field_facts('varchar(255)'); # size 255
538  
539 =back
540  
541 =cut
542  
543 11 ▶sub field_facts {
Subroutine field_facts: called CC=2 SCAR=6.9
Pod documented
544 11 my $self = shift;
545 11 my $field = $self->SUPER::field_facts(@_);
546 11 ▶ delete $field->{size} if $field->{type} =~ m/int$/;
Branch: 1 branch
BranchTrueFalseTotal
if $field->{"type"} =~ /int$/ 2 9 11
547 11 return $field;
548 }
549  
550 =pod
551  
552 =over 4
553  
554 =item [@definitions] = $dialect->_index_sql($table, \@indexes)
555  
556 The indexes as definitions inside the C<CREATE TEMPORARY TABLE>: a separate
557 C<CREATE INDEX> would commit the open transaction.
558  
559 =back
560  
561 =cut
562  
563 17 ▶sub _index_sql {
Subroutine _index_sql: called CC=7 SCAR=19.5
564 17 my $self = shift;
565 17 my ($table, $indexes) = @_;
566 17 my $i = 0;
567 17 my (@inline, @after);
568 17 for my $index (@$indexes) {
569 7 ▶ if (defined $index->{sql}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $index->{"sql"}) 1 6 7
570 1 push @after => $index->{sql};
571 1 next;
572 }
573 6 ▶ my $name = $index->{name} // "${table}_idx_" . ++$i;
Condition100%2/2
$index->{"name"} // "${table}_idx_" . ++$i
l!l
33
MC/DC100%1/1
$index->{"name"} // "${table}_idx_" . ++$i
$index->{"name"}
Truth table100%2/2
A
A= $index->{"name"}
Aresult
11
00
574 6 ▶ my $using = defined($index->{using}) ? ' ' . $self->index_using_sql($index->{using}) : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $index->{'using'} ? : 2 4 6
575 6 ▶ my $cols = ref($index->{using}) ? '' : " (" . join(', ' => map { $self->quote_identifier($_) } @{$index->{columns}}) . ")";
Branch: 1 branch
BranchTrueFalseTotal
ref $index->{'using'} ? : 1 5 6
576 6 ▶ push @inline => ($index->{unique} ? 'UNIQUE ' : '') . "INDEX " . $self->quote_identifier($name) . "$using$cols";
Branch: 1 branch
BranchTrueFalseTotal
$index->{'unique'} ? : 1 5 6
577 }
578 17 return (\@inline, @after);
579 }
580  
581 # Runs before DBI's END block (DBI is loaded first), which would close the parent's session.
582 13 ▶END {
Subroutine END: called CC=7 SCAR=33.2
583 13 local ($?, $@, $!);
584 13 my %drivers = DBI->installed_drivers;
585 13 my $drh = $drivers{MariaDB};
586 0 ▶ for my $dbh (grep { $_ && $_->{Active} } $drh ? @{$drh->{ChildHandles} // []} : ()) {
Branch: 1 branch
BranchTrueFalseTotal
$drh ? : 0 13 13
Condition0%0/5
$_ && $_->{'Active'}
!ll&&!rl&&r
000
$drh->{"ChildHandles"} // []
l!l
00
MC/DC0%0/2
$_ && $_->{'Active'}
$_ $_->{'Active'}
MC/DC0%0/1
$drh->{"ChildHandles"} // []
$drh->{"ChildHandles"}
Truth table0%0/3
A && B
A= $_B= $_->{'Active'}
ABresult
0X0
100
111
Truth table0%0/2
A
A= $drh->{"ChildHandles"}
Aresult
11
00
587 0 my $pid = $dbh->{private_DBIx_QuickORM_pid};
588 0 ▶ __PACKAGE__->release_inherited($dbh) if $pid && $pid != $$;
Branch: 1 branch
BranchTrueFalseTotal
if $pid and $pid != $$ 0 0 0
Condition0%0/3
$pid and $pid != $$
!ll&&!rl&&r
000
MC/DC0%0/2
$pid and $pid != $$
$pid $pid != $$
Truth table0%0/3
A and B
A= $pidB= $pid != $$
ABresult
0X0
100
111
589 }
590 }
591  
592 14 1;
593  
594 __END__
↑ Summary blib/lib/DBIx/QuickORM/Map.pm »