1 package DBIx::Class::Storage::DBI::mysql;
7 DBIx::Class::Storage::DBI::MultiColumnIn
8 DBIx::Class::Storage::DBI
14 __PACKAGE__->sql_maker_class('DBIx::Class::SQLAHacks::MySQL');
16 __PACKAGE__->mk_group_accessors(simple => qw/_bit_as/);
18 sub with_deferred_fk_checks {
19 my ($self, $sub) = @_;
21 $self->_do_query('SET FOREIGN_KEY_CHECKS = 0');
23 $self->_do_query('SET FOREIGN_KEY_CHECKS = 1');
26 sub connect_call_set_strict_mode {
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|);
34 sub _dbh_last_insert_id {
35 my ($self, $dbh, $source, $col) = @_;
36 $dbh->{mysql_insertid};
39 # we need to figure out what mysql version we're running
43 unless ($self->_sql_maker) {
44 my $maker = $self->next::method (@_);
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/;
51 return $self->_sql_maker;
58 sub deployment_statements {
60 my ($schema, $type, $version, $dir, $sqltargs, @rest) = @_;
65 ! exists $sqltargs->{producer_args}{mysql_version}
67 my $dver = $self->_server_info->{normalized_dbms_version}
69 $sqltargs->{producer_args}{mysql_version} = $dver;
72 $self->next::method($schema, $type, $version, $dir, $sqltargs, @rest);
76 my ($self, $name) = @_;
78 $self->_get_dbh->do("SAVEPOINT $name");
82 my ($self, $name) = @_;
84 $self->_get_dbh->do("RELEASE SAVEPOINT $name");
88 my ($self, $name) = @_;
90 $self->_get_dbh->do("ROLLBACK TO SAVEPOINT $name")
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');
98 sub lag_behind_master {
99 return shift->_get_dbh->selectrow_hashref('show slave status')->{Seconds_Behind_Master};
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 (@_);
108 # handle bit fields properly
110 =head2 connect_call_bit_as_unsigned
114 on_connect_call => 'bit_as_unsigned'
116 in your L<connect_info|DBIx::Class::Storage::DBI/connect_info>.
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>.
121 See L<http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html> for details.
123 This option allows you to use C<BIT> columns as C<UNSIGNED> integers in all
126 B<NOTE:> do not insert negative values when using this option, they will not be
131 sub connect_call_bit_as_unsigned {
134 $self->_bit_as('UNSIGNED');
137 sub bind_attribute_by_data_type {
139 my ($data_type) = @_;
141 my $res = $self->next::method(@_) || {};
143 if ($data_type && $self->_bit_as && lc($data_type) eq 'bit') {
144 $res->{TYPE} = SQL_INTEGER;
152 my ($ident, $select) = @_;
154 return $self->next::method(@_) unless $self->_bit_as;
156 my $col_info = $self->_resolve_column_info($ident);
158 for my $select_idx (0..$#$select) {
159 my $selected = $select->[$select_idx];
161 next if ref $selected;
163 my $data_type = $col_info->{$selected}{data_type};
165 if ($data_type && lc($data_type) eq 'bit') {
166 $selected = $self->sql_maker->_quote($selected);
168 $select->[$select_idx] = \("CAST($selected AS " . $self->_bit_as . ")");
172 return $self->next::method(@_);
179 DBIx::Class::Storage::DBI::mysql - Storage::DBI class implementing MySQL specifics
183 Storage::DBI autodetects the underlying MySQL database, and re-blesses the
184 C<$storage> object into this class.
186 my $schema = MyDb::Schema->connect( $dsn, $user, $pass, { on_connect_call => 'set_strict_mode' } );
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>
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.
201 =head1 STORAGE OPTIONS
203 =head2 set_strict_mode
205 Enables session-wide strict options upon connecting. Equivalent to:
209 q|SET SQL_MODE = CONCAT('ANSI,TRADITIONAL,ONLY_FULL_GROUP_BY,', @@sql_mode)|,
210 q|SET SQL_AUTO_IS_NULL = 0|,
216 See L<DBIx::Class/CONTRIBUTORS>
220 You may distribute this code under the same terms as Perl itself.