This file is indexed.

/usr/share/perl5/DBD/Mock/st.pm is in libdbd-mock-perl 1.45-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
package DBD::Mock::st;

use strict;
use warnings;

our $imp_data_size = 0;

sub bind_col {
    my ( $sth, $param_num, $ref, $attr ) = @_;

    my $tracker = $sth->FETCH('mock_my_history');
    $tracker->bind_col( $param_num, $ref );
    return 1;
}

sub bind_param {
    my ( $sth, $param_num, $val, $attr ) = @_;
    my $tracker = $sth->FETCH('mock_my_history');
    $tracker->bound_param( $param_num, $val );
    return 1;
}

sub bind_param_array {
    bind_param(@_);
}

sub bind_param_inout {
    my ( $sth, $param_num, $val, $max_len ) = @_;

    # check that $val is a scalar ref
    ( UNIVERSAL::isa( $val, 'SCALAR' ) )
      || $sth->{Database}
      ->set_err( 1, "need a scalar ref to bind_param_inout, not $val" );

    # check for positive $max_len
    ( $max_len > 0 )
      || $sth->{Database}
      ->set_err( 1, "need to specify a maximum length to bind_param_inout" );
    my $tracker = $sth->FETCH('mock_my_history');
    $tracker->bound_param( $param_num, $val );
    return 1;
}

sub execute_array {
    my ( $sth, $attr, @bind_values ) = @_;

    # no bind values means we're relying on prior calls to bind_param_array()
    # for our data
    my $tracker = $sth->FETCH('mock_my_history');
    # don't use a reference; there's some magic attached to it somewhere
    # so make it a lovely, simple array as soon as possible
    my @bound = @{ $tracker->bound_params() };
    foreach my $p (@bound) {
        my $result = $sth->execute( @$p );
        # store the result from execute() if ArrayTupleStatus attribute is
        # passed
        push @{ $attr->{ArrayTupleStatus} }, $result
            if (exists $attr->{ArrayTupleStatus});
    }

    # TODO: the docs say:
    #   When called in scalar context the execute_array() method returns the
    #   number of tuples executed, or undef if an error occurred. Like
    #   execute(), a successful execute_array() always returns true regardless
    #   of the number of tuples executed, even if it's zero. If there were any
    #   errors the ArrayTupleStatus array can be used to discover which tuples
    #   failed and with what errors.
    #   When called in list context the execute_array() method returns two
    #   scalars; $tuples is the same as calling execute_array() in scalar
    #   context and $rows is the number of rows affected for each tuple, if
    #   available or -1 if the driver cannot determine this. 
    # We have glossed over this...
    return scalar @bound;
}

sub execute {
    my ( $sth, @params ) = @_;
    my $dbh = $sth->{Database};

    unless ( $dbh->{mock_can_connect} ) {
        $dbh->set_err( 1, "No connection present" );
        return 0;
    }
    unless ( $dbh->{mock_can_execute} ) {
        $dbh->set_err( 1, "Cannot execute" );
        return 0;
    }
    $dbh->{mock_can_execute}++ if $dbh->{mock_can_execute} < 0;

    my $tracker = $sth->FETCH('mock_my_history');

    if ( $tracker->has_failure() ) {
        $dbh->set_err( $tracker->get_failure() );
        return 0;
    }

    if (@params) {
        $tracker->bind_params(@params);
    }

    if ( my $session = $dbh->{mock_session} ) {
        eval {
            my $state = $session->current_state;
            $session->verify_statement( $sth->{Statement});
            $session->verify_bound_params( $tracker->bound_params() );

            # Load a copy of the results to return (minus the field
            # names) into the tracker
            my @results = @{ $state->{results} };
            shift @results;
            $tracker->{return_data} = \@results;
        };
        if ($@) {
            my $session_error = $@;
            chomp $session_error;
            $sth->set_err( 1, "Session Error: ${session_error}" );
            return;
        }
    }

    $tracker->mark_executed;
    my $fields = $tracker->fields;
    $sth->STORE( NUM_OF_PARAMS => $tracker->num_params );

    # handle INSERT statements and the mock_last_insert_ids
    # We should only increment these things after the last successful INSERT.
    # -RobK, 2007-10-12
    #use Data::Dumper;warn Dumper $dbh->{mock_last_insert_ids};

    if ( $dbh->{Statement} =~ /^\s*?insert\s+into\s+(\S+)/i ) {
        if ( $dbh->{mock_last_insert_ids}
            && exists $dbh->{mock_last_insert_ids}{$1} )
        {
            $dbh->{mock_last_insert_id} = $dbh->{mock_last_insert_ids}{$1}++;
        }
        else {
            $dbh->{mock_last_insert_id}++;
        }
    }

    #warn "$dbh->{mock_last_insert_id}\n";

    # always return 0E0 for Selects
    if ( $dbh->{Statement} =~ /^\s*?select/i ) {
        return '0E0';
    }
    return ( $sth->rows() || '0E0' );
}

