File Coverage

blib/lib/DBD/ExampleP.pm
Criterion Covered Total %
statement 178 181 98.3
branch 79 88 89.7
condition 25 32 78.1
subroutine 28 30 93.3
pod 0 1 0.0
total 310 332 93.3


line stmt bran cond sub pod time code
1             {
2             package DBD::ExampleP;
3              
4 60     60   356 use strict;
  60         106  
  60         1803  
5 60     60   8357 use Symbol;
  60         21623  
  60         3473  
6              
7 60     60   364 use DBI qw(:sql_types);
  60         106  
  60         34608  
8              
9             require File::Spec;
10            
11             our (@EXPORT,$VERSION,@statnames,%statnames,@stattypes,%stattypes,
12             @statprec,%statprec,$drh,);
13              
14             @EXPORT = qw(); # Do NOT @EXPORT anything.
15             $VERSION = "12.014311";
16              
17             # $Id: ExampleP.pm 14310 2010-08-02 06:35:25Z Jens $
18             #
19             # Copyright (c) 1994,1997,1998 Tim Bunce
20             #
21             # You may distribute under the terms of either the GNU General Public
22             # License or the Artistic License, as specified in the Perl README file.
23              
24             @statnames = qw(dev ino mode nlink
25             uid gid rdev size
26             atime mtime ctime
27             blksize blocks name);
28             @statnames{@statnames} = (0 .. @statnames-1);
29              
30             @stattypes = (SQL_INTEGER, SQL_INTEGER, SQL_INTEGER, SQL_INTEGER,
31             SQL_INTEGER, SQL_INTEGER, SQL_INTEGER, SQL_INTEGER,
32             SQL_INTEGER, SQL_INTEGER, SQL_INTEGER,
33             SQL_INTEGER, SQL_INTEGER, SQL_VARCHAR);
34             @stattypes{@statnames} = @stattypes;
35             @statprec = ((10) x (@statnames-1), 1024);
36             @statprec{@statnames} = @statprec;
37             die unless @statnames == @stattypes;
38             die unless @statprec == @stattypes;
39              
40             $drh = undef; # holds driver handle once initialised
41             #$gensym = "SYM000"; # used by st::execute() for filehandles
42              
43             sub driver{
44 60 50   60 0 215 return $drh if $drh;
45 60         145 my($class, $attr) = @_;
46 60         146 $class .= "::dr";
47 60         402 ($drh) = DBI::_new_drh($class, {
48             'Name' => 'ExampleP',
49             'Version' => $VERSION,
50             'Attribution' => 'DBD Example Perl stub by Tim Bunce',
51             }, ['example implementors private data '.__PACKAGE__]);
52 60         239 $drh;
53             }
54              
55             sub CLONE {
56 0     0   0 undef $drh;
57             }
58             }
59              
60              
61             { package DBD::ExampleP::dr; # ====== DRIVER ======
62             $imp_data_size = 0;
63 60     60   415 use strict;
  60         118  
  60         7472  
64              
65             sub connect { # normally overridden, but a handy default
66 2409     2409   12909 my($drh, $dbname, $user, $auth)= @_;
67 2409         10262 my ($outer, $dbh) = DBI::_new_dbh($drh, {
68             Name => $dbname,
69             examplep_private_dbh_attrib => 42, # an example, for testing
70             });
71             $dbh->{examplep_get_info} = {
72 2409         10161 29 => '"', # SQL_IDENTIFIER_QUOTE_CHAR
73             41 => '.', # SQL_CATALOG_NAME_SEPARATOR
74             114 => 1, # SQL_CATALOG_LOCATION
75             };
76             #$dbh->{Name} = $dbname;
77 2409         10546 $dbh->STORE('Active', 1);
78 2409         10504 return $outer;
79             }
80              
81             sub data_sources {
82 0     0   0 return ("dbi:ExampleP:dir=."); # possibly usefully meaningless
83             }
84              
85             }
86              
87              
88             { package DBD::ExampleP::db; # ====== DATABASE ======
89             $imp_data_size = 0;
90 60     60   366 use strict;
  60         114  
  60         27934  
91              
92             sub prepare {
93 5756     5756   94818 my($dbh, $statement)= @_;
94 5756         6774 my @fields;
95 5756         26318 my($fields, $dir) = $statement =~ m/^\s*select\s+(.*?)\s+from\s+(\S*)/i;
96              
97 5756 100 66     18071 if (defined $fields and defined $dir) {
98 4295 100       18855 @fields = ($fields eq '*')
99             ? keys %DBD::ExampleP::statnames
100             : split(/\s*,\s*/, $fields);
101             }
102             else {
103 1461 50       7381 return $dbh->set_err($DBI::stderr, "Syntax error in select statement (\"$statement\")")
104             unless $statement =~ m/^\s*set\s+/;
105             # the SET syntax is just a hack so the ExampleP driver can
106             # be used to test non-select statements.
107             # Now we have DBI::DBM etc., ExampleP should be deprecated
108             }
109              
110 5756         24523 my ($outer, $sth) = DBI::_new_sth($dbh, {
111             'Statement' => $statement,
112             examplep_private_sth_attrib => 24, # an example, for testing
113             }, ['example implementors private data '.__PACKAGE__]);
114              
115             my @bad = map {
116 5756 100       12682 defined $DBD::ExampleP::statnames{$_} ? () : $_
  8569         17710  
117             } @fields;
118 5756 100       11042 return $dbh->set_err($DBI::stderr, "Unknown field names: @bad")
119             if @bad;
120              
121 5720         21630 $outer->STORE('NUM_OF_FIELDS' => scalar(@fields));
122              
123 5720 100 100     28231 $sth->{examplep_ex_dir} = $dir if defined($dir) && $dir !~ /\?/;
124 5720 100       19139 $outer->STORE('NUM_OF_PARAMS' => ($dir) ? $dir =~ tr/?/?/ : 0);
125              
126 5720 100       17852 if (@fields) {
127 4259         10403 $outer->STORE('NAME' => \@fields);
128 4259         20322 $outer->STORE('NULLABLE' => [ (0) x @fields ]);
129 4259         17527 $outer->STORE('SCALE' => [ (0) x @fields ]);
130             }
131              
132 5720         28161 $outer;
133             }
134              
135              
136             sub table_info {
137 16     16   2410 my $dbh = shift;
138 16         39 my ($catalog, $schema, $table, $type) = @_;
139              
140 16   100     100 my @types = split(/["']*,["']/, $type || 'TABLE');
141 16         37 my %types = map { $_=>$_ } @types;
  16         62  
142              
143             # Return a list of all subdirectories
144 16         63 my $dh = Symbol::gensym(); # "DBD::ExampleP::".++$DBD::ExampleP::gensym;
145 16   66     306 my $dir = $catalog || File::Spec->curdir();
146 16         28 my @list;
147 16 100       43 if ($types{VIEW}) { # for use by test harness
148 4         14 push @list, [ undef, "schema", "table", 'VIEW', undef ];
149 4         14 push @list, [ undef, "sch-ema", "table", 'VIEW', undef ];
150 4         12 push @list, [ undef, "schema", "ta-ble", 'VIEW', undef ];
151 4         12 push @list, [ undef, "sch ema", "table", 'VIEW', undef ];
152 4         14 push @list, [ undef, "schema", "ta ble", 'VIEW', undef ];
153             }
154 16 100       39 if ($types{TABLE}) {
155 60     60   402 no strict 'refs';
  60         113  
  60         42497  
156 12 50       395 opendir($dh, $dir)
157             or return $dbh->set_err(int($!), "Failed to open directory $dir: $!");
158 12         314 while (defined(my $item = readdir($dh))) {
159 432 50       750 if ($^O eq 'VMS') {
160             # if on VMS then avoid warnings from catdir if you use a file
161             # (not a dir) as the item below
162 0 0       0 next if $item !~ /\.dir$/oi;
163             }
164 432         1372 my $file = File::Spec->catdir($dir,$item);
165 432 100       2607 next unless -d $file;
166 92         427 my($dev, $ino, $mode, $nlink, $uid) = lstat($file);
167 92         139 my $pwnam = undef; # eval { scalar(getpwnam($uid)) } || $uid;
168 92         326 push @list, [ $dir, $pwnam, $item, 'TABLE', undef ];
169             }
170 12         32 close($dh);
171             }
172             # We would like to simply do a DBI->connect() here. However,
173             # this is wrong if we are in a subclass like DBI::ProxyServer.
174 16 50 66     121 $dbh->{'dbd_sponge_dbh'} ||= DBI->connect("DBI:Sponge:", '','')
175             or return $dbh->set_err($DBI::err,
176             "Failed to connect to DBI::Sponge: $DBI::errstr");
177              
178 16         128 my $attr = {
179             'rows' => \@list,
180             'NUM_OF_FIELDS' => 5,
181             'NAME' => ['TABLE_CAT', 'TABLE_SCHEM', 'TABLE_NAME',
182             'TABLE_TYPE', 'REMARKS'],
183             'TYPE' => [DBI::SQL_VARCHAR(), DBI::SQL_VARCHAR(),
184             DBI::SQL_VARCHAR(), DBI::SQL_VARCHAR(), DBI::SQL_VARCHAR() ],
185             'NULLABLE' => [1, 1, 1, 1, 1]
186             };
187 16         31 my $sdbh = $dbh->{'dbd_sponge_dbh'};
188 16 50       93 my $sth = $sdbh->prepare("SHOW TABLES FROM $dir", $attr)
189             or return $dbh->set_err($sdbh->err(), $sdbh->errstr());
190 16         393 $sth;
191             }
192              
193              
194             sub type_info_all {
195 12     12   147 my ($dbh) = @_;
196 12         196 my $ti = [
197             { TYPE_NAME => 0,
198             DATA_TYPE => 1,
199             COLUMN_SIZE => 2,
200             LITERAL_PREFIX => 3,
201             LITERAL_SUFFIX => 4,
202             CREATE_PARAMS => 5,
203             NULLABLE => 6,
204             CASE_SENSITIVE => 7,
205             SEARCHABLE => 8,
206             UNSIGNED_ATTRIBUTE=> 9,
207             FIXED_PREC_SCALE=> 10,
208             AUTO_UNIQUE_VALUE => 11,
209             LOCAL_TYPE_NAME => 12,
210             MINIMUM_SCALE => 13,
211             MAXIMUM_SCALE => 14,
212             },
213             [ 'VARCHAR', DBI::SQL_VARCHAR, 1024, "'","'", undef, 0, 1, 1, 0, 0,0,undef,0,0 ],
214             [ 'INTEGER', DBI::SQL_INTEGER, 10, "","", undef, 0, 0, 1, 0, 0,0,undef,0,0 ],
215             ];
216 12         42 return $ti;
217             }
218              
219              
220             sub ping {
221 3033 100   3033   8762 (shift->FETCH('Active')) ? 2 : 0; # the value 2 is checked for by t/80proxy.t
222             }
223              
224              
225             sub disconnect {
226 2389     2389   59566 shift->STORE(Active => 0);
227 2389         6013 return 1;
228             }
229              
230              
231             sub get_info {
232 72     72   1037 my ($dbh, $info_type) = @_;
233 72         289 return $dbh->{examplep_get_info}->{$info_type};
234             }
235              
236              
237             sub FETCH {
238 34327     34327   92696 my ($dbh, $attrib) = @_;
239             # In reality this would interrogate the database engine to
240             # either return dynamic values that cannot be precomputed
241             # or fetch and cache attribute values too expensive to prefetch.
242             # else pass up to DBI to handle
243 34327 100       64966 return $INC{"DBD/ExampleP.pm"} if $attrib eq 'example_driver_path';
244 31988         144450 return $dbh->SUPER::FETCH($attrib);
245             }
246              
247              
248             sub STORE {
249 50503     50503   168740 my ($dbh, $attrib, $value) = @_;
250             # store only known attributes else pass up to DBI to handle
251 50503 100       79623 if ($attrib eq 'examplep_set_err') {
252             # a fake attribute to enable a test case where STORE issues a warning
253 4         17 $dbh->set_err($value, $value);
254 4         76 return;
255             }
256 50499 100       69459 if ($attrib eq 'AutoCommit') {
257             # convert AutoCommit values to magic ones to let DBI
258             # know that the driver has 'handled' the AutoCommit attribute
259 5332 100       9251 $value = ($value) ? -901 : -900;
260             }
261 50499 100       77368 return $dbh->{$attrib} = $value if $attrib =~ /^examplep_/;
262 50495         245907 return $dbh->SUPER::STORE($attrib, $value);
263             }
264              
265             sub DESTROY {
266 2381     2381   20643 my $dbh = shift;
267 2381 100       6344 $dbh->disconnect if $dbh->FETCH('Active');
268             undef
269 2381         49184 }
270              
271              
272             # This is an example to demonstrate the use of driver-specific
273             # methods via $dbh->func().
274             # Use it as follows:
275             # my @tables = $dbh->func($re, 'examplep_tables');
276             #
277             # Returns all the tables that match the regular expression $re.
278             sub examplep_tables {
279 4     4   53 my $dbh = shift; my $re = shift;
  4         9  
280 4         65 grep { $_ =~ /$re/ } $dbh->tables();
  32         211  
281             }
282              
283             sub parse_trace_flag {
284 166     166   13342 my ($h, $name) = @_;
285 166 100       460 return 0x01000000 if $name eq 'foo';
286 152 100       414 return 0x02000000 if $name eq 'bar';
287 138 100       339 return 0x04000000 if $name eq 'baz';
288 124 100       288 return 0x08000000 if $name eq 'boo';
289 110 100       259 return 0x10000000 if $name eq 'bop';
290 96         325 return $h->SUPER::parse_trace_flag($name);
291             }
292              
293             sub private_attribute_info {
294 2317     2317   13254 return { example_driver_path => undef };
295             }
296             }
297              
298              
299             { package DBD::ExampleP::st; # ====== STATEMENT ======
300             $imp_data_size = 0;
301 60     60   433 use strict; no strict 'refs'; # cause problems with filehandles
  60     60   135  
  60         1294  
  60         270  
  60         105  
  60         43770  
302              
303             sub bind_param {
304 3896     3896   11635 my($sth, $param, $value, $attribs) = @_;
305 3896         8974 $sth->{'dbd_param'}->[$param-1] = $value;
306 3896         10596 return 1;
307             }
308              
309              
310             sub execute {
311 5369     5369   48993 my($sth, @dir) = @_;
312 5369         6359 my $dir;
313              
314 5369 100       8852 if (@dir) {
315             $sth->bind_param($_, $dir[$_-1]) or return
316 1898   50     6753 foreach (1..@dir);
317             }
318              
319 5369   100     13154 my $dbd_param = $sth->{'dbd_param'} || [];
320             return $sth->set_err(2, @$dbd_param." values bound when $sth->{NUM_OF_PARAMS} expected")
321 5369 100       10687 unless @$dbd_param == $sth->{NUM_OF_PARAMS};
322              
323 5357 100       24447 return 0 unless $sth->{NUM_OF_FIELDS}; # not a select
324              
325 3896   66     7599 $dir = $dbd_param->[0] || $sth->{examplep_ex_dir};
326 3896 50       6031 return $sth->set_err(2, "No bind parameter supplied")
327             unless defined $dir;
328              
329 3896         10904 $sth->finish;
330              
331             #
332             # If the users asks for directory "long_list_4532", then we fake a
333             # directory with files "file4351", "file4350", ..., "file0".
334             # This is a special case used for testing, especially DBD::Proxy.
335             #
336 3896 100       8275 if ($dir =~ /^long_list_(\d+)$/) {
337 12         44 $sth->{dbd_dir} = [ $1 ]; # array ref indicates special mode
338 12         25 $sth->{dbd_datahandle} = undef;
339             }
340             else {
341 3884         5560 $sth->{dbd_dir} = $dir;
342 3884         8228 my $sym = Symbol::gensym(); # "DBD::ExampleP::".++$DBD::ExampleP::gensym;
343 3884 100       102485 opendir($sym, $dir)
344             or return $sth->set_err(2, "opendir($dir): $!");
345 3872         9027 $sth->{dbd_datahandle} = $sym;
346             }
347 3884         14172 $sth->STORE(Active => 1);
348 3884         16339 return 1;
349             }
350              
351              
352             sub fetch {
353 100426     100426   225868 my $sth = shift;
354 100426         128395 my $dir = $sth->{dbd_dir};
355 100426         100619 my %s;
356              
357 100426 100       132482 if (ref $dir) { # special fake-data test mode
358 1212         1395 my $num = $dir->[0]--;
359 1212 100       1636 unless ($num > 0) {
360 12         47 $sth->finish();
361 12         105 return;
362             }
363 1200         1091 my $time = time;
364 1200         6620 @s{@DBD::ExampleP::statnames} =
365             ( 2051, 1000+$num, 0644, 2, $>, $), 0, 1024,
366             $time, $time, $time, 512, 2, "file$num")
367             }
368             else { # normal mode
369             my $dh = $sth->{dbd_datahandle}
370 99214 100       156330 or return $sth->set_err($DBI::stderr, "fetch without successful execute");
371 99059         251931 my $f = readdir($dh);
372 99059 100       149407 unless ($f) {
373 2021         5428 $sth->finish;
374 2021         10549 return;
375             }
376             # untaint $f so that we can use this for DBI taint tests
377 97038         259635 ($f) = ($f =~ m/^(.*)$/);
378 97038         512167 my $file = File::Spec->catfile($dir, $f);
379             # put in all the data fields
380 97038         871404 @s{ @DBD::ExampleP::statnames } = (lstat($file), $f);
381             }
382              
383             # return just what fields the query asks for
384 98238         159667 my @new = @s{ @{$sth->{NAME}} };
  98238         209488  
385              
386 98238         736315 return $sth->_set_fbav(\@new);
387             }
388             *fetchrow_arrayref = \&fetch;
389              
390              
391             sub finish {
392 9868     9868   33065 my $sth = shift;
393 9868 100       45888 closedir($sth->{dbd_datahandle}) if $sth->{dbd_datahandle};
394 9868         18619 $sth->{dbd_datahandle} = undef;
395 9868         12476 $sth->{dbd_dir} = undef;
396 9868         21356 $sth->SUPER::finish();
397 9868         20583 return 1;
398             }
399              
400              
401             sub FETCH {
402 8615     8615   46110 my ($sth, $attrib) = @_;
403             # In reality this would interrogate the database engine to
404             # either return dynamic values that cannot be precomputed
405             # or fetch and cache attribute values too expensive to prefetch.
406 8615 100       18664 if ($attrib eq 'TYPE'){
    100          
    100          
407 2099         2518 return [ @DBD::ExampleP::stattypes{ @{ $sth->FETCH(q{NAME_lc}) } } ];
  2099         6837  
408             }
409             elsif ($attrib eq 'PRECISION'){
410 2099         2458 return [ @DBD::ExampleP::statprec{ @{ $sth->FETCH(q{NAME_lc}) } } ];
  2099         7252  
411             }
412             elsif ($attrib eq 'ParamValues') {
413 14   50     39 my $dbd_param = $sth->{dbd_param} || [];
414 14         33 my %pv = map { $_ => $dbd_param->[$_-1] } 1..@$dbd_param;
  96         193  
415 14         115 return \%pv;
416             }
417             # else pass up to DBI to handle
418 4403         38144 return $sth->SUPER::FETCH($attrib);
419             }
420              
421              
422             sub STORE {
423 28130     28130   112618 my ($sth, $attrib, $value) = @_;
424             # would normally validate and only store known attributes
425             # else pass up to DBI to handle
426 28130 100 100     115894 return $sth->{$attrib} = $value
      100        
      66        
427             if $attrib eq 'NAME' or $attrib eq 'NULLABLE' or $attrib eq 'SCALE' or $attrib eq 'PRECISION';
428 15351         53968 return $sth->SUPER::STORE($attrib, $value);
429             }
430              
431             *parse_trace_flag = \&DBD::ExampleP::db::parse_trace_flag;
432             }
433              
434             1;
435             # vim: sw=4:ts=8