Add set_ansi_mode POD
[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::AmbiguousGlob
9   DBIx::Class::Storage::DBI
10 /;
11 use mro 'c3';
12
13 __PACKAGE__->sql_maker_class('DBIx::Class::SQLAHacks::MySQL');
14
15 sub with_deferred_fk_checks {
16   my ($self, $sub) = @_;
17
18   $self->_do_query('SET FOREIGN_KEY_CHECKS = 0');
19   $sub->();
20   $self->_do_query('SET FOREIGN_KEY_CHECKS = 1');
21 }
22
23 sub connect_call_set_ansi_mode {
24   my $self = shift;
25   $self->_do_query(q|SET SQL_MODE = 'ANSI,TRADITIONAL'|);
26   $self->_do_query(q|SET SQL_AUTO_IS_NULL = 0|);
27 }
28
29 sub _dbh_last_insert_id {
30   my ($self, $dbh, $source, $col) = @_;
31   $dbh->{mysql_insertid};
32 }
33
34 sub sqlt_type {
35   return 'MySQL';
36 }
37
38 sub _svp_begin {
39     my ($self, $name) = @_;
40
41     $self->dbh->do("SAVEPOINT $name");
42 }
43
44 sub _svp_release {
45     my ($self, $name) = @_;
46
47     $self->dbh->do("RELEASE SAVEPOINT $name");
48 }
49
50 sub _svp_rollback {
51     my ($self, $name) = @_;
52
53     $self->dbh->do("ROLLBACK TO SAVEPOINT $name")
54 }
55
56 sub is_replicating {
57     my $status = shift->dbh->selectrow_hashref('show slave status');
58     return ($status->{Slave_IO_Running} eq 'Yes') && ($status->{Slave_SQL_Running} eq 'Yes');
59 }
60
61 sub lag_behind_master {
62     return shift->dbh->selectrow_hashref('show slave status')->{Seconds_Behind_Master};
63 }
64
65 # MySql can not do subquery update/deletes, only way is slow per-row operations.
66 # This assumes you have set proper transaction isolation and use innodb.
67 sub _subq_update_delete {
68   return shift->_per_row_update_delete (@_);
69 }
70
71 1;
72
73 =head1 NAME
74
75 DBIx::Class::Storage::DBI::mysql - Storage::DBI class implementing MySQL specifics
76
77 =head1 SYNOPSIS
78
79 Storage::DBI autodetects the underlying MySQL database, and re-blesses the
80 C<$storage> object into this class.
81
82   my $schema = MyDatbase->connect( $dsn, $user, $pass, { set_ansi_mode => 1 } );
83
84 =head1 DESCRIPTION
85
86 This class implements MySQL specific bits of L<DBIx::Class::Storage::DBI>.
87
88 It also provides a one-stop macro that sets session variables such that
89 MySQL behaves more predictably as far as the SQL standard is concerned.
90
91 =head1 AUTHORS
92
93 See L<DBIx::Class/CONTRIBUTORS>
94
95 =head1 LICENSE
96
97 You may distribute this code under the same terms as Perl itself.
98
99 =cut