sub fetch {
    my ($sth) = @_;
    my $dbh = $sth->{Database};
    unless ( $dbh->{mock_can_connect} ) {
        $dbh->set_err( 1, "No connection present" );
        return;
    }
    unless ( $dbh->{mock_can_fetch} ) {
        $dbh->set_err( 1, "Cannot fetch" );
        return;
    }
    $dbh->{mock_can_fetch}++ if $dbh->{mock_can_fetch} < 0;

    my $tracker = $sth->FETCH('mock_my_history');

    my $record = $tracker->next_record
      or return;

    if ( my @cols = $tracker->bind_cols() ) {
        for my $i ( grep { ref $cols[$_] } 0 .. $#cols ) {
            ${ $cols[$i] } = $record->[$i];
        }
    }

    return $record;
}

sub fetchrow_array {
    my ($sth) = @_;
    my $row = $sth->DBD::Mock::st::fetch();
    return unless ref($row) eq 'ARRAY';
    return @{$row};
}

sub fetchrow_arrayref {
    my ($sth) = @_;
    return $sth->DBD::Mock::st::fetch();
}

sub fetchrow_hashref {
    my ( $sth, $name ) = @_;
    my $dbh = $sth->{Database};

    # handle any errors since we are grabbing
    # from the tracker directly
    unless ( $dbh->{mock_can_connect} ) {
        $dbh->set_err( 1, "No connection present" );
        return;
    }
    unless ( $dbh->{mock_can_fetch} ) {
        $dbh->set_err( 1, "Cannot fetch" );
        return;
    }
    $dbh->{mock_can_fetch}++ if $dbh->{mock_can_fetch} < 0;

    # first handle the $name, it will default to NAME
    $name ||= 'NAME';

    # then fetch the names from the $sth (per DBI spec)
    my $fields = $sth->FETCH($name);

    # now check the tracker ...
    my $tracker = $sth->FETCH('mock_my_history');

    # and collect the results
    if ( my $record = $tracker->next_record() ) {
        my @values = @{$record};
        return { map { $_ => shift(@values) } @{$fields} };
    }

    return undef;
}

#XXX Isn't this supposed to return an array of hashrefs? -RobK, 2007-10-15
sub fetchall_hashref {
    my ( $sth, $keyfield ) = @_;
    my $dbh = $sth->{Database};

    # handle any errors since we are grabbing
    # from the tracker directly
    unless ( $dbh->{mock_can_connect} ) {
        $dbh->set_err( 1, "No connection present" );
        return;
    }
    unless ( $dbh->{mock_can_fetch} ) {
        $dbh->set_err( 1, "Cannot fetch" );
        return;
    }
    $dbh->{mock_can_fetch}++ if $dbh->{mock_can_fetch} < 0;

    my $tracker = $sth->FETCH('mock_my_history');
    my $rethash = {};

    # get the name set by
    my $name = $sth->{Database}->FETCH('FetchHashKeyName') || 'NAME';
    my $fields = $sth->FETCH($name);

    # check if $keyfield is not an integer
    if ( !( $keyfield =~ /^-?\d+$/ ) ) {
        my $found = 0;

        # search for index of item that matches $keyfield
        foreach my $index ( 0 .. scalar( @{$fields} ) ) {
            if ( $fields->[$index] eq $keyfield ) {
                $found++;

                # now make the keyfield the index
                $keyfield = $index;

                # and jump out of the loop :)
                last;
            }
        }
        unless ($found) {
            $dbh->set_err( 1, "Could not find key field '$keyfield'" );
            return;
        }
    }

    # now loop through all the records ...
    while ( my $record = $tracker->next_record() ) {

        # copy the values so as to preserve
        # the original record...
        my @values = @{$record};

        # populate the hash
        $rethash->{ $record->[$keyfield] } =
          { map { $_ => shift(@values) } @{$fields} };
    }

    return $rethash;
}

