line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catmandu::Store::DBI::Handler::SQLite; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
2634
|
use Catmandu::Sane; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
33
|
|
4
|
3
|
|
|
3
|
|
905
|
use Moo; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
23
|
|
5
|
3
|
|
|
3
|
|
1572
|
use namespace::clean; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
30
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = "0.11"; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
with 'Catmandu::Store::DBI::Handler'; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub _column_sql { |
12
|
12
|
|
|
12
|
|
104
|
my ($self, $map, $bag) = @_; |
13
|
12
|
|
|
|
|
59
|
my $col = $map->{column}; |
14
|
12
|
|
|
|
|
69
|
my $dbh = $bag->store->dbh; |
15
|
12
|
|
|
|
|
58
|
my $sql = $dbh->quote_identifier($col) . " "; |
16
|
12
|
100
|
|
|
|
335
|
if ($map->{type} eq 'string') { |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
17
|
9
|
|
|
|
|
19
|
$sql .= 'TEXT'; |
18
|
|
|
|
|
|
|
} |
19
|
|
|
|
|
|
|
elsif ($map->{type} eq 'integer') { |
20
|
0
|
|
|
|
|
0
|
$sql .= 'INTEGER'; |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
elsif ($map->{type} eq 'binary') { |
23
|
3
|
|
|
|
|
10
|
$sql .= 'BLOB'; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
else { |
26
|
0
|
|
|
|
|
0
|
Catmandu::Error->throw("Unknown type '$map->{type}'"); |
27
|
|
|
|
|
|
|
} |
28
|
12
|
100
|
|
|
|
36
|
if ($map->{unique}) { |
29
|
5
|
|
|
|
|
9
|
$sql .= " UNIQUE"; |
30
|
|
|
|
|
|
|
} |
31
|
12
|
100
|
|
|
|
32
|
if ($map->{required}) { |
32
|
5
|
|
|
|
|
22
|
$sql .= " NOT NULL"; |
33
|
|
|
|
|
|
|
} |
34
|
12
|
|
|
|
|
114
|
$sql; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub create_table { |
38
|
5
|
|
|
5
|
0
|
278
|
my ($self, $bag) = @_; |
39
|
5
|
|
|
|
|
30
|
my $mapping = $bag->mapping; |
40
|
5
|
|
|
|
|
31
|
my $dbh = $bag->store->dbh; |
41
|
5
|
|
|
|
|
23
|
my $name = $bag->name; |
42
|
5
|
|
|
|
|
66
|
my $q_name = $dbh->quote_identifier($name); |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
my $sql |
45
|
|
|
|
|
|
|
= "CREATE TABLE IF NOT EXISTS $q_name(" |
46
|
5
|
|
|
|
|
5936
|
. join(',', map {$self->_column_sql($_, $bag)} values %$mapping) |
|
12
|
|
|
|
|
43
|
|
47
|
|
|
|
|
|
|
. ")"; |
48
|
|
|
|
|
|
|
|
49
|
5
|
50
|
|
|
|
37
|
$dbh->do($sql) or Catmandu::Error->throw($dbh->errstr); |
50
|
|
|
|
|
|
|
|
51
|
5
|
|
|
|
|
30913
|
for my $map (values %$mapping) { |
52
|
12
|
50
|
66
|
|
|
127
|
next if $map->{unique} || !$map->{index}; |
53
|
0
|
|
|
|
|
0
|
my $col = $map->{column}; |
54
|
0
|
|
|
|
|
0
|
my $q_col = $dbh->quote_identifier($col); |
55
|
0
|
|
|
|
|
0
|
my $q_idx = $dbh->quote_identifier("${name}_${col}_idx"); |
56
|
0
|
|
|
|
|
0
|
my $idx_sql |
57
|
|
|
|
|
|
|
= "CREATE INDEX IF NOT EXISTS ${q_idx} ON $q_name($q_col)"; |
58
|
0
|
0
|
|
|
|
0
|
$dbh->do($idx_sql) or Catmandu::Error->throw($dbh->errstr); |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub add_row { |
63
|
50
|
|
|
50
|
0
|
121
|
my ($self, $bag, $row) = @_; |
64
|
50
|
|
|
|
|
251
|
my $dbh = $bag->store->dbh; |
65
|
50
|
|
|
|
|
201
|
my @cols = keys %$row; |
66
|
50
|
|
|
|
|
119
|
my @q_cols = map {$dbh->quote_identifier($_)} @cols; |
|
101
|
|
|
|
|
1878
|
|
67
|
50
|
|
|
|
|
1199
|
my @values = values %$row; |
68
|
50
|
|
|
|
|
205
|
my $q_name = $dbh->quote_identifier($bag->name); |
69
|
50
|
|
|
|
|
1218
|
my $sql |
70
|
|
|
|
|
|
|
= "INSERT OR REPLACE INTO $q_name(" |
71
|
|
|
|
|
|
|
. join(',', @q_cols) |
72
|
|
|
|
|
|
|
. ") VALUES(" |
73
|
|
|
|
|
|
|
. join(',', ('?') x @cols) . ")"; |
74
|
|
|
|
|
|
|
|
75
|
50
|
50
|
|
|
|
279
|
my $sth = $dbh->prepare_cached($sql) |
76
|
|
|
|
|
|
|
or Catmandu::Error->throw($dbh->errstr); |
77
|
50
|
50
|
|
|
|
244301
|
$sth->execute(@values) or Catmandu::Error->throw($sth->errstr); |
78
|
50
|
|
|
|
|
852
|
$sth->finish; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
1; |