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