76637358a49a891ede9ce71a49f4308d6f1fc6cb
[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 use DBI ':sql_types';
12 use namespace::clean;
13
14 __PACKAGE__->sql_maker_class('DBIx::Class::SQLAHacks::MySQL');
15
16 __PACKAGE__->mk_group_accessors(simple => qw/_bit_as/);
17
18 sub with_deferred_fk_checks {
19   my ($self, $sub) = @_;
20
21   $self->_do_query('SET FOREIGN_KEY_CHECKS = 0');
22   $sub->();
23   $self->_do_query('SET FOREIGN_KEY_CHECKS = 1');
24 }
25
26 sub connect_call_set_strict_mode {
27   my $self = shift;
28
29   # the @@sql_mode puts back what was previously set on the session handle
30   $self->_do_query(q|SET SQL_MODE = CONCAT('ANSI,TRADITIONAL,ONLY_FULL_GROUP_BY,', @@sql_mode)|);
31   $self->_do_query(q|SET SQL_AUTO_IS_NULL = 0|);
32 }
33
34 sub _dbh_last_insert_id {
35   my ($self, $dbh, $source, $col) = @_;
36   $dbh->{mysql_insertid};
37 }
38
39 # we need to figure out what mysql version we're running
40 sub sql_maker {
41   my $self = shift;
42
43   unless ($self->_sql_maker) {
44     my $maker = $self->next::method (@_);
45
46     # mysql 3 does not understand a bare JOIN
47     my $mysql_ver = $self->_get_dbh->get_info(18);
48     $maker->{_default_jointype} = 'INNER' if $mysql_ver =~ /^3/;
49   }
50
51   return $self->_sql_maker;
52 }
53
54 sub sqlt_type {
55   return 'MySQL';
56 }
57
58 sub deployment_statements {
59   my $self = shift;
60   my ($schema, $type, $version, $dir, $sqltargs, @rest) = @_;
61
62   $sqltargs ||= {};
63
64   if (
65     ! exists $sqltargs->{producer_args}{mysql_version}
66       and 
67     my $dver = $self->_server_info->{normalized_dbms_version}
68   ) {
69     $sqltargs->{producer_args}{mysql_version} = $dver;
70   }
71
72   $self->next::method($schema, $type, $version, $dir, $sqltargs, @rest);
73 }
74
75 sub _svp_begin {
76     my ($self, $name) = @_;
77
78     $self->_get_dbh->do("SAVEPOINT $name");
79 }
80
81 sub _svp_release {
82     my ($self, $name) = @_;
83
84     $self->_get_dbh->do("RELEASE SAVEPOINT $name");
85 }
86
87 sub _svp_rollback {
88     my ($self, $name) = @_;
89
90     $self->_get_dbh->do("ROLLBACK TO SAVEPOINT $name")
91 }
92
93 sub is_replicating {
94     my $status = shift->_get_dbh->selectrow_hashref('show slave status');
95     return ($status->{Slave_IO_Running} eq 'Yes') && ($status->{Slave_SQL_Running} eq 'Yes');
96 }
97
98 sub lag_behind_master {
99     return shift->_get_dbh->selectrow_hashref('show slave status')->{Seconds_Behind_Master};
100 }
101
102 # MySql can not do subquery update/deletes, only way is slow per-row operations.
103 # This assumes you have set proper transaction isolation and use innodb.
104 sub _subq_update_delete {
105   return shift->_per_row_update_delete (@_);
106 }
107
108 # handle bit fields properly
109
110 =head2 connect_call_bit_as_unsigned
111
112 Used as:
113
114   on_connect_call => 'bit_as_unsigned'
115
116 in your L<connect_info|DBIx::Class::Storage::DBI/connect_info>.
117
118 Beginning in MySQL 5.0.3 C<BIT> columns are stored as binary data, where before
119 they were an alias for C<TINYINT>.
120
121 See L<http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html> for details.
122
123 This option allows you to use C<BIT> columns as C<UNSIGNED> integers in all
124 versions of MySQL.
125
126 B<NOTE:> do not insert negative values when using this option, they will not be
127 inserted correctly.
128
129 =cut
130
131 sub connect_call_bit_as_unsigned {
132   my $self = shift;
133
134   $self->_bit_as('UNSIGNED');
135 }
136
137 sub bind_attribute_by_data_type {
138   my $self        = shift;
139   my ($data_type) = @_;
140
141   my $res = $self->next::method(@_) || {};
142
143   if ($data_type && $self->_bit_as && lc($data_type) eq 'bit') {
144     $res->{TYPE} = SQL_INTEGER;
145   }
146
147   return $res;
148 }
149
150 sub _select_args {
151   my $self             = shift;
152   my ($ident, $select) = @_;
153
154   return $self->next::method(@_) unless $self->_bit_as;
155
156   my $col_info = $self->_resolve_column_info($ident);
157
158   for my $select_idx (0..$#$select) {
159     my $selected = $select->[$select_idx];
160
161     next if ref $selected;
162
163     my $data_type = $col_info->{$selected}{data_type};
164
165     if ($data_type && lc($data_type) eq 'bit') {
166       $selected = $self->sql_maker->_quote($selected);
167
168       $select->[$select_idx] = \("CAST($selected AS " . $self->_bit_as . ")");
169     }
170   }
171
172   return $self->next::method(@_);
173 }
174
175 1;
176
177 =head1 NAME
178
179 DBIx::Class::Storage::DBI::mysql - Storage::DBI class implementing MySQL specifics
180
181 =head1 SYNOPSIS
182
183 Storage::DBI autodetects the underlying MySQL database, and re-blesses the
184 C<$storage> object into this class.
185
186   my $schema = MyDb::Schema->connect( $dsn, $user, $pass, { on_connect_call => 'set_strict_mode' } );
187
188 =head1 DESCRIPTION
189
190 This class implements MySQL specific bits of L<DBIx::Class::Storage::DBI>,
191 like AutoIncrement column support and savepoints. Also it augments the
192 SQL maker to support the MySQL-specific C<STRAIGHT_JOIN> join type, which
193 you can use by specifying C<< join_type => 'straight' >> in the
194 L<relationship attributes|DBIx::Class::Relationship::Base/join_type>
195
196
197 It also provides a one-stop on-connect macro C<set_strict_mode> which sets
198 session variables such that MySQL behaves more predictably as far as the
199 SQL standard is concerned.
200
201 =head1 STORAGE OPTIONS
202
203 =head2 set_strict_mode
204
205 Enables session-wide strict options upon connecting. Equivalent to:
206
207   ->connect ( ... , {
208     on_connect_do => [
209       q|SET SQL_MODE = CONCAT('ANSI,TRADITIONAL,ONLY_FULL_GROUP_BY,', @@sql_mode)|,
210       q|SET SQL_AUTO_IS_NULL = 0|,
211     ]
212   });
213
214 =head1 AUTHORS
215
216 See L<DBIx::Class/CONTRIBUTORS>
217
218 =head1 LICENSE
219
220 You may distribute this code under the same terms as Perl itself.
221
222 =cut