Merge 'reduce_pings' into 'trunk'
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / mysql.pm
1 package DBIx::Class::Storage::DBI::mysql;
2
3 use strict;
4 use warnings;
5
6 use base qw/
7   DBIx::Class::Storage::DBI::MultiColumnIn
8   DBIx::Class::Storage::DBI::AmbiguousGlob
9   DBIx::Class::Storage::DBI
10 /;
11 use mro 'c3';
12
13 __PACKAGE__->sql_maker_class('DBIx::Class::SQLAHacks::MySQL');
14
15 sub with_deferred_fk_checks {
16   my ($self, $sub) = @_;
17
18   $self->_do_query('SET FOREIGN_KEY_CHECKS = 0');
19   $sub->();
20   $self->_do_query('SET FOREIGN_KEY_CHECKS = 1');
21 }
22
23 sub connect_call_set_strict_mode {
24   my $self = shift;
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)|);
28   $self->_do_query(q|SET SQL_AUTO_IS_NULL = 0|);
29 }
30
31 sub _dbh_last_insert_id {
32   my ($self, $dbh, $source, $col) = @_;
33   $dbh->{mysql_insertid};
34 }
35
36 sub sqlt_type {
37   return 'MySQL';
38 }
39
40 sub _svp_begin {
41     my ($self, $name) = @_;
42
43     $self->last_dbh->do("SAVEPOINT $name");
44 }
45
46 sub _svp_release {
47     my ($self, $name) = @_;
48
49     $self->last_dbh->do("RELEASE SAVEPOINT $name");
50 }
51
52 sub _svp_rollback {
53     my ($self, $name) = @_;
54
55     $self->last_dbh->do("ROLLBACK TO SAVEPOINT $name")
56 }
57
58 sub is_replicating {
59     my $status = shift->last_dbh->selectrow_hashref('show slave status');
60     return ($status->{Slave_IO_Running} eq 'Yes') && ($status->{Slave_SQL_Running} eq 'Yes');
61 }
62
63 sub lag_behind_master {
64     return shift->last_dbh->selectrow_hashref('show slave status')->{Seconds_Behind_Master};
65 }
66
67 # MySql can not do subquery update/deletes, only way is slow per-row operations.
68 # This assumes you have set proper transaction isolation and use innodb.
69 sub _subq_update_delete {
70   return shift->_per_row_update_delete (@_);
71 }
72
73 1;
74
75 =head1 NAME
76
77 DBIx::Class::Storage::DBI::mysql - Storage::DBI class implementing MySQL specifics
78
79 =head1 SYNOPSIS
80
81 Storage::DBI autodetects the underlying MySQL database, and re-blesses the
82 C<$storage> object into this class.
83
84   my $schema = MyDb::Schema->connect( $dsn, $user, $pass, { on_connect_call => 'set_strict_mode' } );
85
86 =head1 DESCRIPTION
87
88 This class implements MySQL specific bits of L<DBIx::Class::Storage::DBI>.
89
90 It also provides a one-stop on-connect macro C<set_strict_mode> which sets
91 session variables such that MySQL behaves more predictably as far as the
92 SQL standard is concerned.
93
94 =head1 AUTHORS
95
96 See L<DBIx::Class/CONTRIBUTORS>
97
98 =head1 LICENSE
99
100 You may distribute this code under the same terms as Perl itself.
101
102 =cut