Added looper around to make sure that we don't overflow buffers.
[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::SQLMaker::MySQL');
13 __PACKAGE__->sql_limit_dialect ('LimitXY');
14 __PACKAGE__->sql_quote_char ('`');
15
16 sub with_deferred_fk_checks {
17   my ($self, $sub) = @_;
18
19   $self->_do_query('SET FOREIGN_KEY_CHECKS = 0');
20   $sub->();
21   $self->_do_query('SET FOREIGN_KEY_CHECKS = 1');
22 }
23
24 sub connect_call_set_strict_mode {
25   my $self = shift;
26
27   # the @@sql_mode puts back what was previously set on the session handle
28   $self->_do_query(q|SET SQL_MODE = CONCAT('ANSI,TRADITIONAL,ONLY_FULL_GROUP_BY,', @@sql_mode)|);
29   $self->_do_query(q|SET SQL_AUTO_IS_NULL = 0|);
30 }
31
32 sub _dbh_last_insert_id {
33   my ($self, $dbh, $source, $col) = @_;
34   $dbh->{mysql_insertid};
35 }
36
37 # here may seem like an odd place to override, but this is the first
38 # method called after we are connected *and* the driver is determined
39 # ($self is reblessed). See code flow in ::Storage::DBI::_populate_dbh
40 sub _run_connection_actions {
41   my $self = shift;
42
43   # default mysql_auto_reconnect to off unless explicitly set
44   if (
45     $self->_dbh->{mysql_auto_reconnect}
46       and
47     ! exists $self->_dbic_connect_attributes->{mysql_auto_reconnect}
48   ) {
49     $self->_dbh->{mysql_auto_reconnect} = 0;
50   }
51
52   $self->next::method(@_);
53 }
54
55 # we need to figure out what mysql version we're running
56 sub sql_maker {
57   my $self = shift;
58
59   unless ($self->_sql_maker) {
60     my $maker = $self->next::method (@_);
61
62     # mysql 3 does not understand a bare JOIN
63     my $mysql_ver = $self->_dbh_get_info(18);
64     $maker->{_default_jointype} = 'INNER' if $mysql_ver =~ /^3/;
65   }
66
67   return $self->_sql_maker;
68 }
69
70 sub sqlt_type {
71   return 'MySQL';
72 }
73
74 sub deployment_statements {
75   my $self = shift;
76   my ($schema, $type, $version, $dir, $sqltargs, @rest) = @_;
77
78   $sqltargs ||= {};
79
80   if (
81     ! exists $sqltargs->{producer_args}{mysql_version}
82       and 
83     my $dver = $self->_server_info->{normalized_dbms_version}
84   ) {
85     $sqltargs->{producer_args}{mysql_version} = $dver;
86   }
87
88   $self->next::method($schema, $type, $version, $dir, $sqltargs, @rest);
89 }
90
91 sub _svp_begin {
92     my ($self, $name) = @_;
93
94     $self->_get_dbh->do("SAVEPOINT $name");
95 }
96
97 sub _svp_release {
98     my ($self, $name) = @_;
99
100     $self->_get_dbh->do("RELEASE SAVEPOINT $name");
101 }
102
103 sub _svp_rollback {
104     my ($self, $name) = @_;
105
106     $self->_get_dbh->do("ROLLBACK TO SAVEPOINT $name")
107 }
108
109 sub is_replicating {
110     my $status = shift->_get_dbh->selectrow_hashref('show slave status');
111     return ($status->{Slave_IO_Running} eq 'Yes') && ($status->{Slave_SQL_Running} eq 'Yes');
112 }
113
114 sub lag_behind_master {
115     return shift->_get_dbh->selectrow_hashref('show slave status')->{Seconds_Behind_Master};
116 }
117
118 # MySql can not do subquery update/deletes, only way is slow per-row operations.
119 # This assumes you have set proper transaction isolation and use innodb.
120 sub _subq_update_delete {
121   return shift->_per_row_update_delete (@_);
122 }
123
124 my $INSERT_BULK_SIZE = 80;
125 sub _insert_bulk {
126   my ($self, $source, $cols, $colvalues, $data) = @_;
127
128   my $bind_attrs = $self->source_bind_attributes($source);
129
130   # Organize this way to make context sensitivity easier to code up.
131   while ( @$data > $INSERT_BULK_SIZE ) {
132     my @this_data = splice @$data, 0, $INSERT_BULK_SIZE;
133     $self->_execute(
134       'insert_bulk' => [], $source, $bind_attrs, \@this_data, $cols,
135     );
136   }
137
138   # Don't put this in the while-loop above.
139   return $self->_execute(
140     'insert_bulk' => [], $source, $bind_attrs, $data, $cols,
141   );
142 }
143
144 1;
145
146 =head1 NAME
147
148 DBIx::Class::Storage::DBI::mysql - Storage::DBI class implementing MySQL specifics
149
150 =head1 SYNOPSIS
151
152 Storage::DBI autodetects the underlying MySQL database, and re-blesses the
153 C<$storage> object into this class.
154
155   my $schema = MyDb::Schema->connect( $dsn, $user, $pass, { on_connect_call => 'set_strict_mode' } );
156
157 =head1 DESCRIPTION
158
159 This class implements MySQL specific bits of L<DBIx::Class::Storage::DBI>,
160 like AutoIncrement column support and savepoints. Also it augments the
161 SQL maker to support the MySQL-specific C<STRAIGHT_JOIN> join type, which
162 you can use by specifying C<< join_type => 'straight' >> in the
163 L<relationship attributes|DBIx::Class::Relationship::Base/join_type>
164
165
166 It also provides a one-stop on-connect macro C<set_strict_mode> which sets
167 session variables such that MySQL behaves more predictably as far as the
168 SQL standard is concerned.
169
170 =head1 STORAGE OPTIONS
171
172 =head2 set_strict_mode
173
174 Enables session-wide strict options upon connecting. Equivalent to:
175
176   ->connect ( ... , {
177     on_connect_do => [
178       q|SET SQL_MODE = CONCAT('ANSI,TRADITIONAL,ONLY_FULL_GROUP_BY,', @@sql_mode)|,
179       q|SET SQL_AUTO_IS_NULL = 0|,
180     ]
181   });
182
183 =head1 AUTHORS
184
185 See L<DBIx::Class/CONTRIBUTORS>
186
187 =head1 LICENSE
188
189 You may distribute this code under the same terms as Perl itself.
190
191 =cut