blib/lib/LARC/DB.pm

filter: Statementstmt 7.6% 14 / 184 Branchbran 0.0% 0 / 66 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 30.7% 4 / 13 Podpod 100% 9 / 9 totaltotal 9.9% 27 / 272 CC 34 SCAR 36.5
CC
34
Cov
6%
new
56.0
list
56.0
newdb
56.0
CRAP 38.3
↑ Summary
1 package LARC::DB;
2  
3 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 LARC::DB - Provides a methode for storing SQLite DBs in a pleasantly organized manner.
11  
12 =head1 VERSION
13  
14 Version 0.0.0
15  
16 =cut
17  
18 1 our $VERSION = '0.0.0';
19  
20  
21 =head1 SYNOPSIS
22  
23 use LARC::DB;
24  
25 my $ldb = LARC::DB->new();
26  
27 =head1 FUNCTIONS
28  
29 =head2 new
30  
31 Initializes the module. No arguements are taken. No arguements are required.
32  
33 my $ldb = LARC::DB->new();
34  
35 =cut
36  
37 0 ▶sub new {
Subroutine new: not called CC=7 SCAR=40.3
Pod documented
38 0 my %args;
39 0 ▶ if(defined($_[1])){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1]) 0 0 0
40 0 %args= %{$_[1]};
41 }
42  
43 0 ▶ if (!defined($args{app})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"app"}) 0 0 0
44 0 $args{app}='';
45 }
46  
47 0 ▶ if (!defined($args{version})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"version"}) 0 0 0
48 0 $args{version}='';
49 }
50  
51 0 my $self={error=>undef, errorString=>''};
52 0 bless $self;
53  
54 #makes the $ENV{HOME} is defined
55 0 ▶ if (!defined($ENV{HOME})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ENV{"HOME"}) 0 0 0
56 0 warn('LARC-DB new:2: The enviromental variable "HOME" is not defined');
57 0 $self->{error}=2;
58 0 $self->{errorString}='The enviromental variable "HOME" is not defined';
59 0 return $self;
60 }
61  
62 0 $self->{base}=$ENV{HOME}."/larc/DB/";
63  
64 #checks to see if it needs inited or not
65 0 ▶ if (! -e $self->{base}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -e $self->{'base'}) { } 0 0 0
66 0 $self->{init}=0;
67 }else {
68 #makes sure $self->{base} is a directory
69 0 ▶ if (! -d $self->{base}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -d $self->{'base'}) { } 0 0 0
70 0 warn('LARC-DB new:1: "'.$self->{base}.'" is not a directory');
71 0 $self->{error}=1;
72 0 $self->{errorString}='"'.$self->{base}.'" is not a directory';
73 }else {
74 #init is good if it is a directory
75 0 $self->{init}=1;
76 }
77 }
78  
79 0 return $self;
80 }
81  
82 =head2 connect
83  
84 This generates to a SQLite DB connecting and returns a DBI object.
85  
86 my $dbh=$ldb->connect('some/DB');
87 if($ldb->{error}){
88 print "ERROR!";
89 }
90  
91 =cut
92  
93 0 ▶sub connect{
Subroutine connect: not called CC=3 SCAR=24.8
Pod documented
94 0 my $self=$_[0];
95 0 my $db=$_[1];
96  
97 0 $self->errorBlank;
98  
99 0 ▶ if (!$self->validname($db)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->validname($db)) 0 0 0
100 0 warn('LARC-DB connect:12: "'.$db.'" is a invalid name');
101 0 $self->{error}=12;
102 0 $self->{errorString}='"'.$db.'" is a invalid name';
103 0 return undef;
104 }
105  
106 0 my $dbfile=$self->{base}.$db.'.sqlite';
107  
108 0 ▶ if (! -e $dbfile) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $dbfile) 0 0 0
109 0 warn('LARC-DB connect:4: "'.$self->{base}.$db.'.sqlite" does not exist. '.
110 'Thus connecting to "'.$db.'" is not possible');
111 0 $self->{error}=4;
112 0 $self->{errorString}='"'.$self->{base}.$db.'.sqlite" does not exist. '.
113 'Thus connecting to "'.$db.'" is not possible';
114 0 return undef;
115 }
116  
117 0 my $dbh = DBI->connect("dbi:SQLite:dbname=".$dbfile,"","");
118  
119 0 return $dbh;
120 }
121  
122 =head2 DBexists
123  
124 Checks if a database exists or not. One one option is accepted and
125 that is the DB name.
126  
127 my $returned=$ldb->DBexists('foo/bar');
128 if($ldb->{error}){
129 print 'Error:'.$ldb->{error}.':'.$error->{errorString};
130 }
131 if($returned){
132 print 'It exists';
133 }
134  
135 =cut
136  
137 0 ▶sub DBexists{
Subroutine DBexists: not called CC=3 SCAR=24.8
Pod documented
138 0 my $self=$_[0];
139 0 my $db=$_[1];
140  
141 0 $self->errorBlank;
142  
143 0 ▶ if (!$self->validname($db)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->validname($db)) 0 0 0
144 0 warn('LARC-DB connect:12: "'.$db.'" is a invalid name');
145 0 $self->{error}=12;
146 0 $self->{errorString}='"'.$db.'" is a invalid name';
147 0 return undef;
148 }
149  
150 0 my $dbfile=$self->{base}.$db.'.sqlite';
151  
152 #return if it does not exist
153 0 ▶ if (! -e $dbfile) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $dbfile) 0 0 0
154 0 return undef;
155 }
156  
157 #it exists
158 0 return 1;
159 }
160  
161 =head2 init
162  
163 This initiliazes the support stuff for it all. It currently just creates
164 '~/larc/DB/' if needed.
165  
166 $ldb->init();
167 if($ldb->{error}){
168 print "ERROR!";
169 }
170  
171 =cut
172  
173 0 ▶sub init{
Subroutine init: not called CC=3 SCAR=24.8
Pod documented
174 0 my $self=$_[0];
175  
176 0 $self->errorBlank;
177  
178 #create the larc directory
179 0 ▶ if (!mkdir($ENV{HOME}.'/larc/')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (mkdir $ENV{"HOME"} . '/larc/') 0 0 0
180 0 warn('LARC-DB init:3: Failed to create "~/larc/"');
181 0 $self->{error}=3;
182 0 $self->{errorString}='Failed to create "~/larc/"';
183 0 return undef;
184 }
185  
186 #create the SQLite storage directory
187 0 ▶ if (!mkdir($ENV{HOME}.'/larc/DB')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (mkdir $ENV{"HOME"} . '/larc/DB') 0 0 0
188 0 warn('LARC-DB init:3: Failed to create "~/larc/DB"');
189 0 $self->{error}=3;
190 0 $self->{errorString}='Failed to create "~/larc/DB"';
191 0 return undef;
192 }
193  
194 0 return 1;
195 }
196  
197 =head2 list
198  
199 List DBs under a specific path. The returned value is an array.
200  
201 Any thing ending in '/' is a directory. If something is both a
202 directory and DB, both an entry for the DB and directory is listed.
203  
204 Any thing beginning with an '.' is not returned.
205  
206 my @DBs=$ldb->list('some/');
207 if($ldb->{error}){
208 print "ERROR!";
209 }
210  
211 =cut
212  
213 0 ▶sub list{
Subroutine list: not called CC=7 SCAR=40.3
Pod documented
214 0 my $self=$_[0];
215 0 my $db=$_[1];
216  
217 #makes sure it is a valid name
218 0 ▶ if (!$self->validname($db)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->validname($db)) 0 0 0
219 0 warn('LARC-DB list:12: "'.$db.'" is a invalid name');
220 0 $self->{error}=12;
221 0 $self->{errorString}='"'.$db.'" is a invalid name';
222 0 return undef;
223 }
224  
225 0 $self->errorBlank;
226  
227 0 my $dbpath=$self->{base}.$db;
228  
229 #errors if it is just a DB or does not exist
230 0 ▶ if (! -d) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $_) 0 0 0
231 0 warn('LARC-DB list:11: The specified, "'.$dbpath
232 .'", is either just a DB or does not exist');
233 0 $self->{error}=11;
234 0 $self->{errorString}='The specified, "'.$dbpath
235 .'", is either just a DB or does not exist.';
236 0 return undef;
237 }
238  
239 #opens the dir for latter use by readdir
240 0 ▶ if (opendir(DBDIR, $dbpath)){
Branch: 1 branch
BranchTrueFalseTotal
if (opendir DBDIR, $dbpath) 0 0 0
241 0 warn('LARC-DB list:13: opendir failed for "'.$dbpath.'"');
242 0 $self->{error}=13;
243 0 $self->{errorString}='opendir failed for "'.$dbpath.'"';
244 0 return undef;
245 }
246  
247 #reads it and removes any thing starting with a period
248 0 my @dir=grep(!/^\./, readdir(DBDIR));
249  
250 #process @dir
251 0 my $dirInt=0;
252 0 my @return;
253 0 ▶ while (defined($dir[$dirInt])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dir[$dirInt]) 0 0 0
254 #only adds it if it is not a dot file
255 0 ▶ if (! $dir[$dirInt] =~ /^\./ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (!$dir[$dirInt] =~ /^\./) 0 0 0
256 #if it is a directory add it to the return array with / appended
257 #if not, just add it
258 0 ▶ if(-d $dbpath.'/'.$dir[$dirInt]){
Branch: 1 branch
BranchTrueFalseTotal
if (-d $dbpath . '/' . $dir[$dirInt]) { } 0 0 0
259 0 push(@return, $dir[$dirInt].'/');
260 }else {
261 0 push(@return, $dir[$dirInt]);
262 }
263 }
264 0 $dirInt++;
265 }
266  
267 0 return @return;
268 }
269  
270 =head2 newdb
271  
272 Creates a new DB.
273  
274 $ldb->newdb('some/DB');
275 if($ldb->{error}){
276 print "ERROR!";
277 }
278  
279 =cut
280  
281 0 ▶sub newdb{
Subroutine newdb: not called CC=7 SCAR=40.3
Pod documented
282 0 my $self=$_[0];
283 0 my $db=$_[1];
284  
285 #makes sure it is a valid name
286 0 ▶ if (!$self->validname($db)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->validname($db)) 0 0 0
287 0 warn('LARC-DB newdb:12: "'.$db.'" is a invalid name');
288 0 $self->{error}=12;
289 0 $self->{errorString}='"'.$db.'" is a invalid name';
290 0 return undef;
291 }
292  
293 0 $self->errorBlank;
294  
295 0 my $dbpath=$self->{base}.$db;
296 0 my $dbfile=$dbpath.'.sqlite';
297  
298 0 ▶ if ($dbpath =~ /\/$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbpath =~ m[/$]) 0 0 0
299 #make sure the directory does not already exist
300 0 ▶ if ( -e $dbpath) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $dbpath) 0 0 0
301 0 warn('LARC-DB newdb:4: "'.$dbpath.'" already exists');
302 0 $self->{error}=6;
303 0 $self->{errorString}='"'.$dbpath.'" already exists.';
304 0 return undef;
305 }
306
307 #error if creating it fails
308 0 ▶ if (!mkpath($dbpath)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (mkpath($dbpath)) 0 0 0
309 0 warn('LARC-DB newdb:10: Make path failed for "'.$dbpath.'"');
310 0 $self->{error}=10;
311 0 $self->{errorString}='Mkpath failed for "'.$dbpath.'".';
312 0 return undef;
313 }
314  
315 0 return 1;
316 }
317  
318 #makes sure it does not already exist
319 0 ▶ if ( -e $dbfile) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $dbfile) 0 0 0
320 0 warn('LARC-DB newdb:4: "'.$dbfile.'" already exists');
321 0 $self->{error}=6;
322 0 $self->{errorString}='"'.$dbfile.'" already exists.';
323 0 return undef;
324 }
325  
326 #this will create the new one
327 0 ▶ if (!DBI->connect("dbi:SQLite:dbname=".$dbfile,"","")){
Branch: 1 branch
BranchTrueFalseTotal
unless ("DBI"->connect('dbi:SQLite:dbname=' . $dbfile, "", "")) 0 0 0
328 0 warn('LARC-DB newdb:5: Fa')
329 }
330  
331 0 return 1;
332 }
333  
334 =head2 rmdb
335  
336 Removes a DB.
337  
338 $ldb->rmdb('some/DB');
339 if($ldb->{error}){
340 print "ERROR!";
341 }
342  
343 =cut
344  
345 0 ▶sub rmdb{
Subroutine rmdb: not called CC=7 SCAR=40.3
Pod documented
346 0 my $self=$_[0];
347 0 my $db=$_[1];
348  
349 #makes sure it is a valid name
350 0 ▶ if (!$self->validname($db)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->validname($db)) 0 0 0
351 0 warn('LARC-DB list:12: "'.$db.'" is a invalid name');
352 0 $self->{error}=12;
353 0 $self->{errorString}='"'.$db.'" is a invalid name';
354 0 return undef;
355 }
356  
357 0 $self->errorBlank;
358  
359 0 my $dbpath=$self->{base}.$db;
360 0 my $dbfile=$dbpath.'.sqlite';
361  
362 #removes it if a dir is specified
363 0 ▶ if ($db =~ /\/$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($db =~ m[/$]) 0 0 0
364 #makes sure it exists
365 0 ▶ if (! -d $dbpath) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dbpath) 0 0 0
366 0 warn('LARC-DB rmdb:4: "'.$dbpath.'" does not exist');
367 0 $self->{error}=4;
368 0 $self->{errorString}='"'.$dbpath.'" does not exist.';
369 0 return undef;
370 }
371  
372 #makes is removed
373 0 ▶ if (!rmdir($dbpath)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (rmdir $dbpath) 0 0 0
374 0 warn('LARC-DB rmdb:9: Failed to unlink "'.$dbfile.'".');
375 0 $self->{error}=9;
376 0 $self->{errorString}='Failed to unlink "'.$dbfile.'".';
377 0 return undef;
378 }
379  
380 0 return 1;
381 }
382  
383 #makes sure it does not already exist
384 0 ▶ if (! -e $dbfile) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $dbfile) 0 0 0
385 0 warn('LARC-DB rmdb:4: "'.$dbfile.'" does not exist');
386 0 $self->{error}=4;
387 0 $self->{errorString}='"'.$dbfile.'" already exist.';
388 0 return undef;
389 }
390  
391 #try to remove it
392 0 ▶ if (!unlink($dbfile)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (unlink $dbfile) 0 0 0
393 0 warn('LARC-DB rmdb:8: Failed to unlink "'.$dbfile.'".');
394 0 $self->{error}=8;
395 0 $self->{errorString}='Failed to unlink "'.$dbfile.'".';
396 0 return undef;
397 }
398  
399 0 return 1;
400 }
401  
402 =head2 validname
403  
404 This checks if a DB name is valid or not.
405  
406 if($returned=$ldb->validname('some/DB')){
407 print 'Invalid name'.
408 }
409  
410 =cut
411  
412 0 ▶sub validname{
Subroutine validname: not called CC=4 SCAR=30.0
Pod documented
413 0 my $self=$_[0];
414 0 my $name=$_[1];
415  
416 #return if it is undef
417 0 ▶ if (!defined($name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 0 0 0
418 0 return undef;
419 }
420  
421 #return if it begins it matches /\/./
422 0 ▶ if ($name =~ /\/./) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ m[/.]) 0 0 0
423 0 return undef;
424 }
425  
426 #returns if it matches /^./
427 0 ▶ if ($name =~ /^./) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^./) 0 0 0
428 0 return undef;
429 }
430  
431 #it is valid
432 0 return 1;
433 }
434  
435 =head2 errorBlank
436  
437 This blanks the error storage and is only meant for internal usage.
438  
439 It does the following.
440  
441 $self->{error}=undef;
442 $self->{errorString}="";
443  
444 =cut
445  
446 #blanks the error flags
447 0 ▶sub errorBlank{
Subroutine errorBlank: not called CC=1 SCAR=6.9
Pod documented
448 0 my $self=$_[0];
449  
450 0 $self->{error}=undef;
451 0 $self->{errorString}="";
452  
453 0 return 1;
454 }
455  
456 =head1 STORAGE
457  
458 The base dir used is '$ENV{HOME}/larc/DB/'. The specified DB is then tacked onto
459 that as a path with '.sqlite' appended to the end. So the DB 'foo/bar' then becomes
460 '$ENV{HOME}/larc/DB/foo/bar.sqlite'. This allows a DB to have sub DBs in regards
461 to how the path looks.
462  
463 A DB may not begin with '.' or have that any were after an '/'. Thus the following are
464 all invalid.
465  
466 ./someDB
467 some/.DB
468 some/.something/DB
469  
470 =head1 ERROR CODES
471  
472 =head2 1
473  
474 The base directory, '~/larc/DB/', does exists, but is not a directory.
475  
476 =head2 2
477  
478 The enviromental variable 'HOME' is not defined.
479  
480 =head2 3
481  
482 Failed to create the directory '~/larc' or '~/larc/DB';
483  
484 =head2 4
485  
486 The database does not exist.
487  
488 =head2 5
489  
490 Failed to create the new SQLite file.
491  
492 =head2 6
493  
494 The DB already exists.
495  
496 =head2 7
497  
498 Reservered for future use.
499  
500 =head2 8
501  
502 Failed to unlink a DB file.
503  
504 =head2 9
505  
506 Failed to remove the specified directory.
507  
508 =head2 10
509  
510 Mkpath failed.
511  
512 =head2 11
513  
514 The specified DB is not also a directory.
515  
516 =head2 12
517  
518 Invalid DB name.
519  
520 =head2 13
521  
522 Opendir error.
523  
524 =head1 AUTHOR
525  
526 Zane C. Bowers, C<< <vvelox at vvelox.net> >>
527  
528 =head1 BUGS
529  
530 Please report any bugs or feature requests to C<bug-larc-db at rt.cpan.org>, or through
531 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=LARC-DB>. I will be notified, and then you'll
532 automatically be notified of progress on your bug as I make changes.
533  
534  
535  
536  
537 =head1 SUPPORT
538  
539 You can find documentation for this module with the perldoc command.
540  
541 perldoc LARC::DB
542  
543  
544 You can also look for information at:
545  
546 =over 4
547  
548 =item * RT: CPAN's request tracker
549  
550 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=LARC-DB>
551  
552 =item * AnnoCPAN: Annotated CPAN documentation
553  
554 L<http://annocpan.org/dist/LARC-DB>
555  
556 =item * CPAN Ratings
557  
558 L<http://cpanratings.perl.org/d/LARC-DB>
559  
560 =item * Search CPAN
561  
562 L<http://search.cpan.org/dist/LARC-DB>
563  
564 =back
565  
566  
567 =head1 ACKNOWLEDGEMENTS
568  
569  
570 =head1 COPYRIGHT & LICENSE
571  
572 Copyright 2008 Zane C. Bowers, all rights reserved.
573  
574 This program is free software; you can redistribute it and/or modify it
575 under the same terms as Perl itself.
576  
577  
578 =cut
579  
580 1 1; # End of LARC::DB
↑ Summary