Rename last_dbh and turn it into a public method
[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
e8d293df 36sub sqlt_type {
37 return 'MySQL';
38}
39
adb3554a 40sub _svp_begin {
eeb8cfeb 41 my ($self, $name) = @_;
adb3554a 42
a52c8b22 43 $self->last_dbh->do("SAVEPOINT $name");
adb3554a 44}
45
46sub _svp_release {
eeb8cfeb 47 my ($self, $name) = @_;
adb3554a 48
a52c8b22 49 $self->last_dbh->do("RELEASE SAVEPOINT $name");
adb3554a 50}
51
52sub _svp_rollback {
eeb8cfeb 53 my ($self, $name) = @_;
adb3554a 54
a52c8b22 55 $self->last_dbh->do("ROLLBACK TO SAVEPOINT $name")
adb3554a 56}
48fe9087 57
106d5f3b 58sub is_replicating {
a52c8b22 59 my $status = shift->last_dbh->selectrow_hashref('show slave status');
f797e89e 60 return ($status->{Slave_IO_Running} eq 'Yes') && ($status->{Slave_SQL_Running} eq 'Yes');
106d5f3b 61}
62
63sub lag_behind_master {
a52c8b22 64 return shift->last_dbh->selectrow_hashref('show slave status')->{Seconds_Behind_Master};
106d5f3b 65}
66
613f65e5 67# MySql can not do subquery update/deletes, only way is slow per-row operations.
ab807c12 68# This assumes you have set proper transaction isolation and use innodb.
b5963465 69sub _subq_update_delete {
613f65e5 70 return shift->_per_row_update_delete (@_);
71}
72
843f8ecd 731;
74
75d07914 75=head1 NAME
843f8ecd 76
c9d438ff 77DBIx::Class::Storage::DBI::mysql - Storage::DBI class implementing MySQL specifics
843f8ecd 78
79=head1 SYNOPSIS
80
c9d438ff 81Storage::DBI autodetects the underlying MySQL database, and re-blesses the
82C<$storage> object into this class.
83
97a0a148 84 my $schema = MyDb::Schema->connect( $dsn, $user, $pass, { set_strict_mode => 1 } );
843f8ecd 85
86=head1 DESCRIPTION
87
c9d438ff 88This class implements MySQL specific bits of L<DBIx::Class::Storage::DBI>.
89
97a0a148 90It also provides a one-stop on-connect macro C<set_strict_mode> which sets
91session variables such that MySQL behaves more predictably as far as the
92SQL standard is concerned.
0fde80d9 93
843f8ecd 94=head1 AUTHORS
95
c9d438ff 96See L<DBIx::Class/CONTRIBUTORS>
843f8ecd 97
98=head1 LICENSE
99
100You may distribute this code under the same terms as Perl itself.
101
102=cut