File Coverage

blib/lib/DBIx/Hash2Table.pm
Criterion Covered Total %
statement 9 43 20.9
branch 0 8 0.0
condition 0 9 0.0
subroutine 3 8 37.5
pod 0 2 0.0
total 12 70 17.1


line stmt bran cond sub pod time code
1             package DBIx::Hash2Table;
2              
3             # Name:
4             # DBIx::Hash2Table.
5             #
6             # Documentation:
7             # POD-style documentation is at the end. Extract it with pod2html.*.
8             #
9             # Reference:
10             # Object Oriented Perl
11             # Damian Conway
12             # Manning
13             # 1-884777-79-1
14             # P 114
15             #
16             # Note:
17             # o Tab = 4 spaces || die.
18             #
19             # Author:
20             # Ron Savage
21             # Home page: http://savage.net.au/index.html
22             #
23             # Licence:
24             # Australian copyright (c) 1999-2002 Ron Savage.
25             #
26             # All Programs of mine are 'OSI Certified Open Source Software';
27             # you can redistribute them and/or modify them under the terms of
28             # The Artistic License, a copy of which is available at:
29             # http://www.opensource.org/licenses/index.html
30              
31 1     1   23619 use strict;
  1         2  
  1         45  
32 1     1   6 use warnings;
  1         2  
  1         28  
33              
34 1     1   6 use Carp;
  1         2  
  1         863  
35              
36             require 5.005_62;
37              
38             require Exporter;
39              
40             our @ISA = qw(Exporter);
41              
42             # Items to export into callers namespace by default. Note: do not export
43             # names by default without a very good reason. Use EXPORT_OK instead.
44             # Do not simply export all your public functions/methods/constants.
45              
46             # This allows declaration use DBIx::Hash2Table ':all';
47             # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
48             # will save memory.
49             our %EXPORT_TAGS = ( 'all' => [ qw(
50              
51             ) ] );
52              
53             our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
54              
55             our @EXPORT = qw(
56              
57             );
58             our $VERSION = '2.04';
59              
60             # -----------------------------------------------
61              
62             # Preloaded methods go here.
63              
64             # -----------------------------------------------
65              
66             # Encapsulated class data.
67              
68             {
69             my(%_attr_data) =
70             (
71             _columns => '',
72             _dbh => '',
73             _extras => [],
74             _hash_ref => '',
75             _table_name => '',
76             );
77              
78             sub _default_for
79             {
80 0     0     my($self, $attr_name) = @_;
81              
82 0           $_attr_data{$attr_name};
83             }
84              
85             sub _save
86             {
87 0     0     my($self, $sth, $hash_ref, $parent) = @_;
88              
89 0           my(@bind);
90              
91 0           for my $key (keys %$hash_ref)
92             {
93             # If we have a hash ref, which we almost always do have...
94              
95 0 0         if (ref($$hash_ref{$key}) eq 'HASH')
96             {
97 0           $$self{'_id'}++;
98              
99 0           @bind = ($$self{'_id'}, $parent, $key);
100              
101 0           for (@{$$self{'_extras'} })
  0            
102             {
103 0 0         push(@bind, exists($$hash_ref{$key}{$_}) ? $$hash_ref{$key}{$_} : undef);
104             }
105              
106 0           $sth -> execute(@bind);
107              
108             # Curse again. (Aka 'recurse', for non-native speakers of English.)
109              
110 0           $self -> _save($sth, $$hash_ref{$key}, $$self{'_id'});
111             }
112             }
113              
114             } # End of _save.
115              
116             sub _standard_keys
117             {
118 0     0     keys %_attr_data;
119             }
120             }
121              
122             # -----------------------------------------------
123              
124             sub insert
125             {
126 0     0 0   my($self) = @_;
127 0           $$self{'_extras'} = [sort @{$$self{'_extras'} }];
  0            
128 0           my($sql) = "insert into $$self{'_table_name'} (" . join(', ', @{$$self{'_columns'} }, @{$$self{'_extras'} }) . ') values (' . join(', ', ('?') x ($#{$$self{'_columns'} } + $#{$$self{'_extras'} } + 2) ) . ')';
  0            
  0            
  0            
  0            
129 0           my($sth) = $$self{'_dbh'} -> prepare($sql);
130 0           $$self{'_id'} = 0;
131              
132 0           $self -> _save($sth, $$self{'_hash_ref'}, 0);
133              
134             } # End of insert.
135              
136             # -----------------------------------------------
137              
138             sub new
139             {
140 0     0 0   my($class, %arg) = @_;
141 0           my($self) = bless({}, $class);
142              
143 0           for my $attr_name ($self -> _standard_keys() )
144             {
145 0           my($arg_name) = $attr_name =~ /^_(.*)/;
146              
147 0 0         if (exists($arg{$arg_name}) )
148             {
149 0           $$self{$attr_name} = $arg{$arg_name};
150             }
151             else
152             {
153 0           $$self{$attr_name} = $self -> _default_for($attr_name);
154             }
155             }
156              
157 0 0 0       croak(__PACKAGE__ . ". You must supply a value for each parameter except 'extras'")
      0        
      0        
158             if (! ($$self{'_columns'} && $$self{'_dbh'} && $$self{'_hash_ref'} && $$self{'_table_name'}) );
159              
160 0           return $self;
161              
162             } # End of new.
163              
164             # -----------------------------------------------
165              
166             1;
167              
168             __END__