Automatically mark the cap. framework methods as unimplemented for replication
[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
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_strict_mode {
24   my $self = shift;
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)|);
28   $self->_do_query(q|SET SQL_AUTO_IS_NULL = 0|);
29 }
30
31 sub _dbh_last_insert_id {
32   my ($self, $dbh, $source, $col) = @_;
33   $dbh->{mysql_insertid};
34 }
35
36 # we need to figure out what mysql version we're running
37 sub 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
51 sub sqlt_type {
52   return 'MySQL';
53 }
54
55 sub deployment_statements {
56   my $self = shift;
57   my ($schema, $type, $version, $dir, $sqltargs, @rest) = @_;
58
59   $sqltargs ||= {};
60
61   if (
62     ! exists $sqltargs->{producer_args}{mysql_version}
63       and 
64     my $dver = $self->_server_info->{normalized_dbms_version}
65   ) {
66     $sqltargs->{producer_args}{mysql_version} = $dver;
67   }
68
69   $self->next::method($schema, $type, $version, $dir, $sqltargs, @rest);
70 }
71
72 sub _svp_begin {
73     my ($self, $name) = @_;
74
75     $self->_get_dbh->do("SAVEPOINT $name");
76 }
77
78 sub _svp_release {
79     my ($self, $name) = @_;
80
81     $self->_get_dbh->do("RELEASE SAVEPOINT $name");
82 }
83
84 sub _svp_rollback {
85     my ($self, $name) = @_;
86
87     $self->_get_dbh->do("ROLLBACK TO SAVEPOINT $name")
88 }
89
90 sub is_replicating {
91     my $status = shift->_get_dbh->selectrow_hashref('show slave status');
92     return ($status->{Slave_IO_Running} eq 'Yes') && ($status->{Slave_SQL_Running} eq 'Yes');
93 }
94
95 sub lag_behind_master {
96     return shift->_get_dbh->selectrow_hashref('show slave status')->{Seconds_Behind_Master};
97 }
98
99 # MySql can not do subquery update/deletes, only way is slow per-row operations.
100 # This assumes you have set proper transaction isolation and use innodb.
101 sub _subq_update_delete {
102   return shift->_per_row_update_delete (@_);
103 }
104
105 1;
106
107 =head1 NAME
108
109 DBIx::Class::Storage::DBI::mysql - Storage::DBI class implementing MySQL specifics
110
111 =head1 SYNOPSIS
112
113 Storage::DBI autodetects the underlying MySQL database, and re-blesses the
114 C<$storage> object into this class.
115
116   my $schema = MyDb::Schema->connect( $dsn, $user, $pass, { on_connect_call => 'set_strict_mode' } );
117
118 =head1 DESCRIPTION
119
120 This class implements MySQL specific bits of L<DBIx::Class::Storage::DBI>,
121 like AutoIncrement column support and savepoints. Also it augments the
122 SQL maker to support the MySQL-specific C<STRAIGHT_JOIN> join type, which
123 you can use by specifying C<< join_type => 'straight' >> in the
124 L<relationship attributes|DBIx::Class::Relationship::Base/join_type>
125
126
127 It also provides a one-stop on-connect macro C<set_strict_mode> which sets
128 session variables such that MySQL behaves more predictably as far as the
129 SQL standard is concerned.
130
131 =head1 STORAGE OPTIONS
132
133 =head2 set_strict_mode
134
135 Enables session-wide strict options upon connecting. Equivalent to:
136
137   ->connect ( ... , {
138     on_connect_do => [
139       q|SET SQL_MODE = CONCAT('ANSI,TRADITIONAL,ONLY_FULL_GROUP_BY,', @@sql_mode)|,
140       q|SET SQL_AUTO_IS_NULL = 0|,
141     ]
142   });
143
144 =head1 AUTHORS
145
146 See L<DBIx::Class/CONTRIBUTORS>
147
148 =head1 LICENSE
149
150 You may distribute this code under the same terms as Perl itself.
151
152 =cut