lib/Mail/Decency/Helper/Database/DBD.pm

filter: Statementstmt 57.2% 83 / 145 Branchbran 37.5% 15 / 40 Conditioncond 36.0% 9 / 25 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 76.4% 13 / 17 Podpod 81.8% 9 / 11 totaltotal 50.9% 129 / 253 CC 33 SCAR 32.8
CC
33
Cov
51%
setup
110.0
increment
12.0
search
7.4
CRAP 26.6
« lib/Mail/Decency/ContentFilter/SpamAssassin.pm ↑ Summary lib/Mail/Decency/ContentFilter/Core/User.pm »
1 package Mail::Decency::Helper::Database::DBD;
2  
3 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 extends 'Mail::Decency::Helper::Database';
5 1 ▶use mro 'c3';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use version 0.74; our $VERSION = qv( "v0.1.4" );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use DBIx::Connector;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use SQL::Abstract;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 has db => ( is => "ro", isa => "DBIx::Connector" );
15 1 has sql => ( is => "ro", isa => "SQL::Abstract" );
16 1 has args => ( is => "ro", isa => "ArrayRef", required => 1 );
17  
18 1 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=7.0
Pod undocumented
19 1 my ( $self ) = @_;
20
21 1 $self->{ sql } = SQL::Abstract->new();
22
23 1 eval {
24 # connect via fork save connector
25 1 my $dbh = DBIx::Connector->new( @{ $self->args }, { RaiseError => 1, PrintError => 0 } );
26
27 # use abstracted api
28 1 $self->{ db } = $dbh;
29 };
30 1 ▶ die "Error creating DBD insances: $@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
31
32 1 return $self;
33 }
34  
35  
36 =head2 search
37  
38 Search in database for a key
39  
40 CAUTION: use with care. Always provide ALL search keys, not only one of a kind!
41  
42 =cut
43  
44 4 ▶sub search {
Subroutine search: called CC=7 SCAR=20.1
Pod documented
45 4 my ( $self, $schema, $table, $search_ref, $no_lock ) = @_;
46
47 4 ▶ $self->read_lock unless $no_lock; # accquire semaphore
Branch: 1 branch
BranchTrueFalseTotal
unless $no_lock 3 1 4
48 4 $search_ref = $self->update_query( $search_ref );
49
50 4 my $sth;
51 4 eval {
52 4 my ( $stm, @bind ) = $self->sql->select( "${schema}_${table}" => [ '*' ], $search_ref );
53 4 $sth = $self->db->dbh->prepare_cached( $stm );
54 4 $sth->execute( @bind );
55 };
56 4 ▶ if ( $@ || $DBI::errstr ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or $DBI::errstr) 0 4 4
Condition33%1/3
$@ or $DBI::errstr
l!l&&r!l&&!r
004
MC/DC0%0/2
$@ or $DBI::errstr
$@ $DBI::errstr
Truth table33%1/3
A or B
A= $@B= $DBI::errstr
ABresult
1X1
011
000
57 0 $self->read_unlock; # release semaphore
58 0 die "!! DATABASE ERROR: $DBI::errstr !!\n";
59 }
60
61 4 my @res;
62 4 ▶ while ( my $res = $sth->fetchrow_hashref ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $res = $sth->fetchrow_hashref) 1 4 5
63 1 push @res, $res;
64 }
65
66 4 ▶ $self->read_unlock unless $no_lock; # release semaphore
Branch: 1 branch
BranchTrueFalseTotal
unless $no_lock 3 1 4
67 4 ▶ return wantarray ? @res : \@res;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4 0 4
68 }
69  
70  
71 =head2 search_read
72  
73 Returns read handle and read method name for massive read actions
74  
75 =cut
76  
77 0 ▶sub search_read {
Subroutine search_read: not called CC=2 SCAR=17.9
Pod documented
78 0 my ( $self, $schema, $table, $search_ref ) = @_;
79 0 $search_ref = $self->update_query( $search_ref );
80
81 0 my $sth;
82 0 eval {
83 0 my ( $stm, @bind ) = $self->sql->select( "${schema}_${table}" => [ '*' ], $search_ref );
84 0 $sth = $self->db->dbh->prepare_cached( $stm );
85 0 $sth->execute( @bind );
86 };
87 0 ▶ die "Database error: $DBI::errstr\n" if $DBI::errstr;
Branch: 1 branch
BranchTrueFalseTotal
if $DBI::errstr 0 0 0
88
89 0 return ( $sth, 'fetchrow_hashref' );
90 }
91  
92  
93 =head2 get
94  
95 Search in database for a key
96  
97 CAUTION: use with care. Always provide ALL search keys, not only one of a kind!
98  
99 =cut
100  
101 4 ▶sub get {
Subroutine get: called CC=1 SCAR=0.0
Pod documented
102 4 my ( $self, $schema, $table, $search_ref, $no_lock ) = @_;
103 4 $search_ref = $self->update_query( $search_ref );
104 4 my ( $ref ) = $self->search( $schema => $table => $search_ref, $no_lock );
105 4 my $res = $self->parse_data( $ref );
106 4 return $res;
107 }
108  
109  
110 =head2 set
111  
112 Getter method for BerkeleyDB::*
113  
114 CAUTION: use with care. Always provide ALL search keys, not only one of a kind!
115  
116 =cut
117  
118 1 ▶sub set {
Subroutine set: called CC=5 SCAR=17.6
Pod documented
119 1 my ( $self, $schema, $table, $search_ref, $data_ref ) = @_;
120 1 $self->write_lock; # accquire semaphore
121
122 1 $search_ref = $self->update_query( $search_ref );
123
124 1 ▶ $data_ref ||= $search_ref;
Condition33%1/3
$data_ref ||= $search_ref
l!l&&r!l&&!r
010
MC/DC0%0/2
$data_ref ||= $search_ref
$data_ref $search_ref
Truth table33%1/3
A ||= B
A= $data_refB= $search_ref
ABresult
1X1
011
000
125 1 $data_ref = $self->update_data( $data_ref );
126
127
128 # get existing ..
129 1 my $existing = $self->get( $schema => $table => $search_ref, 1 );
130
131 1 my ( $stm, @bind );
132
133 # update ..
134 1 ▶ if ( $existing ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($existing) { } 0 1 1
135 0 ( $stm, @bind ) = $self->sql->update( "${schema}_${table}" => $data_ref, $search_ref );
136 }
137
138 # insert ..
139 else {
140 1 ( $stm, @bind ) = $self->sql->insert( "${schema}_${table}" => { %$data_ref, %$search_ref } );
141 }
142
143
144 # exec ..
145 1 eval {
146 1 my $sth = $self->db->dbh->prepare( $stm );
147 1 $sth->execute( @bind );
148 #$self->db->dbh->commit;
149 };
150 1 ▶ if ( $@ || $DBI::errstr ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or $DBI::errstr) 0 1 1
Condition33%1/3
$@ or $DBI::errstr
l!l&&r!l&&!r
001
MC/DC0%0/2
$@ or $DBI::errstr
$@ $DBI::errstr
Truth table33%1/3
A or B
A= $@B= $DBI::errstr
ABresult
1X1
011
000
151 0 $self->write_unlock; # release semaphore
152 0 die "!! DATABASE ERROR: $DBI::errstr !!\n";
153 }
154
155 1 $self->write_unlock; # release semaphore
156
157 1 return;
158 }
159  
160  
161  
162  
163 =head2 increment
164  
165 Getter method for BerkeleyDB::*
166  
167 CAUTION: use with care. Always provide ALL search keys, not only one of a kind!
168  
169 =cut
170  
171  
172 0 ▶sub increment {
Subroutine increment: not called CC=3 SCAR=24.8
Pod documented
173 0 my ( $self, $schema, $table, $search_ref, $amount, $key ) = @_;
174 0 ▶ $key ||= 'data';
Condition0%0/2
$key ||= "data"
l!l
00
MC/DC0%0/1
$key ||= "data"
$key
Truth table0%0/2
A
A= $key
Aresult
11
00
175 0 ▶ $amount ||= 1;
Condition0%0/2
$amount ||= 1
l!l
00
MC/DC0%0/1
$amount ||= 1
$amount
Truth table0%0/2
A
A= $amount
Aresult
11
00
176
177 # lock for increment
178 0 $self->usr_lock;
179
180 # read (don't use the actual read, this won't be lock aware!
181 0 $search_ref = $self->update_query( $search_ref );
182 0 my ( $ref ) = $self->search( $schema => $table => $search_ref );
183 0 $ref = $self->parse_data( $ref );
184
185 # increment data
186 #print "$ref->{ $key } -> ";
187 0 $ref->{ $key } += $amount;
188 #print "$ref->{ $key }\n";
189
190 # write data (without locks)
191 0 $self->set( $schema => $table => $search_ref => $ref );
192
193 # unlock after increment
194 0 $self->usr_unlock;
195
196 0 return $ref->{ $key };
197 }
198  
199  
200  
201 =head2 remove
202  
203 Delete a key permanent
204  
205 =cut
206  
207 1 ▶sub remove {
Subroutine remove: called CC=3 SCAR=11.9
Pod documented
208 1 my ( $self, $schema, $table, $search_ref ) = @_;
209
210 1 $self->write_lock; # accquire semaphore
211
212 1 eval {
213 1 my ( $stm, @bind ) = $self->sql->delete( "${schema}_${table}" => $search_ref );
214 1 my $sth = $self->db->dbh->prepare( $stm );
215 1 $sth->execute( @bind );
216 };
217 1 ▶ if ( $@ || $DBI::errstr ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or $DBI::errstr) 0 1 1
Condition33%1/3
$@ or $DBI::errstr
l!l&&r!l&&!r
001
MC/DC0%0/2
$@ or $DBI::errstr
$@ $DBI::errstr
Truth table33%1/3
A or B
A= $@B= $DBI::errstr
ABresult
1X1
011
000
218 0 $self->write_unlock; # release semaphore
219 0 die "Error in remove: $DBI::errstr\n";
220 };
221
222 1 $self->write_unlock; # release semaphore
223
224 1 return;
225 }
226  
227  
228 =head2 ping
229  
230 Check wheter schema/table exists
231  
232 =cut
233  
234 0 ▶sub ping {
Subroutine ping: not called CC=2 SCAR=17.9
Pod documented
235 0 my ( $self, $schema, $table ) = @_;
236
237 0 my ( $stm, @bind ) = $self->sql->select( "${schema}_${table}" => [ 'COUNT( id )' ] );
238 0 $self->db->dbh->{ PrintError } = 0;
239
240 0 eval {
241 0 my $sth = $self->db->dbh->prepare( $stm );
242 0 ▶ if ( $sth ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sth) 0 0 0
243 0 $sth->execute;
244 0 my ( $amount ) = $sth->fetchrow_array;
245 }
246 };
247
248 0 return ! $DBI::errstr;
249 }
250  
251  
252 =head2 create
253  
254 Create database
255  
256 So far supported:
257 Any database supporting VARCHAR, BLOB and INTEGER
258  
259 =cut
260  
261 0 ▶sub setup {
Subroutine setup: not called CC=10 SCAR=47.0
Pod undocumented
262 0 my ( $self, $schema, $table, $columns_ref, $execute ) = @_;
263
264 0 my ( @columns, @indices, @uniques ) = ();
265 0 ▶ while( my ( $name, $ref ) = each %$columns_ref ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$columns_ref) 0 0 0
266 0 ▶ if ( $name eq '-index' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($name eq '-index') { } 0 0 0
elsif ($name eq '-unique') { } 0 0 0
267 0 my $idx = join( "_", @{ $columns_ref->{ -index } } );
268 push @indices, [
269 "${schema}_${table}_${idx} ON ${schema}_${table}",
270 $columns_ref->{ -index }
271 0 ];
272 }
273 elsif ( $name eq '-unique' ) {
274 0 my $idx = join( "_", @{ $columns_ref->{ -unique } } );
275 push @uniques, [
276 "${schema}_${table}_${idx} ON ${schema}_${table}",
277 $columns_ref->{ -unique }
278 0 ];
279 }
280 else {
281 0 ▶ my $type = ref( $ref ) eq 'ARRAY'
Branch: 2 branches
BranchTrueFalseTotal
ref $ref eq 'ARRAY' ? : 0 0 0
$ref->$#* == 0 ? : 0 0 0
282 ? ( $#$ref == 0
283 ? $ref->[0]
284 : "$ref->[0]($ref->[1])"
285 )
286 : $ref
287 ;
288 0 push @columns, "$name $type";
289 }
290 }
291 0 push @columns, "id INTEGER PRIMARY KEY";
292
293 0 my @stm;
294
295 0 push @stm, scalar $self->sql->generate(
296 'create table', "${schema}_${table}" => \@columns );
297
298 push @stm, scalar $self->sql->generate(
299 'create index', $_->[0] => $_->[1] )
300 0 for @indices;
301
302 push @stm, scalar $self->sql->generate(
303 'create unique index', $_->[0] => $_->[1] )
304 0 for @uniques;
305
306 0 ▶ unless ( $execute ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($execute) { } 0 0 0
307 0 print join( "\n",
308 "-- TABLE: ${schema}_${table} (SQLITE):",
309 join( ";\n", @stm ),
310 ). ";\n";
311 0 return 0;
312 }
313 else {
314 0 foreach my $stm( @stm ) {
315 0 $self->db->dbh->do( $stm );
316 }
317 0 return 1;
318 }
319 }
320  
321  
322  
323  
324 =head2 update_data
325  
326 Update input data for write
327  
328 Transforms any complex "data" key into YAML
329  
330 =cut
331  
332 1 ▶sub update_data {
Subroutine update_data: called CC=4 SCAR=16.4
Pod documented
333 1 my ( $self, $data_ref ) = @_;
334 1 $data_ref = $self->next::method( $data_ref );
335 1 ▶ if ( defined $data_ref->{ data } && ref( $data_ref->{ data } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data_ref->{"data"} and ref $data_ref->{"data"}) 0 1 1
Condition33%1/3
defined $data_ref->{"data"} and ref $data_ref->{"data"}
!ll&&!rl&&r
100
MC/DC0%0/2
defined $data_ref->{"data"} and ref $data_ref->{"data"}
defined $data_ref->{"data"} ref $data_ref->{"data"}
Truth table33%1/3
A and B
A= defined $data_ref->{"data"}B= ref $data_ref->{"data"}
ABresult
0X0
100
111
336 0 $data_ref->{ data } = YAML::Dump( $data_ref->{ data } );
337 }
338 1 ▶ return wantarray ? ( $data_ref->{ data } ) : $data_ref;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 1 1
339 }
340  
341 =head2 parse_data
342  
343 Parse data after read. Parses any YAML data in "data" key into perl object
344  
345 =cut
346  
347 4 ▶sub parse_data {
Subroutine parse_data: called CC=4 SCAR=15.5
Pod documented
348 4 my ( $self, $data_ref ) = @_;
349 4 $data_ref = $self->next::method( $data_ref );
350 4 ▶ if ( $data_ref && ref( $data_ref ) && defined $data_ref->{ data } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data_ref and ref $data_ref and defined $data_ref->{"data"}) 0 4 4
Condition66%4/6
$data_ref and ref $data_ref and defined $data_ref->{"data"}
!ll&&!rl&&r
310
$data_ref and ref $data_ref
!ll&&!rl&&r
301
MC/DC0%0/3
$data_ref and ref $data_ref and defined $data_ref->{"data"}
$data_ref ref $data_ref defined $data_ref->{"data"}
Truth table50%2/4
A and B and C
A= $data_refB= ref $data_refC= defined $data_ref->{"data"}
ABCresult
0XX0
10X0
1100
1111
351 0 eval {
352 0 $data_ref->{ data } = YAML::Load( $data_ref->{ data } );
353 };
354 }
355 4 return $data_ref;
356 }
357  
358  
359  
360 =head1 AUTHOR
361  
362 Ulrich Kautz <uk@fortrabbit.de>
363  
364 =head1 COPYRIGHT
365  
366 Copyright (c) 2010 the L</AUTHOR> as listed above
367  
368 =head1 LICENCSE
369  
370 This library is free software and may be distributed under the same terms as perl itself.
371  
372 =cut
373  
374  
375  
376 1 1;
« lib/Mail/Decency/ContentFilter/SpamAssassin.pm ↑ Summary lib/Mail/Decency/ContentFilter/Core/User.pm »