Fix wrong author email from f92a9d79
[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
698775ed 6use base qw/
7 DBIx::Class::Storage::DBI::MultiColumnIn
698775ed 8 DBIx::Class::Storage::DBI
9/;
2ad62d97 10use mro 'c3';
843f8ecd 11
d5dedbd6 12__PACKAGE__->sql_maker_class('DBIx::Class::SQLMaker::MySQL');
6a247f33 13__PACKAGE__->sql_limit_dialect ('LimitXY');
843f8ecd 14
e96a93df 15sub with_deferred_fk_checks {
16 my ($self, $sub) = @_;
17
5cf7285e 18 $self->_do_query('SET FOREIGN_KEY_CHECKS = 0');
e96a93df 19 $sub->();
5cf7285e 20 $self->_do_query('SET FOREIGN_KEY_CHECKS = 1');
e96a93df 21}
22
97a0a148 23sub connect_call_set_strict_mode {
0fde80d9 24 my $self = shift;
97a0a148 25
26 # the @@sql_mode puts back what was previously set on the session handle
27 $self->_do_query(q|SET SQL_MODE = CONCAT('ANSI,TRADITIONAL,ONLY_FULL_GROUP_BY,', @@sql_mode)|);
0fde80d9 28 $self->_do_query(q|SET SQL_AUTO_IS_NULL = 0|);
29}
30
d4f16b21 31sub _dbh_last_insert_id {
32 my ($self, $dbh, $source, $col) = @_;
33 $dbh->{mysql_insertid};
843f8ecd 34}
35
f98120e4 36# here may seem like an odd place to override, but this is the first
37# method called after we are connected *and* the driver is determined
38# ($self is reblessed). See code flow in ::Storage::DBI::_populate_dbh
39sub _run_connection_actions {
40 my $self = shift;
41
42 # default mysql_auto_reconnect to off unless explicitly set
43 if (
44 $self->_dbh->{mysql_auto_reconnect}
45 and
46 ! exists $self->_dbic_connect_attributes->{mysql_auto_reconnect}
47 ) {
48 $self->_dbh->{mysql_auto_reconnect} = 0;
49 }
50
51 $self->next::method(@_);
52}
53
d3944540 54# we need to figure out what mysql version we're running
55sub sql_maker {
56 my $self = shift;
57
58 unless ($self->_sql_maker) {
59 my $maker = $self->next::method (@_);
60
61 # mysql 3 does not understand a bare JOIN
62 my $mysql_ver = $self->_get_dbh->get_info(18);
63 $maker->{_default_jointype} = 'INNER' if $mysql_ver =~ /^3/;
64 }
65
66 return $self->_sql_maker;
67}
68
e8d293df 69sub sqlt_type {
70 return 'MySQL';
71}
72
96736321 73sub deployment_statements {
74 my $self = shift;
75 my ($schema, $type, $version, $dir, $sqltargs, @rest) = @_;
76
77 $sqltargs ||= {};
78
79 if (
80 ! exists $sqltargs->{producer_args}{mysql_version}
81 and
82 my $dver = $self->_server_info->{normalized_dbms_version}
83 ) {
84 $sqltargs->{producer_args}{mysql_version} = $dver;
85 }
86
87 $self->next::method($schema, $type, $version, $dir, $sqltargs, @rest);
88}
89
adb3554a 90sub _svp_begin {
eeb8cfeb 91 my ($self, $name) = @_;
adb3554a 92
9ae966b9 93 $self->_get_dbh->do("SAVEPOINT $name");
adb3554a 94}
95
96sub _svp_release {
eeb8cfeb 97 my ($self, $name) = @_;
adb3554a 98
9ae966b9 99 $self->_get_dbh->do("RELEASE SAVEPOINT $name");
adb3554a 100}
101
102sub _svp_rollback {
eeb8cfeb 103 my ($self, $name) = @_;
adb3554a 104
9ae966b9 105 $self->_get_dbh->do("ROLLBACK TO SAVEPOINT $name")
adb3554a 106}
48fe9087 107
106d5f3b 108sub is_replicating {
9ae966b9 109 my $status = shift->_get_dbh->selectrow_hashref('show slave status');
f797e89e 110 return ($status->{Slave_IO_Running} eq 'Yes') && ($status->{Slave_SQL_Running} eq 'Yes');
106d5f3b 111}
112
113sub lag_behind_master {
9ae966b9 114 return shift->_get_dbh->selectrow_hashref('show slave status')->{Seconds_Behind_Master};
106d5f3b 115}
116
613f65e5 117# MySql can not do subquery update/deletes, only way is slow per-row operations.
ab807c12 118# This assumes you have set proper transaction isolation and use innodb.
b5963465 119sub _subq_update_delete {
613f65e5 120 return shift->_per_row_update_delete (@_);
121}
122
843f8ecd 1231;
124
75d07914 125=head1 NAME
843f8ecd 126
c9d438ff 127DBIx::Class::Storage::DBI::mysql - Storage::DBI class implementing MySQL specifics
843f8ecd 128
129=head1 SYNOPSIS
130
c9d438ff 131Storage::DBI autodetects the underlying MySQL database, and re-blesses the
132C<$storage> object into this class.
133
3ce9dd08 134 my $schema = MyDb::Schema->connect( $dsn, $user, $pass, { on_connect_call => 'set_strict_mode' } );
843f8ecd 135
136=head1 DESCRIPTION
137
b8391c87 138This class implements MySQL specific bits of L<DBIx::Class::Storage::DBI>,
139like AutoIncrement column support and savepoints. Also it augments the
140SQL maker to support the MySQL-specific C<STRAIGHT_JOIN> join type, which
141you can use by specifying C<< join_type => 'straight' >> in the
142L<relationship attributes|DBIx::Class::Relationship::Base/join_type>
143
c9d438ff 144
97a0a148 145It also provides a one-stop on-connect macro C<set_strict_mode> which sets
146session variables such that MySQL behaves more predictably as far as the
147SQL standard is concerned.
0fde80d9 148
3c01add8 149=head1 STORAGE OPTIONS
150
151=head2 set_strict_mode
152
153Enables session-wide strict options upon connecting. Equivalent to:
154
155 ->connect ( ... , {
156 on_connect_do => [
157 q|SET SQL_MODE = CONCAT('ANSI,TRADITIONAL,ONLY_FULL_GROUP_BY,', @@sql_mode)|,
158 q|SET SQL_AUTO_IS_NULL = 0|,
159 ]
160 });
161
843f8ecd 162=head1 AUTHORS
163
c9d438ff 164See L<DBIx::Class/CONTRIBUTORS>
843f8ecd 165
166=head1 LICENSE
167
168You may distribute this code under the same terms as Perl itself.
169
170=cut