Fix self-referential resultset update/delete on MySQL (aggravated by 31073ac7)
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / mysql.pm
CommitLineData
843f8ecd 1package DBIx::Class::Storage::DBI::mysql;
2
3use strict;
4use warnings;
5
be64931c 6use base qw/DBIx::Class::Storage::DBI/;
843f8ecd 7
45150bc4 8use List::Util 'first';
9use namespace::clean;
10
d5dedbd6 11__PACKAGE__->sql_maker_class('DBIx::Class::SQLMaker::MySQL');
6a247f33 12__PACKAGE__->sql_limit_dialect ('LimitXY');
2b8cc2f2 13__PACKAGE__->sql_quote_char ('`');
843f8ecd 14
be64931c 15__PACKAGE__->_use_multicolumn_in (1);
16
e96a93df 17sub with_deferred_fk_checks {
18 my ($self, $sub) = @_;
19
5cf7285e 20 $self->_do_query('SET FOREIGN_KEY_CHECKS = 0');
e96a93df 21 $sub->();
5cf7285e 22 $self->_do_query('SET FOREIGN_KEY_CHECKS = 1');
e96a93df 23}
24
97a0a148 25sub connect_call_set_strict_mode {
0fde80d9 26 my $self = shift;
97a0a148 27
28 # the @@sql_mode puts back what was previously set on the session handle
29 $self->_do_query(q|SET SQL_MODE = CONCAT('ANSI,TRADITIONAL,ONLY_FULL_GROUP_BY,', @@sql_mode)|);
0fde80d9 30 $self->_do_query(q|SET SQL_AUTO_IS_NULL = 0|);
31}
32
d4f16b21 33sub _dbh_last_insert_id {
34 my ($self, $dbh, $source, $col) = @_;
35 $dbh->{mysql_insertid};
843f8ecd 36}
37
45150bc4 38sub _prep_for_execute {
39 my $self = shift;
40 #(my $op, $ident, $args) = @_;
41
42 # Only update and delete need special double-subquery treatment
43 # Insert referencing the same table (i.e. SELECT MAX(id) + 1) seems
44 # to work just fine on MySQL
45 return $self->next::method(@_) if ( $_[0] eq 'select' or $_[0] eq 'insert' );
46
47
48 # FIXME FIXME FIXME - this is a terrible, gross, incomplete hack
49 # it should be trivial for mst to port this to DQ (and a good
50 # exercise as well, since we do not yet have such wide tree walking
51 # in place). For the time being this will work in limited cases,
52 # mainly complex update/delete, which is really all we want it for
53 # currently (allows us to fix some bugs without breaking MySQL in
54 # the process, and is also crucial for Shadow to be usable)
55
56 # extract the source name, construct modification indicator re
57 my $sm = $self->sql_maker;
58
59 my $target_name = $_[1]->from;
60
61 if (ref $target_name) {
62 if (
63 ref $target_name eq 'SCALAR'
64 and
65 $$target_name =~ /^ (?:
66 \` ( [^`]+ ) \` #`
67 | ( [\w\-]+ )
68 ) $/x
69 ) {
70 # this is just a plain-ish name, which has been literal-ed for
71 # whatever reason
72 $target_name = first { defined $_ } ($1, $2);
73 }
74 else {
75 # this is something very complex, perhaps a custom result source or whatnot
76 # can't deal with it
77 undef $target_name;
78 }
79 }
80
81 local $sm->{_modification_target_referenced_re} =
82 qr/ (?<!DELETE) [\s\)] FROM \s (?: \` \Q$target_name\E \` | \Q$target_name\E ) [\s\(] /xi
83 if $target_name;
84
85 $self->next::method(@_);
86}
87
f98120e4 88# here may seem like an odd place to override, but this is the first
89# method called after we are connected *and* the driver is determined
90# ($self is reblessed). See code flow in ::Storage::DBI::_populate_dbh
91sub _run_connection_actions {
92 my $self = shift;
93
94 # default mysql_auto_reconnect to off unless explicitly set
95 if (
96 $self->_dbh->{mysql_auto_reconnect}
97 and
98 ! exists $self->_dbic_connect_attributes->{mysql_auto_reconnect}
99 ) {
100 $self->_dbh->{mysql_auto_reconnect} = 0;
101 }
102
103 $self->next::method(@_);
104}
105
d3944540 106# we need to figure out what mysql version we're running
107sub sql_maker {
108 my $self = shift;
109
110 unless ($self->_sql_maker) {
111 my $maker = $self->next::method (@_);
112
113 # mysql 3 does not understand a bare JOIN
af1f4f84 114 my $mysql_ver = $self->_dbh_get_info('SQL_DBMS_VER');
d3944540 115 $maker->{_default_jointype} = 'INNER' if $mysql_ver =~ /^3/;
116 }
117
118 return $self->_sql_maker;
119}
120
e8d293df 121sub sqlt_type {
122 return 'MySQL';
123}
124
96736321 125sub deployment_statements {
126 my $self = shift;
127 my ($schema, $type, $version, $dir, $sqltargs, @rest) = @_;
128
129 $sqltargs ||= {};
130
131 if (
132 ! exists $sqltargs->{producer_args}{mysql_version}
8273e845 133 and
96736321 134 my $dver = $self->_server_info->{normalized_dbms_version}
135 ) {
136 $sqltargs->{producer_args}{mysql_version} = $dver;
137 }
138
139 $self->next::method($schema, $type, $version, $dir, $sqltargs, @rest);
140}
141
90d7422f 142sub _exec_svp_begin {
eeb8cfeb 143 my ($self, $name) = @_;
adb3554a 144
90d7422f 145 $self->_dbh->do("SAVEPOINT $name");
adb3554a 146}
147
90d7422f 148sub _exec_svp_release {
eeb8cfeb 149 my ($self, $name) = @_;
adb3554a 150
90d7422f 151 $self->_dbh->do("RELEASE SAVEPOINT $name");
adb3554a 152}
153
90d7422f 154sub _exec_svp_rollback {
eeb8cfeb 155 my ($self, $name) = @_;
adb3554a 156
90d7422f 157 $self->_dbh->do("ROLLBACK TO SAVEPOINT $name")
adb3554a 158}
48fe9087 159
106d5f3b 160sub is_replicating {
9ae966b9 161 my $status = shift->_get_dbh->selectrow_hashref('show slave status');
f797e89e 162 return ($status->{Slave_IO_Running} eq 'Yes') && ($status->{Slave_SQL_Running} eq 'Yes');
106d5f3b 163}
164
165sub lag_behind_master {
9ae966b9 166 return shift->_get_dbh->selectrow_hashref('show slave status')->{Seconds_Behind_Master};
106d5f3b 167}
168
843f8ecd 1691;
170
75d07914 171=head1 NAME
843f8ecd 172
c9d438ff 173DBIx::Class::Storage::DBI::mysql - Storage::DBI class implementing MySQL specifics
843f8ecd 174
175=head1 SYNOPSIS
176
c9d438ff 177Storage::DBI autodetects the underlying MySQL database, and re-blesses the
178C<$storage> object into this class.
179
3ce9dd08 180 my $schema = MyDb::Schema->connect( $dsn, $user, $pass, { on_connect_call => 'set_strict_mode' } );
843f8ecd 181
182=head1 DESCRIPTION
183
b8391c87 184This class implements MySQL specific bits of L<DBIx::Class::Storage::DBI>,
185like AutoIncrement column support and savepoints. Also it augments the
186SQL maker to support the MySQL-specific C<STRAIGHT_JOIN> join type, which
187you can use by specifying C<< join_type => 'straight' >> in the
188L<relationship attributes|DBIx::Class::Relationship::Base/join_type>
189
c9d438ff 190
97a0a148 191It also provides a one-stop on-connect macro C<set_strict_mode> which sets
192session variables such that MySQL behaves more predictably as far as the
193SQL standard is concerned.
0fde80d9 194
3c01add8 195=head1 STORAGE OPTIONS
196
197=head2 set_strict_mode
198
199Enables session-wide strict options upon connecting. Equivalent to:
200
201 ->connect ( ... , {
202 on_connect_do => [
203 q|SET SQL_MODE = CONCAT('ANSI,TRADITIONAL,ONLY_FULL_GROUP_BY,', @@sql_mode)|,
204 q|SET SQL_AUTO_IS_NULL = 0|,
205 ]
206 });
207
843f8ecd 208=head1 AUTHORS
209
c9d438ff 210See L<DBIx::Class/CONTRIBUTORS>
843f8ecd 211
212=head1 LICENSE
213
214You may distribute this code under the same terms as Perl itself.
215
216=cut