File Coverage

blib/lib/Anansi/Database.pm
Criterion Covered Total %
statement 3 27 11.1
branch 0 12 0.0
condition n/a
subroutine 1 4 25.0
pod 3 3 100.0
total 7 46 15.2


line stmt bran cond sub pod time code
1             package Anansi::Database;
2              
3              
4             =head1 NAME
5              
6             Anansi::Database - A manager for database interaction.
7              
8             =head1 SYNOPSIS
9              
10             my $OBJECT = Anansi::Database->new();
11             my $component = $OBJECT->addComponent(
12             undef,
13             DRIVER => 'MySQL',
14             );
15             if(defined($component)) {
16             if($OBJECT->channel(
17             'CONNECT',
18             $component,
19             DATABASE => 'someDatabase',
20             PASSWORD => 'somePassword',
21             USERNAME => 'someUser',
22             )) {
23             my $records = $OBJECT->channel(
24             'STATEMENT',
25             $component,
26             INPUT => [
27             {
28             DEFAULT => '0',
29             NAME => 'yetAnotherField',
30             }
31             ],
32             SQL => 'SELECT some_field, another_field FROM some_table WHERE yet_another_field = ?;',
33             yetAnotherField => 123,
34             );
35             if(defined($records)) {
36             if(ref($records) =~ /^ARRAY$/i) {
37             my $record = 0;
38             foreach my $record (@{$records}) {
39             next if(ref($record) !~ /^HASH$/i);
40             print "\n" if(0 < $record);
41             my $field = 0;
42             foreach my $key (keys(%{$record})) {
43             print ', ' if(0 < $field);
44             print '"'.$key.'" = "'.${record}{$key}.'"';
45             $field++;
46             }
47             $record++;
48             }
49             print "\n";
50             }
51             }
52             }
53             }
54              
55             =head1 DESCRIPTION
56              
57             Manages database interactions allowing the creation, interrogation, modification
58             and removal of database structures and table records.
59              
60             =cut
61              
62              
63             our $VERSION = '0.03';
64              
65 1     1   69256 use base qw(Anansi::ComponentManager);
  1         3  
  1         645  
