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

filter: Statementstmt 72.8% 59 / 81 Branchbran 61.1% 11 / 18 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 62.5% 20 / 32 Podpod 100% 26 / 26 totaltotal 67.0% 116 / 173 CC 15 SCAR 18.6
CC
15
Cov
64%
END
27.7
_scan_key_columns
12.0
scan_cache_key
6.0
CRAP 6.4
« blib/lib/DBIx/QuickORM/SQLBuilder/SQLAbstract.pm ↑ Summary blib/lib/DBIx/QuickORM/Dialect/DuckDB.pm »
1 package DBIx::QuickORM::Dialect::MySQL;
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 4 our $VERSION = '0.000032';
6  
7 4 ▶use DBI();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 ▶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 =back
96  
97 =cut
98  
99 452 ▶sub quote_char { '`' }
Subroutine quote_char: called CC=1 SCAR=0.0
Pod documented
100 1 ▶sub supports_returning_insert { 0 }
Subroutine supports_returning_insert: called CC=1 SCAR=0.0
Pod documented
101 2 ▶sub supports_returning_update { 0 }
Subroutine supports_returning_update: called CC=1 SCAR=0.0
Pod documented
102 1 ▶sub supports_returning_delete { 0 }
Subroutine supports_returning_delete: called CC=1 SCAR=0.0
Pod documented
103 1 ▶sub supports_isolation { 1 }
Subroutine supports_isolation: called CC=1 SCAR=0.0
Pod documented
104 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
105 1 ▶sub having_uses_alias { 1 }
Subroutine having_uses_alias: called CC=1 SCAR=0.0
Pod documented
106 1 ▶sub bind_limit { 65535 }
Subroutine bind_limit: called CC=1 SCAR=0.0
Pod documented
107 1 ▶sub datetime_format { ('DateTime::Format::MySQL', 'DateTime::Format::MySQL', {timestamp => 1}) }
Subroutine datetime_format: called CC=1 SCAR=0.0
Pod documented
108 0 ▶sub supports_async { 1 }
Subroutine supports_async: not called CC=1 SCAR=6.9
Pod documented
109 0 ▶sub async_prepare_attrs { {mariadb_async => 1} }
Subroutine async_prepare_attrs: not called CC=1 SCAR=6.9
Pod documented
110 0 ▶sub async_ready { $_[2]->mariadb_async_ready }
Subroutine async_ready: not called CC=1 SCAR=6.9
Pod documented
111 0 ▶sub async_result { $_[2]->mariadb_async_result }
Subroutine async_result: not called CC=1 SCAR=6.9
Pod documented
112 0 ▶sub cancel_async { $_[2]->mariadb_async_result }
Subroutine cancel_async: not called CC=1 SCAR=6.9
Pod documented
113  
114 =pod
115  
116 =over 4
117  
118 =item $class = $dialect->dialect_for_dbh($dbh)
119  
120 L<DBIx::QuickORM::Dialect::MariaDB> when C<SELECT version()> names MariaDB,
121 otherwise this class.
122  
123 =back
124  
125 =cut
126  
127 3 ▶sub dialect_for_dbh {
Subroutine dialect_for_dbh: called CC=2 SCAR=6.9
Pod documented
128 3 my $class = shift;
129 3 my ($dbh) = @_;
130  
131 3 my ($version) = $dbh->selectrow_array('SELECT version()');
132 3 ▶ return $class unless $version =~ m/mariadb/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $version =~ /mariadb/i 2 1 3
133  
134 1 require DBIx::QuickORM::Dialect::MariaDB;
135 1 return 'DBIx::QuickORM::Dialect::MariaDB';
136 }
137  
138 =pod
139  
140 =over 4
141  
142 =item \%kinds = $dialect->native_error_kinds
143  
144 The native codes, since MySQL reports a generic SQLSTATE for most errors:
145  
146 =for highlighter language=text
147  
148 1205 lock_timeout (lock wait timeout)
149 1213 deadlock
150 1062, 1169, 1586 unique_violation (duplicate key)
151 1216, 1217, 1451, 1452 foreign_key_violation (missing or still-referenced parent row)
152 1048, 1364 not_null_violation (NULL, or no value and no default)
153 3819 check_violation (failed CHECK)
154  
155 =back
156  
157 =cut
158  
159 my %NATIVE_KIND = (
160 1205 => 'lock_timeout',
161 1213 => 'deadlock',
162 3819 => 'check_violation',
163 12 (map { $_ => 'unique_violation' } 1062, 1169, 1586),
164 16 (map { $_ => 'foreign_key_violation' } 1216, 1217, 1451, 1452),
165 4 (map { $_ => 'not_null_violation' } 1048, 1364),
166 );
167  
168 13 ▶sub native_error_kinds { \%NATIVE_KIND }
Subroutine native_error_kinds: called CC=1 SCAR=0.0
Pod documented
169  
170 =pod
171  
172 =over 4
173  
174 =item $ns = $dialect->namespace($dbh)
175  
176 C<DATABASE()>, or undef when the connection has no database selected.
177  
178 =back
179  
180 =cut
181  
182 2 ▶sub namespace {
Subroutine namespace: called CC=1 SCAR=0.0
Pod documented
183 2 my $self = shift;
184 2 my ($dbh) = @_;
185 2 my ($ns) = $dbh->selectrow_array('SELECT DATABASE()');
186 2 return $ns;
187 }
188  
189 =pod
190  
191 =over 4
192  
193 =item $key = $dialect->scan_cache_key($dbh, $namespace)
194  
195 The base key, plus what else decides what the scan sees: the server's host
196 name, port, and socket (connection attributes outside the DSN can pick the
197 server), the account the session resolved to, its C<sql_select_limit> (which
198 truncates the catalog queries), its active roles where the
199 server has roles (they can change which tables the catalog shows), and, on
200 MySQL 8.0.30 and later, whether the catalog shows generated invisible primary
201 keys.
202  
203 =back
204  
205 =cut
206  
207 0 ▶sub scan_cache_key {
Subroutine scan_cache_key: not called CC=2 SCAR=17.9
Pod documented
208 0 my $self = shift;
209 0 my ($dbh, $namespace) = @_;
210 0 my @session = $dbh->selectrow_array('SELECT ' . join(', ' => '@@hostname', '@@port', '@@socket', 'CURRENT_USER()', '@@session.sql_select_limit', $self->_scan_key_columns));
211 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
212 }
213  
214 =pod
215  
216 =over 4
217  
218 =item @sql = $dialect->_scan_key_columns
219  
220 The server-specific session values C<scan_cache_key> adds: C<CURRENT_ROLE()>
221 from MySQL 8.0, and the generated invisible primary key setting from 8.0.30.
222  
223 =back
224  
225 =cut
226  
227 0 ▶sub _scan_key_columns {
Subroutine _scan_key_columns: not called CC=3 SCAR=24.8
228 0 my $self = shift;
229 0 my $version = $self->server_version;
230 return (
231 0 ▶ ($version >= 8_000_000 ? 'CURRENT_ROLE()' : ()),
Branch: 2 branches
BranchTrueFalseTotal
$version >= 8000000 ? : 0 0 0
$version >= 8000030 ? : 0 0 0
232 ($version >= 8_000_030 ? '@@session.show_gipk_in_create_table_and_information_schema' : ()),
233 );
234 }
235  
236 =pod
237  
238 =over 4
239  
240 =item $dialect->init_session($dbh, $time_zone)
241  
242 C<SET NAMES utf8mb4>, then C<SET time_zone>. Also records the pid that owns
243 the handle, for C<release_inherited>.
244  
245 C<UTC> is sent as C<+00:00>, which works without the server's time zone
246 tables. Any other zone name needs them.
247  
248 =back
249  
250 =cut
251  
252 2 ▶sub init_session {
Subroutine init_session: called CC=2 SCAR=6.9
Pod documented
253 2 my $self = shift;
254 2 my ($dbh, $time_zone) = @_;
255  
256 2 $dbh->do('SET NAMES utf8mb4');
257 2 $dbh->{private_DBIx_QuickORM_pid} = $$;
258  
259 2 ▶ $time_zone = '+00:00' if $time_zone eq 'UTC';
Branch: 1 branch
BranchTrueFalseTotal
if $time_zone eq "UTC" 1 1 2
260 2 return $dbh->do('SET time_zone = ' . $dbh->quote($time_zone));
261 }
262  
263 =pod
264  
265 =over 4
266  
267 =item $dialect->release_inherited($dbh)
268  
269 Points the child's copy of the handle's socket at F</dev/null>, then
270 disconnects, so the parent's session is untouched. Does nothing for an
271 inactive handle.
272  
273 DBD::MariaDB closes every handle it holds when a process exits,
274 C<InactiveDestroy> or not, so a forked child that exits normally would
275 otherwise end the parent's session. At exit this runs for every still-active
276 handle whose recorded pid is not this process.
277  
278 =back
279  
280 =cut
281  
282 2 ▶sub release_inherited {
Subroutine release_inherited: called CC=2 SCAR=6.9
Pod documented
283 2 my $self = shift;
284 2 my ($dbh) = @_;
285  
286 2 ▶ return unless $dbh->{Active};
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh->{"Active"} 1 1 2
287  
288 1 $self->_null_socket($dbh->{mariadb_sockfd});
289  
290 # Statements still open on the handle are the parent's.
291 1 $dbh->{Warn} = 0;
292 1 $dbh->disconnect;
293 1 return;
294 }
295  
296 =pod
297  
298 =over 4
299  
300 =item $sql = $dialect->tables_sql
301  
302 =item $sql = $dialect->fields_sql
303  
304 =item $sql = $dialect->pk_sql
305  
306 =item $sql = $dialect->unique_sql
307  
308 =item $sql = $dialect->fk_sql
309  
310 The scan queries (see L<C<scan>|DBIx::QuickORM::Dialect/"PUBLIC METHODS">),
311 over C<information_schema> for base tables in C<DATABASE()>.
312  
313 Field types are C<COLUMN_TYPE>.
314  
315 A column the database fills is auto-increment, generated, or has a default.
316 Generated columns refuse values.
317  
318 MariaDB reports the default of a nullable column without one as the text
319 C<NULL>, which is not a default. On MySQL that text is also how a string
320 default C<'NULL'> appears, so such a nullable column misses C<db_fills>.
321 Key columns are never nullable.
322  
323 A unique index over a column prefix or an
324 expression is left out.
325  
326 =back
327  
328 =cut
329  
330 0 ▶sub tables_sql {
Subroutine tables_sql: not called CC=1 SCAR=6.9
Pod documented
331 0 return <<" EOT";
332 SELECT TABLE_NAME
333 FROM information_schema.TABLES
334 WHERE TABLE_SCHEMA = DATABASE()
335 AND TABLE_TYPE = 'BASE TABLE'
336 EOT
337 }
338  
339 0 ▶sub fields_sql {
Subroutine fields_sql: not called CC=1 SCAR=6.9
Pod documented
340 0 return <<" EOT";
341 SELECT TABLE_NAME, COLUMN_NAME, COLUMN_TYPE, NULL,
342 CASE WHEN EXTRA LIKE '%auto_increment%'
343 OR COALESCE(GENERATION_EXPRESSION, '') <> ''
344 OR (COLUMN_DEFAULT IS NOT NULL AND NOT (COLUMN_DEFAULT = 'NULL' AND IS_NULLABLE = 'YES'))
345 THEN 1 END,
346 CASE WHEN COALESCE(GENERATION_EXPRESSION, '') <> '' THEN 1 END
347 FROM information_schema.COLUMNS
348 WHERE TABLE_SCHEMA = DATABASE()
349 EOT
350 }
351  
352 0 ▶sub pk_sql {
Subroutine pk_sql: not called CC=1 SCAR=6.9
Pod documented
353 0 return <<" EOT";
354 SELECT TABLE_NAME, COLUMN_NAME
355 FROM information_schema.STATISTICS
356 WHERE TABLE_SCHEMA = DATABASE()
357 AND INDEX_NAME = 'PRIMARY'
358 ORDER BY TABLE_NAME, SEQ_IN_INDEX
359 EOT
360 }
361  
362 0 ▶sub unique_sql {
Subroutine unique_sql: not called CC=1 SCAR=6.9
Pod documented
363 0 return <<" EOT";
364 SELECT TABLE_NAME, INDEX_NAME, CASE WHEN SUB_PART IS NULL THEN COLUMN_NAME END
365 FROM information_schema.STATISTICS
366 WHERE TABLE_SCHEMA = DATABASE()
367 AND NON_UNIQUE = 0
368 AND INDEX_NAME <> 'PRIMARY'
369 EOT
370 }
371  
372 0 ▶sub fk_sql {
Subroutine fk_sql: not called CC=1 SCAR=6.9
Pod documented
373 0 return <<" EOT";
374 SELECT TABLE_NAME, CONSTRAINT_NAME, COLUMN_NAME, REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME
375 FROM information_schema.KEY_COLUMN_USAGE
376 WHERE TABLE_SCHEMA = DATABASE()
377 AND REFERENCED_TABLE_NAME IS NOT NULL
378 ORDER BY TABLE_NAME, CONSTRAINT_NAME, ORDINAL_POSITION
379 EOT
380 }
381  
382 =pod
383  
384 =over 4
385  
386 =item \%field = $dialect->field_facts($db_type)
387  
388 As inherited, but a type whose name ends in C<int> has no C<size>:
389 C<int(11)> is a display width.
390  
391 =back
392  
393 =cut
394  
395 3 ▶sub field_facts {
Subroutine field_facts: called CC=2 SCAR=6.9
Pod documented
396 3 my $self = shift;
397 3 my $field = $self->SUPER::field_facts(@_);
398 3 ▶ delete $field->{size} if $field->{type} =~ m/int$/;
Branch: 1 branch
BranchTrueFalseTotal
if $field->{"type"} =~ /int$/ 2 1 3
399 3 return $field;
400 }
401  
402 # Runs before DBI's END block (DBI is loaded first), which would close the parent's session.
403 4 ▶END {
Subroutine END: called CC=7 SCAR=33.2
404 4 local ($?, $@, $!);
405 4 my %drivers = DBI->installed_drivers;
406 4 my $drh = $drivers{MariaDB};
407 0 ▶ for my $dbh (grep { $_ && $_->{Active} } $drh ? @{$drh->{ChildHandles} // []} : ()) {
Branch: 1 branch
BranchTrueFalseTotal
$drh ? : 0 4 4
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
408 0 my $pid = $dbh->{private_DBIx_QuickORM_pid};
409 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
410 }
411 }
412  
413 4 1;
414  
415 __END__
« blib/lib/DBIx/QuickORM/SQLBuilder/SQLAbstract.pm ↑ Summary blib/lib/DBIx/QuickORM/Dialect/DuckDB.pm »