SafeBrowsing3/MySQL.pm untested

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