blib/lib/DBI/DBD/Metadata.pm

filter: Statementstmt 19.1% 26 / 136 Branchbran 0.0% 0 / 58 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 63.6% 7 / 11 Podpod 0.0% 0 / 4 totaltotal 15.0% 33 / 220 CC 39 SCAR 52.7
CC
39
Cov
13%
write_typeinfo_pm
310.7
write_getinfo_pm
240.0
fmt_value
42.0
CRAP 193.6
« blib/lib/DBI/Gofer/Response.pm ↑ Summary blib/lib/DBD/File.pm »
1 package DBI::DBD::Metadata;
2  
3 # $Id: Metadata.pm 14213 2010-06-30 19:29:18Z Martin $
4 #
5 # Copyright (c) 1997-2003 Jonathan Leffler, Jochen Wiedmann,
6 # Steffen Goeldner and Tim Bunce
7 #
8 # You may distribute under the terms of either the GNU General Public
9 # License or the Artistic License, as specified in the Perl README file.
10  
11 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 4 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 4 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use DBI::Const::GetInfoType qw(%GetInfoType);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 4 our @ISA = qw(Exporter);
21 4 our @EXPORT = qw(write_getinfo_pm write_typeinfo_pm);
22  
23 4 our $VERSION = "2.014214";
24  
25  
26 =head1 NAME
27  
28 DBI::DBD::Metadata - Generate the code and data for some DBI metadata methods
29  
30 =head1 SYNOPSIS
31  
32 The idea is to extract metadata information from a good quality
33 ODBC driver and use it to generate code and data to use in your own
34 DBI driver for the same database.
35  
36 To generate code to support the get_info method:
37  
38 perl -MDBI::DBD::Metadata -e "write_getinfo_pm('dbi:ODBC:dsn-name','user','pass','Driver')"
39  
40 perl -MDBI::DBD::Metadata -e write_getinfo_pm dbi:ODBC:foo_db username password Driver
41  
42 To generate code to support the type_info method:
43  
44 perl -MDBI::DBD::Metadata -e "write_typeinfo_pm('dbi:ODBC:dsn-name','user','pass','Driver')"
45  
46 perl -MDBI::DBD::Metadata -e write_typeinfo_pm dbi:ODBC:dsn-name user pass Driver
47  
48 Where C<dbi:ODBC:dsn-name> is the connection to use to extract the
49 data, and C<Driver> is the name of the driver you want the code
50 generated for (the driver name gets embedded into the output in
51 numerous places).
52  
53 =head1 Generating a GetInfo package for a driver
54  
55 The C<write_getinfo_pm> in the DBI::DBD::Metadata module generates a
56 DBD::Driver::GetInfo package on standard output.
57  
58 This method generates a DBD::Driver::GetInfo package from the data
59 source you specified in the parameter list or in the environment
60 variable DBI_DSN.
61 DBD::Driver::GetInfo should help a DBD author implement the DBI
62 get_info() method.
63 Because you are just creating this package, it is very unlikely that
64 DBD::Driver already provides a good implementation for get_info().
65 Thus you will probably connect via DBD::ODBC.
66  
67 Once you are sure that it is producing reasonably sane data, you should
68 typically redirect the standard output to lib/DBD/Driver/GetInfo.pm, and
69 then hand edit the result.
70 Do not forget to update your Makefile.PL and MANIFEST to include this as
71 an extra PM file that should be installed.
72  
73 If you connect via DBD::ODBC, you should use version 0.38 or greater;
74  
75 Please take a critical look at the data returned!
76 ODBC drivers vary dramatically in their quality.
77  
78 The generator assumes that most values are static and places these
79 values directly in the %info hash.
80 A few examples show the use of CODE references and the implementation
81 via subroutines.
82 It is very likely that you will have to write additional subroutines for
83 values depending on the session state or server version, e.g.
84 SQL_DBMS_VER.
85  
86 A possible implementation of DBD::Driver::db::get_info() may look like:
87  
88 sub get_info {
89 my($dbh, $info_type) = @_;
90 require DBD::Driver::GetInfo;
91 my $v = $DBD::Driver::GetInfo::info{int($info_type)};
92 $v = $v->($dbh) if ref $v eq 'CODE';
93 return $v;
94 }
95  
96 Please replace Driver (or "<foo>") with the name of your driver.
97 Note that this stub function is generated for you by write_getinfo_pm
98 function, but you must manually transfer the code to Driver.pm.
99  
100 =cut
101  
102 sub write_getinfo_pm
103 0 ▶{
Subroutine write_getinfo_pm: not called CC=15 SCAR=54.8
Pod undocumented
104 0 ▶ my ($dsn, $user, $pass, $driver) = @_ ? @_ : @ARGV;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
105 0 my $dbh = DBI->connect($dsn, $user, $pass, {RaiseError=>1});
106 0 ▶ $driver //= "<foo>";
Condition0%0/2
$driver //= "<foo>"
l!l
00
MC/DC0%0/1
$driver //= "<foo>"
$driver
Truth table0%0/2
A
A= $driver
Aresult
11
00
107  
108 0 print <<PERL;
109  
110 # Transfer this to ${driver}.pm
111  
112 # The get_info function was automatically generated by
113 # DBI::DBD::Metadata::write_getinfo_pm v$DBI::DBD::Metadata::VERSION.
114  
115 package DBD::${driver}::db; # This line can be removed once transferred.
116  
117 sub get_info {
118 my(\$dbh, \$info_type) = \@_;
119 require DBD::${driver}::GetInfo;
120 my \$v = \$DBD::${driver}::GetInfo::info{int(\$info_type)};
121 \$v = \$v->(\$dbh) if ref \$v eq 'CODE';
122 return \$v;
123 }
124  
125 # Transfer this to lib/DBD/${driver}/GetInfo.pm
126  
127 # The \%info hash was automatically generated by
128 # DBI::DBD::Metadata::write_getinfo_pm v$DBI::DBD::Metadata::VERSION.
129  
130 package DBD::${driver}::GetInfo;
131  
132 use strict;
133 use DBD::${driver};
134  
135 # Beware: not officially documented interfaces...
136 # use DBI::Const::GetInfoType qw(\%GetInfoType);
137 # use DBI::Const::GetInfoReturn qw(\%GetInfoReturnTypes \%GetInfoReturnValues);
138  
139 my \$sql_driver = '${driver}';
140 my \$sql_ver_fmt = '%02d.%02d.%04d'; # ODBC version string: ##.##.#####
141 my \$sql_driver_ver = sprintf \$sql_ver_fmt, split (/\\./, \$DBD::${driver}::VERSION);
142 PERL
143  
144 0 my $kw_map = 0;
145 {
146 # Informix CLI (ODBC) v3.81.0000 does not return a list of keywords.
147 0 local $\ = "\n";
148 0 local $, = "\n";
149 0 my ($kw) = $dbh->get_info($GetInfoType{SQL_KEYWORDS});
150 0 ▶ if ($kw)
Branch: 1 branch
BranchTrueFalseTotal
if ($kw) 0 0 0
151 {
152 0 print "\nmy \@Keywords = qw(\n";
153 0 print sort split /,/, $kw;
154 0 print ");\n\n";
155 0 print "sub sql_keywords {\n";
156 0 print q% return join ',', @Keywords;%;
157 0 print "\n}\n\n";
158 0 $kw_map = 1;
159 }
160 }
161  
162 0 print <<'PERL';
163  
164 sub sql_data_source_name {
165 my $dbh = shift;
166 return "dbi:$sql_driver:" . $dbh->{Name};
167 }
168  
169 sub sql_user_name {
170 my $dbh = shift;
171 # CURRENT_USER is a non-standard attribute, probably undef
172 # Username is a standard DBI attribute
173 return $dbh->{CURRENT_USER} || $dbh->{Username};
174 }
175  
176 PERL
177  
178 0 print "\nour \%info = (\n";
179 0 foreach my $key (sort keys %GetInfoType)
180 {
181 0 my $num = $GetInfoType{$key};
182 0 my $val = eval { $dbh->get_info($num); };
183 0 ▶ if ($key eq 'SQL_DATA_SOURCE_NAME') {
Branch: 8 branches
BranchTrueFalseTotal
if ($key eq 'SQL_DATA_SOURCE_NAME') { } 0 0 0
elsif ($key eq 'SQL_KEYWORDS') { } 0 0 0
elsif ($key eq 'SQL_DRIVER_NAME') { } 0 0 0
elsif ($key eq 'SQL_DRIVER_VER') { } 0 0 0
elsif ($key eq 'SQL_USER_NAME') { } 0 0 0
elsif (not defined $val) { } 0 0 0
elsif ($val eq '') { } 0 0 0
elsif ($val =~ /\D/) { } 0 0 0
184 0 $val = '\&sql_data_source_name';
185 }
186 elsif ($key eq 'SQL_KEYWORDS') {
187 0 ▶ $val = ($kw_map) ? '\&sql_keywords' : 'undef';
Branch: 1 branch
BranchTrueFalseTotal
$kw_map ? : 0 0 0
188 }
189 elsif ($key eq 'SQL_DRIVER_NAME') {
190 0 $val = "\$INC{'DBD/$driver.pm'}";
191 }
192 elsif ($key eq 'SQL_DRIVER_VER') {
193 0 $val = '$sql_driver_ver';
194 }
195 elsif ($key eq 'SQL_USER_NAME') {
196 0 $val = '\&sql_user_name';
197 }
198 elsif (not defined $val) {
199 0 $val = 'undef';
200 }
201 elsif ($val eq '') {
202 0 $val = "''";
203 }
204 elsif ($val =~ /\D/) {
205 0 $val =~ s/\\/\\\\/g;
206 0 $val =~ s/'/\\'/g;
207 0 $val = "'$val'";
208 }
209 0 ▶ printf "%s %5d => %-30s # %s\n", (($val eq 'undef') ? '#' : ' '), $num, "$val,", $key;
Branch: 1 branch
BranchTrueFalseTotal
$val eq 'undef' ? : 0 0 0
210 }
211 0 print ");\n\n1;\n\n__END__\n";
212 }
213  
214  
215  
216 =head1 Generating a TypeInfo package for a driver
217  
218 The C<write_typeinfo_pm> function in the DBI::DBD::Metadata module generates
219 on standard output the data needed for a driver's type_info_all method.
220 It also provides default implementations of the type_info_all
221 method for inclusion in the driver's main implementation file.
222  
223 The driver parameter is the name of the driver for which the methods
224 will be generated; for the sake of examples, this will be "Driver".
225 Typically, the dsn parameter will be of the form "dbi:ODBC:odbc_dsn",
226 where the odbc_dsn is a DSN for one of the driver's databases.
227 The user and pass parameters are the other optional connection
228 parameters that will be provided to the DBI connect method.
229  
230 Once you are sure that it is producing reasonably sane data, you should
231 typically redirect the standard output to lib/DBD/Driver/TypeInfo.pm,
232 and then hand edit the result if necessary.
233 Do not forget to update your Makefile.PL and MANIFEST to include this as
234 an extra PM file that should be installed.
235  
236 Please take a critical look at the data returned!
237 ODBC drivers vary dramatically in their quality.
238  
239 The generator assumes that all the values are static and places these
240 values directly in the %info hash.
241  
242 A possible implementation of DBD::Driver::type_info_all() may look like:
243  
244 sub type_info_all {
245 my ($dbh) = @_;
246 require DBD::Driver::TypeInfo;
247 return [ @$DBD::Driver::TypeInfo::type_info_all ];
248 }
249  
250 Please replace Driver (or "<foo>") with the name of your driver.
251 Note that this stub function is generated for you by the write_typeinfo_pm
252 function, but you must manually transfer the code to Driver.pm.
253  
254 =cut
255  
256  
257 # These two are used by fmt_value...
258 4 my %dbi_inv;
259 my %sql_type_inv;
260  
261 #-DEBUGGING-#
262 #sub print_hash
263 #{
264 # my ($name, %hash) = @_;
265 # print "Hash: $name\n";
266 # foreach my $key (keys %hash)
267 # {
268 # print "$key => $hash{$key}\n";
269 # }
270 #}
271 #-DEBUGGING-#
272  
273 sub inverse_hash
274 0 ▶{
Subroutine inverse_hash: not called CC=3 SCAR=24.8
Pod undocumented
275 0 my (%hash) = @_;
276 0 my (%inv);
277 0 foreach my $key (keys %hash)
278 {
279 0 my $val = $hash{$key};
280 die "Double mapping for key value $val ($inv{$val}, $key)!"
281 0 ▶ if (defined $inv{$val});
Branch: 1 branch
BranchTrueFalseTotal
if defined $inv{$val} 0 0 0
282 0 $inv{$val} = $key;
283 }
284 0 return %inv;
285 }
286  
287 sub fmt_value
288 0 ▶{
Subroutine fmt_value: not called CC=6 SCAR=37.4
Pod undocumented
289 0 my ($num, $val) = @_;
290 0 ▶ if (!defined $val)
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $val) { } 0 0 0
elsif (not $val =~ /^[-+]?\d+$/) { } 0 0 0
elsif ($dbi_inv{$num} =~ /^(SQL_)?DATA_TYPE$/) { } 0 0 0
291 {
292 0 $val = "undef";
293 }
294 elsif ($val !~ m/^[-+]?\d+$/)
295 {
296 # All the numbers in type_info_all are integers!
297 # Anything that isn't an integer is a string.
298 # Ensure that no double quotes screw things up.
299 0 ▶ $val =~ s/"/\\"/g if ($val =~ m/"/o);
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ /"/o 0 0 0
300 0 $val = qq{"$val"};
301 }
302 elsif ($dbi_inv{$num} =~ m/^(SQL_)?DATA_TYPE$/)
303 {
304 # All numeric...
305 $val = $sql_type_inv{$val}
306 0 ▶ if (defined $sql_type_inv{$val});
Branch: 1 branch
BranchTrueFalseTotal
if defined $sql_type_inv{$val} 0 0 0
307 }
308 0 return $val;
309 }
310  
311 sub write_typeinfo_pm
312 0 ▶{
Subroutine write_typeinfo_pm: not called CC=18 SCAR=57.4
Pod undocumented
313 0 ▶ my ($dsn, $user, $pass, $driver) = @_ ? @_ : @ARGV;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
314 0 my $dbh = DBI->connect($dsn, $user, $pass, {AutoCommit=>1, RaiseError=>1});
315 0 ▶ $driver //= "<foo>";
Condition0%0/2
$driver //= "<foo>"
l!l
00
MC/DC0%0/1
$driver //= "<foo>"
$driver
Truth table0%0/2
A
A= $driver
Aresult
11
00
316  
317 0 print <<PERL;
318  
319 # Transfer this to ${driver}.pm
320  
321 # The type_info_all function was automatically generated by
322 # DBI::DBD::Metadata::write_typeinfo_pm v$DBI::DBD::Metadata::VERSION.
323  
324 package DBD::${driver}::db; # This line can be removed once transferred.
325  
326 sub type_info_all
327 {
328 my (\$dbh) = \@_;
329 require DBD::${driver}::TypeInfo;
330 return [ \@\$DBD::${driver}::TypeInfo::type_info_all ];
331 }
332  
333 # Transfer this to lib/DBD/${driver}/TypeInfo.pm.
334 # Don't forget to add version and intellectual property control information.
335  
336 # The \%type_info_all hash was automatically generated by
337 # DBI::DBD::Metadata::write_typeinfo_pm v$DBI::DBD::Metadata::VERSION.
338  
339 package DBD::${driver}::TypeInfo;
340  
341 {
342 require Exporter;
343 require DynaLoader;
344 \@ISA = qw(Exporter DynaLoader);
345 \@EXPORT = qw(type_info_all);
346 use DBI qw(:sql_types);
347  
348 PERL
349  
350 # Generate SQL type name mapping hashes.
351 # See code fragment in DBI specification.
352 0 my %sql_type_map;
353 0 foreach (@{$DBI::EXPORT_TAGS{sql_types}})
354 {
355 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
356 0 $sql_type_map{$_} = &{"DBI::$_"}();
357 0 $sql_type_inv{$sql_type_map{$_}} = $_;
358 }
359 #-DEBUG-# print_hash("sql_type_map", %sql_type_map);
360 #-DEBUG-# print_hash("sql_type_inv", %sql_type_inv);
361  
362 0 my %dbi_map =
363 (
364 TYPE_NAME => 0,
365 DATA_TYPE => 1,
366 COLUMN_SIZE => 2,
367 LITERAL_PREFIX => 3,
368 LITERAL_SUFFIX => 4,
369 CREATE_PARAMS => 5,
370 NULLABLE => 6,
371 CASE_SENSITIVE => 7,
372 SEARCHABLE => 8,
373 UNSIGNED_ATTRIBUTE => 9,
374 FIXED_PREC_SCALE => 10,
375 AUTO_UNIQUE_VALUE => 11,
376 LOCAL_TYPE_NAME => 12,
377 MINIMUM_SCALE => 13,
378 MAXIMUM_SCALE => 14,
379 SQL_DATA_TYPE => 15,
380 SQL_DATETIME_SUB => 16,
381 NUM_PREC_RADIX => 17,
382 INTERVAL_PRECISION => 18,
383 );
384  
385 #-DEBUG-# print_hash("dbi_map", %dbi_map);
386  
387 0 %dbi_inv = inverse_hash(%dbi_map);
388  
389 #-DEBUG-# print_hash("dbi_inv", %dbi_inv);
390  
391 0 my $maxlen = 0;
392 0 foreach my $key (keys %dbi_map)
393 {
394 0 ▶ $maxlen = length($key) if length($key) > $maxlen;
Branch: 1 branch
BranchTrueFalseTotal
if length $key > $maxlen 0 0 0
395 }
396  
397 # Print the name/value mapping entry in the type_info_all array;
398 0 my $fmt = " \%-${maxlen}s => \%2d,\n";
399 0 my $numkey = 0;
400 0 my $maxkey = 0;
401 0 print " \$type_info_all = [\n {\n";
402 0 foreach my $i (sort { $a <=> $b } keys %dbi_inv)
403 {
404 0 printf($fmt, $dbi_inv{$i}, $i);
405 0 $numkey++;
406 0 $maxkey = $i;
407 }
408 0 print " },\n";
409  
410 0 ▶ print STDERR "### WARNING - Non-dense set of keys ($numkey keys, $maxkey max key)\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $numkey = $maxkey + 1 0 0 0
411 unless $numkey = $maxkey + 1;
412  
413 0 my $h = $dbh->type_info_all;
414 0 my @tia = @$h;
415 0 my %odbc_map = map { uc $_ => $tia[0]->{$_} } keys %{$tia[0]};
416 0 shift @tia; # Remove the mapping reference.
417 0 my $numtyp = $#tia;
418  
419 #-DEBUG-# print_hash("odbc_map", %odbc_map);
420  
421 # In theory, the key/number mapping sequence for %dbi_map
422 # should be the same as the one from the ODBC driver. However, to
423 # prevent the possibility of mismatches, and to deal with older
424 # missing attributes or unexpected new ones, we chase back through
425 # the %dbi_inv and %odbc_map hashes, generating @dbi_to_odbc
426 # to map our new key number to the old one.
427 # Report if @dbi_to_odbc is not an identity mapping.
428 0 my @dbi_to_odbc;
429 0 foreach my $num (sort { $a <=> $b } keys %dbi_inv)
430 {
431 # Find the name in %dbi_inv that matches this index number.
432 0 my $dbi_key = $dbi_inv{$num};
433 #-DEBUG-# print "dbi_key = $dbi_key\n";
434 #-DEBUG-# print "odbc_key = $odbc_map{$dbi_key}\n";
435 # Find the index in %odbc_map that has this key.
436 0 $dbi_to_odbc[$num] = $odbc_map{$dbi_key};
437 }
438  
439 # Determine the length of the longest formatted value in each field
440 0 my @len;
441 0 ▶ for (my $i = 0; $i <= $numtyp; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $numtyp) 0 0 0
442 {
443 0 my @odbc_val = @{$tia[$i]};
444 0 ▶ for (my $num = 0; $num <= $maxkey; $num++)
Branch: 1 branch
BranchTrueFalseTotal
if ($num <= $maxkey) 0 0 0
445 {
446 # Find the value of the entry in the @odbc_val array.
447 0 ▶ my $val = (defined $dbi_to_odbc[$num]) ? $odbc_val[$dbi_to_odbc[$num]] : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $dbi_to_odbc[$num] ? : 0 0 0
448 0 $val = fmt_value($num, $val);
449 #-DEBUG-# print "val = $val\n";
450 0 $val = "$val,";
451 0 ▶ $len[$num] = length($val) if !defined $len[$num] || length($val) > $len[$num];
Branch: 1 branch
BranchTrueFalseTotal
if not defined $len[$num] or length $val > $len[$num] 0 0 0
Condition0%0/3
not defined $len[$num] or length $val > $len[$num]
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $len[$num] or length $val > $len[$num]
not defined $len[$num] length $val > $len[$num]
Truth table0%0/3
not(A) or B
A= not defined $len[$num]B= length $val > $len[$num]
ABresult
1X1
011
000
452 }
453 }
454  
455 # Generate format strings to left justify each string in maximum field width.
456 0 my @fmt;
457 0 ▶ for (my $i = 0; $i <= $maxkey; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $maxkey) 0 0 0
458 {
459 0 $fmt[$i] = "%-$len[$i]s";
460 #-DEBUG-# print "fmt[$i] = $fmt[$i]\n";
461 }
462  
463 # Format the data from type_info_all
464 0 ▶ for (my $i = 0; $i <= $numtyp; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $numtyp) 0 0 0
465 {
466 0 my @odbc_val = @{$tia[$i]};
467 0 print " [ ";
468 0 ▶ for (my $num = 0; $num <= $maxkey; $num++)
Branch: 1 branch
BranchTrueFalseTotal
if ($num <= $maxkey) 0 0 0
469 {
470 # Find the value of the entry in the @odbc_val array.
471 0 ▶ my $val = (defined $dbi_to_odbc[$num]) ? $odbc_val[$dbi_to_odbc[$num]] : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $dbi_to_odbc[$num] ? : 0 0 0
472 0 $val = fmt_value($num, $val);
473 0 printf $fmt[$num], "$val,";
474 }
475 0 print " ],\n";
476 }
477  
478 0 print " ];\n\n 1;\n}\n\n__END__\n";
479  
480 }
481  
482 4 1;
483  
484 __END__
« blib/lib/DBI/Gofer/Response.pm ↑ Summary blib/lib/DBD/File.pm »