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 CC 8 SCAR 26.1
CC
8
Cov
0%
init
42.0
add_chunks_s
6.0
add_chunks_a
6.0
CRAP 13.6
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 debug => 0,
76  
77 %args,
78 };
79  
80 bless $self, $class or croak "Can't bless $class: $!";
81  
82  
83 $self->init();
84  
85 return $self;
86 }
87  
88 =head1 PUBLIC FUNCTIONS
89  
90 =over 4
91  
92 See L<Net::Google::SafeBrowsing3::Storage> for a complete list of public functions.
93  
94 =back
95  
96 =head2 close()
97  
98 Cleanup old full hashes, and close the connection to the database.
99  
100 $storage->close();
101  
102 =cut
103  
104 sub init {
105 my ($self, %args) = @_;
106  
107 $self->{dbh} = DBI->connect("dbi:SQLite:dbname=" . $self->{file}, "", "");
108 $self->{dbh}->do("PRAGMA journal_mode = OFF");
109 $self->{dbh}->do("PRAGMA synchronous = OFF");
110 $self->{dbh}->do("PRAGMA cache_size = " . $self->{cache_size});
111  
112 my @tables = $self->{dbh}->tables;
113  
114 if (! defined first { $_ eq '"main"."updates"' || $_ eq '"updates"' } @tables) {
115 $self->create_table_updates();
116 }
117 if (! defined first { $_ eq '"main"."a_chunks"' || $_ eq '"a_chunks"' } @tables) {
118 $self->create_table_a_chunks();
119 }
120 if (! defined first { $_ eq '"main"."s_chunks"' || $_ eq '"s_chunks"' } @tables) {
121 $self->create_table_s_chunks();
122 }
123 if (! defined first { $_ eq '"main"."full_hashes"' || $_ eq '"full_hashes"' } @tables) {
124 $self->create_table_full_hashes();
125 }
126 if (! defined first { $_ eq '"main"."full_hashes_errors"' || $_ eq '"full_hashes_errors"' } @tables) {
127 $self->create_table_full_hashes_errors();
128 }
129 }
130  
131  
132 sub create_table_updates {
133 my ($self, %args) = @_;
134  
135 my $schema = qq{
136 CREATE TABLE updates (
137 last INTEGER DEFAULT 0,
138 wait INTEGER DEFAULT 1800,
139 errors INTEGER DEFAULT 0,
140 list TEXT
141 );
142 }; # Need to handle errors
143  
144 $self->{dbh}->do($schema);
145 }
146  
147 sub create_table_a_chunks {
148 my ($self, %args) = @_;
149  
150 my $schema = qq{
151 CREATE TABLE a_chunks (
152 prefix TEXT,
153 num INTEGER,
154 list TEXT
155 );
156 };
157  
158 $self->{dbh}->do($schema);
159  
160 my $index = qq{
161 CREATE INDEX a_chunks_num_list ON a_chunks (
162 num,
163 list
164 );
165 };
166 # $self->{dbh}->do($index);
167  
168 $index = qq{
169 CREATE UNIQUE INDEX a_chunks_unique ON a_chunks (
170 prefix,
171 num,
172 list
173 );
174 };
175 $self->{dbh}->do($index);
176 }
177  
178 sub create_table_s_chunks {
179 my ($self, %args) = @_;
180  
181 my $schema = qq{
182 CREATE TABLE s_chunks (
183 prefix TEXT,
184 num INTEGER,
185 add_num INTEGER,
186 list TEXT
187 );
188 };
189  
190 $self->{dbh}->do($schema);
191  
192 my $index = qq{
193 CREATE INDEX s_chunks_num ON s_chunks (
194 num
195 );
196 };
197 # $self->{dbh}->do($index);
198  
199 $index = qq{
200 CREATE UNIQUE INDEX s_chunks_unique ON s_chunks (
201 prefix,
202 num,
203 add_num,
204 list
205 );
206 };
207 $self->{dbh}->do($index);
208 }
209  
210 sub create_table_full_hashes {
211 my ($self, %args) = @_;
212  
213 my $schema = qq{
214 CREATE TABLE full_hashes (
215 id INTEGER PRIMARY KEY AUTOINCREMENT,
216 hash TEXT,
217 list TEXT,
218 end INTEGER,
219 type INTEGER
220 );
221 };
222  
223 $self->{dbh}->do($schema);
224  
225 my $index = qq{
226 CREATE UNIQUE INDEX hash ON full_hashes (
227 hash,
228 list
229 );
230 };
231 $self->{dbh}->do($index);
232 }
233  
234 sub create_table_full_hashes_errors {
235 my ($self, %args) = @_;
236  
237 my $schema = qq{
238 CREATE TABLE full_hashes_errors (
239 id INTEGER PRIMARY KEY AUTOINCREMENT,
240 errors INTEGER,
241 prefix TEXT,
242 timestamp INTEGER
243 );
244 };
245  
246 $self->{dbh}->do($schema);
247 }
248  
249  
250 sub add_chunks_s {
251 my ($self, %args) = @_;
252 my $chunknum = $args{chunknum} || 0;
253 my $chunks = $args{chunks} || [];
254 my $list = $args{'list'} || '';
255  
256 my $add = $self->{dbh}->prepare('INSERT OR IGNORE INTO s_chunks (prefix, num, add_num, list) VALUES (?, ?, ?, ?)');
257  
258 $self->{dbh}->begin_work;
259 foreach my $chunk (@$chunks) {
260 $add->execute( $chunk->{prefix}, $chunknum, $chunk->{add_chunknum}, $list );
261 }
262  
263 if (scalar @$chunks == 0) { # keep empty chunks
264 $add->execute( '', $chunknum, '', $list );
265 }
266 $self->{dbh}->commit;
267 }
268  
269 sub add_chunks_a {
270 my ($self, %args) = @_;
271 my $chunknum = $args{chunknum} || 0;
272 my $chunks = $args{chunks} || [];
273 my $list = $args{'list'} || '';
274  
275 my $add = $self->{dbh}->prepare('INSERT OR IGNORE INTO a_chunks (prefix, num, list) VALUES (?, ?, ?)');
276  
277 $self->{dbh}->begin_work;
278 foreach my $chunk (@$chunks) {
279 $add->execute( $chunk->{prefix}, $chunknum, $list );
280 }
281  
282 if (scalar @$chunks == 0) { # keep empty chunks
283 $add->execute( '', $chunknum, $list );
284 }
285 $self->{dbh}->commit;
286 }
287  
288  
289 =head1 CHANGELOG
290  
291 =over 4
292  
293  
294 =item 0.1
295  
296 Initial release
297  
298 =back
299  
300 =head1 SEE ALSO
301  
302 See L<Net::Google::SafeBrowsing3> for handling Google Safe Browsing v3.
303  
304 See L<Net::Google::SafeBrowsing3::Storage> for the list of public functions.
305  
306 See L<Net::Google::SafeBrowsing3::MySQL> for a back-end using Sqlite.
307  
308 Google Safe Browsing v3 API: L<https://developers.google.com/safe-browsing/developers_guide_v3>
309  
310  
311 =head1 AUTHOR
312  
313 Julien Sobrier, E<lt>julien@sobrier.netE<gt>
314  
315 =head1 COPYRIGHT AND LICENSE
316  
317 Copyright (C) 2015 by Julien Sobrier
318  
319 This library is free software; you can redistribute it and/or modify
320 it under the same terms as Perl itself, either Perl version 5.8.8 or,
321 at your option, any later version of Perl 5 you may have available.
322  
323  
324 =cut
325  
326 1;