| 1 | # $Id$ | ||
| 2 | |||
| 3 | package Data::ObjectDriver::Driver::DBD::mysql; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use base qw( Data::ObjectDriver::Driver::DBD ); | ||
| 7 | |||
| 8 | use Carp qw( croak ); | ||
| 9 | use Data::ObjectDriver::Errors; | ||
| 10 | |||
| 11 | use constant ERROR_MAP => { | ||
| 12 | 1062 => Data::ObjectDriver::Errors->UNIQUE_CONSTRAINT, | ||
| 13 | }; | ||
| 14 | |||
| 15 | sub fetch_id { $_[3]->{mysql_insertid} || $_[3]->{insertid} } | ||
| 16 | |||
| 17 | sub map_error_code { | ||
| 18 | my $dbd = shift; | ||
| 19 | my($code, $msg) = @_; | ||
| 20 | return if !defined $code; | ||
| 21 | return ERROR_MAP->{$code}; | ||
| 22 | } | ||
| 23 | |||
| 24 | sub sql_for_unixtime { | ||
| 25 | return "UNIX_TIMESTAMP()"; | ||
| 26 | } | ||
| 27 | |||
| 28 | # yes, MySQL supports LIMIT on a DELETE | ||
| 29 | sub can_delete_with_limit { 1 } | ||
| 30 | |||
| 31 | # yes, MySQL makes every search case insensitive | ||
| 32 | sub is_case_insensitive { 1 }; | ||
| 33 | |||
| 34 | # yes, MySQL invented(?) REPLACE INTO extension | ||
| 35 | sub can_replace { 1 } | ||
| 36 | |||
| 37 | |||
| 38 | sub bulk_insert { | ||
| 39 | my $dbd = shift; | ||
| 40 | my $dbh = shift; | ||
| 41 | my $table = shift; | ||
| 42 | |||
| 43 | my $cols = shift; | ||
| 44 | my $rows_ref = shift; | ||
| 45 | |||
| 46 | croak "Usage bulk_insert(dbd, dbh, table, columnref, rowsref)" | ||
| 47 | unless (defined $dbd && defined $dbh && defined $table && defined $cols && | ||
| 48 | defined $rows_ref); | ||
| 49 | |||
| 50 | return 0e0 if (scalar(@{$rows_ref}) == 0); | ||
| 51 | |||
| 52 | my $sql = "INSERT INTO $table (" . join(',', @{$cols}) . ") VALUES\n"; | ||
| 53 | |||
| 54 | my $one_data_row = "(" . (join ',', (('?') x @$cols)) . ")"; | ||
| 55 | my $ph = join ",", (($one_data_row) x @$rows_ref); | ||
| 56 | $sql .= $ph; | ||
| 57 | |||
| 58 | # For now just write all data, at some point we need to lookup the | ||
| 59 | # maximum packet size for SQL | ||
| 60 | |||
| 61 | return $dbh->do($sql, undef, map { @$_ } @$rows_ref); | ||
| 62 | } | ||
| 63 | |||
| 64 | 1; |