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