| 1 | # $Id$ | ||
| 2 | |||
| 3 | package Data::ObjectDriver::Driver::DBD::MariaDB; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use Carp; | ||
| 7 | use base qw( Data::ObjectDriver::Driver::DBD::mysql ); | ||
| 8 | |||
| 9 | sub fetch_id { $_[3]->{mariadb_insertid} || $_[3]->{insertid} } | ||
| 10 | |||
| 11 | sub bind_param_attributes { | ||
| 12 | my ($dbd, $data_type) = @_; | ||
| 13 | if ($data_type) { | ||
| 14 | if ($data_type eq 'blob') { | ||
| 15 | return DBI::SQL_BINARY; | ||
| 16 | } elsif ($data_type eq 'binchar') { | ||
| 17 | return DBI::SQL_BINARY; | ||
| 18 | } | ||
| 19 | } | ||
| 20 | return; | ||
| 21 | } | ||
| 22 | |||
| 23 | sub bulk_insert { | ||
| 24 | my $dbd = shift; | ||
| 25 | my $dbh = shift; | ||
| 26 | my $table = shift; | ||
| 27 | |||
| 28 | my $cols = shift; | ||
| 29 | my $rows_ref = shift; | ||
| 30 | my $attrs = shift || {}; | ||
| 31 | |||
| 32 | croak "Usage bulk_insert(dbd, dbh, table, columnref, rowsref)" | ||
| 33 | unless (defined $dbd && defined $dbh && defined $table && defined $cols && | ||
| 34 | defined $rows_ref); | ||
| 35 | |||
| 36 | return 0e0 if (scalar(@{$rows_ref}) == 0); | ||
| 37 | |||
| 38 | my $sql = "INSERT INTO $table (" . join(',', @{$cols}) . ") VALUES\n"; | ||
| 39 | |||
| 40 | my $one_data_row = "(" . (join ',', (('?') x @$cols)) . ")"; | ||
| 41 | my $ph = join ",", (($one_data_row) x @$rows_ref); | ||
| 42 | $sql .= $ph; | ||
| 43 | |||
| 44 | # For now just write all data, at some point we need to lookup the | ||
| 45 | # maximum packet size for SQL | ||
| 46 | |||
| 47 | if (%$attrs) { | ||
| 48 | my $sth = $dbh->prepare($sql); | ||
| 49 | my $i = 1; | ||
| 50 | for my $row (@$rows_ref) { | ||
| 51 | for (my $j = 0; $j < @$cols; $j++) { | ||
| 52 | $sth->bind_param($i++, $row->[$j], $attrs->{$cols->[$j]}); | ||
| 53 | } | ||
| 54 | } | ||
| 55 | $sth->execute; | ||
| 56 | } else { | ||
| 57 | return $dbh->do($sql, undef, map { @$_ } @$rows_ref); | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | 1; |