Debian Codegraph @

lib/DBD/Mock/st.pm

raw
1 package DBD::Mock::st;
2
3 use strict;
4 use warnings;
5
6 our $imp_data_size = 0;
7
8 sub bind_col {
9 my ( $sth, $param_num, $ref, $attr ) = @_;
10
11 my $tracker = $sth->FETCH('mock_my_history');
12 $tracker->bind_col( $param_num, $ref );
13 return 1;
14 }
15
16 sub bind_param {
17 my ( $sth, $param_num, $val, $attr ) = @_;
18 my $tracker = $sth->FETCH('mock_my_history');
19 $tracker->bound_param( $param_num, $val, $attr );
20 return 1;
21 }
22
23 sub bind_param_array {
24 bind_param(@_);
25 }
26
27 sub bind_param_inout {
28 my ( $sth, $param_num, $val, $max_len ) = @_;
29
30 # check that $val is a scalar ref
31 ( UNIVERSAL::isa( $val, 'SCALAR' ) )
32 || $sth->{Database}
33 ->set_err( 1, "need a scalar ref to bind_param_inout, not $val" );
34
35 # check for positive $max_len
36 ( $max_len > 0 )
37 || $sth->{Database}
38 ->set_err( 1, "need to specify a maximum length to bind_param_inout" );
39 my $tracker = $sth->FETCH('mock_my_history');
40 $tracker->bound_param( $param_num, $val );
41 return 1;
42 }
43
44 sub execute_array {
45 my ( $sth, $attr, @bind_values ) = @_;
46
47 # no bind values means we're relying on prior calls to bind_param_array()
48 # for our data
49 my $tracker = $sth->FETCH('mock_my_history');
50 # don't use a reference; there's some magic attached to it somewhere
51 # so make it a lovely, simple array as soon as possible
52 my @bound = @{ $tracker->bound_params() };
53 foreach my $p (@bound) {
54 my $result = $sth->execute( @$p );
55 # store the result from execute() if ArrayTupleStatus attribute is
56 # passed
57 push @{ $attr->{ArrayTupleStatus} }, $result
58 if (exists $attr->{ArrayTupleStatus});
59 }
60
61 # TODO: the docs say:
62 # When called in scalar context the execute_array() method returns the
63 # number of tuples executed, or undef if an error occurred. Like
64 # execute(), a successful execute_array() always returns true regardless
65 # of the number of tuples executed, even if it's zero. If there were any
66 # errors the ArrayTupleStatus array can be used to discover which tuples
67 # failed and with what errors.
68 # When called in list context the execute_array() method returns two
69 # scalars; $tuples is the same as calling execute_array() in scalar
70 # context and $rows is the number of rows affected for each tuple, if
71 # available or -1 if the driver cannot determine this.
72 # We have glossed over this...
73 return scalar @bound;
74 }
75
76 sub execute {
77 my ( $sth, @params ) = @_;
78 my $dbh = $sth->{Database};
79
80 unless ( $dbh->{mock_can_connect} ) {
81 $dbh->set_err( 1, "No connection present" );
82 return 0;
83 }
84 unless ( $dbh->{mock_can_execute} ) {
85 $dbh->set_err( 1, "Cannot execute" );
86 return 0;
87 }
88 $dbh->{mock_can_execute}++ if $dbh->{mock_can_execute} < 0;
89
90 my $tracker = $sth->FETCH('mock_my_history');
91
92 if ( $tracker->has_failure() ) {
93 $dbh->set_err( $tracker->get_failure() );
94 return 0;
95 }
96
97 if (@params) {
98 $tracker->bind_params(@params);
99 }
100
101 if ( my $session = $dbh->{mock_session} ) {
102 eval {
103 my $state = $session->current_state;
104 $session->verify_statement( $sth->{Statement});
105 $session->verify_bound_params( $tracker->bound_params(),
106 { bound_arrays => $dbh->{mock_bound_array_support} } );
107
108 # Load a copy of the results to return (minus the field
109 # names) into the tracker
110 my @results = @{ $state->{results} };
111 shift @results;
112 $tracker->{return_data} = \@results;
113 };
114 if ($@) {
115 my $session_error = $@;
116 chomp $session_error;
117 $sth->set_err( 1, "Session Error: ${session_error}" );
118 return;
119 }
120 }
121
122 $tracker->mark_executed;
123 my $fields = $tracker->fields;
124 $sth->STORE( NUM_OF_FIELDS => scalar @{ $fields ? $fields : [] } );
125 $sth->STORE( NAME => $fields );
126
127 $sth->STORE( NUM_OF_PARAMS => $tracker->num_params );
128
129 # handle INSERT statements and the mock_last_insert_ids
130 # We should only increment these things after the last successful INSERT.
131 # -RobK, 2007-10-12
132 #use Data::Dumper;warn Dumper $dbh->{mock_last_insert_ids};
133
134 if ( $dbh->{Statement} =~ /^\s*?insert(?:\s+ignore)?\s+into\s+(\S+)/i ) {
135 if ( $tracker->{last_insert_id} ) {
136 $dbh->{mock_last_insert_id} = $tracker->{last_insert_id};
137
138 } elsif ( $dbh->{mock_last_insert_ids}
139 && exists $dbh->{mock_last_insert_ids}{$1} )
140 {
141 $dbh->{mock_last_insert_id} = $dbh->{mock_last_insert_ids}{$1}++;
142 }
143 else {
144 $dbh->{mock_last_insert_id}++;
145 }
146 }
147
148 #warn "$dbh->{mock_last_insert_id}\n";
149
150 # always return 0E0 for Selects
151 if ( $dbh->{Statement} =~ /^\s*?select/i ) {
152 return '0E0';
153 }
154 return ( $sth->rows() || '0E0' );
155 }
156
157 sub fetch {
158 my ($sth) = @_;
159 my $dbh = $sth->{Database};
160 unless ( $dbh->{mock_can_connect} ) {
161 $dbh->set_err( 1, "No connection present" );
162 return;
163 }
164 unless ( $dbh->{mock_can_fetch} ) {
165 $dbh->set_err( 1, "Cannot fetch" );
166 return;
167 }
168 $dbh->{mock_can_fetch}++ if $dbh->{mock_can_fetch} < 0;
169
170 my $tracker = $sth->FETCH('mock_my_history');
171
172 my $record = $tracker->next_record
173 or return;
174
175 if ( my @cols = $tracker->bind_cols() ) {
176 for my $i ( grep { ref $cols[$_] } 0 .. $#cols ) {
177 ${ $cols[$i] } = $record->[$i];
178 }
179 }
180
181 return $record;
182 }
183
184 sub fetchrow_array {
185 my ($sth) = @_;
186 my $row = $sth->DBD::Mock::st::fetch();
187 return unless ref($row) eq 'ARRAY';
188 return @{$row};
189 }
190
191 sub fetchrow_arrayref {
192 my ($sth) = @_;
193 return $sth->DBD::Mock::st::fetch();
194 }
195
196 sub fetchrow_hashref {
197 my ( $sth, $name ) = @_;
198 my $dbh = $sth->{Database};
199
200 # handle any errors since we are grabbing
201 # from the tracker directly
202 unless ( $dbh->{mock_can_connect} ) {
203 $dbh->set_err( 1, "No connection present" );
204 return;
205 }
206 unless ( $dbh->{mock_can_fetch} ) {
207 $dbh->set_err( 1, "Cannot fetch" );
208 return;
209 }
210 $dbh->{mock_can_fetch}++ if $dbh->{mock_can_fetch} < 0;
211
212 # first handle the $name, it will default to NAME
213 $name ||= 'NAME';
214
215 # then fetch the names from the $sth (per DBI spec)
216 my $fields = $sth->FETCH($name);
217
218 # now check the tracker ...
219 my $tracker = $sth->FETCH('mock_my_history');
220
221 # and collect the results
222 if ( my $record = $tracker->next_record() ) {
223 my @values = @{$record};
224 return { map { $_ => shift(@values) } @{$fields} };
225 }
226
227 return undef;
228 }
229
230 #XXX Isn't this supposed to return an array of hashrefs? -RobK, 2007-10-15
231 sub fetchall_hashref {
232 my ( $sth, $keyfield ) = @_;
233 my $dbh = $sth->{Database};
234
235 # handle any errors since we are grabbing
236 # from the tracker directly
237 unless ( $dbh->{mock_can_connect} ) {
238 $dbh->set_err( 1, "No connection present" );
239 return;
240 }
241 unless ( $dbh->{mock_can_fetch} ) {
242 $dbh->set_err( 1, "Cannot fetch" );
243 return;
244 }
245 $dbh->{mock_can_fetch}++ if $dbh->{mock_can_fetch} < 0;
246
247 # get the case conversion to use for hash key names (NAME/NAME_lc/NAME_uc)
248 my $hash_key_name = $sth->{Database}->FETCH('FetchHashKeyName') || 'NAME';
249
250 # get a hashref mapping field names to their corresponding indexes. indexes
251 # start at zero
252 my $names_hash = $sth->FETCH("${hash_key_name}_hash");
253
254 # as of DBI v1.48, the $keyfield argument can be either an arrayref of field
255 # names/indexes or a single field name/index
256 my @key_fields = ref $keyfield ? @{$keyfield} : $keyfield;
257
258 my $num_fields = $sth->FETCH('NUM_OF_FIELDS');
259
260 # get the index(es) of the given key field(s). a key field can be specified
261 # as either the name of a field or an integer column number
262 my @key_indexes;
263 foreach my $field (@key_fields) {
264 if (defined $names_hash->{$field}) {
265 push @key_indexes, $names_hash->{$field};
266 }
267 elsif (DBI::looks_like_number($field) && $field >= 1 && $field <= $num_fields) {
268 # convert from column number to array index. column numbers start at
269 # one, while indexes start at zero
270 push @key_indexes, $field - 1;
271 }
272 else {
273 my $err = "Could not find key field '$field' (not one of " .
274 join(' ', keys %{$names_hash}) . ')';
275 $dbh->set_err( 1, $err );
276 return;
277 }
278 }
279
280 my $tracker = $sth->FETCH('mock_my_history');
281 my $rethash = {};
282
283 # now loop through all the records ...
284 while ( my $record = $tracker->next_record() ) {
285
286 # populate the hash, adding a layer of nesting for each key field
287 # specified by the user
288 my $ref = $rethash;
289 foreach my $index (@key_indexes) {
290 my $value = $record->[$index];
291 $ref->{$value} = {} if ! defined $ref->{$value};
292 $ref = $ref->{$value};
293 }
294
295 # copy all of the returned data into the most-nested level of the hash
296 foreach my $field (keys %{$names_hash}) {
297 my $index = $names_hash->{$field};
298 $ref->{$field} = $record->[$index];
299 }
300 }
301
302 return $rethash;
303 }
304
305 sub last_insert_id {
306 my ( $sth, @params ) = @_;
307 return $sth->{Database}->last_insert_id( @params );
308 }
309
310 sub finish {
311 my ($sth) = @_;
312 $sth->FETCH('mock_my_history')->is_finished('yes');
313 }
314
315 sub rows {
316 my ($sth) = @_;
317 $sth->FETCH('mock_num_rows');
318 }
319
320 sub FETCH {
321 my ( $sth, $attrib ) = @_;
322 $sth->trace_msg("Fetching ST attribute '$attrib'\n");
323 my $tracker = $sth->{mock_my_history};
324 $sth->trace_msg( "Retrieved tracker: " . ref($tracker) . "\n" );
325
326 # NAME attributes
327 if ( $attrib eq 'NAME' ) {
328 return [ @{ $tracker->fields } ];
329 }
330 elsif ( $attrib eq 'NAME_lc' ) {
331 return [ map { lc($_) } @{ $tracker->fields } ];
332 }
333 elsif ( $attrib eq 'NAME_uc' ) {
334 return [ map { uc($_) } @{ $tracker->fields } ];
335 }
336
337 # NAME_hash attributes
338 elsif ( $attrib eq 'NAME_hash' ) {
339 my $i = 0;
340 return { map { $_ => $i++ } @{ $tracker->fields } };
341 }
342 elsif ( $attrib eq 'NAME_hash_lc' ) {
343 my $i = 0;
344 return { map { lc($_) => $i++ } @{ $tracker->fields } };
345 }
346 elsif ( $attrib eq 'NAME_hash_uc' ) {
347 my $i = 0;
348 return { map { uc($_) => $i++ } @{ $tracker->fields } };
349 }
350
351 # others
352 elsif ( $attrib eq 'NUM_OF_FIELDS' ) {
353 return $tracker->num_fields;
354 }
355 elsif ( $attrib eq 'NUM_OF_PARAMS' ) {
356 return $tracker->num_params;
357 }
358 elsif ( $attrib eq 'TYPE' ) {
359 my $num_fields = $tracker->num_fields;
360 return [ map { $DBI::SQL_VARCHAR } ( 0 .. $num_fields ) ];
361 }
362 elsif ( $attrib eq 'Active' ) {
363 return $tracker->is_active;
364 }
365 elsif ( exists $tracker->{driver_attributes}->{$attrib} ) {
366 return $tracker->{driver_attributes}->{$attrib};
367 }
368 elsif ( $attrib !~ /^mock/ ) {
369 if ( $sth->{Database}->{mock_attribute_aliases} ) {
370 if (
371 exists ${ $sth->{Database}->{mock_attribute_aliases}->{st} }
372 {$attrib} )
373 {
374 my $mock_attrib =
375 $sth->{Database}->{mock_attribute_aliases}->{st}->{$attrib};
376 if ( ref($mock_attrib) eq 'CODE' ) {
377 return $mock_attrib->($sth);
378 }
379 else {
380 return $sth->FETCH($mock_attrib);
381 }
382 }
383 }
384 return $sth->SUPER::FETCH($attrib);
385 }
386
387 # now do our stuff...
388
389 if ( $attrib eq 'mock_my_history' ) {
390 return $tracker;
391 }
392 elsif ( $attrib eq 'mock_execution_history' ) {
393 return $tracker->execution_history();
394 }
395 elsif ( $attrib eq 'mock_statement' ) {
396 return $tracker->statement;
397 }
398 elsif ( $attrib eq 'mock_params' ) {
399 return $tracker->bound_params;
400 }
401 elsif ( $attrib eq 'mock_param_attrs' ) {
402 return $tracker->bound_param_attrs;
403 }
404 elsif ( $attrib eq 'mock_records' ) {
405 return $tracker->return_data;
406 }
407 elsif ( $attrib eq 'mock_num_records' || $attrib eq 'mock_num_rows' ) {
408 return $tracker->num_rows;
409 }
410 elsif ( $attrib eq 'mock_current_record_num' ) {
411 return $tracker->current_record_num;
412 }
413 elsif ( $attrib eq 'mock_fields' ) {
414 return $tracker->fields;
415 }
416 elsif ( $attrib eq 'mock_is_executed' ) {
417 return $tracker->is_executed;
418 }
419 elsif ( $attrib eq 'mock_is_finished' ) {
420 return $tracker->is_finished;
421 }
422 elsif ( $attrib eq 'mock_is_depleted' ) {
423 return $tracker->is_depleted;
424 }
425 else {
426 die "I don't know how to retrieve statement attribute '$attrib'\n";
427 }
428 }
429
430 sub STORE {
431 my ( $sth, $attrib, $value ) = @_;
432 $sth->trace_msg("Storing ST attribute '$attrib'\n");
433 if ( $attrib =~ /^mock/ ) {
434 return $sth->{$attrib} = $value;
435 }
436 elsif ( $attrib =~ /^NAME/ ) {
437
438 # no-op...
439 return;
440 }
441 else {
442 $value ||= 0;
443 return $sth->SUPER::STORE( $attrib, $value );
444 }
445 }
446
447 sub DESTROY { undef }
448
449 1;