File Coverage

blib/lib/DBI/Easy/DBH.pm
Criterion Covered Total %
statement 93 156 59.6
branch 17 54 31.4
condition 12 21 57.1
subroutine 8 16 50.0
pod 0 12 0.0
total 130 259 50.1


line stmt bran cond sub pod time code
1             package DBI::Easy;
2              
3             # use Hash::Merge;
4              
5 6     6   33 use Class::Easy;
  6         12  
  6         61  
6              
7             sub statement {
8 61     61 0 108 my $self = shift;
9 61         105 my $statement = shift;
10 61   100     342 my $dbh_method = shift || 'dbh';
11            
12 61         213 my $dbh = $self->$dbh_method;
13            
14 61         285 my $sth;
15 61 100       232 if (ref $statement eq 'DBI::st') {
    50          
16 2         3 $sth = $statement;
17             } elsif (ref $statement) {
18 0         0 die "can't use '$statement' as sql statement";
19             } else {
20            
21 59         943 my $prepare_method = $self->prepare_method;
22 59         661 $sth = $dbh->$prepare_method (($statement, {}, $self->prepare_param));
23             }
24            
25 61         18286 return $sth;
26             }
27              
28             sub bind_values {
29 61     61 0 187 my $self = shift;
30 61         103 my $sth = shift;
31 61         128 my $bind = shift;
32            
33 61         237 foreach my $i (0 .. $#$bind) {
34 73         136 my $v = $bind->[$i];
35 73         205 my @bind_v = ($v);
36 73 50       208 if (ref $v eq 'ARRAY') {
37 0 0       0 die "you must supply bind type within \%DBI::Easy::BIND_TYPES"
38             unless exists $DBI::Easy::BIND_TYPES{$v->[1]};
39            
40 0         0 my $opts = $DBI::Easy::BIND_TYPES{$v->[1]};
41            
42             #if (exists $opts->{ora_type}) {
43             #
44             # warn Encode::is_utf8 ($v->[0]);
45             # $opts->{ora_csform} = 1;
46             #}
47            
48 0         0 $opts->{ora_field} = $v->[2];
49            
50 0         0 @bind_v = ($v->[0], $opts);
51             #use Data::Dumper;
52             #warn "bind for param ", $i + 1, ", for " . Dumper $opts;
53             }
54 73         580 $sth->bind_param ($i + 1, @bind_v);
55             }
56             }
57              
58             # for every query except select we must use this routine
59             sub no_fetch {
60 21     21 0 60 my $self = shift;
61 21         38 my $statement = shift;
62 21         38 my $params = shift;
63 21         35 my $seq = shift;
64            
65 21 0       66 $params = [defined $params ? $params : ()]
    50          
66             unless ref $params;
67            
68 21         134 my $dbh_method = 'dbh_modify';
69            
70 21         192 my $dbh = $self->dbh_modify;
71 21         114 my $rows_affected;
72            
73 21         37 eval {
74 21         148 my $sth = $self->statement ($statement, $dbh_method);
75 21         164 $self->bind_values ($sth, $params);
76              
77 21         346511 $rows_affected = $sth->execute;
78             # $sth->finish;
79            
80 21 100 66     863 if (! ref $statement and $statement =~ /^\s*insert/io and defined $rows_affected) {
      66        
81 16 50 33     256 if (defined $seq) {
    50          
82            
83 0         0 $rows_affected = $self->fetch_single ("select ${seq}.currval as maxid from dual");
84              
85             } elsif ($self->dbh_vendor ne 'oracle' and defined $self->_pk_) {
86 16         558 $rows_affected = $dbh->last_insert_id (
87             undef,
88             undef,
89             $self->table_name,
90             $self->_pk_column_
91             );
92             } else {
93             # try to deal with return of pk id instead rows_affected
94 0         0 $rows_affected = "0E$rows_affected";
95             }
96             }
97            
98             };
99            
100             return undef
101 21 50       747 if $self->_dbh_error ($@);
102            
103 21   50     366 return $rows_affected || 0;
104             }
105              
106             sub fetch_single {
107 16     16 0 32 my $self = shift;
108 16         24 my $statement = shift;
109 16         22 my $params = shift;
110            
111 16 0       54 $params = [defined $params ? $params : ()]
    50          
112             unless ref $params;
113            
114 16         57 my $dbh = $self->dbh;
115            
116 16         76 my $single;
117 16         24 eval {
118            
119 16         75 my $sth = $self->statement ($statement);
120            
121 16         114 $self->bind_values ($sth, $params);
122              
123 16 50       2811 die unless $sth->execute;
124            
125 16         106 $sth->bind_columns (\$single);
126            
127 16         1269 $sth->fetch;
128             };
129            
130 16 50       176 return if $self->_dbh_error ($@);
131            
132 16         54 return $single;
133             }
134              
135             sub fetch_column {
136 0     0 0 0 my $self = shift;
137 0         0 my $statement = shift;
138 0         0 my $params = shift;
139            
140 0 0       0 $params = [defined $params ? $params : ()]
    0          
141             unless ref $params;
142            
143 0         0 my $dbh = $self->dbh;
144            
145 0         0 my $single;
146             my $column;
147 0         0 eval {
148 0         0 my $sth = $dbh->prepare_cached($statement, {}, 3);
149              
150 0         0 $self->bind_values ($sth, $params);
151 0         0 my $rows_affected = $sth->execute;
152              
153 0         0 $sth->bind_columns(\$single);
154            
155 0         0 while ($sth->fetch) {
156 0         0 push @$column, $single;
157             }
158             };
159            
160 0 0       0 return if $self->_dbh_error ($@);
161            
162 0         0 return $column;
163             }
164              
165             sub fetch_columns {
166 0     0 0 0 my $self = shift;
167 0         0 my $statement = shift;
168 0         0 my $params = shift;
169            
170 0 0       0 $params = [defined $params ? $params : ()]
    0          
171             unless ref $params;
172            
173 0         0 my $dbh = $self->dbh;
174            
175 0         0 my $columns = [];
176 0         0 eval {
177 0         0 my $sth = $dbh->prepare_cached($statement, {}, 3);
178              
179 0         0 $self->bind_values ($sth, $params);
180 0         0 my $rows_affected = $sth->execute;
181              
182 0         0 while (my @arr = $sth->fetchrow_array) {
183 0         0 foreach (0 .. $#arr) {
184 0         0 push @{$columns->[$_]}, $arr[$_];
  0         0  
185             }
186             }
187             };
188            
189 0 0       0 return if $self->_dbh_error ($@);
190            
191 0         0 return $columns;
192             }
193              
194             sub fetch_row {
195 7     7 0 16 my $self = shift;
196 7         17 my $statement = shift;
197 7         13 my $params = shift;
198            
199 7 0       30 $params = [defined $params ? $params : ()]
    50          
200             unless ref $params;
201            
202 7         26 my $dbh = $self->dbh;
203            
204 7         37 my $row;
205 7         19 eval {
206 7         89 my $sth = $dbh->prepare_cached($statement, {}, 3);
207              
208 7         3144 $row = $dbh->selectrow_hashref ($sth, {}, @$params);
209             };
210            
211 7 50       2511 return if $self->_dbh_error ($@);
212            
213 7         29 return $row;
214             }
215              
216             sub fetch_row_in_place {
217 0     0 0 0 my $self = shift;
218            
219 0         0 my $row = $self->fetch_row (@_);
220            
221             # Hash::Merge::set_clone_behavior (0);
222              
223             # Hash::Merge::specify_behavior(
224             {
225             'SCALAR' => {
226 0     0   0 'SCALAR' => sub { $_[1] },
227             'ARRAY' => \&strict_behavior_error,
228             'HASH' => \&strict_behavior_error,
229             },
230             'ARRAY' => {
231             'SCALAR' => \&strict_behavior_error,
232 0     0   0 'ARRAY' => sub { $_[1] },
233             'HASH' => \&strict_behavior_error,
234             },
235             'HASH' => {
236             'SCALAR' => \&strict_behavior_error,
237             'ARRAY' => \&strict_behavior_error,
238 0     0   0 'HASH' => sub { Hash::Merge::_merge_hashes( $_[0], $_[1] ) },
239             },
240             },
241 0         0 'Strict Override',
242             #);
243            
244             # return unless "$structure" =~ /^(?:([^=]+)=)?([A-Z]+)\(0x([^\)]+)\)$/;
245             #
246             # my ($type, $address) = ($2, $3);
247            
248             # warn Dumper $self, $row;
249            
250             # Hash::Merge::merge ($self, $row);
251            
252             # warn Dumper $self;
253             }
254              
255             sub strict_behavior_error {
256 0     0 0 0 die "'", ref $_[0], "' to '", ref $_[1], "' not supported";
257             }
258              
259             sub fetch_hashref {
260 1     1 0 444 my $self = shift;
261 1         3 my $statement = shift;
262 1         3 my $key = shift;
263 1         2 my $params = shift;
264 1   50     7 $params ||= [];
265              
266 1         4 my $dbh = $self->dbh;
267 1         6 my $result;
268             my $rows_affected;
269              
270 1         2 eval {
271 1         5 my $sth = $self->statement ($statement);
272            
273 1         5 $self->bind_values ($sth, $params);
274 1         7 $rows_affected = $sth->execute;
275 1         25 $result = $sth->fetchall_hashref($key);
276             };
277              
278 1 50       130 return if $self->_dbh_error ($@);
279              
280 1         4 return $result;
281             }
282              
283             sub fetch_arrayref {
284 23     23 0 1863 my $self = shift;
285 23         42 my $statement = shift;
286 23         44 my $params = shift;
287 23   100     63 $params ||= [];
288            
289 23         34 my $sql_args = shift;
290 23   50     171 $sql_args ||= {Slice => {}, MaxRows => undef};
291            
292 23         43 my $fetch_handler = shift;
293            
294 23         89 my $dbh = $self->dbh;
295 23         124 my $result;
296             my $rows_affected;
297              
298 23         43 eval {
299 23         91 my $sth = $self->statement ($statement);
300 23         186 $self->bind_values ($sth, $params);
301 23         3438 $rows_affected = $sth->execute;
302 23         324 $result = $sth->fetchall_arrayref ($sql_args->{Slice}, $sql_args->{MaxRows});
303             };
304              
305 23 50       4147 return if $self->_dbh_error ($@);
306              
307 23         734 return $result;
308             }
309              
310             sub fetch_handled {
311 0     0 0   my $self = shift;
312 0           my $statement = shift;
313 0           my $params = shift;
314 0   0       $params ||= [];
315            
316 0           my $fetch_handler = shift;
317            
318 0           my $dbh = $self->dbh;
319 0           my $result;
320             my $rows_affected;
321            
322 0           my $failed = 0;
323            
324 0           eval {
325 0           my $sth = $self->statement ($statement);
326 0           $self->bind_values ($sth, $params);
327 0           $rows_affected = $sth->execute;
328 0           while (my $row = $sth->fetchrow_hashref) {
329 0 0         unless (defined &$fetch_handler ($row)) {
330 0           $failed = 1;
331 0           last;
332             }
333             }
334             };
335              
336 0 0         return if $self->_dbh_error ($@);
337              
338 0           return $rows_affected;
339             }
340              
341              
342             1;