66              
67              
68             =head1 METHODS
69              
70             =cut
71              
72              
73             =head2 Anansi::Class
74              
75             See L for details. A parent module of L.
76              
77             =cut
78              
79              
80             =head3 DESTROY
81              
82             See L for details. Overridden by L.
83              
84             =cut
85              
86              
87             =head3 finalise
88              
89             See L for details. A virtual method.
90              
91             =cut
92              
93              
94             =head3 implicate
95              
96             See L for details. A virtual method.
97              
98             =cut
99              
100              
101             =head3 import
102              
103             See L for details.
104              
105             =cut
106              
107              
108             =head3 initialise
109              
110             See L for details. Overridden by L. A virtual method.
111              
112             =cut
113              
114              
115             =head3 new
116              
117             See L for details. Overridden by L.
118              
119             =cut
120              
121              
122             =head3 old
123              
124             See L for details.
125              
126             =cut
127              
128              
129             =head3 used
130              
131             See L for details.
132              
133             =cut
134              
135              
136             =head3 uses
137              
138             See L for details.
139              
140             =cut
141              
142              
143             =head3 using
144              
145             See L for details.
146              
147             =cut
148              
149              
150             =head2 Anansi::ComponentManager
151              
152             See L for details. A parent module of L.
153              
154             =cut
155              
156              
157             =head3 Anansi::Singleton
158              
159             See L for details. A parent module of L.
160              
161             =cut
162              
163              
164             =head3 addChannel
165              
166             See L for details.
167              
168             =cut
169              
170              
171             =head3 addComponent
172              
173             See L for details.
174              
175             =cut
176              
177              
178             =head3 channel
179              
180             See L for details.
181              
182             =cut
183              
184              
185             =head3 component
186              
187             See L for details.
188              
189             =cut
190              
191              
192             =head3 componentIdentification
193              
194             See L for details.
195              
196             =cut
197              
198              
199             =head3 components
200              
201             See L for details.
202              
203             =cut
204              
205              
206             =head3 initialise
207              
208             See L for details. Overrides L. A virtual method.
209              
210             =cut
211              
212              
213             =head3 priorities
214              
215             See L for details.
216              
217             =cut
218              
219              
220             =head3 reinitialise
221              
222             See L for details. Overrides L. A virtual method.
223              
224             =cut
225              
226              
227             =head3 removeChannel
228              
229             See L for details.
230              
231             =cut
232              
233              
234             =head3 removeComponent
235              
236             See L for details.
237              
238             =cut
239              
240              
241             =head2 Anansi::Singleton
242              
243             See L for details. A parent module of L.
244              
245             =cut
246              
247              
248             =head3 Anansi::Class
249              
250             See L for details. A parent module of L.
251              
252             =cut
253              
254              
255             =head3 DESTROY
256              
257             See L for details. Overrides L.
258              
259             =cut
260              
261              
262             =head3 fixate
263              
264             See L for details. A virtual method.
265              
266             =cut
267              
268              
269             =head3 new
270              
271             See L for details. Overrides L.
272              
273             =cut
274              
275              
276             =head3 reinitialise
277              
278             See L for details. Overridden by L. A virtual method.
279              
280             =cut
281              
282              
283             =head2 connect
284              
285             my $component = Anansi::Database->addComponent();
286             my $connection = Anansi::Database->connect(
287             undef,
288             $component,
289             DATABASE => 'someDatabase',
290             PASSWORD => 'somePassword',
291             USERNAME => 'someUsername',
292             );
293             if(!defined($connection));
294              
295             my $handle = DBI->connect('DBI:mysql:someDatabase', 'someUsername', 'somePassword');
296             my $component = Anansi::Database->addComponent();
297             my $connection = Anansi::Database->channel(
298             'CONNECT',
299             $component,
300             HANDLE => $handle,
301             );
302             if(!defined($connection));
303              
304             =over 4
305              
306             =item self I<(Blessed Hash B String, Required)>
307              
308             Either an object or a string of this namespace.
309              
310             =item channel I<(String, Required)>
311              
312             The abstract identifier of a subroutine.
313              
314             =item component I<(String, Required)>
315              
316             The name associated with the component.
317              
318             =item parameters I<(Hash, Optional)>
319              
320             Named parameters.
321              
322             =back
323              
324             Attempts to load the appropriate database driver and connect to a database.
325             Returns B<1> I<(one)> on success and B<0> I<(zero)> on failure. Returns an
326             B when an error occurs.
327              
328             =cut
329              
330              
331             sub connect {
332 0     0 1   my ($self, $channel, $component, %parameters) = @_;
333 0           my $channels = Anansi::Database->component($component);
334 0 0         return if(!defined($channels));
335 0           my %hash = map { $_ => 1 } (@{$channels});
  0            
  0            
336 0 0         return if(!defined($hash{CONNECT}));
337 0           return Anansi::Database->component($component, 'CONNECT', %parameters);
338             }
339              
340             Anansi::Database->addChannel('CONNECT' => 'connect');
341              
342              
343             =head2 disconnect
344              
345             my $component = Anansi::Database->addComponent();
346             my $connection = Anansi::Database->connect(undef, $component);
347             if(defined($connection)) (
348             Anansi::Database->disconnect(undef, $component);
349             }
350              
351             my $component = Anansi::Database->addComponent();
352             my $connection = Anansi::Database->channel('CONNECT', $component);
353             if(defined($connection)) {
354             Anansi::Database->channel('DISCONNECT', $component)
355             }
356              
357             =over 4
358              
359             =item self I<(Blessed Hash B String, Required)>
360              
361             Either an object or a string of this namespace.
362              
363             =item channel I<(String, Required)>
364              
365             The abstract identifier of a subroutine.
366              
367             =item component I<(String, Required)>
368              
369             The name associated with the component.
370              
371             =item parameters I<(Hash, Optional)>
372              
373             Named parameters.
374              
375             =back
376              
377             Attempts to disconnect from a database. Returns B<1> I<(one)> on success and
378             B<0> I<(zero)> on failure. Returns an B when an error occurs.
379              
380             =cut
381              
382              
383             sub disconnect {
384 0     0 1   my ($self, $channel, $component, %parameters) = @_;
385 0           my $channels = Anansi::Database->component($component);
386 0 0         return if(!defined($channels));
387 0           my %hash = map { $_ => 1 } (@{$channels});
  0            
  0            
388 0 0         return if(!defined($hash{DISCONNECT}));
389 0           return Anansi::Database->component($component, 'DISCONNECT', %parameters);
390             }
391              
392             Anansi::Database->addChannel('DISCONNECT' => 'disconnect');
393              
394              
395             =head2 statement
396              
397             my $result = Anansi::Database::statement(
398             $OBJECT,
399             undef,
400             INPUT => [
401             'hij' => 'someParameter',
402             'klm' => 'anotherParameter'
403             ],
404             SQL => 'SELECT abc, def FROM some_table WHERE hij = ? AND klm = ?;',
405             STATEMENT => 'someStatement',
406             someParameter => 123,
407             anotherParameter => 456
408             );
409              
410             my $result = Anansi::Database::channel(
411             $OBJECT,
412             'STATEMENT',
413             STATEMENT => 'someStatement',
414             someParameter => 234,
415             anotherParameter => 'abc'
416             );
417              
418             my $result = $OBJECT->statement(
419             undef,
420             STATEMENT => 'someStatement',
421             someParameter => 345,
422             anotherParameter => 789
423             );
424              
425             my $result = $OBJECT->channel(
426             'STATEMENT',
427             STATEMENT => 'someStatement',
428             someParameter => 456,
429             anotherParameter => 'def'
430             );
431              
432             =over 4
433              
434             =item self I<(Blessed Hash B String, Required)>
435              
436             Either an object or a string of this namespace.
437              
438             =item channel I<(String, Required)>
439              
440             The abstract identifier of a subroutine.
441              
442             =item component I<(String, Required)>
443              
444             The name associated with the component.
445              
446             =item parameters I<(Hash, Optional)>
447              
448             Named parameters.
449              
450             =over 4
451              
452             =item INPUT I<(Array, Optional)>
453              
454             An array of hashes with each element corresponding to an equivalent B
455             I<(Question mark)> found within the supplied B. If the number of elements
456             is not the same as the number of B I<(Question mark)>s found in the statement
457             then the statement is invalid. See the L
458             method for details.
459              
460             =item SQL I<(String, Optional)>
461              
462             The SQL statement to execute.
463              
464             =item STATEMENT I<(String, Optional)>
465              
466             The name associated with a prepared SQL statement. This is interchangeable with
467             the B parameter but helps to speed up repetitive database interaction.
468              
469             =back
470              
471             =back
472              
473             Attempts to execute the supplied B with the supplied named parameters.
474             Either returns an array of retrieved record data or a B<1> I<(one)> on success
475             and a B<0> I<(zero)> on failure as appropriate to the SQL statement. Returns an
476             B when an error occurs.
477              
478             =cut
479              
480              
481             sub statement {
482 0     0 1   my ($self, $channel, $component, %parameters) = @_;
483 0           my $channels = Anansi::Database->component($component);
484 0 0         return if(!defined($channels));
485 0           my %hash = map { $_ => 1 } (@{$channels});
  0            
  0            
486 0 0         return if(!defined($hash{STATEMENT}));
487 0           return Anansi::Database->component($component, 'STATEMENT', %parameters);
488             }
489              
490             Anansi::Database->addChannel('STATEMENT' => 'statement');
491              
492              
493             =head1 NOTES
494              
495             This module is designed to make it simple, easy and quite fast to code your
496             design in perl. If for any reason you feel that it doesn't achieve these goals
497             then please let me know. I am here to help. All constructive criticisms are
498             also welcomed.
499              
500             =cut
501              
502              
503             =head1 AUTHOR
504              
505             Kevin Treleaven treleaven I net>
506              
507             =cut
508              
509              
510             1;
511