MySQL/Storage.pm

filter: Statementstmt 37.1% 23 / 62 Branchbran 20.0% 2 / 10 Conditioncond 11.7% 2 / 17 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 19.0% 4 / 21 Podpod 90.9% 10 / 11 totaltotal 31.0% 41 / 132 CC 12 SCAR 18.8
CC
12
Cov
30%
deployment_statements
20.0
_dbh_last_insert_id
12.0
is_replicating
12.0
CRAP 6.6
« MySQL/Introspect.pm ↑ Summary MySQL/Storage/MariaDB.pm »
1 package DBIO::MySQL::Storage;
2 # ABSTRACT: MySQL storage layer for DBIO
3  
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 ▶use base qw/DBIO::Storage::DBI/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 6 __PACKAGE__->register_driver('mysql' => __PACKAGE__);
10  
11 # Async is an explicit, per-connection mode (core ADR 0030): a connection opened
12 # with connect(..., { async => 'ev' }) resolves the 'ev' mode through the mode
13 # registry to the native MySQL EV backend below. Registering on this driver
14 # storage class MRO-shadows the generic modes (forked/future_io) registered on
15 # the base storage, so 'ev' picks the MySQL-specific backend. There is no auto-
16 # fallback and no silent degrade: a mode that is neither requested nor registered
17 # is simply unavailable, and requesting an unregistered/uninstalled mode croaks
18 # (see DBIO::Storage::DBI::_async_storage). This only stores the class name --
19 # DBIO::MySQL::EV::Storage (the optional dbio-mysql-ev dist) is loaded lazily
20 # on first use, so registering it here is safe when the dist is absent.
21 6 __PACKAGE__->register_async_mode( ev => 'DBIO::MySQL::EV::Storage' );
22  
23 # This is the ONLY async registration this driver needs, and it stays as-is
24 # under the storage-layer composition model (core karr #70): a mode maps to a
25 # transport BASE class, once, on this concrete driver storage. This dist writes
26 # storage_type only from its Schema components' connection() (DBIO::MySQL /
27 # DBIO::MySQL::MariaDB) -- choosing the driver BASE storage stays the driver's
28 # job. A driver EXTENSION does not subclass storage_type nor add a shadow
29 # register_async_mode / ::Async of its own: it registers a plain storage LAYER
30 # (DBIO::Schema::register_storage_layer) whose async mirror composes ON TOP of
31 # the resolved transport via DBIO::Storage::Composed. MySQL ships no such
32 # extension today, but the mechanism is core-provided and this reference driver
33 # honours it -- composition rides on top of this base; the mode->transport-base
34 # map does not grow per extension.
35  
36 0 ▶sub dbio_deploy_class { 'DBIO::MySQL::Deploy' }
Subroutine dbio_deploy_class: not called CC=1 SCAR=6.9
Pod undocumented
37  
38 6 __PACKAGE__->sql_maker_class('DBIO::MySQL::SQLMaker');
39 6 __PACKAGE__->sql_quote_char('`');
40 6 __PACKAGE__->datetime_parser_type('DateTime::Format::MySQL');
41  
42 6 __PACKAGE__->_use_multicolumn_in(1);
43  
44  
45 0 ▶sub with_deferred_fk_checks {
Subroutine with_deferred_fk_checks: not called CC=1 SCAR=6.9
Pod documented
46 0 my ($self, $sub) = @_;
47  
48 0 $self->_do_query('SET FOREIGN_KEY_CHECKS = 0');
49 0 $sub->();
50 0 $self->_do_query('SET FOREIGN_KEY_CHECKS = 1');
51 }
52  
53  
54 0 ▶sub connect_call_set_strict_mode {
Subroutine connect_call_set_strict_mode: not called CC=1 SCAR=6.9
Pod documented
55 0 my $self = shift;
56  
57 # the @@sql_mode puts back what was previously set on the session handle
58 0 $self->_do_query(q|SET SQL_MODE = CONCAT('ANSI,TRADITIONAL,ONLY_FULL_GROUP_BY,', @@sql_mode)|);
59 0 $self->_do_query(q|SET SQL_AUTO_IS_NULL = 0|);
60 }
61  
62  
63 0 ▶sub _dbh_last_insert_id {
Subroutine _dbh_last_insert_id: not called CC=3 SCAR=24.8
64 0 my ($self) = @_;
65  
66 # default mysql_auto_reconnect to off unless explicitly set
67 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_dbh->{"mysql_auto_reconnect"} and not exists $self->_dbio_connect_attributes->{"mysql_auto_reconnect"}) 0 0 0
Condition0%0/3
$self->_dbh->{"mysql_auto_reconnect"} and not exists $self->_dbio_connect_attributes->{"mysql_auto_reconnect"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->_dbh->{"mysql_auto_reconnect"} and not exists $self->_dbio_connect_attributes->{"mysql_auto_reconnect"}
$self->_dbh->{"mysql_auto_reconnect"} not exists $self->_dbio_connect_attributes->{"mysql_auto_reconnect"}
Truth table0%0/3
A and not(B)
A= $self->_dbh->{"mysql_auto_reconnect"}B= not exists $self->_dbio_connect_attributes->{"mysql_auto_reconnect"}
ABresult
0X0
100
111
68 $self->_dbh->{mysql_auto_reconnect}
69 and
70 ! exists $self->_dbio_connect_attributes->{mysql_auto_reconnect}
71 ) {
72 0 $self->_dbh->{mysql_auto_reconnect} = 0;
73 }
74  
75 0 $self->_dbh->{mysql_insertid};
76 }
77  
78 # we need to figure out what mysql version we're running
79 56 ▶sub sql_maker {
Subroutine sql_maker: called CC=4 SCAR=14.6
Pod documented
80 56 my $self = shift;
81  
82 # it is critical to get the version *before* calling next::method
83 # otherwise the potential connect will obliterate the sql_maker
84 # next::method will populate in the _sql_maker accessor
85 56 my $mysql_ver = $self->_server_info->{normalized_dbms_version};
86  
87 56 my $sm = $self->next::method(@_);
88  
89 # MySQL always uses backtick quoting — ensure it is active even when
90 # the caller did not pass quote_names in connect_info.
91 56 ▶ $sm->{quote_char} //= $self->sql_quote_char;
Condition33%1/3
$sm->{"quote_char"} //= $self->sql_quote_char
l!l&&r!l&&!r
5600
MC/DC0%0/2
$sm->{"quote_char"} //= $self->sql_quote_char
$sm->{"quote_char"} $self->sql_quote_char
Truth table33%1/3
A //= B
A= $sm->{"quote_char"}B= $self->sql_quote_char
ABresult
1X1
011
000
92 56 ▶ $sm->{name_sep} //= $self->sql_name_sep;
Condition33%1/3
$sm->{"name_sep"} //= $self->sql_name_sep
l!l&&r!l&&!r
5600
MC/DC0%0/2
$sm->{"name_sep"} //= $self->sql_name_sep
$sm->{"name_sep"} $self->sql_name_sep
Truth table33%1/3
A //= B
A= $sm->{"name_sep"}B= $self->sql_name_sep
ABresult
1X1
011
000
93  
94 # mysql 3 does not understand a bare JOIN
95 56 ▶ $sm->{_default_jointype} = 'INNER' if $mysql_ver < 4;
Branch: 1 branch
BranchTrueFalseTotal
if $mysql_ver < 4 5 51 56
96  
97 56 $sm;
98 }
99  
100 0 ▶sub sqlt_type {
Subroutine sqlt_type: not called CC=1 SCAR=6.9
Pod documented
101 0 return 'MySQL';
102 }
103  
104  
105 0 ▶sub deploy_defaults {
Subroutine deploy_defaults: not called CC=1 SCAR=6.9
Pod documented
106 0 return (add_drop_table => 1);
107 }
108  
109  
110 0 ▶sub deploy_setup {
Subroutine deploy_setup: not called CC=1 SCAR=6.9
Pod documented
111 0 my ($self, $schema) = @_;
112 0 eval { $self->connect_call_set_zero_date_permissive };
113 }
114  
115 0 ▶sub connect_call_set_zero_date_permissive {
Subroutine connect_call_set_zero_date_permissive: not called CC=1 SCAR=6.9
Pod documented
116 0 my $self = shift;
117 0 $self->_do_query(
118 q{SET SESSION sql_mode = REPLACE(REPLACE(@@SESSION.sql_mode,'NO_ZERO_DATE',''),'NO_ZERO_IN_DATE','')}
119 );
120 }
121  
122  
123 0 ▶sub _random_function { 'RAND()' }
Subroutine _random_function: not called CC=1 SCAR=6.9
124  
125 0 ▶sub _explain_sql { "EXPLAIN $_[1]" }
Subroutine _explain_sql: not called CC=1 SCAR=6.9
126  
127 0 ▶sub deployment_statements {
Subroutine deployment_statements: not called CC=4 SCAR=30.0
Pod documented
128 0 my $self = shift;
129 0 my ($schema, $type, $version, $dir, $sqltargs, @rest) = @_;
130  
131 0 ▶ $sqltargs //= {};
Condition0%0/2
$sqltargs //= {}
l!l
00
MC/DC0%0/1
$sqltargs //= {}
$sqltargs
Truth table0%0/2
A
A= $sqltargs
Aresult
11
00
132  
133 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $sqltargs->{"producer_args"}{"mysql_version"} and my $dver = $self->_server_info->{"normalized_dbms_version"}) 0 0 0
Condition0%0/3
not exists $sqltargs->{"producer_args"}{"mysql_version"} and my $dver = $self->_server_info->{"normalized_dbms_version"}
!ll&&!rl&&r
000
MC/DC0%0/2
not exists $sqltargs->{"producer_args"}{"mysql_version"} and my $dver = $self->_server_info->{"normalized_dbms_version"}
not exists $sqltargs->{"producer_args"}{"mysql_version"} my $dver = $self->_server_info->{"normalized_dbms_version"}
Truth table0%0/3
not(A) and B
A= not exists $sqltargs->{"producer_args"}{"mysql_version"}B= my $dver = $self->_server_info->{"normalized_dbms_version"}
ABresult
0X0
100
111
134 ! exists $sqltargs->{producer_args}{mysql_version}
135 and
136 my $dver = $self->_server_info->{normalized_dbms_version}
137 ) {
138 0 $sqltargs->{producer_args}{mysql_version} = $dver;
139 }
140  
141 0 $self->next::method($schema, $type, $version, $dir, $sqltargs, @rest);
142 }
143  
144  
145 0 ▶sub _exec_svp_begin {
Subroutine _exec_svp_begin: not called CC=1 SCAR=6.9
146 0 my ($self, $name) = @_;
147  
148 0 $self->_dbh->do("SAVEPOINT $name");
149 }
150  
151 0 ▶sub _exec_svp_release {
Subroutine _exec_svp_release: not called CC=1 SCAR=6.9
152 0 my ($self, $name) = @_;
153  
154 0 $self->_dbh->do("RELEASE SAVEPOINT $name");
155 }
156  
157 0 ▶sub _exec_svp_rollback {
Subroutine _exec_svp_rollback: not called CC=1 SCAR=6.9
158 0 my ($self, $name) = @_;
159  
160 0 $self->_dbh->do("ROLLBACK TO SAVEPOINT $name")
161 }
162  
163 0 ▶sub is_replicating {
Subroutine is_replicating: not called CC=3 SCAR=24.8
Pod documented
164 0 ▶ my $status = shift->_replication_status_row
Branch: 1 branch
BranchTrueFalseTotal
my $status = (shift())->_replication_status_row or return 0 0 0
165 or return;
166 0 ▶ return ($status->{Slave_IO_Running} eq 'Yes') && ($status->{Slave_SQL_Running} eq 'Yes');
Condition0%0/3
$status->{'Slave_IO_Running'} eq 'Yes' && $status->{'Slave_SQL_Running'} eq 'Yes'
!ll&&!rl&&r
000
MC/DC0%0/2
$status->{'Slave_IO_Running'} eq 'Yes' && $status->{'Slave_SQL_Running'} eq 'Yes'
$status->{'Slave_IO_Running'} eq 'Yes' $status->{'Slave_SQL_Running'} eq 'Yes'
Truth table0%0/3
A && B
A= $status->{'Slave_IO_Running'} eq 'Yes'B= $status->{'Slave_SQL_Running'} eq 'Yes'
ABresult
0X0
100
111
167 }
168  
169  
170 0 ▶sub lag_behind_master {
Subroutine lag_behind_master: not called CC=2 SCAR=17.9
Pod documented
171 0 ▶ my $status = shift->_replication_status_row
Branch: 1 branch
BranchTrueFalseTotal
my $status = (shift())->_replication_status_row or return undef 0 0 0
172 or return undef;
173 0 return $status->{Seconds_Behind_Master};
174 }
175  
176  
177 0 ▶sub _replication_status_row {
Subroutine _replication_status_row: not called CC=1 SCAR=6.9
178 0 my $self = shift;
179 0 return $self->_get_dbh->selectrow_hashref('SHOW SLAVE STATUS');
180 }
181  
182  
183 6 1;
184  
185 __END__
186  
187 =pod
188  
189 =encoding UTF-8
190  
191 =head1 NAME
192  
193 DBIO::MySQL::Storage - MySQL storage layer for DBIO
194  
195 =head1 VERSION
196  
197 version 0.900001
198  
199 =head1 SYNOPSIS
200  
201 package MyApp::Schema;
202 use base 'DBIO::Schema';
203 __PACKAGE__->load_components('MySQL');
204  
205 # Optionally enable strict mode on connect
206 my $schema = MyApp::Schema->connect(
207 $dsn, $user, $pass,
208 { on_connect_call => 'set_strict_mode' },
209 );
210  
211 =head1 DESCRIPTION
212  
213 MySQL storage backend for L<DBIO>. Extends L<DBIO::Storage::DBI> with
214 MySQL-specific behavior:
215  
216 =over 4
217  
218 =item *
219  
220 Uses backtick quoting and C<LimitXY> pagination dialect.
221  
222 =item *
223  
224 Routes SQL generation through L<DBIO::MySQL::SQLMaker>.
225  
226 =item *
227  
228 Routes SQL generation through L<DBIO::MySQL::SQLMaker>, which
229 automatically wraps C<UPDATE> and C<DELETE> statements that reference
230 the modification target in a subquery.
231  
232 =item *
233  
234 Disables C<mysql_auto_reconnect> by default to prevent silent transaction
235 loss.
236  
237 =back
238  
239 This class is auto-registered for the C<mysql> DBI driver and is set as the
240 active storage class when L<DBIO::MySQL/connection> is called.
241  
242 =head1 METHODS
243  
244 =head2 with_deferred_fk_checks
245  
246 $storage->with_deferred_fk_checks(sub { ... });
247  
248 Executes the given coderef with C<FOREIGN_KEY_CHECKS> disabled for the
249 duration of the call. Useful when loading data with circular foreign key
250 dependencies.
251  
252 =head2 connect_call_set_strict_mode
253  
254 my $schema = MyApp::Schema->connect(
255 $dsn, $user, $pass,
256 { on_connect_call => 'set_strict_mode' },
257 );
258  
259 Connection callback that enables C<ANSI>, C<TRADITIONAL>, and
260 C<ONLY_FULL_GROUP_BY> SQL modes and disables C<SQL_AUTO_IS_NULL>. Pass
261 C<set_strict_mode> as an C<on_connect_call> option to activate it.
262  
263 =head2 deploy_defaults
264  
265 MySQL has no transactional DDL and no C<IF NOT EXISTS> on all statement types.
266 Returns C<< add_drop_table => 1 >> so that L<DBIO::Test> can safely re-deploy
267 the same schema in a test sequence without hitting C<table already exists>
268 errors.
269  
270 =head2 deploy_setup
271  
272 Strips C<NO_ZERO_DATE> and C<NO_ZERO_IN_DATE> from the session C<sql_mode>
273 before deploying a schema during tests. MySQL 8 strict mode rejects
274 C<0000-00-00> which the test suite uses to verify
275 C<datetime_undef_if_invalid> behaviour.
276  
277 Delegates to L</connect_call_set_zero_date_permissive>. Note this only
278 relaxes the C<sql_mode> on the handle live at deploy time; when later
279 B<writes> (not just the deploy) must tolerate zero dates, wire
280 C<set_zero_date_permissive> through C<on_connect_call> instead so every
281 (re)connect inherits the relaxed mode.
282  
283 =head2 connect_call_set_zero_date_permissive
284  
285 my $schema = MyApp::Schema->connect(
286 $dsn, $user, $pass,
287 { on_connect_call => 'set_zero_date_permissive' },
288 );
289  
290 Strips C<NO_ZERO_DATE> and C<NO_ZERO_IN_DATE> from the session C<sql_mode>
291 so C<0000-00-00> can be written under MySQL 8 strict mode. Installed as an
292 C<on_connect_call> it re-runs on every (re)connect, so the session actually
293 issuing the writes -- not merely the connection alive at deploy time --
294 carries the relaxed mode. This is the seam L</deploy_setup> delegates to.
295  
296 =head2 deployment_statements
297  
298 Overrides the base implementation to automatically pass C<mysql_version> to
299 the SQL::Translator producer args when deploying a schema, so that generated
300 DDL is compatible with the connected server version.
301  
302 =head2 is_replicating
303  
304 Returns true if the connected MySQL replica is currently replicating from its
305 master (both IO and SQL threads running). Intended for use with
306 L<DBIO::Replicated::Storage>.
307  
308 =head2 _replication_status_row
309  
310 my $row = $storage->_replication_status_row;
311  
312 Returns the C<SHOW SLAVE STATUS> / C<SHOW REPLICA STATUS> row as a hashref,
313 or C<undef> when the connected server is not a replica. The default
314 implementation queries the legacy C<SHOW SLAVE STATUS> statement; the
315 MariaDB subclass tries the modern C<SHOW REPLICA STATUS> first and falls
316 back to the legacy statement.
317  
318 This is the engine seam: anything that needs to read replication state
319 should go through here instead of issuing C<SHOW ... STATUS> itself.
320  
321 =head2 lag_behind_master
322  
323 Returns the number of seconds the replica is behind the master, as reported
324 by C<SHOW SLAVE STATUS>. Returns C<undef> if replication status is
325 unavailable.
326  
327 =seealso
328  
329 =over 4
330  
331 =item * L<DBIO::MySQL> - Schema component that activates this storage
332  
333 =item * L<DBIO::MySQL::SQLMaker> - SQL generation used by this storage
334  
335 =item * L<DBIO::MySQL::Storage::MariaDB> - MariaDB-specific subclass
336  
337 =back
338  
339 =head1 AUTHOR
340  
341 DBIO & DBIx::Class Authors
342  
343 =head1 COPYRIGHT AND LICENSE
344  
345 Copyright (C) 2026 DBIO Authors
346 Portions Copyright (C) 2005-2025 DBIx::Class Authors
347 Based on DBIx::Class, heavily modified.
348  
349 This is free software; you can redistribute it and/or modify it under
350 the same terms as the Perl 5 programming language system itself.
351  
352 =cut
« MySQL/Introspect.pm ↑ Summary MySQL/Storage/MariaDB.pm »