Fixed deadlock test
[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/DBIx::Class::Storage::DBI::MultiColumnIn/;
7
8 __PACKAGE__->sql_maker_class('DBIx::Class::SQLAHacks::MySQL');
9
10 sub with_deferred_fk_checks {
11   my ($self, $sub) = @_;
12
13   $self->dbh->do('SET foreign_key_checks=0');
14   $sub->();
15   $self->dbh->do('SET foreign_key_checks=1');
16 }
17
18 sub connect_call_set_ansi_mode {
19   my $self = shift;
20   $self->dbh->do(q|SET sql_mode = 'ANSI,TRADITIONAL'|);
21   $self->dbh->do(q|SET sql_mode = 'ANSI,TRADITIONAL'|);
22 }
23
24 sub _dbh_last_insert_id {
25   my ($self, $dbh, $source, $col) = @_;
26   $dbh->{mysql_insertid};
27 }
28
29 sub sqlt_type {
30   return 'MySQL';
31 }
32
33 sub _svp_begin {
34     my ($self, $name) = @_;
35
36     $self->dbh->do("SAVEPOINT $name");
37 }
38
39 sub _svp_release {
40     my ($self, $name) = @_;
41
42     $self->dbh->do("RELEASE SAVEPOINT $name");
43 }
44
45 sub _svp_rollback {
46     my ($self, $name) = @_;
47
48     $self->dbh->do("ROLLBACK TO SAVEPOINT $name")
49 }
50  
51 sub is_replicating {
52     my $status = shift->dbh->selectrow_hashref('show slave status');
53     return ($status->{Slave_IO_Running} eq 'Yes') && ($status->{Slave_SQL_Running} eq 'Yes');
54 }
55
56 sub lag_behind_master {
57     return shift->dbh->selectrow_hashref('show slave status')->{Seconds_Behind_Master};
58 }
59
60 # MySql can not do subquery update/deletes, only way is slow per-row operations.
61 # This assumes you have set proper transaction isolation and use innodb.
62 sub _subq_update_delete {
63   return shift->_per_row_update_delete (@_);
64 }
65
66 # MySql chokes on things like:
67 # COUNT(*) FROM (SELECT tab1.col, tab2.col FROM tab1 JOIN tab2 ... )
68 # claiming that col is a duplicate column (it loses the table specifiers by
69 # the time it gets to the *). Thus for any subquery count we select only the
70 # primary keys of the main table in the inner query. This hopefully still
71 # hits the indexes and keeps mysql happy.
72 # (mysql does not care if the SELECT and the GROUP BY match)
73 sub _subq_count_select {
74   my ($self, $source, $rs_attrs) = @_;
75   my @pcols = map { join '.', $rs_attrs->{alias}, $_ } ($source->primary_columns);
76   return @pcols ? \@pcols : [ 1 ];
77 }
78
79 1;
80
81 =head1 NAME
82
83 DBIx::Class::Storage::DBI::mysql - Automatic primary key class for MySQL
84
85 =head1 SYNOPSIS
86
87   # In your table classes
88   __PACKAGE__->load_components(qw/PK::Auto Core/);
89   __PACKAGE__->set_primary_key('id');
90
91 =head1 DESCRIPTION
92
93 This class implements autoincrements for MySQL.
94
95 =head1 AUTHORS
96
97 Matt S. Trout <mst@shadowcatsystems.co.uk>
98
99 =head1 LICENSE
100
101 You may distribute this code under the same terms as Perl itself.
102
103 =cut