Consolidate last_insert_id handling with a fallback-attempt on DBI::last_insert_id
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / mysql.pm
CommitLineData
843f8ecd 1package DBIx::Class::Storage::DBI::mysql;
2
3use strict;
4use warnings;
5
698775ed 6use base qw/
7 DBIx::Class::Storage::DBI::MultiColumnIn
8 DBIx::Class::Storage::DBI::AmbiguousGlob
9 DBIx::Class::Storage::DBI
10/;
2ad62d97 11use mro 'c3';
843f8ecd 12
87aa29e2 13__PACKAGE__->sql_maker_class('DBIx::Class::SQLAHacks::MySQL');
843f8ecd 14
e96a93df 15sub with_deferred_fk_checks {
16 my ($self, $sub) = @_;
17
5cf7285e 18 $self->_do_query('SET FOREIGN_KEY_CHECKS = 0');
e96a93df 19 $sub->();
5cf7285e 20 $self->_do_query('SET FOREIGN_KEY_CHECKS = 1');
e96a93df 21}
22
97a0a148 23sub connect_call_set_strict_mode {
0fde80d9 24 my $self = shift;
97a0a148 25
26 # the @@sql_mode puts back what was previously set on the session handle
27 $self->_do_query(q|SET SQL_MODE = CONCAT('ANSI,TRADITIONAL,ONLY_FULL_GROUP_BY,', @@sql_mode)|);
0fde80d9 28 $self->_do_query(q|SET SQL_AUTO_IS_NULL = 0|);
29}
30
d4f16b21 31sub _dbh_last_insert_id {
32 my ($self, $dbh, $source, $col) = @_;
33 $dbh->{mysql_insertid};
843f8ecd 34}
35
d3944540 36# we need to figure out what mysql version we're running
37sub sql_maker {
38 my $self = shift;
39
40 unless ($self->_sql_maker) {
41 my $maker = $self->next::method (@_);
42
43 # mysql 3 does not understand a bare JOIN
44 my $mysql_ver = $self->_get_dbh->get_info(18);
45 $maker->{_default_jointype} = 'INNER' if $mysql_ver =~ /^3/;
46 }
47
48 return $self->_sql_maker;
49}
50
e8d293df 51sub sqlt_type {
52 return 'MySQL';
53}
54
adb3554a 55sub _svp_begin {
eeb8cfeb 56 my ($self, $name) = @_;
adb3554a 57
9ae966b9 58 $self->_get_dbh->do("SAVEPOINT $name");
adb3554a 59}
60
61sub _svp_release {
eeb8cfeb 62 my ($self, $name) = @_;
adb3554a 63
9ae966b9 64 $self->_get_dbh->do("RELEASE SAVEPOINT $name");
adb3554a 65}
66
67sub _svp_rollback {
eeb8cfeb 68 my ($self, $name) = @_;
adb3554a 69
9ae966b9 70 $self->_get_dbh->do("ROLLBACK TO SAVEPOINT $name")
adb3554a 71}
48fe9087 72
106d5f3b 73sub is_replicating {
9ae966b9 74 my $status = shift->_get_dbh->selectrow_hashref('show slave status');
f797e89e 75 return ($status->{Slave_IO_Running} eq 'Yes') && ($status->{Slave_SQL_Running} eq 'Yes');
106d5f3b 76}
77
78sub lag_behind_master {
9ae966b9 79 return shift->_get_dbh->selectrow_hashref('show slave status')->{Seconds_Behind_Master};
106d5f3b 80}
81
613f65e5 82# MySql can not do subquery update/deletes, only way is slow per-row operations.
ab807c12 83# This assumes you have set proper transaction isolation and use innodb.
b5963465 84sub _subq_update_delete {
613f65e5 85 return shift->_per_row_update_delete (@_);
86}
87
843f8ecd 881;
89
75d07914 90=head1 NAME
843f8ecd 91
c9d438ff 92DBIx::Class::Storage::DBI::mysql - Storage::DBI class implementing MySQL specifics
843f8ecd 93
94=head1 SYNOPSIS
95
c9d438ff 96Storage::DBI autodetects the underlying MySQL database, and re-blesses the
97C<$storage> object into this class.
98
3ce9dd08 99 my $schema = MyDb::Schema->connect( $dsn, $user, $pass, { on_connect_call => 'set_strict_mode' } );
843f8ecd 100
101=head1 DESCRIPTION
102
c9d438ff 103This class implements MySQL specific bits of L<DBIx::Class::Storage::DBI>.
104
97a0a148 105It also provides a one-stop on-connect macro C<set_strict_mode> which sets
106session variables such that MySQL behaves more predictably as far as the
107SQL standard is concerned.
0fde80d9 108
3c01add8 109=head1 STORAGE OPTIONS
110
111=head2 set_strict_mode
112
113Enables session-wide strict options upon connecting. Equivalent to:
114
115 ->connect ( ... , {
116 on_connect_do => [
117 q|SET SQL_MODE = CONCAT('ANSI,TRADITIONAL,ONLY_FULL_GROUP_BY,', @@sql_mode)|,
118 q|SET SQL_AUTO_IS_NULL = 0|,
119 ]
120 });
121
843f8ecd 122=head1 AUTHORS
123
c9d438ff 124See L<DBIx::Class/CONTRIBUTORS>
843f8ecd 125
126=head1 LICENSE
127
128You may distribute this code under the same terms as Perl itself.
129
130=cut