File Coverage

blib/lib/DBIx/Table2Hash.pm
Criterion Covered Total %
statement 9 81 11.1
branch 0 24 0.0
condition 0 18 0.0
subroutine 3 12 25.0
pod 0 5 0.0
total 12 140 8.5


line stmt bran cond sub pod time code
1             package DBIx::Table2Hash;
2              
3             # Name:
4             # DBIx::Table2Hash.
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   26325 use strict;
  1         3  
  1         43  
32 1     1   7 use warnings;
  1         1  
  1         34  
33              
34 1     1   5 use Carp;
  1         1  
  1         1564  
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 = '1.17';
59              
60             # -----------------------------------------------
61              
62             # Preloaded methods go here.
63              
64             # -----------------------------------------------
65              
66             # Encapsulated class data.
67              
68             {
69             my(%_attr_data) =
70             (
71             _child_column => '',
72             _dbh => '',
73             _skip_columns => [],
74             _hash_ref => '',
75             _key_column => '',
76             _parent_column => '',
77             _table_name => '',
78             _value_column => '',
79             _where => '',
80             );
81              
82             sub _default_for
83             {
84 0     0     my($self, $attr_name) = @_;
85              
86 0           $_attr_data{$attr_name};
87             }
88              
89             sub _get_column_names
90             {
91 0     0     my($self, $table_name) = @_;
92 0           my($sth) = $$self{'_dbh'} -> prepare("select * from $table_name where 1=2");
93              
94 0           $sth -> execute();
95              
96 0           $$self{'_column_name'} = $$sth{'NAME_lc'};
97              
98 0           $sth -> finish();
99              
100             } # End of _get_column_names.
101              
102             sub _select_tree
103             {
104 0     0     my($self, $root, $children, $parent_id) = @_;
105 0           my($skip) = join('|', $$self{'_key_column'}, $$self{'_child_column'}, $$self{'_parent_column'}, @{$$self{'_skip_columns'} });
  0            
106 0           $skip = qr/$skip/;
107              
108 0           my($child, $name, $key);
109              
110 0           for my $child (@{$$children{$parent_id} })
  0            
111             {
112 0           $name = $$child{$$self{'_key_column'} };
113 0           $$root{$name} = {};
114              
115 0           for $key (keys %$child)
116             {
117 0 0         next if ($key =~ /$skip/);
118              
119 0 0         $$root{$name}{$key} = $$child{$key} if ($$child{$key});
120             }
121              
122 0           $self -> _select_tree($$root{$$child{$$self{'_key_column'} } }, $children, $$child{$$self{'_child_column'}});
123             }
124              
125             } # End of _select_tree.
126              
127             sub _standard_keys
128             {
129 0     0     keys %_attr_data;
130             }
131              
132             } # End of encapsulated class data.
133              
134             # -----------------------------------------------
135              
136             sub new
137             {
138 0     0 0   my($class, %arg) = @_;
139 0           my($self) = bless({}, $class);
140              
141 0           for my $attr_name ($self -> _standard_keys() )
142             {
143 0           my($arg_name) = $attr_name =~ /^_(.*)/;
144              
145 0 0         if (exists($arg{$arg_name}) )
146             {
147 0           $$self{$attr_name} = $arg{$arg_name};
148             }
149             else
150             {
151 0           $$self{$attr_name} = $self -> _default_for($attr_name);
152             }
153             }
154              
155 0           return $self;
156              
157             } # End of new.
158              
159             # -----------------------------------------------
160              
161             sub select
162             {
163 0     0 0   my($self, %arg) = @_;
164              
165 0           $self -> set(%arg);
166              
167 0 0 0       croak(__PACKAGE__ . '. You must supply a value for the parameters dbh, table_name, key_column and value_column')
      0        
      0        
168             if (! ($$self{'_dbh'} && $$self{'_table_name'} && $$self{'_key_column'} && $$self{'_value_column'}) );
169              
170 0           my($sql) = "select $$self{'_key_column'}, $$self{'_value_column'} from $$self{'_table_name'} $$self{'_where'}";
171 0           my($sth) = $$self{'_dbh'} -> prepare($sql);
172              
173 0           $sth -> execute();
174              
175 0           my($data, %h);
176              
177 0           while ($data = $sth -> fetch() )
178             {
179 0 0         $h{$$data[0]} = $$data[1] if (defined $$data[0]);
180             }
181              
182 0           $$self{'_hash_ref'} = \%h;
183              
184             } # End of select.
185              
186             # -----------------------------------------------
187              
188             sub select_hashref
189             {
190 0     0 0   my($self, %arg) = @_;
191              
192 0           $self -> set(%arg);
193              
194 0 0 0       croak(__PACKAGE__ . '. You must supply a value for the parameters dbh, table_name and key_column')
      0        
195             if (! ($$self{'_dbh'} && $$self{'_table_name'} && $$self{'_key_column'}) );
196              
197 0           $self -> _get_column_names($$self{'_table_name'});
198              
199 0           my(%column_name);
200              
201 0           @column_name{@{$$self{'_column_name'} } } = (1) x @{$$self{'_column_name'} };
  0            
  0            
202              
203             # Due to a bug in MySQL, we cannot say 'col, *', we must say '*, col'.
204              
205 0 0         my($column_set) = $column_name{lc $$self{'_key_column'} } ? '*' : "*, $$self{'_key_column'}";
206 0           my($sql) = "select $column_set from $$self{'_table_name'} $$self{'_where'}";
207 0           my($sth) = $$self{'_dbh'} -> prepare($sql);
208              
209 0           $sth -> execute();
210              
211 0           my($data, %h);
212              
213 0           while ($data = $sth -> fetchrow_hashref() )
214             {
215 0 0         $h{$$data{$$self{'_key_column'} } } = {%$data} if (defined $$data{$$self{'_key_column'} });
216             }
217              
218 0           $$self{'_hash_ref'} = \%h;
219              
220             } # End of select_hashref.
221              
222             # -----------------------------------------------
223              
224             sub select_tree
225             {
226 0     0 0   my($self, %arg) = @_;
227              
228 0           $self -> set(%arg);
229              
230 0 0 0       croak(__PACKAGE__ . '. You must supply a value for the parameters child_column and parent_column')
231             if (! ($$self{'_child_column'} && $$self{'_parent_column'}) );
232              
233 0 0         $self -> select_hashref() if (! $$self{'_hash_ref'});
234              
235 0           my($id, $parent_id, %children);
236              
237 0           for $id (keys %{$$self{'_hash_ref'} })
  0            
238             {
239 0           $parent_id = $$self{'_hash_ref'}{$id}{$$self{'_parent_column'} };
240 0 0         $children{$parent_id} = [] if (! $children{$parent_id});
241 0           push @{$children{$parent_id} }, $$self{'_hash_ref'}{$id};
  0            
242             }
243              
244 0           my($tree) = {};
245              
246 0           $self -> _select_tree($tree, \%children, 0);
247              
248 0           $tree;
249              
250             } # End of select_tree.
251              
252             # -----------------------------------------------
253              
254             sub set
255             {
256 0     0 0   my($self, %arg) = @_;
257              
258 0           for my $arg (keys %arg)
259             {
260 0 0         $$self{"_$arg"} = $arg{$arg} if (exists($$self{"_$arg"}) );
261             }
262              
263             } # End of set.
264              
265             # -----------------------------------------------
266              
267             1;
268              
269             __END__