release v0.08198
[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
be64931c 6use base qw/DBIx::Class::Storage::DBI/;
843f8ecd 7
d5dedbd6 8__PACKAGE__->sql_maker_class('DBIx::Class::SQLMaker::MySQL');
6a247f33 9__PACKAGE__->sql_limit_dialect ('LimitXY');
2b8cc2f2 10__PACKAGE__->sql_quote_char ('`');
843f8ecd 11
be64931c 12__PACKAGE__->_use_multicolumn_in (1);
13
e96a93df 14sub with_deferred_fk_checks {
15 my ($self, $sub) = @_;
16
5cf7285e 17 $self->_do_query('SET FOREIGN_KEY_CHECKS = 0');
e96a93df 18 $sub->();
5cf7285e 19 $self->_do_query('SET FOREIGN_KEY_CHECKS = 1');
e96a93df 20}
21
97a0a148 22sub connect_call_set_strict_mode {
0fde80d9 23 my $self = shift;
97a0a148 24
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)|);
0fde80d9 27 $self->_do_query(q|SET SQL_AUTO_IS_NULL = 0|);
28}
29
d4f16b21 30sub _dbh_last_insert_id {
31 my ($self, $dbh, $source, $col) = @_;
32 $dbh->{mysql_insertid};
843f8ecd 33}
34
f98120e4 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
38sub _run_connection_actions {
39 my $self = shift;
40
41 # default mysql_auto_reconnect to off unless explicitly set
42 if (
43 $self->_dbh->{mysql_auto_reconnect}
44 and
45 ! exists $self->_dbic_connect_attributes->{mysql_auto_reconnect}
46 ) {
47 $self->_dbh->{mysql_auto_reconnect} = 0;
48 }
49
50 $self->next::method(@_);
51}
52
d3944540 53# we need to figure out what mysql version we're running
54sub sql_maker {
55 my $self = shift;
56
57 unless ($self->_sql_maker) {
58 my $maker = $self->next::method (@_);
59
60 # mysql 3 does not understand a bare JOIN
af1f4f84 61 my $mysql_ver = $self->_dbh_get_info('SQL_DBMS_VER');
d3944540 62 $maker->{_default_jointype} = 'INNER' if $mysql_ver =~ /^3/;
63 }
64
65 return $self->_sql_maker;
66}
67
e8d293df 68sub sqlt_type {
69 return 'MySQL';
70}
71
96736321 72sub deployment_statements {
73 my $self = shift;
74 my ($schema, $type, $version, $dir, $sqltargs, @rest) = @_;
75
76 $sqltargs ||= {};
77
78 if (
79 ! exists $sqltargs->{producer_args}{mysql_version}
8273e845 80 and
96736321 81 my $dver = $self->_server_info->{normalized_dbms_version}
82 ) {
83 $sqltargs->{producer_args}{mysql_version} = $dver;
84 }
85
86 $self->next::method($schema, $type, $version, $dir, $sqltargs, @rest);
87}
88
90d7422f 89sub _exec_svp_begin {
eeb8cfeb 90 my ($self, $name) = @_;
adb3554a 91
90d7422f 92 $self->_dbh->do("SAVEPOINT $name");
adb3554a 93}
94
90d7422f 95sub _exec_svp_release {
eeb8cfeb 96 my ($self, $name) = @_;
adb3554a 97
90d7422f 98 $self->_dbh->do("RELEASE SAVEPOINT $name");
adb3554a 99}
100
90d7422f 101sub _exec_svp_rollback {
eeb8cfeb 102 my ($self, $name) = @_;
adb3554a 103
90d7422f 104 $self->_dbh->do("ROLLBACK TO SAVEPOINT $name")
adb3554a 105}
48fe9087 106
106d5f3b 107sub is_replicating {
9ae966b9 108 my $status = shift->_get_dbh->selectrow_hashref('show slave status');
f797e89e 109 return ($status->{Slave_IO_Running} eq 'Yes') && ($status->{Slave_SQL_Running} eq 'Yes');
106d5f3b 110}
111
112sub lag_behind_master {
9ae966b9 113 return shift->_get_dbh->selectrow_hashref('show slave status')->{Seconds_Behind_Master};
106d5f3b 114}
115
843f8ecd 1161;
117
75d07914 118=head1 NAME
843f8ecd 119
c9d438ff 120DBIx::Class::Storage::DBI::mysql - Storage::DBI class implementing MySQL specifics
843f8ecd 121
122=head1 SYNOPSIS
123
c9d438ff 124Storage::DBI autodetects the underlying MySQL database, and re-blesses the
125C<$storage> object into this class.
126
3ce9dd08 127 my $schema = MyDb::Schema->connect( $dsn, $user, $pass, { on_connect_call => 'set_strict_mode' } );
843f8ecd 128
129=head1 DESCRIPTION
130
b8391c87 131This class implements MySQL specific bits of L<DBIx::Class::Storage::DBI>,
132like AutoIncrement column support and savepoints. Also it augments the
133SQL maker to support the MySQL-specific C<STRAIGHT_JOIN> join type, which
134you can use by specifying C<< join_type => 'straight' >> in the
135L<relationship attributes|DBIx::Class::Relationship::Base/join_type>
136
c9d438ff 137
97a0a148 138It also provides a one-stop on-connect macro C<set_strict_mode> which sets
139session variables such that MySQL behaves more predictably as far as the
140SQL standard is concerned.
0fde80d9 141
3c01add8 142=head1 STORAGE OPTIONS
143
144=head2 set_strict_mode
145
146Enables session-wide strict options upon connecting. Equivalent to:
147
148 ->connect ( ... , {
149 on_connect_do => [
150 q|SET SQL_MODE = CONCAT('ANSI,TRADITIONAL,ONLY_FULL_GROUP_BY,', @@sql_mode)|,
151 q|SET SQL_AUTO_IS_NULL = 0|,
152 ]
153 });
154
843f8ecd 155=head1 AUTHORS
156
c9d438ff 157See L<DBIx::Class/CONTRIBUTORS>
843f8ecd 158
159=head1 LICENSE
160
161You may distribute this code under the same terms as Perl itself.
162
163=cut