| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | package # hide from PAUSE | ||
| 5 | DBIx::DBO::DBD::mysql; | ||
| 6 | use Carp qw(croak); | ||
| 7 | |||
| 8 | sub _get_table_schema { | ||
| 9 | my($class, $me, $schema, $table) = @_; | ||
| 10 | |||
| 11 | if (defined $schema and length $schema) { | ||
| 12 | (my $q_schema = $me->rdbh->quote($schema)) =~ s/([\\_%])/\\$1/g; | ||
| 13 | my($got_schema) = $me->selectrow_array('SHOW DATABASES LIKE '.$q_schema); | ||
| 14 | croak 'Invalid table: '.$class->_qi($me, $schema, $table) unless defined $got_schema; | ||
| 15 | return $got_schema; | ||
| 16 | } | ||
| 17 | |||
| 18 | ($schema) = $me->selectrow_array('SELECT DATABASE()'); | ||
| 19 | croak 'Invalid table: '.$class->_qi($me, $table).' (No database selected)' unless defined $schema; | ||
| 20 | return $schema; | ||
| 21 | } | ||
| 22 | |||
| 23 | sub _set_table_key_info { | ||
| 24 | my($class, $me, $schema, $table, $h) = @_; | ||
| 25 | |||
| 26 | if (my $keys = $me->rdbh->primary_key_info(undef, $schema, $table)) { | ||
| 27 | $h->{PrimaryKeys}[$_->{KEY_SEQ} - 1] = $_->{COLUMN_NAME} for @{$keys->fetchall_arrayref({})}; | ||
| 28 | } else { | ||
| 29 | # Support for older DBD::mysql - Simulate primary_key_info() | ||
| 30 | local $me->rdbh->{FetchHashKeyName} = 'NAME_lc'; | ||
| 31 | my $info = $me->rdbh->selectall_arrayref('SHOW KEYS FROM '.$class->_qi($me, $schema, $table), {Columns => {}}); | ||
| 32 | $_->{key_name} eq 'PRIMARY' and $h->{PrimaryKeys}[$_->{seq_in_index} - 1] = $_->{column_name} for @$info; | ||
| 33 | } | ||
| 34 | } | ||
| 35 | |||
| 36 | sub _unquote_table { | ||
| 37 | $_[2] =~ /^(?:(`|"|)(.+)\1\.|)(`|"|)(.+)\3$/ or croak "Invalid table: \"$_[2]\""; | ||
| 38 | return ($2, $4); | ||
| 39 | } | ||
| 40 | |||
| 41 | sub _save_last_insert_id { | ||
| 42 | my($class, $me, $sth) = @_; | ||
| 43 | |||
| 44 | return $sth->{mysql_insertid}; | ||
| 45 | } | ||
| 46 | |||
| 47 | sub _get_config { | ||
| 48 | my $class = shift; | ||
| 49 | my $val = $class->SUPER::_get_config(@_); | ||
| 50 | # MySQL supports LIMIT on UPDATE/DELETE by default | ||
| 51 | ($_[0] ne 'LimitRowUpdate' and $_[0] ne 'LimitRowDelete' or defined $val) ? $val : 1; | ||
| 52 | } | ||
| 53 | |||
| 54 | # Query | ||
| 55 | sub _calc_found_rows { | ||
| 56 | my($class, $me) = @_; | ||
| 57 | if ($me->sql =~ / SQL_CALC_FOUND_ROWS /) { | ||
| 58 | $me->run unless $me->_sth->{Executed}; | ||
| 59 | return $me->{Found_Rows} = ($class->_selectrow_array($me, 'SELECT FOUND_ROWS()'))[0]; | ||
| 60 | } | ||
| 61 | $class->SUPER::_calc_found_rows($me); | ||
| 62 | } | ||
| 63 | |||
| 64 | sub _build_sql_select { | ||
| 65 | my($class, $me) = @_; | ||
| 66 | my $sql = $class->SUPER::_build_sql_select($me); | ||
| 67 | $sql =~ s/SELECT /SELECT SQL_CALC_FOUND_ROWS / if $me->config('CalcFoundRows'); | ||
| 68 | return $sql; | ||
| 69 | } | ||
| 70 | |||
| 71 | # MySQL doesn't allow the use of aliases in the WHERE clause | ||
| 72 | sub _alias_preference { | ||
| 73 | my($class, $me, $method) = @_; | ||
| 74 | $method ||= ((caller(2))[3] =~ /\b(\w+)$/); | ||
| 75 | return 0 if $method eq 'join_on' or $method eq 'where'; | ||
| 76 | return 1; | ||
| 77 | } | ||
| 78 | |||
| 79 | sub _bulk_insert { | ||
| 80 | shift->_fast_bulk_insert(@_); | ||
| 81 | } | ||
| 82 | |||
| 83 | 1; |