File Coverage

blib/lib/LARC/DB.pm
Criterion Covered Total %
statement 12 182 6.5
branch 0 64 0.0
condition n/a
subroutine 4 13 30.7
pod 9 9 100.0
total 25 268 9.3


line stmt bran cond sub pod time code
1             package LARC::DB;
2              
3 1     1   20694 use DBI;
  1         16143  
  1         64  
4 1     1   12 use File::Path;
  1         3  
  1         63  
5 1     1   6 use warnings;
  1         6  
  1         38  
6 1     1   6 use strict;
  1         2  
  1         1633  
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             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             sub new {
38 0     0 1   my %args;
39 0 0         if(defined($_[1])){
40 0           %args= %{$_[1]};
  0            
41             }
42              
43 0 0         if (!defined($args{app})) {
44 0           $args{app}='';
45             }
46              
47 0 0         if (!defined($args{version})) {
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 0         if (!defined($ENV{HOME})) {
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 0         if (! -e $self->{base}) {
66 0           $self->{init}=0;
67             }else {
68             #makes sure $self->{base} is a directory
69 0 0         if (! -d $self->{base}) {
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             sub connect{
94 0     0 1   my $self=$_[0];
95 0           my $db=$_[1];
96              
97 0           $self->errorBlank;
98              
99 0 0         if (!$self->validname($db)) {
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 0         if (! -e $dbfile) {
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             sub DBexists{
138 0     0 1   my $self=$_[0];
139 0           my $db=$_[1];
140              
141 0           $self->errorBlank;
142              
143 0 0         if (!$self->validname($db)) {
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 0         if (! -e $dbfile) {
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             sub init{
174 0     0 1   my $self=$_[0];
175              
176 0           $self->errorBlank;
177              
178             #create the larc directory
179 0 0         if (!mkdir($ENV{HOME}.'/larc/')) {
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 0         if (!mkdir($ENV{HOME}.'/larc/DB')) {
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             sub list{
214 0     0 1   my $self=$_[0];
215 0           my $db=$_[1];
216              
217             #makes sure it is a valid name
218 0 0         if (!$self->validname($db)) {
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 0         if (! -d) {
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 0         if (opendir(DBDIR, $dbpath)){
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])) {
254             #only adds it if it is not a dot file
255 0 0         if (! $dir[$dirInt] =~ /^\./ ) {
256             #if it is a directory add it to the return array with / appended
257             #if not, just add it
258 0 0         if(-d $dbpath.'/'.$dir[$dirInt]){
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             sub newdb{
282 0     0 1   my $self=$_[0];
283 0           my $db=$_[1];
284              
285             #makes sure it is a valid name
286 0 0         if (!$self->validname($db)) {
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 0         if ($dbpath =~ /\/$/) {
299             #make sure the directory does not already exist
300 0 0         if ( -e $dbpath) {
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 0         if (!mkpath($dbpath)) {
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 0         if ( -e $dbfile) {
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 0         if (!DBI->connect("dbi:SQLite:dbname=".$dbfile,"","")){
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             sub rmdb{
346 0     0 1   my $self=$_[0];
347 0           my $db=$_[1];
348              
349             #makes sure it is a valid name
350 0 0         if (!$self->validname($db)) {
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 0         if ($db =~ /\/$/) {
364             #makes sure it exists
365 0 0         if (! -d $dbpath) {
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 0         if (!rmdir($dbpath)) {
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 0         if (! -e $dbfile) {
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 0         if (!unlink($dbfile)) {
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             sub validname{
413 0     0 1   my $self=$_[0];
414 0           my $name=$_[1];
415              
416             #return if it is undef
417 0 0         if (!defined($name)) {
418 0           return undef;
419             }
420              
421             #return if it begins it matches /\/./
422 0 0         if ($name =~ /\/./) {
423 0           return undef;
424             }
425              
426             #returns if it matches /^./
427 0 0         if ($name =~ /^./) {
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             sub errorBlank{
448 0     0 1   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<< >>
527              
528             =head1 BUGS
529              
530             Please report any bugs or feature requests to C, or through
531             the web interface at L. 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
551              
552             =item * AnnoCPAN: Annotated CPAN documentation
553              
554             L
555              
556             =item * CPAN Ratings
557              
558             L
559              
560             =item * Search CPAN
561              
562             L
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; # End of LARC::DB