SafeBrowsing3/Sqlite.pm untested

filter: Statementstmt 0.0% 0 / 90 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 29 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 157 SCAR 42.8
CC
8
Cov
0%
init
42.0
add_chunks_s
6.0
add_chunks_a
6.0
CRAP 72.0
1 package Net::Google::SafeBrowsing3::Sqlite;
2  
3 use strict;
4 use warnings;
5  
6 use base 'Net::Google::SafeBrowsing3::DBI';
7  
8 use Carp;
9 use DBI;
10 use List::Util qw(first);
11  
12  
13 our $VERSION = '0.1';
14  
15  
16 =head1 NAME
17  
18 Net::Google::SafeBrowsing3::Sqlite - Sqlite as back-end storage for the Google Safe Browsing v3 database
19  
20 =head1 SYNOPSIS
21  
22 use Net::Google::SafeBrowsing3::Sqlite;
23  
24 my $storage = Net::Google::SafeBrowsing3::Sqlite->new(file => 'google-v3.db');
25 ...
26 $storage->close();
27  
28 =head1 DESCRIPTION
29  
30 This is an implementation of L<Net::Google::SafeBrowsing3::Storage> using Sqlite.
31  
32 =cut
33  
34  
35 =head1 CONSTRUCTOR
36  
37 =over 4
38  
39 =back
40  
41 =head2 new()
42  
43 Create a Net::Google::SafeBrowsing3::Sqlite object
44  
45 my $storage = Net::Google::SafeBrowsing3::Sqlite->new(file => 'google-v3.db');
46  
47 Arguments
48  
49 =over 4
50  
51 =item file
52  
53 Required. File to store the database.
54  
55 =item keep_all
56  
57 Optional. Set to 1 to keep old information (such as expiring full hashes) in the database. 0 (delete) by default.
58  
59 =item cache_size
60  
61 Sqlite cache size. 20000 by default.
62  
63 =back
64  
65  
66 =cut
67  
68 sub new {
69 my ($class, %args) = @_;
70  
71 my $self = { # default arguments
72 keep_all => 0,
73 file => 'gsb3.db',
74 cache_size => 20000,
75  
76 %args,
77 };
78  
79 bless $self, $class or croak "Can't bless $class: $!";
80  
81  
82 $self->init();
83  
84 return $self;
85 }
86  
87 =head1 PUBLIC FUNCTIONS
88  
89 =over 4
90  
91 See L<Net::Google::SafeBrowsing3::Storage> for a complete list of public functions.
92  
93 =back
94  
95 =head2 close()
96  
97 Cleanup old full hashes, and close the connection to the database.
98  
99 $storage->close();
100  
101 =cut
102  
103 sub init {
104 my ($self, %args) = @_;
105  
106 $self->{dbh} = DBI->connect("dbi:SQLite:dbname=" . $self->{file}, "", "");
107 $self->{dbh}->do("PRAGMA journal_mode = OFF");
108 $self->{dbh}->do("PRAGMA synchronous = OFF");
109 $self->{dbh}->do("PRAGMA cache_size = " . $self->{cache_size});
110  
111 my @tables = $self->{dbh}->tables;
112  
113 if (! defined first { $_ eq '"main"."updates"' || $_ eq '"updates"' } @tables) {
114 $self->create_table_updates();
115 }
116 if (! defined first { $_ eq '"main"."a_chunks"' || $_ eq '"a_chunks"' } @tables) {
117 $self->create_table_a_chunks();
118 }
119 if (! defined first { $_ eq '"main"."s_chunks"' || $_ eq '"s_chunks"' } @tables) {
120 $self->create_table_s_chunks();
121 }
122 if (! defined first { $_ eq '"main"."full_hashes"' || $_ eq '"full_hashes"' } @tables) {
123 $self->create_table_full_hashes();
124 }
125 if (! defined first { $_ eq '"main"."full_hashes_errors"' || $_ eq '"full_hashes_errors"' } @tables) {
126 $self->create_table_full_hashes_errors();
127 }
128 }
129  
130  
131 sub create_table_updates {
132 my ($self, %args) = @_;
133  
134 my $schema = qq{
135 CREATE TABLE updates (
136 last INTEGER DEFAULT 0,
137 wait INTEGER DEFAULT 1800,
138 errors INTEGER DEFAULT 0,
139 list TEXT
140 );
141 }; # Need to handle errors
142  
143 $self->{dbh}->do($schema);
144 }
145  
146 sub create_table_a_chunks {
147 my ($self, %args) = @_;
148  
149 my $schema = qq{
150 CREATE TABLE a_chunks (
151 prefix TEXT,
152 num INTEGER,
153 list TEXT
154 );
155 };
156  
157 $self->{dbh}->do($schema);
158  
159 my $index = qq{
160 CREATE INDEX a_chunks_num_list ON a_chunks (
161 num,
162 list
163 );
164 };
165 # $self->{dbh}->do($index);
166  
167 $index = qq{
168 CREATE UNIQUE INDEX a_chunks_unique ON a_chunks (
169 prefix,
170 num,
171 list
172 );
173 };
174 $self->{dbh}->do($index);
175 }
176  
177 sub create_table_s_chunks {
178 my ($self, %args) = @_;
179  
180 my $schema = qq{
181 CREATE TABLE s_chunks (
182 prefix TEXT,
183 num INTEGER,
184 add_num INTEGER,
185 list TEXT
186 );
187 };
188  
189 $self->{dbh}->do($schema);
190  
191 my $index = qq{
192 CREATE INDEX s_chunks_num ON s_chunks (
193 num
194 );
195 };
196 # $self->{dbh}->do($index);
197  
198 $index = qq{
199 CREATE UNIQUE INDEX s_chunks_unique ON s_chunks (
200 prefix,
201 num,
202 add_num,
203 list
204 );
205 };
206 $self->{dbh}->do($index);
207 }
208  
209 sub create_table_full_hashes {
210 my ($self, %args) = @_;
211  
212 my $schema = qq{
213 CREATE TABLE full_hashes (
214 id INTEGER PRIMARY KEY AUTOINCREMENT,
215 hash TEXT,
216 list TEXT,
217 end INTEGER,
218 type INTEGER
219 );
220 };
221  
222 $self->{dbh}->do($schema);
223  
224 my $index = qq{
225 CREATE UNIQUE INDEX hash ON full_hashes (
226 hash,
227 list
228 );
229 };
230 $self->{dbh}->do($index);
231 }
232  
233 sub create_table_full_hashes_errors {
234 my ($self, %args) = @_;
235  
236 my $schema = qq{
237 CREATE TABLE full_hashes_errors (
238 id INTEGER PRIMARY KEY AUTOINCREMENT,
239 errors INTEGER,
240 prefix TEXT,
241 timestamp INTEGER
242 );
243 };
244  
245 $self->{dbh}->do($schema);
246 }
247  
248  
249 sub add_chunks_s {
250 my ($self, %args) = @_;
251 my $chunknum = $args{chunknum} || 0;
252 my $chunks = $args{chunks} || [];
253 my $list = $args{'list'} || '';
254  
255 my $add = $self->{dbh}->prepare('INSERT OR IGNORE INTO s_chunks (prefix, num, add_num, list) VALUES (?, ?, ?, ?)');
256  
257 $self->{dbh}->begin_work;
258 foreach my $chunk (@$chunks) {
259 $add->execute( $chunk->{prefix}, $chunknum, $chunk->{add_chunknum}, $list );
260 }
261  
262 if (scalar @$chunks == 0) { # keep empty chunks
263 $add->execute( '', $chunknum, '', $list );
264 }
265 $self->{dbh}->commit;
266 }
267  
268 sub add_chunks_a {
269 my ($self, %args) = @_;
270 my $chunknum = $args{chunknum} || 0;
271 my $chunks = $args{chunks} || [];
272 my $list = $args{'list'} || '';
273  
274 my $add = $self->{dbh}->prepare('INSERT OR IGNORE INTO a_chunks (prefix, num, list) VALUES (?, ?, ?)');
275  
276 $self->{dbh}->begin_work;
277 foreach my $chunk (@$chunks) {
278 $add->execute( $chunk->{prefix}, $chunknum, $list );
279 }
280  
281 if (scalar @$chunks == 0) { # keep empty chunks
282 $add->execute( '', $chunknum, $list );
283 }
284 $self->{dbh}->commit;
285 }
286  
287  
288 =head1 CHANGELOG
289  
290 =over 4
291  
292  
293 =item 0.1
294  
295 Initial release
296  
297 =back
298  
299 =head1 SEE ALSO
300  
301 See L<Net::Google::SafeBrowsing3> for handling Google Safe Browsing v3.
302  
303 See L<Net::Google::SafeBrowsing3::Storage> for the list of public functions.
304  
305 See L<Net::Google::SafeBrowsing3::MySQL> for a back-end using Sqlite.
306  
307 Google Safe Browsing v3 API: L<https://developers.google.com/safe-browsing/developers_guide_v3>
308  
309  
310 =head1 AUTHOR
311  
312 Julien Sobrier, E<lt>julien@sobrier.netE<gt>
313  
314 =head1 COPYRIGHT AND LICENSE
315  
316 Copyright (C) 2015 by Julien Sobrier
317  
318 This library is free software; you can redistribute it and/or modify
319 it under the same terms as Perl itself, either Perl version 5.8.8 or,
320 at your option, any later version of Perl 5 you may have available.
321  
322  
323 =cut
324  
325 1;