sub finish {
    my ($sth) = @_;
    $sth->FETCH('mock_my_history')->is_finished('yes');
}

sub rows {
    my ($sth) = @_;
    $sth->FETCH('mock_num_rows');
}

sub FETCH {
    my ( $sth, $attrib ) = @_;
    $sth->trace_msg("Fetching ST attribute '$attrib'\n");
    my $tracker = $sth->{mock_my_history};
    $sth->trace_msg( "Retrieved tracker: " . ref($tracker) . "\n" );

    # NAME attributes
    if ( $attrib eq 'NAME' ) {
        return [ @{ $tracker->fields } ];
    }
    elsif ( $attrib eq 'NAME_lc' ) {
        return [ map { lc($_) } @{ $tracker->fields } ];
    }
    elsif ( $attrib eq 'NAME_uc' ) {
        return [ map { uc($_) } @{ $tracker->fields } ];
    }

    # NAME_hash attributes
    elsif ( $attrib eq 'NAME_hash' ) {
        my $i = 0;
        return { map { $_ => $i++ } @{ $tracker->fields } };
    }
    elsif ( $attrib eq 'NAME_hash_lc' ) {
        my $i = 0;
        return { map { lc($_) => $i++ } @{ $tracker->fields } };
    }
    elsif ( $attrib eq 'NAME_hash_uc' ) {
        my $i = 0;
        return { map { uc($_) => $i++ } @{ $tracker->fields } };
    }

    # others
    elsif ( $attrib eq 'NUM_OF_FIELDS' ) {
        return $tracker->num_fields;
    }
    elsif ( $attrib eq 'NUM_OF_PARAMS' ) {
        return $tracker->num_params;
    }
    elsif ( $attrib eq 'TYPE' ) {
        my $num_fields = $tracker->num_fields;
        return [ map { $DBI::SQL_VARCHAR } ( 0 .. $num_fields ) ];
    }
    elsif ( $attrib eq 'Active' ) {
        return $tracker->is_active;
    }
    elsif ( $attrib !~ /^mock/ ) {
        if ( $sth->{Database}->{mock_attribute_aliases} ) {
            if (
                exists ${ $sth->{Database}->{mock_attribute_aliases}->{st} }
                {$attrib} )
            {
                my $mock_attrib =
                  $sth->{Database}->{mock_attribute_aliases}->{st}->{$attrib};
                if ( ref($mock_attrib) eq 'CODE' ) {
                    return $mock_attrib->($sth);
                }
                else {
                    return $sth->FETCH($mock_attrib);
                }
            }
        }
        return $sth->SUPER::FETCH($attrib);
    }

    # now do our stuff...

    if ( $attrib eq 'mock_my_history' ) {
        return $tracker;
    }
    if ( $attrib eq 'mock_statement' ) {
        return $tracker->statement;
    }
    elsif ( $attrib eq 'mock_params' ) {
        return $tracker->bound_params;
    }
    elsif ( $attrib eq 'mock_records' ) {
        return $tracker->return_data;
    }
    elsif ( $attrib eq 'mock_num_records' || $attrib eq 'mock_num_rows' ) {
        return $tracker->num_rows;
    }
    elsif ( $attrib eq 'mock_current_record_num' ) {
        return $tracker->current_record_num;
    }
    elsif ( $attrib eq 'mock_fields' ) {
        return $tracker->fields;
    }
    elsif ( $attrib eq 'mock_is_executed' ) {
        return $tracker->is_executed;
    }
    elsif ( $attrib eq 'mock_is_finished' ) {
        return $tracker->is_finished;
    }
    elsif ( $attrib eq 'mock_is_depleted' ) {
        return $tracker->is_depleted;
    }
    else {
        die "I don't know how to retrieve statement attribute '$attrib'\n";
    }
}

sub STORE {
    my ( $sth, $attrib, $value ) = @_;
    $sth->trace_msg("Storing ST attribute '$attrib'\n");
    if ( $attrib =~ /^mock/ ) {
        return $sth->{$attrib} = $value;
    }
    elsif ( $attrib =~ /^NAME/ ) {

        # no-op...
        return;
    }
    else {
        $value ||= 0;
        return $sth->SUPER::STORE( $attrib, $value );
    }
}

sub DESTROY { undef }

1;