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   352 use strict;
  60         105  
  60         1618  
5 60     60   8083 use Symbol;
  60         20767  
  60         3371  
6              
7 60     60   340 use DBI qw(:sql_types);
  60         100  
  60         33325  
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 222 return $drh if $drh;
45 60         165 my($class, $attr) = @_;
46 60         153 $class .= "::dr";
47 60         393 ($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         243 $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   414 use strict;
  60         111  
  60         7230  
64              
65             sub connect { # normally overridden, but a handy default
66 2409     2409   12516 my($drh, $dbname, $user, $auth)= @_;
67 2409         9475 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         10480 29 => '"', # SQL_IDENTIFIER_QUOTE_CHAR
73             41 => '.', # SQL_CATALOG_NAME_SEPARATOR
74             114 => 1, # SQL_CATALOG_LOCATION
75             };
76             #$dbh->{Name} = $dbname;
77 2409         10698 $dbh->STORE('Active', 1);
78 2409         10242 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   380 use strict;
  60         119  
  60         27242  
91              
92             sub prepare {
93 5756     5756   102823 my($dbh, $statement)= @_;
94 5756         6976 my @fields;
95 5756         27192 my($fields, $dir) = $statement =~ m/^\s*select\s+(.*?)\s+from\s+(\S*)/i;
96              
97 5756 100 66     18292 if (defined $fields and defined $dir) {
98 4295 100       19237 @fields = ($fields eq '*')
99             ? keys %DBD::ExampleP::statnames
100             : split(/\s*,\s*/, $fields);
101             }
102             else {
103 1461 50       7112 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         24439 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       12482 defined $DBD::ExampleP::statnames{$_} ? () : $_
  8569         18145  
117             } @fields;
118 5756 100       11068 return $dbh->set_err($DBI::stderr, "Unknown field names: @bad")
119             if @bad;
120              
121 5720         21142 $outer->STORE('NUM_OF_FIELDS' => scalar(@fields));
122              
123 5720 100 100     29275 $sth->{examplep_ex_dir} = $dir if defined($dir) && $dir !~ /\?/;
124 5720 100       19175 $outer->STORE('NUM_OF_PARAMS' => ($dir) ? $dir =~ tr/?/?/ : 0);
125              
126 5720 100       17941 if (@fields) {
127 4259         10518 $outer->STORE('NAME' => \@fields);
128 4259         20371 $outer->STORE('NULLABLE' => [ (0) x @fields ]);
129 4259         17248 $outer->STORE('SCALE' => [ (0) x @fields ]);
130             }
131              
132 5720         28300 $outer;
133             }
134              
135              
136             sub table_info {
137 16     16   3531 my $dbh = shift;
138 16         45 my ($catalog, $schema, $table, $type) = @_;
139              
140 16   100     132 my @types = split(/["']*,["']/, $type || 'TABLE');
141 16         52 my %types = map { $_=>$_ } @types;
  16         76  
142              
143             # Return a list of all subdirectories
144 16         75 my $dh = Symbol::gensym(); # "DBD::ExampleP::".++$DBD::ExampleP::gensym;
145 16   66     379 my $dir = $catalog || File::Spec->curdir();
146 16         30 my @list;
147 16 100       51 if ($types{VIEW}) { # for use by test harness
148 4         18 push @list, [ undef, "schema", "table", 'VIEW', undef ];
149 4         17 push @list, [ undef, "sch-ema", "table", 'VIEW', undef ];
150 4         13 push @list, [ undef, "schema", "ta-ble", 'VIEW', undef ];
151 4         14 push @list, [ undef, "sch ema", "table", 'VIEW', undef ];
152 4         18 push @list, [ undef, "schema", "ta ble", 'VIEW', undef ];
153             }
154 16 100       50 if ($types{TABLE}) {
155 60     60   413 no strict 'refs';
  60         114  
  60         41301  
156 12 50       469 opendir($dh, $dir)
157             or return $dbh->set_err(int($!), "Failed to open directory $dir: $!");
158 12         370 while (defined(my $item = readdir($dh))) {
159 432 50       947 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         1639 my $file = File::Spec->catdir($dir,$item);
165 432 100       3192 next unless -d $file;
166 92         509 my($dev, $ino, $mode, $nlink, $uid) = lstat($file);
167 92         168 my $pwnam = undef; # eval { scalar(getpwnam($uid)) } || $uid;
168 92         394 push @list, [ $dir, $pwnam, $item, 'TABLE', undef ];
169             }
170 12         40 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     130 $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         183 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         39 my $sdbh = $dbh->{'dbd_sponge_dbh'};
188 16 50       133 my $sth = $sdbh->prepare("SHOW TABLES FROM $dir", $attr)
189             or return $dbh->set_err($sdbh->err(), $sdbh->errstr());
190 16         419 $sth;
191             }
192              
193              
194             sub type_info_all {
195 12     12   137 my ($dbh) = @_;
196 12         156 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         39 return $ti;
217             }
218              
219              
220             sub ping {
221 3033 100   3033   8920 (shift->FETCH('Active')) ? 2 : 0; # the value 2 is checked for by t/80proxy.t
222             }
223              
224              
225             sub disconnect {
226 2389     2389   58098 shift->STORE(Active => 0);
227 2389         6160 return 1;
228             }
229              
230              
231             sub get_info {
232 72     72   1101 my ($dbh, $info_type) = @_;
233 72         282 return $dbh->{examplep_get_info}->{$info_type};
234             }
235              
236              
237             sub FETCH {
238 34327     34327   91593 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       63865 return $INC{"DBD/ExampleP.pm"} if $attrib eq 'example_driver_path';
244 31988         139978 return $dbh->SUPER::FETCH($attrib);
245             }
246              
247              
248             sub STORE {
249 50503     50503   161313 my ($dbh, $attrib, $value) = @_;
250             # store only known attributes else pass up to DBI to handle
251 50503 100       79051 if ($attrib eq 'examplep_set_err') {
252             # a fake attribute to enable a test case where STORE issues a warning
253 4         49 $dbh->set_err($value, $value);
254 4         90 return;
255             }
256 50499 100       69022 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       9415 $value = ($value) ? -901 : -900;
260             }
261 50499 100       76182 return $dbh->{$attrib} = $value if $attrib =~ /^examplep_/;
262 50495         228816 return $dbh->SUPER::STORE($attrib, $value);
263             }
264              
265             sub DESTROY {
266 2381     2381   18966 my $dbh = shift;
267 2381 100       6321 $dbh->disconnect if $dbh->FETCH('Active');
268             undef
269 2381         49018 }
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   87 my $dbh = shift; my $re = shift;
  4         12  
280 4         22 grep { $_ =~ /$re/ } $dbh->tables();
  32         209  
281             }
282              
283             sub parse_trace_flag {
284 166     166   56074 my ($h, $name) = @_;
285 166 100       461 return 0x01000000 if $name eq 'foo';
286 152 100       302 return 0x02000000 if $name eq 'bar';
287 138 100       285 return 0x04000000 if $name eq 'baz';
288 124 100       2851 return 0x08000000 if $name eq 'boo';
289 110 100       234 return 0x10000000 if $name eq 'bop';
290 96         271 return $h->SUPER::parse_trace_flag($name);
291             }
292              
293             sub private_attribute_info {
294 2317     2317   12646 return { example_driver_path => undef };
295             }
296             }
297              
298              
299             { package DBD::ExampleP::st; # ====== STATEMENT ======
300             $imp_data_size = 0;
301 60     60   481 use strict; no strict 'refs'; # cause problems with filehandles
  60     60   119  
  60         1310  
  60         283  
  60         109  
  60         43032  
302              
303             sub bind_param {
304 3896     3896   11488 my($sth, $param, $value, $attribs) = @_;
305 3896         9173 $sth->{'dbd_param'}->[$param-1] = $value;
306 3896         10260 return 1;
307             }
308              
309              
310             sub execute {
311 5369     5369   47112 my($sth, @dir) = @_;
312 5369         6381 my $dir;
313              
314 5369 100       8925 if (@dir) {
315             $sth->bind_param($_, $dir[$_-1]) or return
316 1898   50     6445 foreach (1..@dir);
317             }
318              
319 5369   100     12234 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       10149 unless @$dbd_param == $sth->{NUM_OF_PARAMS};
322              
323 5357 100       23097 return 0 unless $sth->{NUM_OF_FIELDS}; # not a select
324              
325 3896   66     7106 $dir = $dbd_param->[0] || $sth->{examplep_ex_dir};
326 3896 50       6267 return $sth->set_err(2, "No bind parameter supplied")
327             unless defined $dir;
328              
329 3896         10876 $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       7447 if ($dir =~ /^long_list_(\d+)$/) {
337 12         59 $sth->{dbd_dir} = [ $1 ]; # array ref indicates special mode
338 12         30 $sth->{dbd_datahandle} = undef;
339             }
340             else {
341 3884         5337 $sth->{dbd_dir} = $dir;
342 3884         8916 my $sym = Symbol::gensym(); # "DBD::ExampleP::".++$DBD::ExampleP::gensym;
343 3884 100       102329 opendir($sym, $dir)
344             or return $sth->set_err(2, "opendir($dir): $!");
345 3872         8297 $sth->{dbd_datahandle} = $sym;
346             }
347 3884         13976 $sth->STORE(Active => 1);
348 3884         16146 return 1;
349             }
350              
351              
352             sub fetch {
353 100426     100426   216581 my $sth = shift;
354 100426         124496 my $dir = $sth->{dbd_dir};
355 100426         102468 my %s;
356              
357 100426 100       134825 if (ref $dir) { # special fake-data test mode
358 1212         1601 my $num = $dir->[0]--;
359 1212 100       1848 unless ($num > 0) {
360 12         58 $sth->finish();
361 12         98 return;
362             }
363 1200         1239 my $time = time;
364 1200         7878 @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       158984 or return $sth->set_err($DBI::stderr, "fetch without successful execute");
371 99059         250175 my $f = readdir($dh);
372 99059 100       152272 unless ($f) {
373 2021         5643 $sth->finish;
374 2021         10641 return;
375             }
376             # untaint $f so that we can use this for DBI taint tests
377 97038         265121 ($f) = ($f =~ m/^(.*)$/);
378 97038         514876 my $file = File::Spec->catfile($dir, $f);
379             # put in all the data fields
380 97038         870211 @s{ @DBD::ExampleP::statnames } = (lstat($file), $f);
381             }
382              
383             # return just what fields the query asks for
384 98238         161475 my @new = @s{ @{$sth->{NAME}} };
  98238         211802  
385              
386 98238         726385 return $sth->_set_fbav(\@new);
387             }
388             *fetchrow_arrayref = \&fetch;
389              
390              
391             sub finish {
392 9868     9868   31371 my $sth = shift;
393 9868 100       42824 closedir($sth->{dbd_datahandle}) if $sth->{dbd_datahandle};
394 9868         17963 $sth->{dbd_datahandle} = undef;
395 9868         11725 $sth->{dbd_dir} = undef;
396 9868         20760 $sth->SUPER::finish();
397 9868         19843 return 1;
398             }
399              
400              
401             sub FETCH {
402 8615     8615   51892 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       18712 if ($attrib eq 'TYPE'){
    100          
    100          
407 2099         2534 return [ @DBD::ExampleP::stattypes{ @{ $sth->FETCH(q{NAME_lc}) } } ];
  2099         7364  
408             }
409             elsif ($attrib eq 'PRECISION'){
410 2099         2520 return [ @DBD::ExampleP::statprec{ @{ $sth->FETCH(q{NAME_lc}) } } ];
  2099         6991  
411             }
412             elsif ($attrib eq 'ParamValues') {
413 14   50     49 my $dbd_param = $sth->{dbd_param} || [];
414 14         45 my %pv = map { $_ => $dbd_param->[$_-1] } 1..@$dbd_param;
  96         229  
415 14         161 return \%pv;
416             }
417             # else pass up to DBI to handle
418 4403         37026 return $sth->SUPER::FETCH($attrib);
419             }
420              
421              
422             sub STORE {
423 28130     28130   113489 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     115383 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         51934 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