Filter/Storage/DBI.pm untested

filter: Statementstmt 0.0% 0 / 123 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 171 CC 6 SCAR 16.6
CC
6
Cov
0%
create_table
12.0
save
12.0
dbh
6.0
CRAP 5.2
1 package List::Filter::Storage::DBI;
2 use base qw( List::Filter::StorageBase );
3  
4 =head1 NAME
5  
6 List::Filter::Storage::DBI - filter storage via DBI
7  
8 =head1 SYNOPSIS
9  
10 # This is a plugin, not intended for direct use.
11 # See: List:Filter:Storage
12  
13 use List::Filter::Storage::DBI;
14 my $lfps_dbi = List::Filter::Storage::DBI->new(
15 type => 'filter',
16 connect_to => $connect_to,
17 owner => $owner,
18 password => $password,
19 );
20  
21 =head1 DESCRIPTION
22  
23 This is a general, database-neutral (or so I hope) storage plugin
24 that should allow for reading and writing List::Filter filters
25 from any database with a DBI driver.
26  
27 Note that in the event that database-specific code is needed,
28 A specific plugin that inherits from this one can be written:
29 the List::Filter system should find it automatically, if it's
30 named in the standard way (ala DBD::*). E.g. a postgresql
31 driver would be
32  
33 List::Filter::Storage::DBI::Pg
34  
35 =head2 METHODS
36  
37 =cut
38  
39 use 5.8.0;
40 use strict;
41 use warnings;
42 use Carp;
43 use Data::Dumper;
44 use Hash::Util qw( lock_keys unlock_keys );
45  
46 our $VERSION = '0.01';
47  
48 =head2 initialization code
49  
50 =over
51  
52 =item new
53  
54 Takes an optional hashref as an argument, with named fields
55 identical to the names of the object attributes.
56  
57 With no arguments, the newly created filter will be empty.
58  
59 =cut
60  
61 # Note: new is inherited from Class::Base.
62 # It calls the following "init" routine automatically.
63  
64 =item init
65  
66 Initialize object attributes and then lock them down to prevent
67 accidental creation of new ones.
68  
69 Note: there is no leading underscore on name "init", though it's
70 arguably an "internal" routine (i.e. not likely to be of use to
71 client code).
72  
73 =cut
74  
75 sub init {
76 my $self = shift;
77 my $args = shift;
78 unlock_keys( %{ $self } );
79  
80 $self->SUPER::init( $args ); # uncomment if this is a child class
81  
82 # Ensure that filter table exists in the database
83 $self->create_filter_table;
84  
85 lock_keys( %{ $self } );
86 return $self;
87 }
88  
89  
90 =item create_filter_table
91  
92 Creates the standard storage tables for this object's type.
93  
94 =cut
95  
96 sub create_filter_table {
97 my $self = shift;
98 my $table = $self->type;
99  
100 my $sql =<<"ENDSKULL";
101 CREATE TABLE $table (
102 id INTEGER PRIMARY KEY,
103 name TEXT UNIQUE,
104 terms TEXT, -- more portable than "BLOB"?
105 method TEXT,
106 description TEXT,
107 modifiers TEXT
108 );
109 ENDSKULL
110  
111 $sql .= 'CREATE INDEX idx_' . $table . "_name ON $table (name)\n";
112  
113 $self->create_table( $sql );
114  
115 return $self;
116 }
117  
118  
119 =item create_table
120  
121 Takes a block of sql as an argument, which is expected to contain a
122 CREATE TABLE statement. Tries to run the sql, but does not object if
123 the table already exists already. However, this method "croaks" on
124 any other error.
125  
126 Note: It tries to create the table, and traps the error if it
127 exists already. This is more portable -- to my knowledge --
128 than trying to get a listing of existing tables.
129  
130 =cut
131  
132 sub create_table {
133 my $self = shift;
134 my $create_sql = shift;
135  
136 $self->debug("sql: \n$create_sql");
137  
138 my $dbh = $self->dbh;
139  
140  
141 { # switch off RaiseError setting temporarily
142 local $dbh->{RaiseError};
143 local $dbh->{PrintError};
144  
145 # attempt to create table, ignoring error if it exists already.
146 eval {
147 $dbh->do( $create_sql )
148 };
149 if ( ($@) or (my $err = $dbh->err) ) { # note, with RaiseError off, "err" is tripped, but not $@
150 my $errstr = $dbh->errstr;
151  
152 # Expected values:
153 # err code: 1
154 # errstr: table filter already exists(1) [...]
155  
156 my $mess = "create_filter_table: err code: $err errstr: $errstr";
157  
158 # ignore only the expected error message
159 if ( not( (
160 ( $err eq 1 ) &&
161 ( $errstr =~ m{ table \s+ (.*?) \s+ already \s+ exists }x )
162 )
163 ) ) {
164 croak "Error: $mess";
165  
166 $self->debug( "Warning: $mess" );
167 }
168 }
169 }
170 return $self;
171 }
172  
173 =item init_dbh
174  
175 Initializes database connection.
176  
177 =cut
178  
179 sub init_dbh {
180 my $self = shift;
181  
182 my $connect_to = $self->connect_to;
183 my $owner = $self->owner;
184 my $password = $self->password;
185 my $attributes = $self->attributes;
186  
187 my $dbh = DBI->connect($connect_to, $owner, $password, $attributes)
188 or croak "Database connection failed: $connect_to";
189  
190 $self->set_dbh( $dbh );
191 return $dbh;
192 }
193  
194 =head2 main methods
195  
196 =over
197  
198 =item lookup
199  
200 =cut
201  
202 sub lookup {
203 my $self = shift;
204 my $name = shift;
205  
206 my $table = $self->type;
207 my $sql = "SELECT terms, method, description, modifiers FROM $table WHERE name = ?";
208 my $dbh = $self->dbh;
209 my $sth = $dbh->prepare($sql);
210 $sth->execute( $name );
211 my $row = $sth->fetchrow_arrayref;
212  
213 my $terms_serialized = $row->[0];
214 my $method = $row->[1];
215 my $description = $row->[2];
216 my $modifiers = $row->[3];
217  
218 # Unpacking aref serialized with Data::Dumper->Dump
219 my $terms;
220 eval $terms_serialized;
221  
222 # create a filter object
223 my $filter_class = $self->define_filter_class;
224 my $filter = $filter_class->new(
225 { name => $name,
226 terms => $terms,
227 method => $method,
228 description => $description,
229 modifiers => $modifiers,
230 } );
231  
232 return $filter;
233 }
234  
235 =item save
236  
237 Saves the given filter object to the database.
238  
239 Refuses to save filters named with a leading underscore.
240  
241 Returns the filter on success, otherwise undef.
242  
243 =cut
244  
245 sub save {
246 my $self = shift;
247 my $filter = shift;
248  
249 my $dbh = $self->dbh;
250  
251 my $name = $filter->name;
252  
253 if ($name =~ m{^_}x) {
254 return;
255 } else {
256 $self->debug("Refusing to save filter named with leading underscore: $name");
257 }
258  
259 my $method = $filter->method;
260 my $description = $filter->description;
261 my $modifiers = $filter->modifiers;
262 my $terms = $filter->terms;
263  
264 my $terms_serialized
265 = Data::Dumper->Dump( [$terms], ['terms'] );
266  
267 my $table = $self->type;
268  
269 my $sql =
270 "INSERT INTO $table (name, terms, method, description, modifiers)
271 VALUES (?, ?, ?, ?, ?)";
272  
273 my $sth = $dbh->prepare( $sql );
274 $sth->execute( $name,
275 $terms_serialized,
276 $method,
277 $description,
278 $modifiers );
279  
280 if ($dbh->err) {
281 my $errstr = $dbh->errstr;
282 carp "save: problem on INSERT: $errstr "
283 }
284  
285 return $filter;
286 }
287  
288  
289 =item list_filters
290  
291 Returns a list of all avaliable named filters.
292  
293 =cut
294  
295 sub list_filters {
296 my $self = shift;
297  
298 my $table = $self->type;
299 my $sql = "SELECT name FROM $table";
300 my $dbh = $self->dbh;
301 my $sth = $dbh->prepare($sql);
302 $sth->execute();
303 my $row = $sth->fetchrow_arrayref;
304  
305 return $row;
306 }
307  
308  
309  
310 =back
311  
312 =head2 special accessors (access the "extra" namespace)
313  
314 =over
315  
316 =item dbh
317  
318 Getter for object attribute dbh
319  
320 When called for the first time, initiates database connection.
321  
322 =cut
323  
324 sub dbh {
325 my $self = shift;
326 my $dbh = $self->extra->{ dbh };
327  
328 if ( not( $dbh ) ) {
329 $dbh = $self->init_dbh;
330 }
331  
332 return $dbh;
333 }
334  
335 =item set_dbh
336  
337 Setter for object attribute set_dbh
338  
339 =cut
340  
341 sub set_dbh {
342 my $self = shift;
343 my $dbh = shift;
344 $self->extra->{ dbh } = $dbh;
345 return $dbh;
346 }
347  
348 1;
349  
350 =back
351  
352 =head2 basic accessors (defined in List::Filter::Storage);
353  
354 =over
355  
356 =item connect_to
357  
358 Getter for object attribute connect_to
359  
360 =item set_connect_to
361  
362 Setter for object attribute set_connect_to
363  
364 =item owner
365  
366 Getter for object attribute owner
367  
368 =cut
369  
370 =item set_owner
371  
372 Setter for object attribute set_owner
373  
374 =cut
375  
376 =item password
377  
378 Getter for object attribute password
379  
380 =cut
381  
382 =item set_password
383  
384 Setter for object attribute set_password
385  
386 =cut
387  
388 =item attributes
389  
390 Getter for object attribute attributes
391  
392 =item set_attributes
393  
394 Setter for object attribute set_attributes
395  
396 =item extra
397  
398 Getter for object attribute extra
399  
400 =item set_extra
401  
402 Setter for object attribute set_extra
403  
404 =back
405  
406 =head2 Design Notes
407  
408 This single table schema isn't an efficient use of an RDMS,
409 but the needs here are relatively simple, and this should
410 be fairly portable.
411  
412 The primary simplification used here is to serialize the array
413 of terms and store it in a single text field.
414  
415 Data::Dumper has been used for serialization (in preference to
416 Storable) to improve the readability of the database.
417  
418 =head1 SEE ALSO
419  
420 L<List::Filter::Project>
421 L<List:Filter:Storage>
422 L<List::Filter>
423  
424 =head1 AUTHOR
425  
426 Joseph Brenner, E<lt>doom@kzsu.stanford.eduE<gt>,
427 18 May 2007
428  
429 =head1 COPYRIGHT AND LICENSE
430  
431 Copyright (C) 2007 by Joseph Brenner
432  
433 This library is free software; you can redistribute it and/or modify
434 it under the same terms as Perl itself, either Perl version 5.8.2 or,
435 at your option, any later version of Perl 5 you may have available.
436  
437 =head1 BUGS
438  
439 None reported... yet.
440  
441 =cut