Straight_join support RT55579
[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
9 /;
10 use mro 'c3';
11
12 __PACKAGE__->sql_maker_class('DBIx::Class::SQLAHacks::MySQL');
13
14 sub with_deferred_fk_checks {
15   my ($self, $sub) = @_;
16
17   $self->_do_query('SET FOREIGN_KEY_CHECKS = 0');
18   $sub->();
19   $self->_do_query('SET FOREIGN_KEY_CHECKS = 1');
20 }
21
22 sub connect_call_set_strict_mode {
23   my $self = shift;
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)|);
27   $self->_do_query(q|SET SQL_AUTO_IS_NULL = 0|);
28 }
29
30 sub _dbh_last_insert_id {
31   my ($self, $dbh, $source, $col) = @_;
32   $dbh->{mysql_insertid};
33 }
34
35 # we need to figure out what mysql version we're running
36 sub 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
50 sub sqlt_type {
51   return 'MySQL';
52 }
53
54 sub _svp_begin {
55     my ($self, $name) = @_;
56
57     $self->_get_dbh->do("SAVEPOINT $name");
58 }
59
60 sub _svp_release {
61     my ($self, $name) = @_;
62
63     $self->_get_dbh->do("RELEASE SAVEPOINT $name");
64 }
65
66 sub _svp_rollback {
67     my ($self, $name) = @_;
68
69     $self->_get_dbh->do("ROLLBACK TO SAVEPOINT $name")
70 }
71
72 sub is_replicating {
73     my $status = shift->_get_dbh->selectrow_hashref('show slave status');
74     return ($status->{Slave_IO_Running} eq 'Yes') && ($status->{Slave_SQL_Running} eq 'Yes');
75 }
76
77 sub lag_behind_master {
78     return shift->_get_dbh->selectrow_hashref('show slave status')->{Seconds_Behind_Master};
79 }
80
81 # MySql can not do subquery update/deletes, only way is slow per-row operations.
82 # This assumes you have set proper transaction isolation and use innodb.
83 sub _subq_update_delete {
84   return shift->_per_row_update_delete (@_);
85 }
86
87 1;
88
89 =head1 NAME
90
91 DBIx::Class::Storage::DBI::mysql - Storage::DBI class implementing MySQL specifics
92
93 =head1 SYNOPSIS
94
95 Storage::DBI autodetects the underlying MySQL database, and re-blesses the
96 C<$storage> object into this class.
97
98   my $schema = MyDb::Schema->connect( $dsn, $user, $pass, { on_connect_call => 'set_strict_mode' } );
99
100 =head1 DESCRIPTION
101
102 This class implements MySQL specific bits of L<DBIx::Class::Storage::DBI>,
103 like AutoIncrement column support and savepoints. Also it augments the
104 SQL maker to support the MySQL-specific C<STRAIGHT_JOIN> join type, which
105 you can use by specifying C<< join_type => 'straight' >> in the
106 L<relationship attributes|DBIx::Class::Relationship::Base/join_type>
107
108
109 It also provides a one-stop on-connect macro C<set_strict_mode> which sets
110 session variables such that MySQL behaves more predictably as far as the
111 SQL standard is concerned.
112
113 =head1 STORAGE OPTIONS
114
115 =head2 set_strict_mode
116
117 Enables session-wide strict options upon connecting. Equivalent to:
118
119   ->connect ( ... , {
120     on_connect_do => [
121       q|SET SQL_MODE = CONCAT('ANSI,TRADITIONAL,ONLY_FULL_GROUP_BY,', @@sql_mode)|,
122       q|SET SQL_AUTO_IS_NULL = 0|,
123     ]
124   });
125
126 =head1 AUTHORS
127
128 See L<DBIx::Class/CONTRIBUTORS>
129
130 =head1 LICENSE
131
132 You may distribute this code under the same terms as Perl itself.
133
134 =cut