| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package DBIx::Table2Hash; |
|
2
|
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
70795
|
use strict; |
|
|
2
|
|
|
|
|
13
|
|
|
|
2
|
|
|
|
|
58
|
|
|
4
|
2
|
|
|
2
|
|
11
|
use warnings; |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
45
|
|
|
5
|
|
|
|
|
|
|
|
|
6
|
2
|
|
|
2
|
|
9
|
use Carp; |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
2579
|
|
|
7
|
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '1.18'; |
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
# ----------------------------------------------- |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
# Preloaded methods go here. |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
# ----------------------------------------------- |
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
# Encapsulated class data. |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
{ |
|
19
|
|
|
|
|
|
|
my(%_attr_data) = |
|
20
|
|
|
|
|
|
|
( |
|
21
|
|
|
|
|
|
|
_child_column => '', |
|
22
|
|
|
|
|
|
|
_dbh => '', |
|
23
|
|
|
|
|
|
|
_skip_columns => [], |
|
24
|
|
|
|
|
|
|
_hash_ref => '', |
|
25
|
|
|
|
|
|
|
_key_column => '', |
|
26
|
|
|
|
|
|
|
_parent_column => '', |
|
27
|
|
|
|
|
|
|
_table_name => '', |
|
28
|
|
|
|
|
|
|
_value_column => '', |
|
29
|
|
|
|
|
|
|
_where => '', |
|
30
|
|
|
|
|
|
|
); |
|
31
|
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub _default_for |
|
33
|
|
|
|
|
|
|
{ |
|
34
|
0
|
|
|
0
|
|
|
my($self, $attr_name) = @_; |
|
35
|
|
|
|
|
|
|
|
|
36
|
0
|
|
|
|
|
|
$_attr_data{$attr_name}; |
|
37
|
|
|
|
|
|
|
} |
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub _get_column_names |
|
40
|
|
|
|
|
|
|
{ |
|
41
|
0
|
|
|
0
|
|
|
my($self, $table_name) = @_; |
|
42
|
0
|
|
|
|
|
|
my($sth) = $$self{'_dbh'} -> prepare("select * from $table_name where 1=2"); |
|
43
|
|
|
|
|
|
|
|
|
44
|
0
|
|
|
|
|
|
$sth -> execute(); |
|
45
|
|
|
|
|
|
|
|
|
46
|
0
|
|
|
|
|
|
$$self{'_column_name'} = $$sth{'NAME_lc'}; |
|
47
|
|
|
|
|
|
|
|
|
48
|
0
|
|
|
|
|
|
$sth -> finish(); |
|
49
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
} # End of _get_column_names. |
|
51
|
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub _select_tree |
|
53
|
|
|
|
|
|
|
{ |
|
54
|
0
|
|
|
0
|
|
|
my($self, $root, $children, $parent_id) = @_; |
|
55
|
0
|
|
|
|
|
|
my($skip) = join('|', $$self{'_key_column'}, $$self{'_child_column'}, $$self{'_parent_column'}, @{$$self{'_skip_columns'} }); |
|
|
0
|
|
|
|
|
|
|
|
56
|
0
|
|
|
|
|
|
$skip = qr/$skip/; |
|
57
|
|
|
|
|
|
|
|
|
58
|
0
|
|
|
|
|
|
my($child, $name, $key); |
|
59
|
|
|
|
|
|
|
|
|
60
|
0
|
|
|
|
|
|
for my $child (@{$$children{$parent_id} }) |
|
|
0
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
{ |
|
62
|
0
|
|
|
|
|
|
$name = $$child{$$self{'_key_column'} }; |
|
63
|
0
|
|
|
|
|
|
$$root{$name} = {}; |
|
64
|
|
|
|
|
|
|
|
|
65
|
0
|
|
|
|
|
|
for $key (keys %$child) |
|
66
|
|
|
|
|
|
|
{ |
|
67
|
0
|
0
|
|
|
|
|
next if ($key =~ /$skip/); |
|
68
|
|
|
|
|
|
|
|
|
69
|
0
|
0
|
|
|
|
|
$$root{$name}{$key} = $$child{$key} if ($$child{$key}); |
|
70
|
|
|
|
|
|
|
} |
|
71
|
|
|
|
|
|
|
|
|
72
|
0
|
|
|
|
|
|
$self -> _select_tree($$root{$$child{$$self{'_key_column'} } }, $children, $$child{$$self{'_child_column'}}); |
|
73
|
|
|
|
|
|
|
} |
|
74
|
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
} # End of _select_tree. |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub _standard_keys |
|
78
|
|
|
|
|
|
|
{ |
|
79
|
0
|
|
|
0
|
|
|
keys %_attr_data; |
|
80
|
|
|
|
|
|
|
} |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
} # End of encapsulated class data. |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
# ----------------------------------------------- |
|
85
|
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub new |
|
87
|
|
|
|
|
|
|
{ |
|
88
|
0
|
|
|
0
|
0
|
|
my($class, %arg) = @_; |
|
89
|
0
|
|
|
|
|
|
my($self) = bless({}, $class); |
|
90
|
|
|
|
|
|
|
|
|
91
|
0
|
|
|
|
|
|
for my $attr_name ($self -> _standard_keys() ) |
|
92
|
|
|
|
|
|
|
{ |
|
93
|
0
|
|
|
|
|
|
my($arg_name) = $attr_name =~ /^_(.*)/; |
|
94
|
|
|
|
|
|
|
|
|
95
|
0
|
0
|
|
|
|
|
if (exists($arg{$arg_name}) ) |
|
96
|
|
|
|
|
|
|
{ |
|
97
|
0
|
|
|
|
|
|
$$self{$attr_name} = $arg{$arg_name}; |
|
98
|
|
|
|
|
|
|
} |
|
99
|
|
|
|
|
|
|
else |
|
100
|
|
|
|
|
|
|
{ |
|
101
|
0
|
|
|
|
|
|
$$self{$attr_name} = $self -> _default_for($attr_name); |
|
102
|
|
|
|
|
|
|
} |
|
103
|
|
|
|
|
|
|
} |
|
104
|
|
|
|
|
|
|
|
|
105
|
0
|
|
|
|
|
|
return $self; |
|
106
|
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
} # End of new. |
|
108
|
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
# ----------------------------------------------- |
|
110
|
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub select |
|
112
|
|
|
|
|
|
|
{ |
|
113
|
0
|
|
|
0
|
0
|
|
my($self, %arg) = @_; |
|
114
|
|
|
|
|
|
|
|
|
115
|
0
|
|
|
|
|
|
$self -> set(%arg); |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
croak(__PACKAGE__ . '. You must supply a value for the parameters dbh, table_name, key_column and value_column') |
|
118
|
0
|
0
|
0
|
|
|
|
if (! ($$self{'_dbh'} && $$self{'_table_name'} && $$self{'_key_column'} && $$self{'_value_column'}) ); |
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
119
|
|
|
|
|
|
|
|
|
120
|
0
|
|
|
|
|
|
my($sql) = "select $$self{'_key_column'}, $$self{'_value_column'} from $$self{'_table_name'} $$self{'_where'}"; |
|
121
|
0
|
|
|
|
|
|
my($sth) = $$self{'_dbh'} -> prepare($sql); |
|
122
|
|
|
|
|
|
|
|
|
123
|
0
|
|
|
|
|
|
$sth -> execute(); |
|
124
|
|
|
|
|
|
|
|
|
125
|
0
|
|
|
|
|
|
my($data, %h); |
|
126
|
|
|
|
|
|
|
|
|
127
|
0
|
|
|
|
|
|
while ($data = $sth -> fetch() ) |
|
128
|
|
|
|
|
|
|
{ |
|
129
|
0
|
0
|
|
|
|
|
$h{$$data[0]} = $$data[1] if (defined $$data[0]); |
|
130
|
|
|
|
|
|
|
} |
|
131
|
|
|
|
|
|
|
|
|
132
|
0
|
|
|
|
|
|
$$self{'_hash_ref'} = \%h; |
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
} # End of select. |
|
135
|
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
# ----------------------------------------------- |
|
137
|
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
sub select_hashref |
|
139
|
|
|
|
|
|
|
{ |
|
140
|
0
|
|
|
0
|
0
|
|
my($self, %arg) = @_; |
|
141
|
|
|
|
|
|
|
|
|
142
|
0
|
|
|
|
|
|
$self -> set(%arg); |
|
143
|
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
croak(__PACKAGE__ . '. You must supply a value for the parameters dbh, table_name and key_column') |
|
145
|
0
|
0
|
0
|
|
|
|
if (! ($$self{'_dbh'} && $$self{'_table_name'} && $$self{'_key_column'}) ); |
|
|
|
|
0
|
|
|
|
|
|
146
|
|
|
|
|
|
|
|
|
147
|
0
|
|
|
|
|
|
$self -> _get_column_names($$self{'_table_name'}); |
|
148
|
|
|
|
|
|
|
|
|
149
|
0
|
|
|
|
|
|
my(%column_name); |
|
150
|
|
|
|
|
|
|
|
|
151
|
0
|
|
|
|
|
|
@column_name{@{$$self{'_column_name'} } } = (1) x @{$$self{'_column_name'} }; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
# Due to a bug in MySQL, we cannot say 'col, *', we must say '*, col'. |
|
154
|
|
|
|
|
|
|
|
|
155
|
0
|
0
|
|
|
|
|
my($column_set) = $column_name{lc $$self{'_key_column'} } ? '*' : "*, $$self{'_key_column'}"; |
|
156
|
0
|
|
|
|
|
|
my($sql) = "select $column_set from $$self{'_table_name'} $$self{'_where'}"; |
|
157
|
0
|
|
|
|
|
|
my($sth) = $$self{'_dbh'} -> prepare($sql); |
|
158
|
|
|
|
|
|
|
|
|
159
|
0
|
|
|
|
|
|
$sth -> execute(); |
|
160
|
|
|
|
|
|
|
|
|
161
|
0
|
|
|
|
|
|
my($data, %h); |
|
162
|
|
|
|
|
|
|
|
|
163
|
0
|
|
|
|
|
|
while ($data = $sth -> fetchrow_hashref() ) |
|
164
|
|
|
|
|
|
|
{ |
|
165
|
0
|
0
|
|
|
|
|
$h{$$data{$$self{'_key_column'} } } = {%$data} if (defined $$data{$$self{'_key_column'} }); |
|
166
|
|
|
|
|
|
|
} |
|
167
|
|
|
|
|
|
|
|
|
168
|
0
|
|
|
|
|
|
$$self{'_hash_ref'} = \%h; |
|
169
|
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
} # End of select_hashref. |
|
171
|
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
# ----------------------------------------------- |
|
173
|
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
sub select_tree |
|
175
|
|
|
|
|
|
|
{ |
|
176
|
0
|
|
|
0
|
0
|
|
my($self, %arg) = @_; |
|
177
|
|
|
|
|
|
|
|
|
178
|
0
|
|
|
|
|
|
$self -> set(%arg); |
|
179
|
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
croak(__PACKAGE__ . '. You must supply a value for the parameters child_column and parent_column') |
|
181
|
0
|
0
|
0
|
|
|
|
if (! ($$self{'_child_column'} && $$self{'_parent_column'}) ); |
|
182
|
|
|
|
|
|
|
|
|
183
|
0
|
0
|
|
|
|
|
$self -> select_hashref() if (! $$self{'_hash_ref'}); |
|
184
|
|
|
|
|
|
|
|
|
185
|
0
|
|
|
|
|
|
my($id, $parent_id, %children); |
|
186
|
|
|
|
|
|
|
|
|
187
|
0
|
|
|
|
|
|
for $id (keys %{$$self{'_hash_ref'} }) |
|
|
0
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
{ |
|
189
|
0
|
|
|
|
|
|
$parent_id = $$self{'_hash_ref'}{$id}{$$self{'_parent_column'} }; |
|
190
|
0
|
0
|
|
|
|
|
$children{$parent_id} = [] if (! $children{$parent_id}); |
|
191
|
0
|
|
|
|
|
|
push @{$children{$parent_id} }, $$self{'_hash_ref'}{$id}; |
|
|
0
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
} |
|
193
|
|
|
|
|
|
|
|
|
194
|
0
|
|
|
|
|
|
my($tree) = {}; |
|
195
|
|
|
|
|
|
|
|
|
196
|
0
|
|
|
|
|
|
$self -> _select_tree($tree, \%children, 0); |
|
197
|
|
|
|
|
|
|
|
|
198
|
0
|
|
|
|
|
|
$tree; |
|
199
|
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
} # End of select_tree. |
|
201
|
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
# ----------------------------------------------- |
|
203
|
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
sub set |
|
205
|
|
|
|
|
|
|
{ |
|
206
|
0
|
|
|
0
|
0
|
|
my($self, %arg) = @_; |
|
207
|
|
|
|
|
|
|
|
|
208
|
0
|
|
|
|
|
|
for my $arg (keys %arg) |
|
209
|
|
|
|
|
|
|
{ |
|
210
|
0
|
0
|
|
|
|
|
$$self{"_$arg"} = $arg{$arg} if (exists($$self{"_$arg"}) ); |
|
211
|
|
|
|
|
|
|
} |
|
212
|
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
} # End of set. |
|
214
|
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
# ----------------------------------------------- |
|
216
|
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
1; |
|
218
|
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
__END__ |