1 package DBIx::Class::Storage::DBI::mysql;
6 use base qw/DBIx::Class::Storage::DBI/;
8 __PACKAGE__->sql_maker_class('DBIx::Class::SQLMaker::MySQL');
9 __PACKAGE__->sql_limit_dialect ('LimitXY');
10 __PACKAGE__->sql_quote_char ('`');
12 __PACKAGE__->_use_multicolumn_in (1);
14 sub with_deferred_fk_checks {
15 my ($self, $sub) = @_;
17 $self->_do_query('SET FOREIGN_KEY_CHECKS = 0');
19 $self->_do_query('SET FOREIGN_KEY_CHECKS = 1');
22 sub connect_call_set_strict_mode {
25 # the @@sql_mode puts back what was previously set on the session handle
26 $self->_do_query(q|SET SQL_MODE = CONCAT('ANSI,TRADITIONAL,ONLY_FULL_GROUP_BY,', @@sql_mode)|);
27 $self->_do_query(q|SET SQL_AUTO_IS_NULL = 0|);
30 sub _dbh_last_insert_id {
31 my ($self, $dbh, $source, $col) = @_;
32 $dbh->{mysql_insertid};
35 # here may seem like an odd place to override, but this is the first
36 # method called after we are connected *and* the driver is determined
37 # ($self is reblessed). See code flow in ::Storage::DBI::_populate_dbh
38 sub _run_connection_actions {
41 # default mysql_auto_reconnect to off unless explicitly set
43 $self->_dbh->{mysql_auto_reconnect}
45 ! exists $self->_dbic_connect_attributes->{mysql_auto_reconnect}
47 $self->_dbh->{mysql_auto_reconnect} = 0;
50 $self->next::method(@_);
53 # we need to figure out what mysql version we're running
57 unless ($self->_sql_maker) {
58 my $maker = $self->next::method (@_);
60 # mysql 3 does not understand a bare JOIN
61 my $mysql_ver = $self->_dbh_get_info('SQL_DBMS_VER');
62 $maker->{_default_jointype} = 'INNER' if $mysql_ver =~ /^3/;
65 return $self->_sql_maker;
72 sub deployment_statements {
74 my ($schema, $type, $version, $dir, $sqltargs, @rest) = @_;
79 ! exists $sqltargs->{producer_args}{mysql_version}
81 my $dver = $self->_server_info->{normalized_dbms_version}
83 $sqltargs->{producer_args}{mysql_version} = $dver;
86 $self->next::method($schema, $type, $version, $dir, $sqltargs, @rest);
90 my ($self, $name) = @_;
92 $self->_dbh->do("SAVEPOINT $name");
95 sub _exec_svp_release {
96 my ($self, $name) = @_;
98 $self->_dbh->do("RELEASE SAVEPOINT $name");
101 sub _exec_svp_rollback {
102 my ($self, $name) = @_;
104 $self->_dbh->do("ROLLBACK TO SAVEPOINT $name")
108 my $status = shift->_get_dbh->selectrow_hashref('show slave status');
109 return ($status->{Slave_IO_Running} eq 'Yes') && ($status->{Slave_SQL_Running} eq 'Yes');
112 sub lag_behind_master {
113 return shift->_get_dbh->selectrow_hashref('show slave status')->{Seconds_Behind_Master};
120 DBIx::Class::Storage::DBI::mysql - Storage::DBI class implementing MySQL specifics
124 Storage::DBI autodetects the underlying MySQL database, and re-blesses the
125 C<$storage> object into this class.
127 my $schema = MyDb::Schema->connect( $dsn, $user, $pass, { on_connect_call => 'set_strict_mode' } );
131 This class implements MySQL specific bits of L<DBIx::Class::Storage::DBI>,
132 like AutoIncrement column support and savepoints. Also it augments the
133 SQL maker to support the MySQL-specific C<STRAIGHT_JOIN> join type, which
134 you can use by specifying C<< join_type => 'straight' >> in the
135 L<relationship attributes|DBIx::Class::Relationship::Base/join_type>
138 It also provides a one-stop on-connect macro C<set_strict_mode> which sets
139 session variables such that MySQL behaves more predictably as far as the
140 SQL standard is concerned.
142 =head1 STORAGE OPTIONS
144 =head2 set_strict_mode
146 Enables session-wide strict options upon connecting. Equivalent to:
150 q|SET SQL_MODE = CONCAT('ANSI,TRADITIONAL,ONLY_FULL_GROUP_BY,', @@sql_mode)|,
151 q|SET SQL_AUTO_IS_NULL = 0|,
157 See L<DBIx::Class/CONTRIBUTORS>
161 You may distribute this code under the same terms as Perl itself.