blib/lib/DBIx/Class/Storage/DBI/MariaDB.pm

filter: Statementstmt 32.3% 21 / 65 Branchbran 11.1% 2 / 18 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 27.7% 5 / 18 Podpod 87.5% 7 / 8 totaltotal 25.5% 35 / 137 CC 16 SCAR 31.1
CC
16
Cov
23%
_prep_for_execute
72.0
deployment_statements
20.0
_run_connection_actions
12.0
CRAP 22.4
↑ Summary
1 package DBIx::Class::Storage::DBI::MariaDB;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use base qw/DBIx::Class::Storage::DBI/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 our $VERSION = '0.1.4';
10  
11 4 __PACKAGE__->sql_maker_class('DBIx::Class::SQLMaker::MySQL');
12 4 __PACKAGE__->sql_limit_dialect('LimitXY');
13 4 __PACKAGE__->sql_quote_char('`');
14  
15 4 __PACKAGE__->_use_multicolumn_in(1);
16  
17 0 ▶sub with_deferred_fk_checks {
Subroutine with_deferred_fk_checks: not called CC=1 SCAR=6.9
Pod documented
18 0 my ( $self, $sub ) = @_;
19  
20 0 $self->_do_query('SET FOREIGN_KEY_CHECKS = 0');
21 0 $sub->();
22 0 $self->_do_query('SET FOREIGN_KEY_CHECKS = 1');
23 }
24  
25 0 ▶sub connect_call_set_strict_mode {
Subroutine connect_call_set_strict_mode: not called CC=1 SCAR=6.9
Pod undocumented
26 0 my $self = shift;
27  
28 # the @@sql_mode puts back what was previously set on the session handle
29 0 $self->_do_query(
30 q|SET SQL_MODE = CONCAT('ANSI,TRADITIONAL,ONLY_FULL_GROUP_BY,', @@sql_mode)|
31 );
32 0 $self->_do_query(q|SET SQL_AUTO_IS_NULL = 0|);
33 }
34  
35 0 ▶sub _dbh_last_insert_id {
Subroutine _dbh_last_insert_id: not called CC=1 SCAR=6.9
36 0 my ( $self, $dbh, $source, $col ) = @_;
37 0 $dbh->{mariadb_insertid};
38 }
39  
40 0 ▶sub _prep_for_execute {
Subroutine _prep_for_execute: not called CC=8 SCAR=42.8
41 0 my $self = shift;
42  
43 # Only update and delete need special double-subquery treatment
44 # Insert referencing the same table (i.e. SELECT MAX(id) + 1) seems
45 # to work just fine on MariaDB
46 0 ▶ return $self->next::method(@_)
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq "select" or $_[0] eq "insert" 0 0 0
Condition0%0/3
$_[0] eq "select" or $_[0] eq "insert"
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[0] eq "select" or $_[0] eq "insert"
$_[0] eq "select" $_[0] eq "insert"
Truth table0%0/3
A or B
A= $_[0] eq "select"B= $_[0] eq "insert"
ABresult
1X1
011
000
47 if ( $_[0] eq 'select' or $_[0] eq 'insert' );
48  
49 # FIXME FIXME FIXME - this is a terrible, gross, incomplete, MariaDB-specific
50 # hack but it works rather well for the limited amount of actual use cases
51 # which can not be done in any other way on MariaDB. This allows us to fix
52 # some bugs without breaking MariaDB support in the process and is also
53 # crucial for more complex things like Shadow to be usable
54 #
55 # This code is just a pre-analyzer, working in tandem with ::SQLMaker::MySQL,
56 # where the possibly-set value of {_modification_target_referenced_re} is
57 # used to demarcate which part of the final SQL to double-wrap in a subquery.
58 #
59 # This is covered extensively by "offline" tests, so that competing SQLMaker
60 # implementations could benefit from the existing tests just as well.
61  
62 # extract the source name, construct modification indicator re
63 0 my $sm = $self->sql_maker;
64  
65 0 my $target_name = $_[1]->from;
66  
67 0 ▶ if ( ref $target_name ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $target_name) 0 0 0
68 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (ref $target_name eq 'SCALAR' and $$target_name =~ /^ (?: \` ( [^`]+ ) \` #` | ( [\w\-]+ ) ) $/x) { } 0 0 0
Condition0%0/3
ref $target_name eq "SCALAR" and $$target_name =~ /^ (?: \` ( [^`]+ ) \` #` | ( [\w\-]+ ) ) $/x
!ll&&!rl&&r
000
MC/DC0%0/2
ref $target_name eq "SCALAR" and $$target_name =~ /^ (?: \` ( [^`]+ ) \` #` | ( [\w\-]+ ) ) $/x
ref $target_name eq "SCALAR" $$target_name =~ /^ (?: \` ( [^`]+ ) \` #` | ( [\w\-]+ ) ) $/x
Truth table0%0/3
A and B
A= ref $target_name eq "SCALAR"B= $$target_name =~ /^ (?: \` ( [^`]+ ) \` #` | ( [\w\-]+ ) ) $/x
ABresult
0X0
100
111
69 ref $target_name eq 'SCALAR'
70 and $$target_name =~ /^ (?:
71 \` ( [^`]+ ) \` #`
72 | ( [\w\-]+ )
73 ) $/x
74 )
75 {
76 # this is just a plain-ish name, which has been literal-ed for
77 # whatever reason
78 0 ▶ $target_name = ( defined $1 ) ? $1 : $2;
Branch: 1 branch
BranchTrueFalseTotal
defined $1 ? : 0 0 0
79 } else {
80  
81 # this is something very complex, perhaps a custom result source or whatnot
82 # can't deal with it
83 0 undef $target_name;
84 }
85 }
86  
87 local $sm->{_modification_target_referenced_re} =
88 0 ▶qr/ (?<!DELETE) [\s\)] (?: FROM | JOIN ) \s (?: \` \Q$target_name\E \` | \Q$target_name\E ) [\s\(] /xi
Branch: 1 branch
BranchTrueFalseTotal
if $target_name 0 0 0
89 if $target_name;
90  
91 0 $self->next::method(@_);
92 }
93  
94 # here may seem like an odd place to override, but this is the first
95 # method called after we are connected *and* the driver is determined
96 # ($self is reblessed). See code flow in ::Storage::DBI::_populate_dbh
97 0 ▶sub _run_connection_actions {
Subroutine _run_connection_actions: not called CC=3 SCAR=24.8
98 0 my $self = shift;
99  
100 # default mariadb_auto_reconnect to off unless explicitly set
101 0 ▶ if ( $self->_dbh->{mariadb_auto_reconnect}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_dbh->{"mariadb_auto_reconnect"} and not exists $self->_dbic_connect_attributes->{"mariadb_auto_reconnect"}) 0 0 0
Condition0%0/3
$self->_dbh->{"mariadb_auto_reconnect"} and not exists $self->_dbic_connect_attributes->{"mariadb_auto_reconnect"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->_dbh->{"mariadb_auto_reconnect"} and not exists $self->_dbic_connect_attributes->{"mariadb_auto_reconnect"}
$self->_dbh->{"mariadb_auto_reconnect"} not exists $self->_dbic_connect_attributes->{"mariadb_auto_reconnect"}
Truth table0%0/3
A and not(B)
A= $self->_dbh->{"mariadb_auto_reconnect"}B= not exists $self->_dbic_connect_attributes->{"mariadb_auto_reconnect"}
ABresult
0X0
100
111
102 and !exists $self->_dbic_connect_attributes->{mariadb_auto_reconnect} )
103 {
104 0 $self->_dbh->{mariadb_auto_reconnect} = 0;
105 }
106  
107 0 $self->next::method(@_);
108 }
109  
110 # we need to figure out what mysql version we're running
111 0 ▶sub sql_maker {
Subroutine sql_maker: not called CC=2 SCAR=17.9
Pod documented
112 0 my $self = shift;
113  
114 # it is critical to get the version *before* calling next::method
115 # otherwise the potential connect will obliterate the sql_maker
116 # next::method will populate in the _sql_maker accessor
117 0 my $mariadb_ver = $self->_server_info->{normalized_dbms_version};
118  
119 0 my $sm = $self->next::method(@_);
120  
121 # mysql 3 does not understand a bare JOIN
122 0 ▶ $sm->{_default_jointype} = 'INNER' if $mariadb_ver < 4;
Branch: 1 branch
BranchTrueFalseTotal
if $mariadb_ver < 4 0 0 0
123  
124 0 $sm;
125 }
126  
127 0 ▶sub sqlt_type {
Subroutine sqlt_type: not called CC=1 SCAR=6.9
Pod documented
128 # used by SQL::Translator
129 0 return 'MySQL';
130 }
131  
132 0 ▶sub deployment_statements {
Subroutine deployment_statements: not called CC=4 SCAR=30.0
Pod documented
133 0 my $self = shift;
134 0 my ( $schema, $type, $version, $dir, $sqltargs, @rest ) = @_;
135  
136 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
137  
138 0 ▶ if ( !exists $sqltargs->{producer_args}{mysql_version}
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
139 and my $dver = $self->_server_info->{normalized_dbms_version} )
140 {
141 0 $sqltargs->{producer_args}{mysql_version} = $dver;
142 }
143  
144 0 $self->next::method( $schema, $type, $version, $dir, $sqltargs, @rest );
145 }
146  
147 0 ▶sub _exec_svp_begin {
Subroutine _exec_svp_begin: not called CC=1 SCAR=6.9
148 0 my ( $self, $name ) = @_;
149  
150 0 $self->_dbh->do("SAVEPOINT $name");
151 }
152  
153 0 ▶sub _exec_svp_release {
Subroutine _exec_svp_release: not called CC=1 SCAR=6.9
154 0 my ( $self, $name ) = @_;
155  
156 0 $self->_dbh->do("RELEASE SAVEPOINT $name");
157 }
158  
159 0 ▶sub _exec_svp_rollback {
Subroutine _exec_svp_rollback: not called CC=1 SCAR=6.9
160 0 my ( $self, $name ) = @_;
161  
162 0 $self->_dbh->do("ROLLBACK TO SAVEPOINT $name");
163 }
164  
165 0 ▶sub is_replicating {
Subroutine is_replicating: not called CC=2 SCAR=17.9
Pod documented
166 0 my $status = shift->_get_dbh->selectrow_hashref('show slave status');
167 return ( $status->{Slave_IO_Running} eq 'Yes' )
168 0 ▶ && ( $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
169 }
170  
171 0 ▶sub lag_behind_master {
Subroutine lag_behind_master: not called CC=1 SCAR=6.9
Pod documented
172 return
173 shift->_get_dbh->selectrow_hashref('show slave status')
174 0 ->{Seconds_Behind_Master};
175 }
176  
177 20 ▶sub bind_attribute_by_data_type {
Subroutine bind_attribute_by_data_type: called CC=2 SCAR=6.9
Pod documented
178 20 ▶ if ( $_[1] =~ /^(?:(?:tiny|medium|long|)blob|(?:var|)binary)$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[1] =~ /^(?:(?:tiny|medium|long|)blob|(?:var|)binary)$/i) 12 8 20
179 12 return DBI::SQL_BINARY;
180 }
181 8 return;
182 }
183  
184 4 1;
185  
186 =head1 NAME
187  
188 DBIx::Class::Storage::DBI::MariaDB - Storage::DBI class implementing MariaDB specifics
189  
190 =head1 DESCRIPTION
191  
192 This module adds support for MariaDB in the DBIx::Class ORM. It supports
193 exactly the same parameters as the L<DBIx::Class::Storage::DBI::mysql>
194 module, so check that for further documentation.
195  
196 =head1 USAGE
197  
198 Similar to other storage modules that are builtin to DBIx::Class, all you need
199 to do is ensure DBIx::Class::Storage::DBI::MariaDB is loaded and specify
200 MariaDB in the DSN. For example:
201  
202 package MyApp::Schema;
203 use base 'DBIx::Class::Schema';
204  
205 # register classes
206 # ...
207 # load mariadb storage
208 __PACKAGE__->ensure_class_loaded('DBIx::Class::Storage::DBI::MariaDB');
209  
210 package MyApp;
211 use MyApp::Schema;
212  
213 my $dsn = "dbi:MariaDB:database=mydb";
214 my $user = "noone";
215 my $pass = "topsecret";
216 my $schema = MyApp::Schema->connect($dsn, $user, $pass);
217  
218 =head1 COPYRIGHT AND LICENSE
219  
220 Copyright (C) 2023 Siemplexus
221  
222 This library is free software; you can redistribute it and/or modify
223 it under the same terms as Perl itself.
224  
225 =head1 AUTHORS
226  
227 Antonis Kalou E<lt>a.kalou@shadowcat.co.ukE<gt>
228 Jess Robinson E<lt>j.robinson@shadowcat.co.ukE<gt>
↑ Summary