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