DBIx/Class/Storage/DBI/mysql.pm

filter: Statementstmt 53.4% 31 / 58 Branchbran 56.2% 9 / 16 Conditioncond 17.6% 3 / 17 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 37.5% 6 / 16 Podpod 85.7% 6 / 7 totaltotal 44.0% 55 / 125 CC 15 SCAR 19.5
CC
15
Cov
47%
deployment_statements
20.0
_prep_for_execute
10.7
is_replicating
6.0
CRAP 7.1
« DBIx/Class/CDBICompat/GetSet.pm ↑ Summary DBIx/Class/CDBICompat/Copy.pm »
1 package DBIx::Class::Storage::DBI::mysql;
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 base qw/DBIx::Class::Storage::DBI/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 __PACKAGE__->sql_maker_class('DBIx::Class::SQLMaker::MySQL');
9 4 __PACKAGE__->sql_limit_dialect ('LimitXY');
10 4 __PACKAGE__->sql_quote_char ('`');
11  
12 4 __PACKAGE__->_use_multicolumn_in (1);
13  
14 0 ▶sub with_deferred_fk_checks {
Subroutine with_deferred_fk_checks: not called CC=1 SCAR=6.9
Pod documented
15 0 my ($self, $sub) = @_;
16  
17 0 $self->_do_query('SET FOREIGN_KEY_CHECKS = 0');
18 0 $sub->();
19 0 $self->_do_query('SET FOREIGN_KEY_CHECKS = 1');
20 }
21  
22 0 ▶sub connect_call_set_strict_mode {
Subroutine connect_call_set_strict_mode: not called CC=1 SCAR=6.9
Pod undocumented
23 0 my $self = shift;
24  
25 # the @@sql_mode puts back what was previously set on the session handle
26 0 $self->_do_query(q|SET SQL_MODE = CONCAT('ANSI,TRADITIONAL,ONLY_FULL_GROUP_BY,', @@sql_mode)|);
27 0 $self->_do_query(q|SET SQL_AUTO_IS_NULL = 0|);
28 }
29  
30 0 ▶sub _dbh_last_insert_id {
Subroutine _dbh_last_insert_id: not called CC=1 SCAR=6.9
31 0 my ($self, $dbh, $source, $col) = @_;
32 0 $dbh->{mysql_insertid};
33 }
34  
35 4 ▶sub _prep_for_execute {
Subroutine _prep_for_execute: called CC=8 SCAR=23.7
36 4 my $self = shift;
37 #(my $op, $ident, $args) = @_;
38  
39 # Only update and delete need special double-subquery treatment
40 # Insert referencing the same table (i.e. SELECT MAX(id) + 1) seems
41 # to work just fine on MySQL
42 4 ▶ return $self->next::method(@_) if ( $_[0] eq 'select' or $_[0] eq 'insert' );
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq "select" or $_[0] eq "insert" 0 4 4
Condition33%1/3
$_[0] eq "select" or $_[0] eq "insert"
l!l&&r!l&&!r
004
MC/DC0%0/2
$_[0] eq "select" or $_[0] eq "insert"
$_[0] eq "select" $_[0] eq "insert"
Truth table33%1/3
A or B
A= $_[0] eq "select"B= $_[0] eq "insert"
ABresult
1X1
011
000
43  
44  
45 # FIXME FIXME FIXME - this is a terrible, gross, incomplete, MySQL-specific
46 # hack but it works rather well for the limited amount of actual use cases
47 # which can not be done in any other way on MySQL. This allows us to fix
48 # some bugs without breaking MySQL support in the process and is also
49 # crucial for more complex things like Shadow to be usable
50 #
51 # This code is just a pre-analyzer, working in tandem with ::SQLMaker::MySQL,
52 # where the possibly-set value of {_modification_target_referenced_re} is
53 # used to demarcate which part of the final SQL to double-wrap in a subquery.
54 #
55 # This is covered extensively by "offline" tests, so that competing SQLMaker
56 # implementations could benefit from the existing tests just as well.
57  
58 # extract the source name, construct modification indicator re
59 4 my $sm = $self->sql_maker;
60  
61 4 my $target_name = $_[1]->from;
62  
63 4 ▶ if (ref $target_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $target_name) 2 2 4
64 2 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (ref $target_name eq 'SCALAR' and $$target_name =~ /^ (?: \` ( [^`]+ ) \` #` | ( [\w\-]+ ) ) $/x) { } 2 0 2
Condition33%1/3
ref $target_name eq "SCALAR" and $$target_name =~ /^ (?: \` ( [^`]+ ) \` #` | ( [\w\-]+ ) ) $/x
!ll&&!rl&&r
002
MC/DC0%0/2
ref $target_name eq "SCALAR" and $$target_name =~ /^ (?: \` ( [^`]+ ) \` #` | ( [\w\-]+ ) ) $/x
ref $target_name eq "SCALAR" $$target_name =~ /^ (?: \` ( [^`]+ ) \` #` | ( [\w\-]+ ) ) $/x
Truth table33%1/3
A and B
A= ref $target_name eq "SCALAR"B= $$target_name =~ /^ (?: \` ( [^`]+ ) \` #` | ( [\w\-]+ ) ) $/x
ABresult
0X0
100
111
65 ref $target_name eq 'SCALAR'
66 and
67 $$target_name =~ /^ (?:
68 \` ( [^`]+ ) \` #`
69 | ( [\w\-]+ )
70 ) $/x
71 ) {
72 # this is just a plain-ish name, which has been literal-ed for
73 # whatever reason
74 2 ▶ $target_name = (defined $1) ? $1 : $2;
Branch: 1 branch
BranchTrueFalseTotal
defined $1 ? : 0 2 2
75 }
76 else {
77 # this is something very complex, perhaps a custom result source or whatnot
78 # can't deal with it
79 0 undef $target_name;
80 }
81 }
82  
83 local $sm->{_modification_target_referenced_re} =
84 4 ▶ qr/ (?<!DELETE) [\s\)] (?: FROM | JOIN ) \s (?: \` \Q$target_name\E \` | \Q$target_name\E ) [\s\(] /xi
Branch: 1 branch
BranchTrueFalseTotal
if $target_name 4 0 4
85 if $target_name;
86  
87 4 $self->next::method(@_);
88 }
89  
90 # here may seem like an odd place to override, but this is the first
91 # method called after we are connected *and* the driver is determined
92 # ($self is reblessed). See code flow in ::Storage::DBI::_populate_dbh
93 3 ▶sub _run_connection_actions {
Subroutine _run_connection_actions: called CC=3 SCAR=13.3
94 3 my $self = shift;
95  
96 # default mysql_auto_reconnect to off unless explicitly set
97 3 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_dbh->{"mysql_auto_reconnect"} and not exists $self->_dbic_connect_attributes->{"mysql_auto_reconnect"}) 0 3 3
Condition33%1/3
$self->_dbh->{"mysql_auto_reconnect"} and not exists $self->_dbic_connect_attributes->{"mysql_auto_reconnect"}
!ll&&!rl&&r
300
MC/DC0%0/2
$self->_dbh->{"mysql_auto_reconnect"} and not exists $self->_dbic_connect_attributes->{"mysql_auto_reconnect"}
$self->_dbh->{"mysql_auto_reconnect"} not exists $self->_dbic_connect_attributes->{"mysql_auto_reconnect"}
Truth table33%1/3
A and not(B)
A= $self->_dbh->{"mysql_auto_reconnect"}B= not exists $self->_dbic_connect_attributes->{"mysql_auto_reconnect"}
ABresult
0X0
100
111
98 $self->_dbh->{mysql_auto_reconnect}
99 and
100 ! exists $self->_dbic_connect_attributes->{mysql_auto_reconnect}
101 ) {
102 0 $self->_dbh->{mysql_auto_reconnect} = 0;
103 }
104  
105 3 $self->next::method(@_);
106 }
107  
108 # we need to figure out what mysql version we're running
109 34 ▶sub sql_maker {
Subroutine sql_maker: called CC=2 SCAR=6.9
Pod documented
110 34 my $self = shift;
111  
112 # it is critical to get the version *before* calling next::method
113 # otherwise the potential connect will obliterate the sql_maker
114 # next::method will populate in the _sql_maker accessor
115 34 my $mysql_ver = $self->_server_info->{normalized_dbms_version};
116  
117 34 my $sm = $self->next::method(@_);
118  
119 # mysql 3 does not understand a bare JOIN
120 34 ▶ $sm->{_default_jointype} = 'INNER' if $mysql_ver < 4;
Branch: 1 branch
BranchTrueFalseTotal
if $mysql_ver < 4 4 30 34
121  
122 34 $sm;
123 }
124  
125 0 ▶sub sqlt_type {
Subroutine sqlt_type: not called CC=1 SCAR=6.9
Pod documented
126 0 return 'MySQL';
127 }
128  
129 0 ▶sub deployment_statements {
Subroutine deployment_statements: not called CC=4 SCAR=30.0
Pod documented
130 0 my $self = shift;
131 0 my ($schema, $type, $version, $dir, $sqltargs, @rest) = @_;
132  
133 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
134  
135 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
136 ! exists $sqltargs->{producer_args}{mysql_version}
137 and
138 my $dver = $self->_server_info->{normalized_dbms_version}
139 ) {
140 0 $sqltargs->{producer_args}{mysql_version} = $dver;
141 }
142  
143 0 $self->next::method($schema, $type, $version, $dir, $sqltargs, @rest);
144 }
145  
146 0 ▶sub _exec_svp_begin {
Subroutine _exec_svp_begin: not called CC=1 SCAR=6.9
147 0 my ($self, $name) = @_;
148  
149 0 $self->_dbh->do("SAVEPOINT $name");
150 }
151  
152 0 ▶sub _exec_svp_release {
Subroutine _exec_svp_release: not called CC=1 SCAR=6.9
153 0 my ($self, $name) = @_;
154  
155 0 $self->_dbh->do("RELEASE SAVEPOINT $name");
156 }
157  
158 0 ▶sub _exec_svp_rollback {
Subroutine _exec_svp_rollback: not called CC=1 SCAR=6.9
159 0 my ($self, $name) = @_;
160  
161 0 $self->_dbh->do("ROLLBACK TO SAVEPOINT $name")
162 }
163  
164 0 ▶sub is_replicating {
Subroutine is_replicating: not called CC=2 SCAR=17.9
Pod documented
165 0 my $status = shift->_get_dbh->selectrow_hashref('show slave status');
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 0 ▶sub lag_behind_master {
Subroutine lag_behind_master: not called CC=1 SCAR=6.9
Pod documented
170 0 return shift->_get_dbh->selectrow_hashref('show slave status')->{Seconds_Behind_Master};
171 }
172  
173 4 1;
174  
175 =head1 NAME
176  
177 DBIx::Class::Storage::DBI::mysql - Storage::DBI class implementing MySQL specifics
178  
179 =head1 SYNOPSIS
180  
181 Storage::DBI autodetects the underlying MySQL database, and re-blesses the
182 C<$storage> object into this class.
183  
184 my $schema = MyApp::Schema->connect( $dsn, $user, $pass, { on_connect_call => 'set_strict_mode' } );
185  
186 =head1 DESCRIPTION
187  
188 This class implements MySQL specific bits of L<DBIx::Class::Storage::DBI>,
189 like AutoIncrement column support and savepoints. Also it augments the
190 SQL maker to support the MySQL-specific C<STRAIGHT_JOIN> join type, which
191 you can use by specifying C<< join_type => 'straight' >> in the
192 L<relationship attributes|DBIx::Class::Relationship::Base/join_type>
193  
194  
195 It also provides a one-stop on-connect macro C<set_strict_mode> which sets
196 session variables such that MySQL behaves more predictably as far as the
197 SQL standard is concerned.
198  
199 =head1 STORAGE OPTIONS
200  
201 =head2 set_strict_mode
202  
203 Enables session-wide strict options upon connecting. Equivalent to:
204  
205 ->connect ( ... , {
206 on_connect_do => [
207 q|SET SQL_MODE = CONCAT('ANSI,TRADITIONAL,ONLY_FULL_GROUP_BY,', @@sql_mode)|,
208 q|SET SQL_AUTO_IS_NULL = 0|,
209 ]
210 });
211  
212 =head1 FURTHER QUESTIONS?
213  
214 Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>.
215  
216 =head1 COPYRIGHT AND LICENSE
217  
218 This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE>
219 by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can
220 redistribute it and/or modify it under the same terms as the
221 L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>.
« DBIx/Class/CDBICompat/GetSet.pm ↑ Summary DBIx/Class/CDBICompat/Copy.pm »