File Coverage

blib/lib/DBD/Sponge.pm
Criterion Covered Total %
statement 72 81 88.8
branch 34 44 77.2
condition 8 10 80.0
subroutine 11 13 84.6
pod 0 1 0.0
total 125 149 83.8


line stmt bran cond sub pod time code
1             {
2             package DBD::Sponge;
3              
4             require DBI;
5             require Carp;
6              
7             our @EXPORT = qw(); # Do NOT @EXPORT anything.
8             our $VERSION = "12.010003";
9              
10             # $Id: Sponge.pm 10002 2007-09-26 21:03:25Z Tim $
11             #
12             # Copyright (c) 1994-2003 Tim Bunce Ireland
13             #
14             # You may distribute under the terms of either the GNU General Public
15             # License or the Artistic License, as specified in the Perl README file.
16              
17             $drh = undef; # holds driver handle once initialised
18             my $methods_already_installed;
19              
20             sub driver{
21 48 50   48 0 154 return $drh if $drh;
22              
23 48 50       617 DBD::Sponge::db->install_method("sponge_test_installed_method")
24             unless $methods_already_installed++;
25              
26 48         111 my($class, $attr) = @_;
27 48         88 $class .= "::dr";
28 48         350 ($drh) = DBI::_new_drh($class, {
29             'Name' => 'Sponge',
30             'Version' => $VERSION,
31             'Attribution' => "DBD::Sponge $VERSION (fake cursor driver) by Tim Bunce",
32             });
33 48         198 $drh;
34             }
35              
36             sub CLONE {
37 0     0   0 undef $drh;
38             }
39             }
40              
41              
42             { package DBD::Sponge::dr; # ====== DRIVER ======
43             $imp_data_size = 0;
44             # we use default (dummy) connect method
45             }
46              
47              
48             { package DBD::Sponge::db; # ====== DATABASE ======
49             $imp_data_size = 0;
50 48     48   249 use strict;
  48         76  
  48         27993  
51              
52             sub prepare {
53 92     92   48922 my($dbh, $statement, $attribs) = @_;
54 92 50       367 my $rows = delete $attribs->{'rows'}
55             or return $dbh->set_err($DBI::stderr,"No rows attribute supplied to prepare");
56 92 100       614 my ($outer, $sth) = DBI::_new_sth($dbh, {
57             'Statement' => $statement,
58             'rows' => $rows,
59 92         209 (map { exists $attribs->{$_} ? ($_=>$attribs->{$_}) : () }
60             qw(execute_hook)
61             ),
62             });
63 92 50       470 if (my $behave_like = $attribs->{behave_like}) {
64             $outer->{$_} = $behave_like->{$_}
65 0         0 foreach (qw(RaiseError PrintError HandleError ShowErrorStatement));
66             }
67              
68 92 100       317 if ($statement =~ /^\s*insert\b/) { # very basic, just for testing execute_array()
69 8         23 $sth->{is_insert} = 1;
70 8 50       26 my $NUM_OF_PARAMS = $attribs->{NUM_OF_PARAMS}
71             or return $dbh->set_err($DBI::stderr,"NUM_OF_PARAMS not specified for INSERT statement");
72 8         49 $sth->STORE('NUM_OF_PARAMS' => $attribs->{NUM_OF_PARAMS} );
73             }
74             else { #assume select
75              
76             # we need to set NUM_OF_FIELDS
77 84         108 my $numFields;
78 84 100       348 if ($attribs->{'NUM_OF_FIELDS'}) {
    100          
    50          
    50          
79 20         34 $numFields = $attribs->{'NUM_OF_FIELDS'};
80             } elsif ($attribs->{'NAME'}) {
81 52         61 $numFields = @{$attribs->{NAME}};
  52         104  
82             } elsif ($attribs->{'TYPE'}) {
83 0         0 $numFields = @{$attribs->{TYPE}};
  0         0  
84             } elsif (my $firstrow = $rows->[0]) {
85 12         16 $numFields = scalar @$firstrow;
86             } else {
87 0         0 return $dbh->set_err($DBI::stderr, 'Cannot determine NUM_OF_FIELDS');
88             }
89 84         411 $sth->STORE('NUM_OF_FIELDS' => $numFields);
90             $sth->{NAME} = $attribs->{NAME}
91 84   100     604 || [ map { "col$_" } 1..$numFields ];
92 84   100     556 $sth->{TYPE} = $attribs->{TYPE}
93             || [ (DBI::SQL_VARCHAR()) x $numFields ];
94             $sth->{PRECISION} = $attribs->{PRECISION}
95 84   50     1261 || [ map { length($sth->{NAME}->[$_]) } 0..$numFields -1 ];
96 84   50     461 $sth->{SCALE} = $attribs->{SCALE}
97             || [ (0) x $numFields ];
98 84   100     437 $sth->{NULLABLE} = $attribs->{NULLABLE}
99             || [ (2) x $numFields ];
100             }
101              
102 92         385 $outer;
103             }
104              
105             sub type_info_all {
106 0     0   0 my ($dbh) = @_;
107 0         0 my $ti = [
108             { TYPE_NAME => 0,
109             DATA_TYPE => 1,
110             PRECISION => 2,
111             LITERAL_PREFIX => 3,
112             LITERAL_SUFFIX => 4,
113             CREATE_PARAMS => 5,
114             NULLABLE => 6,
115             CASE_SENSITIVE => 7,
116             SEARCHABLE => 8,
117             UNSIGNED_ATTRIBUTE=> 9,
118             MONEY => 10,
119             AUTO_INCREMENT => 11,
120             LOCAL_TYPE_NAME => 12,
121             MINIMUM_SCALE => 13,
122             MAXIMUM_SCALE => 14,
123             },
124             [ 'VARCHAR', DBI::SQL_VARCHAR(), undef, "'","'", undef, 0, 1, 1, 0, 0,0,undef,0,0 ],
125             ];
126 0         0 return $ti;
127             }
128              
129             sub FETCH {
130 82     82   8999 my ($dbh, $attrib) = @_;
131             # In reality this would interrogate the database engine to
132             # either return dynamic values that cannot be precomputed
133             # or fetch and cache attribute values too expensive to prefetch.
134 82 100       212 return 1 if $attrib eq 'AutoCommit';
135             # else pass up to DBI to handle
136 78         412 return $dbh->SUPER::FETCH($attrib);
137             }
138              
139             sub STORE {
140 636     636   3879 my ($dbh, $attrib, $value) = @_;
141             # would normally validate and only store known attributes
142             # else pass up to DBI to handle
143 636 100       1095 if ($attrib eq 'AutoCommit') {
144 86 50       415 return 1 if $value; # is already set
145 0         0 Carp::croak("Can't disable AutoCommit");
146             }
147 550         2277 return $dbh->SUPER::STORE($attrib, $value);
148             }
149              
150             sub sponge_test_installed_method {
151 8     8   5105 my ($dbh, @args) = @_;
152 8 100       105 return $dbh->set_err(42, "not enough parameters") unless @args >= 2;
153 4         13 return \@args;
154             }
155             }
156              
157              
158             { package DBD::Sponge::st; # ====== STATEMENT ======
159             $imp_data_size = 0;
160 48     48   257 use strict;
  48         79  
  48         15611  
161              
162             sub execute {
163 116     116   4145 my $sth = shift;
164              
165             # hack to support ParamValues (when not using bind_param)
166 116 100       374 $sth->{ParamValues} = (@_) ? { map { $_ => $_[$_-1] } 1..@_ } : undef;
  240         466  
167              
168 116 100       373 if (my $hook = $sth->{execute_hook}) {
169 60 100       114 &$hook($sth, @_) or return;
170             }
171              
172 112 100       883 if ($sth->{is_insert}) {
173 56         59 my $row;
174 56 50       173 $row = (@_) ? [ @_ ] : die "bind_param not supported yet" ;
175 56         65 my $NUM_OF_PARAMS = $sth->{NUM_OF_PARAMS};
176 56 50       112 return $sth->set_err($DBI::stderr, @$row." values bound (@$row) but $NUM_OF_PARAMS expected")
177             if @$row != $NUM_OF_PARAMS;
178 56         50 { local $^W; $sth->trace_msg("inserting (@$row)\n"); }
  56         73  
  56         318  
179 56         222 push @{ $sth->{rows} }, $row;
  56         92  
180             }
181             else { # mark select sth as Active
182 56         165 $sth->STORE(Active => 1);
183             }
184             # else do nothing for select as data is already in $sth->{rows}
185 112         466 return 1;
186             }
187              
188             sub fetch {
189 392     392   48346 my ($sth) = @_;
190 392         361 my $row = shift @{$sth->{'rows'}};
  392         571  
191 392 100       742 unless ($row) {
192 64         203 $sth->STORE(Active => 0);
193 64         369 return undef;
194             }
195 328         1549 return $sth->_set_fbav($row);
196             }
197             *fetchrow_arrayref = \&fetch;
198              
199             sub FETCH {
200 61     61   317 my ($sth, $attrib) = @_;
201             # would normally validate and only fetch known attributes
202             # else pass up to DBI to handle
203 61         438 return $sth->SUPER::FETCH($attrib);
204             }
205              
206             sub STORE {
207 272     272   10318 my ($sth, $attrib, $value) = @_;
208             # would normally validate and only store known attributes
209             # else pass up to DBI to handle
210 272         1217 return $sth->SUPER::STORE($attrib, $value);
211             }
212             }
213              
214             1;
215              
216             __END__