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

filter: Statementstmt 80.8% 59 / 73 Branchbran 78.5% 11 / 14 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 66.6% 20 / 30 Podpod 100% 25 / 25 totaltotal 74.1% 115 / 155 CC 12 SCAR 18.0
CC
12
Cov
74%
END
27.7
begin_sql
2.0
supports_async
2.0
CRAP 6.0
« blib/lib/DBIx/QuickORM/SQLBuilder/SQLAbstract.pm ↑ Summary blib/lib/DBIx/QuickORM/Source/Query.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.000031';
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 $dialect->init_session($dbh, $time_zone)
194  
195 C<SET NAMES utf8mb4>, then C<SET time_zone>. Also records the pid that owns
196 the handle, for C<release_inherited>.
197  
198 C<UTC> is sent as C<+00:00>, which works without the server's time zone
199 tables. Any other zone name needs them.
200  
201 =back
202  
203 =cut
204  
205 2 ▶sub init_session {
Subroutine init_session: called CC=2 SCAR=6.9
Pod documented
206 2 my $self = shift;
207 2 my ($dbh, $time_zone) = @_;
208  
209 2 $dbh->do('SET NAMES utf8mb4');
210 2 $dbh->{private_DBIx_QuickORM_pid} = $$;
211  
212 2 ▶ $time_zone = '+00:00' if $time_zone eq 'UTC';
Branch: 1 branch
BranchTrueFalseTotal
if $time_zone eq "UTC" 1 1 2
213 2 return $dbh->do('SET time_zone = ' . $dbh->quote($time_zone));
214 }
215  
216 =pod
217  
218 =over 4
219  
220 =item $dialect->release_inherited($dbh)
221  
222 Points the child's copy of the handle's socket at F</dev/null>, then
223 disconnects, so the parent's session is untouched. Does nothing for an
224 inactive handle.
225  
226 DBD::MariaDB closes every handle it holds when a process exits,
227 C<InactiveDestroy> or not, so a forked child that exits normally would
228 otherwise end the parent's session. At exit this runs for every still-active
229 handle whose recorded pid is not this process.
230  
231 =back
232  
233 =cut
234  
235 2 ▶sub release_inherited {
Subroutine release_inherited: called CC=2 SCAR=6.9
Pod documented
236 2 my $self = shift;
237 2 my ($dbh) = @_;
238  
239 2 ▶ return unless $dbh->{Active};
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh->{"Active"} 1 1 2
240  
241 1 $self->_null_socket($dbh->{mariadb_sockfd});
242  
243 # Statements still open on the handle are the parent's.
244 1 $dbh->{Warn} = 0;
245 1 $dbh->disconnect;
246 1 return;
247 }
248  
249 =pod
250  
251 =over 4
252  
253 =item $sql = $dialect->tables_sql
254  
255 =item $sql = $dialect->fields_sql
256  
257 =item $sql = $dialect->pk_sql
258  
259 =item $sql = $dialect->unique_sql
260  
261 =item $sql = $dialect->fk_sql
262  
263 The scan queries (see L<C<scan>|DBIx::QuickORM::Dialect/"PUBLIC METHODS">),
264 over C<information_schema> for base tables in C<DATABASE()>.
265  
266 Field types are C<COLUMN_TYPE>.
267  
268 A column the database fills is auto-increment, generated, or has a default.
269 Generated columns refuse values.
270  
271 MariaDB reports the default of a nullable column without one as the text
272 C<NULL>, which is not a default. On MySQL that text is also how a string
273 default C<'NULL'> appears, so such a nullable column misses C<db_fills>.
274 Key columns are never nullable.
275  
276 A unique index over a column prefix or an
277 expression is left out.
278  
279 =back
280  
281 =cut
282  
283 0 ▶sub tables_sql {
Subroutine tables_sql: not called CC=1 SCAR=6.9
Pod documented
284 0 return <<" EOT";
285 SELECT TABLE_NAME
286 FROM information_schema.TABLES
287 WHERE TABLE_SCHEMA = DATABASE()
288 AND TABLE_TYPE = 'BASE TABLE'
289 EOT
290 }
291  
292 0 ▶sub fields_sql {
Subroutine fields_sql: not called CC=1 SCAR=6.9
Pod documented
293 0 return <<" EOT";
294 SELECT TABLE_NAME, COLUMN_NAME, COLUMN_TYPE, NULL,
295 CASE WHEN EXTRA LIKE '%auto_increment%'
296 OR COALESCE(GENERATION_EXPRESSION, '') <> ''
297 OR (COLUMN_DEFAULT IS NOT NULL AND NOT (COLUMN_DEFAULT = 'NULL' AND IS_NULLABLE = 'YES'))
298 THEN 1 END,
299 CASE WHEN COALESCE(GENERATION_EXPRESSION, '') <> '' THEN 1 END
300 FROM information_schema.COLUMNS
301 WHERE TABLE_SCHEMA = DATABASE()
302 EOT
303 }
304  
305 0 ▶sub pk_sql {
Subroutine pk_sql: not called CC=1 SCAR=6.9
Pod documented
306 0 return <<" EOT";
307 SELECT TABLE_NAME, COLUMN_NAME
308 FROM information_schema.STATISTICS
309 WHERE TABLE_SCHEMA = DATABASE()
310 AND INDEX_NAME = 'PRIMARY'
311 ORDER BY TABLE_NAME, SEQ_IN_INDEX
312 EOT
313 }
314  
315 0 ▶sub unique_sql {
Subroutine unique_sql: not called CC=1 SCAR=6.9
Pod documented
316 0 return <<" EOT";
317 SELECT TABLE_NAME, INDEX_NAME, CASE WHEN SUB_PART IS NULL THEN COLUMN_NAME END
318 FROM information_schema.STATISTICS
319 WHERE TABLE_SCHEMA = DATABASE()
320 AND NON_UNIQUE = 0
321 AND INDEX_NAME <> 'PRIMARY'
322 EOT
323 }
324  
325 0 ▶sub fk_sql {
Subroutine fk_sql: not called CC=1 SCAR=6.9
Pod documented
326 0 return <<" EOT";
327 SELECT TABLE_NAME, CONSTRAINT_NAME, COLUMN_NAME, REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME
328 FROM information_schema.KEY_COLUMN_USAGE
329 WHERE TABLE_SCHEMA = DATABASE()
330 AND REFERENCED_TABLE_NAME IS NOT NULL
331 ORDER BY TABLE_NAME, CONSTRAINT_NAME, ORDINAL_POSITION
332 EOT
333 }
334  
335 =pod
336  
337 =over 4
338  
339 =item \%field = $dialect->field_facts($db_type)
340  
341 As inherited, but a type whose name ends in C<int> has no C<size>:
342 C<int(11)> is a display width.
343  
344 =back
345  
346 =cut
347  
348 3 ▶sub field_facts {
Subroutine field_facts: called CC=2 SCAR=6.9
Pod documented
349 3 my $self = shift;
350 3 my $field = $self->SUPER::field_facts(@_);
351 3 ▶ delete $field->{size} if $field->{type} =~ m/int$/;
Branch: 1 branch
BranchTrueFalseTotal
if $field->{"type"} =~ /int$/ 2 1 3
352 3 return $field;
353 }
354  
355 # Runs before DBI's END block (DBI is loaded first), which would close the parent's session.
356 4 ▶END {
Subroutine END: called CC=7 SCAR=33.2
357 4 local ($?, $@, $!);
358 4 my %drivers = DBI->installed_drivers;
359 4 my $drh = $drivers{MariaDB};
360 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
361 0 my $pid = $dbh->{private_DBIx_QuickORM_pid};
362 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
363 }
364 }
365  
366 4 1;
367  
368 __END__
« blib/lib/DBIx/QuickORM/SQLBuilder/SQLAbstract.pm ↑ Summary blib/lib/DBIx/QuickORM/Source/Query.pm »