forked from sixapart/data-objectdriver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLite.pm
More file actions
90 lines (65 loc) · 1.99 KB
/
SQLite.pm
File metadata and controls
90 lines (65 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# $Id
package Data::ObjectDriver::Driver::DBD::SQLite;
use strict;
use warnings;
use base qw( Data::ObjectDriver::Driver::DBD );
use Data::ObjectDriver::Errors;
# yes according to http://www.sqlite.org/lang_replace.html
sub can_replace { 1 }
# SQLite has problems with prepare_cached
sub can_prepare_cached_statements { 0 }
sub fetch_id { $_[2]->func('last_insert_rowid') }
sub bind_param_attributes {
my ($dbd, $data_type) = @_;
if ($data_type) {
if ($data_type eq 'blob') {
return DBI::SQL_BLOB;
} elsif ($data_type eq 'binchar') {
return DBI::SQL_VARCHAR;
}
}
return;
}
sub map_error_code {
my $dbd = shift;
my($code, $msg) = @_;
if ($msg && $msg =~ /(?:not unique|UNIQUE constraint failed)/) {
return Data::ObjectDriver::Errors->UNIQUE_CONSTRAINT;
} else {
return;
}
}
sub bulk_insert {
my $dbd = shift;
my $dbh = shift;
my $table = shift;
my $cols = shift;
my $rows_ref = shift;
my $attrs = shift || {};
my $sql = "INSERT INTO $table(" . join(',', @{$cols}) . ") VALUES (" . join(',', map {'?'} @{$cols}) . ")\n";
my $sth = $dbh->prepare($sql);
foreach my $row (@{$rows_ref}) {
my $i = 1;
for (my $j = 0; $j < @$cols; $j++) {
$sth->bind_param($i++, $row->[$j], $attrs->{$cols->[$j]});
}
$sth->execute;
}
# For now just write all data, at some point we need to lookup the
# maximum packet size for SQL
return 1;
}
1;
=pod
=head1 NAME
Data::ObjectDriver::Driver::DBD::SQLite - SQLite driver
=head2 DESCRIPTION
This class provides an interface to the SQLite (L<http://sqlite.org>)
database through DBI.
=head2 NOTES & BUGS
This is experimental.
With the 1.11 version of L<DBD::SQLite> Blobs are handled transparently,
so C<bind_param_attributes> is optional.
With previous version of L<DBD::SQLite> users have experimented issues
with binary data in CHAR (partially solved by the DBI::SQL_BINARY binding).
=cut