1 package DBIx::Class::Storage::DBI::mysql;
6 use base qw/DBIx::Class::Storage::DBI/;
10 __PACKAGE__->sql_maker_class('DBIx::Class::SQLMaker::MySQL');
11 __PACKAGE__->sql_limit_dialect ('LimitXY');
12 __PACKAGE__->sql_quote_char ('`');
14 __PACKAGE__->_use_multicolumn_in (1);
16 sub with_deferred_fk_checks {
17 my ($self, $sub) = @_;
19 $self->_do_query('SET FOREIGN_KEY_CHECKS = 0');
21 $self->_do_query('SET FOREIGN_KEY_CHECKS = 1');
24 sub connect_call_set_strict_mode {
27 # the @@sql_mode puts back what was previously set on the session handle
28 $self->_do_query(q|SET SQL_MODE = CONCAT('ANSI,TRADITIONAL,ONLY_FULL_GROUP_BY,', @@sql_mode)|);
29 $self->_do_query(q|SET SQL_AUTO_IS_NULL = 0|);
32 sub _dbh_last_insert_id {
33 my ($self, $dbh, $source, $col) = @_;
34 $dbh->{mysql_insertid};
37 sub _prep_for_execute {
39 #(my $op, $ident, $args) = @_;
41 # Only update and delete need special double-subquery treatment
42 # Insert referencing the same table (i.e. SELECT MAX(id) + 1) seems
43 # to work just fine on MySQL
44 return $self->next::method(@_) if ( $_[0] eq 'select' or $_[0] eq 'insert' );
47 # FIXME FIXME FIXME - this is a terrible, gross, incomplete hack
48 # it should be trivial for mst to port this to DQ (and a good
49 # exercise as well, since we do not yet have such wide tree walking
50 # in place). For the time being this will work in limited cases,
51 # mainly complex update/delete, which is really all we want it for
52 # currently (allows us to fix some bugs without breaking MySQL in
53 # the process, and is also crucial for Shadow to be usable)
55 # extract the source name, construct modification indicator re
56 my $sm = $self->sql_maker;
58 my $target_name = $_[1]->from;
60 if (ref $target_name) {
62 ref $target_name eq 'SCALAR'
64 $$target_name =~ /^ (?:
69 # this is just a plain-ish name, which has been literal-ed for
71 $target_name = (defined $1) ? $1 : $2;
74 # this is something very complex, perhaps a custom result source or whatnot
80 local $sm->{_modification_target_referenced_re} =
81 qr/ (?<!DELETE) [\s\)] (?: FROM | JOIN ) \s (?: \` \Q$target_name\E \` | \Q$target_name\E ) [\s\(] /xi
84 $self->next::method(@_);
87 # here may seem like an odd place to override, but this is the first
88 # method called after we are connected *and* the driver is determined
89 # ($self is reblessed). See code flow in ::Storage::DBI::_populate_dbh
90 sub _run_connection_actions {
93 # default mysql_auto_reconnect to off unless explicitly set
95 $self->_dbh->{mysql_auto_reconnect}
97 ! exists $self->_dbic_connect_attributes->{mysql_auto_reconnect}
99 $self->_dbh->{mysql_auto_reconnect} = 0;
102 $self->next::method(@_);
105 # we need to figure out what mysql version we're running
109 unless ($self->_sql_maker) {
110 my $maker = $self->next::method (@_);
112 # mysql 3 does not understand a bare JOIN
113 my $mysql_ver = $self->_dbh_get_info('SQL_DBMS_VER');
114 $maker->{_default_jointype} = 'INNER' if $mysql_ver =~ /^3/;
117 return $self->_sql_maker;
124 sub deployment_statements {
126 my ($schema, $type, $version, $dir, $sqltargs, @rest) = @_;
131 ! exists $sqltargs->{producer_args}{mysql_version}
133 my $dver = $self->_server_info->{normalized_dbms_version}
135 $sqltargs->{producer_args}{mysql_version} = $dver;
138 $self->next::method($schema, $type, $version, $dir, $sqltargs, @rest);
141 sub _exec_svp_begin {
142 my ($self, $name) = @_;
144 $self->_dbh->do("SAVEPOINT $name");
147 sub _exec_svp_release {
148 my ($self, $name) = @_;
150 $self->_dbh->do("RELEASE SAVEPOINT $name");
153 sub _exec_svp_rollback {
154 my ($self, $name) = @_;
156 $self->_dbh->do("ROLLBACK TO SAVEPOINT $name")
160 my $status = shift->_get_dbh->selectrow_hashref('show slave status');
161 return ($status->{Slave_IO_Running} eq 'Yes') && ($status->{Slave_SQL_Running} eq 'Yes');
164 sub lag_behind_master {
165 return shift->_get_dbh->selectrow_hashref('show slave status')->{Seconds_Behind_Master};
172 DBIx::Class::Storage::DBI::mysql - Storage::DBI class implementing MySQL specifics
176 Storage::DBI autodetects the underlying MySQL database, and re-blesses the
177 C<$storage> object into this class.
179 my $schema = MyDb::Schema->connect( $dsn, $user, $pass, { on_connect_call => 'set_strict_mode' } );
183 This class implements MySQL specific bits of L<DBIx::Class::Storage::DBI>,
184 like AutoIncrement column support and savepoints. Also it augments the
185 SQL maker to support the MySQL-specific C<STRAIGHT_JOIN> join type, which
186 you can use by specifying C<< join_type => 'straight' >> in the
187 L<relationship attributes|DBIx::Class::Relationship::Base/join_type>
190 It also provides a one-stop on-connect macro C<set_strict_mode> which sets
191 session variables such that MySQL behaves more predictably as far as the
192 SQL standard is concerned.
194 =head1 STORAGE OPTIONS
196 =head2 set_strict_mode
198 Enables session-wide strict options upon connecting. Equivalent to:
202 q|SET SQL_MODE = CONCAT('ANSI,TRADITIONAL,ONLY_FULL_GROUP_BY,', @@sql_mode)|,
203 q|SET SQL_AUTO_IS_NULL = 0|,
209 See L<DBIx::Class/CONTRIBUTORS>
213 You may distribute this code under the same terms as Perl itself.