Skip to content

Commit 58eac4e

Browse files
committed
Tweak ::DBD::Pg::bulk_insert not to use COPY statement if some of the attributes require binary handling
1 parent 937504f commit 58eac4e

1 file changed

Lines changed: 26 additions & 8 deletions

File tree

  • lib/Data/ObjectDriver/Driver/DBD

lib/Data/ObjectDriver/Driver/DBD/Pg.pm

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,33 @@ sub bulk_insert {
5151

5252
my $cols = shift;
5353
my $rows_ref = shift;
54-
55-
my $sql = "COPY $table (" . join(',', @{$cols}) . ') from stdin';
56-
57-
$dbh->do($sql);
58-
foreach my $row (@{$rows_ref}) {
59-
my $line = join("\t", map {$_ || '\N'} @{$row});
60-
$dbh->pg_putline("$line\n");
54+
my $attrs = shift || {};
55+
56+
if (%$attrs) {
57+
my $sql = "INSERT INTO $table (" . join(',', @{$cols}) . ") VALUES\n";
58+
59+
my $one_data_row = "(" . (join ',', (('?') x @$cols)) . ")";
60+
my $ph = join ",", (($one_data_row) x @$rows_ref);
61+
$sql .= $ph;
62+
63+
my $sth = $dbh->prepare($sql);
64+
my $i = 1;
65+
for my $row (@$rows_ref) {
66+
for (my $j = 0; $j < @$cols; $j++) {
67+
$sth->bind_param($i++, $row->[$j], $attrs->{$cols->[$j]});
68+
}
69+
}
70+
$sth->execute;
71+
} else {
72+
my $sql = "COPY $table (" . join(',', @{$cols}) . ') from stdin';
73+
74+
$dbh->do($sql);
75+
foreach my $row (@{$rows_ref}) {
76+
my $line = join("\t", map {$_ || '\N'} @{$row});
77+
$dbh->pg_putline("$line\n");
78+
}
79+
return $dbh->pg_endcopy();
6180
}
62-
return $dbh->pg_endcopy();
6381
}
6482

6583
sub map_error_code {

0 commit comments

Comments
 (0)