File Coverage

blib/lib/DBI/DBD/SqlEngine.pm
Criterion Covered Total %
statement 554 706 78.4
branch 197 346 56.9
condition 59 170 34.7
subroutine 88 118 74.5
pod 0 1 0.0
total 898 1341 66.9


line stmt bran cond sub pod time code
1             # -*- perl -*-
2             #
3             # DBI::DBD::SqlEngine - A base class for implementing DBI drivers that
4             # have not an own SQL engine
5             #
6             # This module is currently maintained by
7             #
8             # H.Merijn Brand & Jens Rehsack
9             #
10             # The original author is Jochen Wiedmann.
11             #
12             # Copyright (C) 2009-2013 by H.Merijn Brand & Jens Rehsack
13             # Copyright (C) 2004 by Jeff Zucker
14             # Copyright (C) 1998 by Jochen Wiedmann
15             #
16             # All rights reserved.
17             #
18             # You may distribute this module under the terms of either the GNU
19             # General Public License or the Artistic License, as specified in
20             # the Perl README file.
21              
22             require 5.008;
23              
24 48     48   11594 use strict;
  48         72  
  48         1578  
25              
26 48     48   183 use DBI ();
  48         58  
  48         1496  
27             require DBI::SQL::Nano;
28              
29             package DBI::DBD::SqlEngine;
30              
31 48     48   168 use strict;
  48         58  
  48         1119  
32              
33 48     48   183 use Carp;
  48         66  
  48         3171  
34 48     48   349 use vars qw( @ISA $VERSION $drh %methods_installed);
  48         65  
  48         7134  
35              
36             $VERSION = "0.06";
37              
38             $drh = undef; # holds driver handle(s) once initialized
39              
40             DBI->setup_driver("DBI::DBD::SqlEngine"); # only needed once but harmless to repeat
41              
42             my %accessors = (
43             versions => "get_driver_versions",
44             get_meta => "get_sql_engine_meta",
45             set_meta => "set_sql_engine_meta",
46             clear_meta => "clear_sql_engine_meta",
47             );
48              
49             sub driver ($;$)
50             {
51 40     40 0 83 my ( $class, $attr ) = @_;
52              
53             # Drivers typically use a singleton object for the $drh
54             # We use a hash here to have one singleton per subclass.
55             # (Otherwise DBD::CSV and DBD::DBM, for example, would
56             # share the same driver object which would cause problems.)
57             # An alternative would be to not cache the $drh here at all
58             # and require that subclasses do that. Subclasses should do
59             # their own caching, so caching here just provides extra safety.
60 40 50       163 $drh->{$class} and return $drh->{$class};
61              
62 40   50     146 $attr ||= {};
63             {
64 48     48   236 no strict "refs";
  48         59  
  48         14924  
  40         60  
65 40 50       172 unless ( $attr->{Attribution} )
66             {
67 0 0       0 $class eq "DBI::DBD::SqlEngine"
68             and $attr->{Attribution} = "$class by Jens Rehsack";
69 0   0     0 $attr->{Attribution} ||= ${ $class . "::ATTRIBUTION" }
      0        
70             || "oops the author of $class forgot to define this";
71             }
72 40   33     131 $attr->{Version} ||= ${ $class . "::VERSION" };
  0         0  
73 40 50       143 $attr->{Name} or ( $attr->{Name} = $class ) =~ s/^DBD\:\://;
74             }
75              
76 40         226 $drh->{$class} = DBI::_new_drh( $class . "::dr", $attr );
77 40         1075 $drh->{$class}->STORE( ShowErrorStatement => 1 );
78              
79 40         449 my $prefix = DBI->driver_prefix($class);
80 40 50       146 if ($prefix)
81             {
82 40         116 my $dbclass = $class . "::db";
83 40         235 while ( my ( $accessor, $funcname ) = each %accessors )
84             {
85 160         262 my $method = $prefix . $accessor;
86 160 50       1997 $dbclass->can($method) and next;
87 160         646 my $inject = sprintf <<'EOI', $dbclass, $method, $dbclass, $funcname;
88             sub %s::%s
89             {
90             my $func = %s->can (q{%s});
91             goto &$func;
92             }
93             EOI
94 160     0   8915 eval $inject;
  0     32   0  
  0     8   0  
  32     8   7456  
  32     4   149  
  8     0   4942  
  8     0   36  
  8     8   272  
  8         43  
  4         2562  
  4         22  
  0         0  
  0         0  
  0         0  
  0         0  
  8         424  
  8         41  
95 160         824 $dbclass->install_method($method);
96             }
97             }
98             else
99             {
100 0         0 warn "Using DBI::DBD::SqlEngine with unregistered driver $class.\n"
101             . "Reading documentation how to prevent is strongly recommended.\n";
102              
103             }
104              
105             # XXX inject DBD::XXX::Statement unless exists
106              
107 40         122 my $stclass = $class . "::st";
108 40 50       689 $stclass->install_method("sql_get_colnames") unless ( $methods_installed{__PACKAGE__}++ );
109              
110 40         216 return $drh->{$class};
111             } # driver
112              
113             sub CLONE
114             {
115 0     0   0 undef $drh;
116             } # CLONE
117              
118             # ====== DRIVER ================================================================
119              
120             package DBI::DBD::SqlEngine::dr;
121              
122 48     48   239 use strict;
  48         65  
  48         1298  
123 48     48   196 use warnings;
  48         70  
  48         1419  
124              
125 48     48   183 use vars qw(@ISA $imp_data_size);
  48         290  
  48         2133  
126              
127 48     48   213 use Carp qw/carp/;
  48         56  
  48         33500  
128              
129             $imp_data_size = 0;
130              
131             sub connect ($$;$$$)
132             {
133 576     576   1046 my ( $drh, $dbname, $user, $auth, $attr ) = @_;
134              
135             # create a 'blank' dbh
136 576         3293 my $dbh = DBI::_new_dbh(
137             $drh,
138             {
139             Name => $dbname,
140             USER => $user,
141             CURRENT_USER => $user,
142             }
143             );
144              
145 576 50       1638 if ($dbh)
146             {
147             # must be done first, because setting flags implicitly calls $dbdname::db->STORE
148 576         3276 $dbh->func( 0, "init_default_attributes" );
149 576         2236 my $two_phased_init;
150 576 50       5727 defined $dbh->{sql_init_phase} and $two_phased_init = ++$dbh->{sql_init_phase};
151 576         5331 my %second_phase_attrs;
152             my @func_inits;
153              
154             # this must be done to allow DBI.pm reblessing got handle after successful connecting
155 576 50       1533 exists $attr->{RootClass} and $second_phase_attrs{RootClass} = delete $attr->{RootClass};
156              
157 576         617 my ( $var, $val );
158 576         1742 while ( length $dbname )
159             {
160 1136 100       8603 if ( $dbname =~ s/^((?:[^\\;]|\\.)*?);//s )
161             {
162 804         1416 $var = $1;
163             }
164             else
165             {
166 332         469 $var = $dbname;
167 332         432 $dbname = "";
168             }
169              
170 1136 50       3778 if ( $var =~ m/^(.+?)=(.*)/s )
    0          
171             {
172 1136         2458 $var = $1;
173 1136         1737 ( $val = $2 ) =~ s/\\(.)/$1/g;
174 1136 50 33     3428 exists $attr->{$var}
175             and carp("$var is given in DSN *and* \$attr during DBI->connect()")
176             if ($^W);
177 1136 50       3870 exists $attr->{$var} or $attr->{$var} = $val;
178             }
179             elsif ( $var =~ m/^(.+?)=>(.*)/s )
180             {
181 0         0 $var = $1;
182 0         0 ( $val = $2 ) =~ s/\\(.)/$1/g;
183 0         0 my $ref = eval $val;
184             # $dbh->$var($ref);
185 0         0 push( @func_inits, $var, $ref );
186             }
187             }
188              
189             # The attributes need to be sorted in a specific way as the
190             # assignment is through tied hashes and calls STORE on each
191             # attribute. Some attributes require to be called prior to
192             # others
193             # e.g. f_dir *must* be done before xx_tables in DBD::File
194             # The dbh attribute sql_init_order is a hash with the order
195             # as key (low is first, 0 .. 100) and the attributes that
196             # are set to that oreder as anon-list as value:
197             # { 0 => [qw( AutoCommit PrintError RaiseError Profile ... )],
198             # 10 => [ list of attr to be dealt with immediately after first ],
199             # 50 => [ all fields that are unspecified or default sort order ],
200             # 90 => [ all fields that are needed after other initialisation ],
201             # }
202              
203 1152         1323 my %order = map {
204 576         5880 my $order = $_;
205 1152         1017 map { ( $_ => $order ) } @{ $dbh->{sql_init_order}{$order} };
  3840         12176  
  1152         3780  
206 576 50       849 } sort { $a <=> $b } keys %{ $dbh->{sql_init_order} || {} };
  576         4029  
207 5896         6900 my @ordered_attr =
208 13110         12042 map { $_->[0] }
209 5896 100       11432 sort { $a->[1] <=> $b->[1] }
210 576         2741 map { [ $_, defined $order{$_} ? $order{$_} : 50 ] }
211             keys %$attr;
212              
213             # initialize given attributes ... lower weighted before higher weighted
214 576         3308 foreach my $a (@ordered_attr)
215             {
216 5896 50       9453 exists $attr->{$a} or next;
217 5896 50       9029 $two_phased_init and eval {
218 5896         20010 $dbh->{$a} = $attr->{$a};
219 5896         26749 delete $attr->{$a};
220             };
221 5896 50       9024 $@ and $second_phase_attrs{$a} = delete $attr->{$a};
222 5896 50       10484 $two_phased_init or $dbh->STORE( $a, delete $attr->{$a} );
223             }
224              
225 576 50       3067 $two_phased_init and $dbh->func( 1, "init_default_attributes" );
226 576         2761 %$attr = %second_phase_attrs;
227              
228 576         1945 for ( my $i = 0; $i < scalar(@func_inits); $i += 2 )
229             {
230 0         0 my $func = $func_inits[$i];
231 0         0 my $arg = $func_inits[ $i + 1 ];
232 0         0 $dbh->$func($arg);
233             }
234              
235 576         1871 $dbh->func("init_done");
236              
237 576         2745 $dbh->STORE( Active => 1 );
238             }
239              
240 576         4790 return $dbh;
241             } # connect
242              
243             sub data_sources ($;$)
244             {
245 32     32   556 my ( $drh, $attr ) = @_;
246              
247 32         44 my $tbl_src;
248 32 50 33     200 $attr
      33        
249             and defined $attr->{sql_table_source}
250             and $attr->{sql_table_source}->isa('DBI::DBD::SqlEngine::TableSource')
251             and $tbl_src = $attr->{sql_table_source};
252              
253 32 50 33     480 !defined($tbl_src)
254             and $drh->{ImplementorClass}->can('default_table_source')
255             and $tbl_src = $drh->{ImplementorClass}->default_table_source();
256 32 50       93 defined($tbl_src) or return;
257              
258 32         241 $tbl_src->data_sources( $drh, $attr );
259             } # data_sources
260              
261             sub disconnect_all
262 0     0   0 {
263             } # disconnect_all
264              
265             sub DESTROY
266             {
267 0     0   0 undef;
268             } # DESTROY
269              
270             # ====== DATABASE ==============================================================
271              
272             package DBI::DBD::SqlEngine::db;
273              
274 48     48   268 use strict;
  48         189  
  48         1376  
275 48     48   197 use warnings;
  48         58  
  48         2309  
276              
277 48     48   223 use vars qw(@ISA $imp_data_size);
  48         59  
  48         2196  
278              
279 48     48   210 use Carp;
  48         62  
  48         173043  
280              
281             if ( eval { require Clone; } )
282             {
283             Clone->import("clone");
284             }
285             else
286             {
287             require Storable; # in CORE since 5.7.3
288             *clone = \&Storable::dclone;
289             }
290              
291             $imp_data_size = 0;
292              
293             sub ping
294             {
295 348 50   348   3624 ( $_[0]->FETCH("Active") ) ? 1 : 0;
296             } # ping
297              
298             sub data_sources
299             {
300 24     24   37 my ( $dbh, $attr, @other ) = @_;
301 24         49 my $drh = $dbh->{Driver}; # XXX proxy issues?
302 24 50       63 ref($attr) eq 'HASH' or $attr = {};
303 24 50       121 defined( $attr->{sql_table_source} ) or $attr->{sql_table_source} = $dbh->{sql_table_source};
304 24         105 return $drh->data_sources( $attr, @other );
305             }
306              
307             sub prepare ($$;@)
308             {
309 724     724   29739 my ( $dbh, $statement, @attribs ) = @_;
310              
311             # create a 'blank' sth
312 724         3387 my $sth = DBI::_new_sth( $dbh, { Statement => $statement } );
313              
314 724 50       2013 if ($sth)
315             {
316 724         2852 my $class = $sth->FETCH("ImplementorClass");
317 724         6363 $class =~ s/::st$/::Statement/;
318 724         924 my $stmt;
319              
320             # if using SQL::Statement version > 1
321             # cache the parser object if the DBD supports parser caching
322             # SQL::Nano and older SQL::Statements don't support this
323              
324 724 50       7288 if ( $class->isa("SQL::Statement") )
325             {
326 0         0 my $parser = $dbh->{sql_parser_object};
327 0   0     0 $parser ||= eval { $dbh->func("sql_parser_object") };
  0         0  
328 0 0       0 if ($@)
329             {
330 0         0 $stmt = eval { $class->new($statement) };
  0         0  
331             }
332             else
333             {
334 0         0 $stmt = eval { $class->new( $statement, $parser ) };
  0         0  
335             }
336             }
337             else
338             {
339 724         1034 $stmt = eval { $class->new($statement) };
  724         3096  
340             }
341 724 100 66     3426 if ( $@ || $stmt->{errstr} )
342             {
343 48   33     462 $dbh->set_err( $DBI::stderr, $@ || $stmt->{errstr} );
344 48         270 undef $sth;
345             }
346             else
347             {
348 676         3130 $sth->STORE( "sql_stmt", $stmt );
349 676         3799 $sth->STORE( "sql_params", [] );
350 676         4161 $sth->STORE( "NUM_OF_PARAMS", scalar( $stmt->params() ) );
351 676         4287 my @colnames = $sth->sql_get_colnames();
352 676         5182 $sth->STORE( "NUM_OF_FIELDS", scalar @colnames );
353             }
354             }
355 724         4668 return $sth;
356             } # prepare
357              
358             sub set_versions
359             {
360 576     576   792 my $dbh = $_[0];
361 576         1001 $dbh->{sql_engine_version} = $DBI::DBD::SqlEngine::VERSION;
362 576         1256 for (qw( nano_version statement_version ))
363             {
364 1152 100       3519 defined $DBI::SQL::Nano::versions->{$_} or next;
365 576         1649 $dbh->{"sql_$_"} = $DBI::SQL::Nano::versions->{$_};
366             }
367 576 50       1914 $dbh->{sql_handler} =
368             $dbh->{sql_statement_version}
369             ? "SQL::Statement"
370             : "DBI::SQL::Nano";
371              
372 576         1307 return $dbh;
373             } # set_versions
374              
375             sub init_valid_attributes
376             {
377 576     576   883 my $dbh = $_[0];
378              
379 576         5516 $dbh->{sql_valid_attrs} = {
380             sql_engine_version => 1, # DBI::DBD::SqlEngine version
381             sql_handler => 1, # Nano or S:S
382             sql_nano_version => 1, # Nano version
383             sql_statement_version => 1, # S:S version
384             sql_flags => 1, # flags for SQL::Parser
385             sql_dialect => 1, # dialect for SQL::Parser
386             sql_quoted_identifier_case => 1, # case for quoted identifiers
387             sql_identifier_case => 1, # case for non-quoted identifiers
388             sql_parser_object => 1, # SQL::Parser instance
389             sql_sponge_driver => 1, # Sponge driver for table_info ()
390             sql_valid_attrs => 1, # SQL valid attributes
391             sql_readonly_attrs => 1, # SQL readonly attributes
392             sql_init_phase => 1, # Only during initialization
393             sql_meta => 1, # meta data for tables
394             sql_meta_map => 1, # mapping table for identifier case
395             };
396 576         3069 $dbh->{sql_readonly_attrs} = {
397             sql_engine_version => 1, # DBI::DBD::SqlEngine version
398             sql_handler => 1, # Nano or S:S
399             sql_nano_version => 1, # Nano version
400             sql_statement_version => 1, # S:S version
401             sql_quoted_identifier_case => 1, # case for quoted identifiers
402             sql_parser_object => 1, # SQL::Parser instance
403             sql_sponge_driver => 1, # Sponge driver for table_info ()
404             sql_valid_attrs => 1, # SQL valid attributes
405             sql_readonly_attrs => 1, # SQL readonly attributes
406             };
407              
408 576         1690 return $dbh;
409             } # init_valid_attributes
410              
411             sub init_default_attributes
412             {
413 1152     1152   1283 my ( $dbh, $phase ) = @_;
414 1152         1262 my $given_phase = $phase;
415              
416 1152 50       2378 unless ( defined($phase) )
417             {
418             # we have an "old" driver here
419 0         0 $phase = defined $dbh->{sql_init_phase};
420 0 0       0 $phase and $phase = $dbh->{sql_init_phase};
421             }
422              
423 1152 100       2242 if ( 0 == $phase )
424             {
425             # must be done first, because setting flags implicitly calls $dbdname::db->STORE
426 576         2141 $dbh->func("init_valid_attributes");
427              
428 576         4239 $dbh->func("set_versions");
429              
430 576         2309 $dbh->{sql_identifier_case} = 2; # SQL_IC_LOWER
431 576         842 $dbh->{sql_quoted_identifier_case} = 3; # SQL_IC_SENSITIVE
432              
433 576         992 $dbh->{sql_dialect} = "CSV";
434              
435 576         1819 $dbh->{sql_init_phase} = $given_phase;
436              
437             # complete derived attributes, if required
438 576         3202 ( my $drv_class = $dbh->{ImplementorClass} ) =~ s/::db$//;
439 576         2272 my $drv_prefix = DBI->driver_prefix($drv_class);
440 576         1017 my $valid_attrs = $drv_prefix . "valid_attrs";
441 576         839 my $ro_attrs = $drv_prefix . "readonly_attrs";
442              
443             # check whether we're running in a Gofer server or not (see
444             # validate_FETCH_attr for details)
445 576   100     6114 $dbh->{sql_engine_in_gofer} =
446             ( defined $INC{"DBD/Gofer.pm"} && ( caller(5) )[0] eq "DBI::Gofer::Execute" );
447 576         1368 $dbh->{sql_meta} = {};
448 576         1123 $dbh->{sql_meta_map} = {}; # choose new name because it contains other keys
449              
450             # init_default_attributes calls inherited routine before derived DBD's
451             # init their default attributes, so we don't override something here
452             #
453             # defining an order of attribute initialization from connect time
454             # specified ones with a magic baarier (see next statement)
455 576         979 my $drv_pfx_meta = $drv_prefix . "meta";
456 576 100       3675 $dbh->{sql_init_order} = {
457             0 => [qw( Profile RaiseError PrintError AutoCommit )],
458             90 => [ "sql_meta", $dbh->{$drv_pfx_meta} ? $dbh->{$drv_pfx_meta} : () ],
459             };
460             # ensuring Profile, RaiseError, PrintError, AutoCommit are initialized
461             # first when initializing attributes from connect time specified
462             # attributes
463             # further, initializations to predefined tables are happens after any
464             # unspecified attribute initialization (that default to order 50)
465              
466 576         1579 my @comp_attrs = qw(valid_attrs version readonly_attrs);
467              
468 576 100 66     2720 if ( exists $dbh->{$drv_pfx_meta} and !$dbh->{sql_engine_in_gofer} )
469             {
470 384         603 my $attr = $dbh->{$drv_pfx_meta};
471 384 50 33     2922 defined $attr
      33        
472             and defined $dbh->{$valid_attrs}
473             and !defined $dbh->{$valid_attrs}{$attr}
474             and $dbh->{$valid_attrs}{$attr} = 1;
475              
476 384         453 my %h;
477 384         2179 tie %h, "DBI::DBD::SqlEngine::TieTables", $dbh;
478 384         1196 $dbh->{$attr} = \%h;
479              
480 384         806 push @comp_attrs, "meta";
481             }
482              
483 576         1045 foreach my $comp_attr (@comp_attrs)
484             {
485 2112         2431 my $attr = $drv_prefix . $comp_attr;
486 2112 50 33     8159 defined $dbh->{$valid_attrs}
487             and !defined $dbh->{$valid_attrs}{$attr}
488             and $dbh->{$valid_attrs}{$attr} = 1;
489 2112 50 33     9279 defined $dbh->{$ro_attrs}
490             and !defined $dbh->{$ro_attrs}{$attr}
491             and $dbh->{$ro_attrs}{$attr} = 1;
492             }
493             }
494              
495 1152         2233 return $dbh;
496             } # init_default_attributes
497              
498             sub init_done
499             {
500 576 50   576   5016 defined $_[0]->{sql_init_phase} and delete $_[0]->{sql_init_phase};
501 576         1283 delete $_[0]->{sql_valid_attrs}->{sql_init_phase};
502 576         1016 return;
503             }
504              
505             sub sql_parser_object
506             {
507 0     0   0 my $dbh = $_[0];
508 0   0     0 my $dialect = $dbh->{sql_dialect} || "CSV";
509 0         0 my $parser = {
510             RaiseError => $dbh->FETCH("RaiseError"),
511             PrintError => $dbh->FETCH("PrintError"),
512             };
513 0   0     0 my $sql_flags = $dbh->FETCH("sql_flags") || {};
514 0         0 %$parser = ( %$parser, %$sql_flags );
515 0         0 $parser = SQL::Parser->new( $dialect, $parser );
516 0         0 $dbh->{sql_parser_object} = $parser;
517 0         0 return $parser;
518             } # sql_parser_object
519              
520             sub sql_sponge_driver
521             {
522 28     28   350 my $dbh = $_[0];
523 28         64 my $dbh2 = $dbh->{sql_sponge_driver};
524 28 50       125 unless ($dbh2)
525             {
526 28         191 $dbh2 = $dbh->{sql_sponge_driver} = DBI->connect("DBI:Sponge:");
527 28 50       267 unless ($dbh2)
528             {
529 0         0 $dbh->set_err( $DBI::stderr, $DBI::errstr );
530 0         0 return;
531             }
532             }
533             }
534              
535             sub disconnect ($)
536             {
537 208     208   12635 %{ $_[0]->{sql_meta} } = ();
  208         872  
538 208         255 %{ $_[0]->{sql_meta_map} } = ();
  208         426  
539 208         815 $_[0]->STORE( Active => 0 );
540 208         1011 return 1;
541             } # disconnect
542              
543             sub validate_FETCH_attr
544             {
545 0     0   0 my ( $dbh, $attrib ) = @_;
546              
547             # If running in a Gofer server, access to our tied compatibility hash
548             # would force Gofer to serialize the tieing object including it's
549             # private $dbh reference used to do the driver function calls.
550             # This will result in nasty exceptions. So return a copy of the
551             # sql_meta structure instead, which is the source of for the compatibility
552             # tie-hash. It's not as good as liked, but the best we can do in this
553             # situation.
554 0 0       0 if ( $dbh->{sql_engine_in_gofer} )
555             {
556 0         0 ( my $drv_class = $dbh->{ImplementorClass} ) =~ s/::db$//;
557 0         0 my $drv_prefix = DBI->driver_prefix($drv_class);
558 0 0 0     0 exists $dbh->{ $drv_prefix . "meta" } && $attrib eq $dbh->{ $drv_prefix . "meta" }
559             and $attrib = "sql_meta";
560             }
561              
562 0         0 return $attrib;
563             }
564              
565             sub FETCH ($$)
566             {
567 3676     3676   16764 my ( $dbh, $attrib ) = @_;
568 3676 50       6640 $attrib eq "AutoCommit"
569             and return 1;
570              
571             # Driver private attributes are lower cased
572 3676 50       6614 if ( $attrib eq ( lc $attrib ) )
573             {
574             # first let the implementation deliver an alias for the attribute to fetch
575             # after it validates the legitimation of the fetch request
576 0 0       0 $attrib = $dbh->func( $attrib, "validate_FETCH_attr" ) or return;
577              
578 0         0 my $attr_prefix;
579 0 0       0 $attrib =~ m/^([a-z]+_)/ and $attr_prefix = $1;
580 0 0       0 unless ($attr_prefix)
581             {
582 0         0 ( my $drv_class = $dbh->{ImplementorClass} ) =~ s/::db$//;
583 0         0 $attr_prefix = DBI->driver_prefix($drv_class);
584 0         0 $attrib = $attr_prefix . $attrib;
585             }
586 0         0 my $valid_attrs = $attr_prefix . "valid_attrs";
587 0         0 my $ro_attrs = $attr_prefix . "readonly_attrs";
588              
589 0 0 0     0 exists $dbh->{$valid_attrs}
590             and ( $dbh->{$valid_attrs}{$attrib}
591             or return $dbh->set_err( $DBI::stderr, "Invalid attribute '$attrib'" ) );
592 0 0 0     0 exists $dbh->{$ro_attrs}
      0        
      0        
593             and $dbh->{$ro_attrs}{$attrib}
594             and defined $dbh->{$attrib}
595             and refaddr( $dbh->{$attrib} )
596             and return clone( $dbh->{$attrib} );
597              
598 0         0 return $dbh->{$attrib};
599             }
600             # else pass up to DBI to handle
601 3676         15973 return $dbh->SUPER::FETCH($attrib);
602             } # FETCH
603              
604             sub validate_STORE_attr
605             {
606 4072     4072   4609 my ( $dbh, $attrib, $value ) = @_;
607              
608 4072 50 66     14632 if ( $attrib eq "sql_identifier_case" || $attrib eq "sql_quoted_identifier_case"
      33        
      66        
609             and $value < 1 || $value > 4 )
610             {
611 0         0 croak "attribute '$attrib' must have a value from 1 .. 4 (SQL_IC_UPPER .. SQL_IC_MIXED)";
612             # XXX correctly a remap of all entries in sql_meta/sql_meta_map is required here
613             }
614              
615 4072         18209 ( my $drv_class = $dbh->{ImplementorClass} ) =~ s/::db$//;
616 4072         11105 my $drv_prefix = DBI->driver_prefix($drv_class);
617              
618 4072 100 100     16344 exists $dbh->{ $drv_prefix . "meta" }
619             and $attrib eq $dbh->{ $drv_prefix . "meta" }
620             and $attrib = "sql_meta";
621              
622 4072         12740 return ( $attrib, $value );
623             }
624              
625             # the ::db::STORE method is what gets called when you set
626             # a lower-cased database handle attribute such as $dbh->{somekey}=$someval;
627             #
628             # STORE should check to make sure that "somekey" is a valid attribute name
629             # but only if it is really one of our attributes (starts with dbm_ or foo_)
630             # You can also check for valid values for the attributes if needed
631             # and/or perform other operations
632             #
633             sub STORE ($$$)
634             {
635 9784     9784   58881 my ( $dbh, $attrib, $value ) = @_;
636              
637 9784 100       16108 if ( $attrib eq "AutoCommit" )
638             {
639 768 50       2833 $value and return 1; # is already set
640 0         0 croak "Can't disable AutoCommit";
641             }
642              
643 9016 100       15798 if ( $attrib eq lc $attrib )
644             {
645             # Driver private attributes are lower cased
646              
647 4072         11836 ( $attrib, $value ) = $dbh->func( $attrib, $value, "validate_STORE_attr" );
648 4072 50       21442 $attrib or return;
649              
650 4072         3432 my $attr_prefix;
651 4072 50       18657 $attrib =~ m/^([a-z]+_)/ and $attr_prefix = $1;
652 4072 50       6164 unless ($attr_prefix)
653             {
654 0         0 ( my $drv_class = $dbh->{ImplementorClass} ) =~ s/::db$//;
655 0         0 $attr_prefix = DBI->driver_prefix($drv_class);
656 0         0 $attrib = $attr_prefix . $attrib;
657             }
658 4072         6078 my $valid_attrs = $attr_prefix . "valid_attrs";
659 4072         4302 my $ro_attrs = $attr_prefix . "readonly_attrs";
660              
661 4072 100 100     11176 exists $dbh->{$valid_attrs}
662             and ( $dbh->{$valid_attrs}{$attrib}
663             or return $dbh->set_err( $DBI::stderr, "Invalid attribute '$attrib'" ) );
664 4068 50 66     14489 exists $dbh->{$ro_attrs}
      33        
665             and $dbh->{$ro_attrs}{$attrib}
666             and defined $dbh->{$attrib}
667             and return $dbh->set_err( $DBI::stderr,
668             "attribute '$attrib' is readonly and must not be modified" );
669              
670 4068 100       6618 if ( $attrib eq "sql_meta" )
671             {
672 36         181 while ( my ( $k, $v ) = each %$value )
673             {
674 36         146 $dbh->{$attrib}{$k} = $v;
675             }
676             }
677             else
678             {
679 4032         7009 $dbh->{$attrib} = $value;
680             }
681              
682 4068         14812 return 1;
683             }
684              
685 4944         18235 return $dbh->SUPER::STORE( $attrib, $value );
686             } # STORE
687              
688             sub get_driver_versions
689             {
690 16     16   32 my ( $dbh, $table ) = @_;
691 16         380 my %vsn = (
692             OS => "$^O ($Config::Config{osvers})",
693             Perl => "$] ($Config::Config{archname})",
694             DBI => $DBI::VERSION,
695             );
696 16         43 my %vmp;
697              
698 16 50       110 my $sql_engine_verinfo =
699             join " ",
700             $dbh->{sql_engine_version}, "using", $dbh->{sql_handler},
701             $dbh->{sql_handler} eq "SQL::Statement"
702             ? $dbh->{sql_statement_version}
703             : $dbh->{sql_nano_version};
704              
705 16         25 my $indent = 0;
706 16         45 my @deriveds = ( $dbh->{ImplementorClass} );
707 16         53 while (@deriveds)
708             {
709 40         61 my $derived = shift @deriveds;
710 40 100       108 $derived eq "DBI::DBD::SqlEngine::db" and last;
711 24 50       265 $derived->isa("DBI::DBD::SqlEngine::db") or next;
712             #no strict 'refs';
713 24         1545 eval "push \@deriveds, \@${derived}::ISA";
714             #use strict;
715 24         127 ( my $drv_class = $derived ) =~ s/::db$//;
716 24         120 my $drv_prefix = DBI->driver_prefix($drv_class);
717 24         174 my $ddgv = $dbh->{ImplementorClass}->can("get_${drv_prefix}versions");
718 24 50       129 my $drv_version = $ddgv ? &$ddgv( $dbh, $table ) : $dbh->{ $drv_prefix . "version" };
719             $drv_version ||=
720 24   33     70 eval { $derived->VERSION() }; # XXX access $drv_class::VERSION via symbol table
  0         0  
721 24         53 $vsn{$drv_class} = $drv_version;
722 24 100       77 $indent and $vmp{$drv_class} = " " x $indent . $drv_class;
723 24         68 $indent += 2;
724             }
725              
726 16         39 $vsn{"DBI::DBD::SqlEngine"} = $sql_engine_verinfo;
727 16 50       93 $indent and $vmp{"DBI::DBD::SqlEngine"} = " " x $indent . "DBI::DBD::SqlEngine";
728              
729 16 100       60 $DBI::PurePerl and $vsn{"DBI::PurePerl"} = $DBI::PurePerl::VERSION;
730              
731 16         28 $indent += 20;
732 96 100 66     533 my @versions = map { sprintf "%-${indent}s %s", $vmp{$_} || $_, $vsn{$_} }
  167         608  
733             sort {
734 16         88 $a->isa($b) and return -1;
735 151 100       545 $b->isa($a) and return 1;
736 138 100       390 $a->isa("DBI::DBD::SqlEngine") and return -1;
737 95 100       291 $b->isa("DBI::DBD::SqlEngine") and return 1;
738 63         84 return $a cmp $b;
739             } keys %vsn;
740              
741 16 50       218 return wantarray ? @versions : join "\n", @versions;
742             } # get_versions
743              
744             sub get_single_table_meta
745             {
746 56     56   84 my ( $dbh, $table, $attr ) = @_;
747 56         54 my $meta;
748              
749 56 50       120 $table eq "."
750             and return $dbh->FETCH($attr);
751              
752 56         270 ( my $class = $dbh->{ImplementorClass} ) =~ s/::db$/::Table/;
753 56         206 ( undef, $meta ) = $class->get_table_meta( $dbh, $table, 1 );
754 56 50       124 $meta or croak "No such table '$table'";
755              
756             # prevent creation of undef attributes
757 56         1046 return $class->get_table_meta_attr( $meta, $attr );
758             } # get_single_table_meta
759              
760             sub get_sql_engine_meta
761             {
762 32     32   64 my ( $dbh, $table, $attr ) = @_;
763              
764 32         159 my $gstm = $dbh->{ImplementorClass}->can("get_single_table_meta");
765              
766 0         0 $table eq "*"
767 32 50       109 and $table = [ ".", keys %{ $dbh->{sql_meta} } ];
768 0         0 $table eq "+"
769 32 50       76 and $table = [ grep { m/^[_A-Za-z0-9]+$/ } keys %{ $dbh->{sql_meta} } ];
  0         0  
770 0         0 ref $table eq "Regexp"
771 32 50       88 and $table = [ grep { $_ =~ $table } keys %{ $dbh->{sql_meta} } ];
  0         0  
772              
773 32 100 66     211 ref $table || ref $attr
774             or return &$gstm( $dbh, $table, $attr );
775              
776 8 50       33 ref $table or $table = [$table];
777 8 50       28 ref $attr or $attr = [$attr];
778 8 50       1783 "ARRAY" eq ref $table
779             or return
780             $dbh->set_err( $DBI::stderr,
781             "Invalid argument for \$table - SCALAR, Regexp or ARRAY expected but got " . ref $table );
782 8 50       430 "ARRAY" eq ref $attr
783             or return $dbh->set_err(
784             "Invalid argument for \$attr - SCALAR or ARRAY expected but got " . ref $attr );
785              
786 8         16 my %results;
787 8         15 foreach my $tname ( @{$table} )
  8         30  
788             {
789 16         20 my %tattrs;
790 16         21 foreach my $aname ( @{$attr} )
  16         29  
791             {
792 32         59 $tattrs{$aname} = &$gstm( $dbh, $tname, $aname );
793             }
794 16         49 $results{$tname} = \%tattrs;
795             }
796              
797 8         60 return \%results;
798             } # get_sql_engine_meta
799              
800             sub set_single_table_meta
801             {
802 8     8   15 my ( $dbh, $table, $attr, $value ) = @_;
803 8         8 my $meta;
804              
805 8 50       18 $table eq "."
806             and return $dbh->STORE( $attr, $value );
807              
808 8         40 ( my $class = $dbh->{ImplementorClass} ) =~ s/::db$/::Table/;
809 8         43 ( undef, $meta ) = $class->get_table_meta( $dbh, $table, 1 );
810 8 50       29 $meta or croak "No such table '$table'";
811 8         39 $class->set_table_meta_attr( $meta, $attr, $value );
812              
813 8         37 return $dbh;
814             } # set_single_table_meta
815              
816             sub set_sql_engine_meta
817             {
818 8     8   15 my ( $dbh, $table, $attr, $value ) = @_;
819              
820 8         66 my $sstm = $dbh->{ImplementorClass}->can("set_single_table_meta");
821              
822 0         0 $table eq "*"
823 8 50       28 and $table = [ ".", keys %{ $dbh->{sql_meta} } ];
824 0         0 $table eq "+"
825 8 50       17 and $table = [ grep { m/^[_A-Za-z0-9]+$/ } keys %{ $dbh->{sql_meta} } ];
  0         0  
826 0         0 ref($table) eq "Regexp"
827 8 50       21 and $table = [ grep { $_ =~ $table } keys %{ $dbh->{sql_meta} } ];
  0         0  
828              
829 8 100 66     52 ref $table || ref $attr
830             or return &$sstm( $dbh, $table, $attr, $value );
831              
832 4 50       16 ref $table or $table = [$table];
833 4 50       13 ref $attr or $attr = { $attr => $value };
834 4 50       13 "ARRAY" eq ref $table
835             or croak "Invalid argument for \$table - SCALAR, Regexp or ARRAY expected but got "
836             . ref $table;
837 4 50       19 "HASH" eq ref $attr
838             or croak "Invalid argument for \$attr - SCALAR or HASH expected but got " . ref $attr;
839              
840 4         16 foreach my $tname ( @{$table} )
  4         11  
841             {
842 4         9 my %tattrs;
843 4         20 while ( my ( $aname, $aval ) = each %$attr )
844             {
845 4         9 &$sstm( $dbh, $tname, $aname, $aval );
846             }
847             }
848              
849 4         19 return $dbh;
850             } # set_file_meta
851              
852             sub clear_sql_engine_meta
853             {
854 4     4   10 my ( $dbh, $table ) = @_;
855              
856 4         26 ( my $class = $dbh->{ImplementorClass} ) =~ s/::db$/::Table/;
857 4         26 my ( undef, $meta ) = $class->get_table_meta( $dbh, $table, 1 );
858 4 50       21 $meta and %{$meta} = ();
  4         32  
859              
860 4         17 return;
861             } # clear_file_meta
862              
863             sub DESTROY ($)
864             {
865 192     192   4491 my $dbh = shift;
866 192 50       1039 $dbh->SUPER::FETCH("Active") and $dbh->disconnect;
867 192         5798 undef $dbh->{sql_parser_object};
868             } # DESTROY
869              
870             sub type_info_all ($)
871             {
872             [
873             {
874 0     0   0 TYPE_NAME => 0,
875             DATA_TYPE => 1,
876             PRECISION => 2,
877             LITERAL_PREFIX => 3,
878             LITERAL_SUFFIX => 4,
879             CREATE_PARAMS => 5,
880             NULLABLE => 6,
881             CASE_SENSITIVE => 7,
882             SEARCHABLE => 8,
883             UNSIGNED_ATTRIBUTE => 9,
884             MONEY => 10,
885             AUTO_INCREMENT => 11,
886             LOCAL_TYPE_NAME => 12,
887             MINIMUM_SCALE => 13,
888             MAXIMUM_SCALE => 14,
889             },
890             [
891             "VARCHAR", DBI::SQL_VARCHAR(), undef, "'", "'", undef, 0, 1, 1, 0, 0, 0, undef, 1, 999999,
892             ],
893             [ "CHAR", DBI::SQL_CHAR(), undef, "'", "'", undef, 0, 1, 1, 0, 0, 0, undef, 1, 999999, ],
894             [ "INTEGER", DBI::SQL_INTEGER(), undef, "", "", undef, 0, 0, 1, 0, 0, 0, undef, 0, 0, ],
895             [ "REAL", DBI::SQL_REAL(), undef, "", "", undef, 0, 0, 1, 0, 0, 0, undef, 0, 0, ],
896             [
897             "BLOB", DBI::SQL_LONGVARBINARY(), undef, "'", "'", undef, 0, 1, 1, 0, 0, 0, undef, 1,
898             999999,
899             ],
900             [
901             "BLOB", DBI::SQL_LONGVARBINARY(), undef, "'", "'", undef, 0, 1, 1, 0, 0, 0, undef, 1,
902             999999,
903             ],
904             [
905             "TEXT", DBI::SQL_LONGVARCHAR(), undef, "'", "'", undef, 0, 1, 1, 0, 0, 0, undef, 1,
906             999999,
907             ],
908             ];
909             } # type_info_all
910              
911             sub get_avail_tables
912             {
913 36     36   320 my $dbh = $_[0];
914 36         85 my @tables = ();
915              
916 36 50 33     184 if ( $dbh->{sql_handler} eq "SQL::Statement" and $dbh->{sql_ram_tables} )
917             {
918             # XXX map +[ undef, undef, $_, "TABLE", "TEMP" ], keys %{...}
919 0         0 foreach my $table ( keys %{ $dbh->{sql_ram_tables} } )
  0         0  
920             {
921 0         0 push @tables, [ undef, undef, $table, "TABLE", "TEMP" ];
922             }
923             }
924              
925 36         49 my $tbl_src;
926 36 50 33     223 defined $dbh->{sql_table_source}
927             and $dbh->{sql_table_source}->isa('DBI::DBD::SqlEngine::TableSource')
928             and $tbl_src = $dbh->{sql_table_source};
929              
930 36 50 33     680 !defined($tbl_src)
931             and $dbh->{Driver}->{ImplementorClass}->can('default_table_source')
932             and $tbl_src = $dbh->{Driver}->{ImplementorClass}->default_table_source();
933 36 50       336 defined($tbl_src) and push( @tables, $tbl_src->avail_tables($dbh) );
934              
935 36         174 return @tables;
936             } # get_avail_tables
937              
938             {
939             my $names = [qw( TABLE_QUALIFIER TABLE_OWNER TABLE_NAME TABLE_TYPE REMARKS )];
940              
941             sub table_info ($)
942             {
943 28     28   2907 my $dbh = shift;
944              
945 28         188 my @tables = $dbh->func("get_avail_tables");
946              
947             # Temporary kludge: DBD::Sponge dies if @tables is empty. :-(
948             # this no longer seems to be true @tables or return;
949              
950 28         353 my $dbh2 = $dbh->func("sql_sponge_driver");
951 28         456 my $sth = $dbh2->prepare(
952             "TABLE_INFO",
953             {
954             rows => \@tables,
955             NAME => $names,
956             }
957             );
958 28 50       330 $sth or return $dbh->set_err( $DBI::stderr, $dbh2->errstr );
959 28 50       227 $sth->execute or return;
960 28         286 return $sth;
961             } # table_info
962             }
963              
964             sub list_tables ($)
965             {
966 8     8   106 my $dbh = shift;
967 8         17 my @table_list;
968              
969 8 50       48 my @tables = $dbh->func("get_avail_tables") or return;
970 8         62 foreach my $ref (@tables)
971             {
972             # rt69260 and rt67223 - the same issue in 2 different queues
973 16         29 push @table_list, $ref->[2];
974             }
975              
976 8         39 return @table_list;
977             } # list_tables
978              
979             sub quote ($$;$)
980             {
981 0     0   0 my ( $self, $str, $type ) = @_;
982 0 0       0 defined $str or return "NULL";
983 0 0 0     0 defined $type && ( $type == DBI::SQL_NUMERIC()
      0        
984             || $type == DBI::SQL_DECIMAL()
985             || $type == DBI::SQL_INTEGER()
986             || $type == DBI::SQL_SMALLINT()
987             || $type == DBI::SQL_FLOAT()
988             || $type == DBI::SQL_REAL()
989             || $type == DBI::SQL_DOUBLE()
990             || $type == DBI::SQL_TINYINT() )
991             and return $str;
992              
993 0         0 $str =~ s/\\/\\\\/sg;
994 0         0 $str =~ s/\0/\\0/sg;
995 0         0 $str =~ s/\'/\\\'/sg;
996 0         0 $str =~ s/\n/\\n/sg;
997 0         0 $str =~ s/\r/\\r/sg;
998 0         0 return "'$str'";
999             } # quote
1000              
1001             sub commit ($)
1002             {
1003 0     0   0 my $dbh = shift;
1004 0 0       0 $dbh->FETCH("Warn")
1005             and carp "Commit ineffective while AutoCommit is on", -1;
1006 0         0 return 1;
1007             } # commit
1008              
1009             sub rollback ($)
1010             {
1011 0     0   0 my $dbh = shift;
1012 0 0       0 $dbh->FETCH("Warn")
1013             and carp "Rollback ineffective while AutoCommit is on", -1;
1014 0         0 return 0;
1015             } # rollback
1016              
1017             # ====== Tie-Meta ==============================================================
1018              
1019             package DBI::DBD::SqlEngine::TieMeta;
1020              
1021 48     48   387 use Carp qw(croak);
  48         78  
  48         15722  
1022             require Tie::Hash;
1023             @DBI::DBD::SqlEngine::TieMeta::ISA = qw(Tie::Hash);
1024              
1025             sub TIEHASH
1026             {
1027 24     24   40 my ( $class, $tblClass, $tblMeta ) = @_;
1028              
1029 24         92 my $self = bless(
1030             {
1031             tblClass => $tblClass,
1032             tblMeta => $tblMeta,
1033             },
1034             $class
1035             );
1036 24         58 return $self;
1037             } # new
1038              
1039             sub STORE
1040             {
1041 4     4   11 my ( $self, $meta_attr, $meta_val ) = @_;
1042              
1043 4         54 $self->{tblClass}->set_table_meta_attr( $self->{tblMeta}, $meta_attr, $meta_val );
1044              
1045 4         14 return;
1046             } # STORE
1047              
1048             sub FETCH
1049             {
1050 20     20   54 my ( $self, $meta_attr ) = @_;
1051              
1052 20         96 return $self->{tblClass}->get_table_meta_attr( $self->{tblMeta}, $meta_attr );
1053             } # FETCH
1054              
1055             sub FIRSTKEY
1056             {
1057 0     0   0 my $a = scalar keys %{ $_[0]->{tblMeta} };
  0         0  
1058 0         0 each %{ $_[0]->{tblMeta} };
  0         0  
1059             } # FIRSTKEY
1060              
1061             sub NEXTKEY
1062             {
1063 0     0   0 each %{ $_[0]->{tblMeta} };
  0         0  
1064             } # NEXTKEY
1065              
1066             sub EXISTS
1067             {
1068 0     0   0 exists $_[0]->{tblMeta}{ $_[1] };
1069             } # EXISTS
1070              
1071             sub DELETE
1072             {
1073 0     0   0 croak "Can't delete single attributes from table meta structure";
1074             } # DELETE
1075              
1076             sub CLEAR
1077             {
1078 0     0   0 %{ $_[0]->{tblMeta} } = ();
  0         0  
1079             } # CLEAR
1080              
1081             sub SCALAR
1082             {
1083 0     0   0 scalar %{ $_[0]->{tblMeta} };
  0         0  
1084             } # SCALAR
1085              
1086             # ====== Tie-Tables ============================================================
1087              
1088             package DBI::DBD::SqlEngine::TieTables;
1089              
1090 48     48   251 use Carp qw(croak);
  48         63  
  48         23888  
1091             require Tie::Hash;
1092             @DBI::DBD::SqlEngine::TieTables::ISA = qw(Tie::Hash);
1093              
1094             sub TIEHASH
1095             {
1096 384     384   550 my ( $class, $dbh ) = @_;
1097              
1098 384         1986 ( my $tbl_class = $dbh->{ImplementorClass} ) =~ s/::db$/::Table/;
1099 384         1620 my $self = bless(
1100             {
1101             dbh => $dbh,
1102             tblClass => $tbl_class,
1103             },
1104             $class
1105             );
1106 384         1006 return $self;
1107             } # new
1108              
1109             sub STORE
1110             {
1111 0     0   0 my ( $self, $table, $tbl_meta ) = @_;
1112              
1113 0 0       0 "HASH" eq ref $tbl_meta
1114             or croak "Invalid data for storing as table meta data (must be hash)";
1115              
1116 0         0 ( undef, my $meta ) = $self->{tblClass}->get_table_meta( $self->{dbh}, $table, 1 );
1117 0 0       0 $meta or croak "Invalid table name '$table'";
1118              
1119 0         0 while ( my ( $meta_attr, $meta_val ) = each %$tbl_meta )
1120             {
1121 0         0 $self->{tblClass}->set_table_meta_attr( $meta, $meta_attr, $meta_val );
1122             }
1123              
1124 0         0 return;
1125             } # STORE
1126              
1127             sub FETCH
1128             {
1129 24     24   7045 my ( $self, $table ) = @_;
1130              
1131 24         128 ( undef, my $meta ) = $self->{tblClass}->get_table_meta( $self->{dbh}, $table, 1 );
1132 24 50       63 $meta or croak "Invalid table name '$table'";
1133              
1134 24         30 my %h;
1135 24         124 tie %h, "DBI::DBD::SqlEngine::TieMeta", $self->{tblClass}, $meta;
1136              
1137 24         160 return \%h;
1138             } # FETCH
1139              
1140             sub FIRSTKEY
1141             {
1142 0     0   0 my $a = scalar keys %{ $_[0]->{dbh}->{sql_meta} };
  0         0  
1143 0         0 each %{ $_[0]->{dbh}->{sql_meta} };
  0         0  
1144             } # FIRSTKEY
1145              
1146             sub NEXTKEY
1147             {
1148 0     0   0 each %{ $_[0]->{dbh}->{sql_meta} };
  0         0  
1149             } # NEXTKEY
1150              
1151             sub EXISTS
1152             {
1153 0 0   0   0 exists $_[0]->{dbh}->{sql_meta}->{ $_[1] }
1154             or exists $_[0]->{dbh}->{sql_meta_map}->{ $_[1] };
1155             } # EXISTS
1156              
1157             sub DELETE
1158             {
1159 0     0   0 my ( $self, $table ) = @_;
1160              
1161 0         0 ( undef, my $meta ) = $self->{tblClass}->get_table_meta( $self->{dbh}, $table, 1 );
1162 0 0       0 $meta or croak "Invalid table name '$table'";
1163              
1164 0         0 delete $_[0]->{dbh}->{sql_meta}->{ $meta->{table_name} };
1165             } # DELETE
1166              
1167             sub CLEAR
1168             {
1169 0     0   0 %{ $_[0]->{dbh}->{sql_meta} } = ();
  0         0  
1170 0         0 %{ $_[0]->{dbh}->{sql_meta_map} } = ();
  0         0  
1171             } # CLEAR
1172              
1173             sub SCALAR
1174             {
1175 0     0   0 scalar %{ $_[0]->{dbh}->{sql_meta} };
  0         0  
1176             } # SCALAR
1177              
1178             # ====== STATEMENT =============================================================
1179              
1180             package DBI::DBD::SqlEngine::st;
1181              
1182 48     48   259 use strict;
  48         210  
  48         1417  
1183 48     48   188 use warnings;
  48         77  
  48         1483  
1184              
1185 48     48   186 use vars qw(@ISA $imp_data_size);
  48         75  
  48         26254  
1186              
1187             $imp_data_size = 0;
1188              
1189             sub bind_param ($$$;$)
1190             {
1191 108     108   1210 my ( $sth, $pNum, $val, $attr ) = @_;
1192 108 50 33     284 if ( $attr && defined $val )
1193             {
1194 0 0       0 my $type = ref $attr eq "HASH" ? $attr->{TYPE} : $attr;
1195 0 0 0     0 if ( $type == DBI::SQL_BIGINT()
    0 0        
      0        
      0        
      0        
      0        
      0        
1196             || $type == DBI::SQL_INTEGER()
1197             || $type == DBI::SQL_SMALLINT()
1198             || $type == DBI::SQL_TINYINT() )
1199             {
1200 0         0 $val += 0;
1201             }
1202             elsif ( $type == DBI::SQL_DECIMAL()
1203             || $type == DBI::SQL_DOUBLE()
1204             || $type == DBI::SQL_FLOAT()
1205             || $type == DBI::SQL_NUMERIC()
1206             || $type == DBI::SQL_REAL() )
1207             {
1208 0         0 $val += 0.;
1209             }
1210             else
1211             {
1212 0         0 $val = "$val";
1213             }
1214             }
1215 108         326 $sth->{sql_params}[ $pNum - 1 ] = $val;
1216 108         269 return 1;
1217             } # bind_param
1218              
1219             sub execute
1220             {
1221 476     476   9532 my $sth = shift;
1222 476 100       1319 my $params = @_ ? ( $sth->{sql_params} = [@_] ) : $sth->{sql_params};
1223              
1224 476         1830 $sth->finish;
1225 476         2568 my $stmt = $sth->{sql_stmt};
1226              
1227             # must not proved when already executed - SQL::Statement modifies
1228             # received params
1229 476 50       1704 unless ( $sth->{sql_params_checked}++ )
1230             {
1231             # SQL::Statement and DBI::SQL::Nano will return the list of required params
1232             # when called in list context. Do not look into the several items, they're
1233             # implementation specific and may change without warning
1234 476 50       1320 unless ( ( my $req_prm = $stmt->params() ) == ( my $nparm = @$params ) )
1235             {
1236 0         0 my $msg = "You passed $nparm parameters where $req_prm required";
1237 0         0 return $sth->set_err( $DBI::stderr, $msg );
1238             }
1239             }
1240              
1241 476         1038 my @err;
1242             my $result;
1243 476         1026 eval {
1244 476     0   4076 local $SIG{__WARN__} = sub { push @err, @_ };
  0         0  
1245 476         1947 $result = $stmt->execute( $sth, $params );
1246             };
1247 476 100       2971 unless ( defined $result )
1248             {
1249 32   33     376 $sth->set_err( $DBI::stderr, $@ || $stmt->{errstr} || $err[0] );
1250 32         453 return;
1251             }
1252              
1253 444 100       1916 if ( $stmt->{NUM_OF_FIELDS} )
1254             { # is a SELECT statement
1255 92         456 $sth->STORE( Active => 1 );
1256 92 100       728 $sth->FETCH("NUM_OF_FIELDS")
1257             or $sth->STORE( "NUM_OF_FIELDS", $stmt->{NUM_OF_FIELDS} );
1258             }
1259 444         2390 return $result;
1260             } # execute
1261              
1262             sub finish
1263             {
1264 904     904   10412 my $sth = $_[0];
1265 904         2541 $sth->SUPER::STORE( Active => 0 );
1266 904         1755 delete $sth->{sql_stmt}{data};
1267 904         1945 return 1;
1268             } # finish
1269              
1270             sub fetch ($)
1271             {
1272 316     316   6778 my $sth = $_[0];
1273 316         526 my $data = $sth->{sql_stmt}{data};
1274 316 100 66     1410 if ( !$data || ref $data ne "ARRAY" )
1275             {
1276 64         417 $sth->set_err(
1277             $DBI::stderr,
1278             "Attempt to fetch row without a preceding execute () call or from a non-SELECT statement"
1279             );
1280 64         486 return;
1281             }
1282 252         297 my $dav = shift @$data;
1283 252 100       525 unless ($dav)
1284             {
1285 80         267 $sth->finish;
1286 80         530 return;
1287             }
1288 172 50       507 if ( $sth->FETCH("ChopBlanks") ) # XXX: (TODO) Only chop on CHAR fields,
1289             { # not on VARCHAR or NUMERIC (see DBI docs)
1290 0   0     0 $_ && $_ =~ s/ +$// for @$dav;
1291             }
1292 172         2077 return $sth->_set_fbav($dav);
1293             } # fetch
1294              
1295 48     48   256 no warnings 'once';
  48         73  
  48         2577  
1296             *fetchrow_arrayref = \&fetch;
1297              
1298 48     48   239 use warnings;
  48         70  
  48         20332  
1299              
1300             sub sql_get_colnames
1301             {
1302 2020     2020   19942 my $sth = $_[0];
1303             # Being a bit dirty here, as neither SQL::Statement::Structure nor
1304             # DBI::SQL::Nano::Statement_ does not offer an interface to the
1305             # required data
1306 2020         1804 my @colnames;
1307 2020 100 66     12172 if ( $sth->{sql_stmt}->{NAME} and "ARRAY" eq ref( $sth->{sql_stmt}->{NAME} ) )
    50          
1308             {
1309 624         553 @colnames = @{ $sth->{sql_stmt}->{NAME} };
  624         1532  
1310             }
1311             elsif ( $sth->{sql_stmt}->isa('SQL::Statement') )
1312             {
1313 0   0     0 my $stmt = $sth->{sql_stmt} || {};
1314 0 0       0 my @coldefs = @{ $stmt->{column_defs} || [] };
  0         0  
1315 0 0       0 @colnames = map { $_->{name} || $_->{value} } @coldefs;
  0         0  
1316             }
1317 2020 100       6626 @colnames = $sth->{sql_stmt}->column_names() unless (@colnames);
1318              
1319 2020 50       3291 @colnames = () if ( grep { m/\*/ } @colnames );
  1992         4537  
1320              
1321 2020         5877 return @colnames;
1322             }
1323              
1324             sub FETCH ($$)
1325             {
1326 2006     2006   1963 my ( $sth, $attrib ) = @_;
1327              
1328 2006 100       3993 $attrib eq "NAME" and return [ $sth->sql_get_colnames() ];
1329              
1330 1670 100       3440 $attrib eq "TYPE" and return [ ( DBI::SQL_VARCHAR() ) x scalar $sth->sql_get_colnames() ];
1331 1334 50       2068 $attrib eq "TYPE_NAME" and return [ ("VARCHAR") x scalar $sth->sql_get_colnames() ];
1332 1334 100       2850 $attrib eq "PRECISION" and return [ (0) x scalar $sth->sql_get_colnames() ];
1333 998 100       1796 $attrib eq "NULLABLE" and return [ (1) x scalar $sth->sql_get_colnames() ];
1334              
1335 926 100       2058 if ( $attrib eq lc $attrib )
1336             {
1337             # Private driver attributes are lower cased
1338 336         1604 return $sth->{$attrib};
1339             }
1340              
1341             # else pass up to DBI to handle
1342 590         2902 return $sth->SUPER::FETCH($attrib);
1343             } # FETCH
1344              
1345             sub STORE ($$$)
1346             {
1347 2900     2900   26875 my ( $sth, $attrib, $value ) = @_;
1348 2900 100       5796 if ( $attrib eq lc $attrib ) # Private driver attributes are lower cased
1349             {
1350 1352         2173 $sth->{$attrib} = $value;
1351 1352         2758 return 1;
1352             }
1353 1548         6671 return $sth->SUPER::STORE( $attrib, $value );
1354             } # STORE
1355              
1356             sub DESTROY ($)
1357             {
1358 724     724   34785 my $sth = shift;
1359 724 100       3173 $sth->SUPER::FETCH("Active") and $sth->finish;
1360 724         1495 undef $sth->{sql_stmt};
1361 724         7045 undef $sth->{sql_params};
1362             } # DESTROY
1363              
1364             sub rows ($)
1365             {
1366 212     212   8566 return $_[0]->{sql_stmt}{NUM_OF_ROWS};
1367             } # rows
1368              
1369             # ====== TableSource ===========================================================
1370              
1371             package DBI::DBD::SqlEngine::TableSource;
1372              
1373 48     48   271 use strict;
  48         67  
  48         1337  
1374 48     48   186 use warnings;
  48         74  
  48         1152  
1375              
1376 48     48   192 use Carp;
  48         71  
  48         7487  
1377              
1378             sub data_sources ($;$)
1379             {
1380 0     0   0 my ( $class, $drh, $attrs ) = @_;
1381 0 0       0 croak( ( ref( $_[0] ) ? ref( $_[0] ) : $_[0] ) . " must implement data_sources" );
1382             }
1383              
1384             sub avail_tables
1385             {
1386 0     0   0 my ( $self, $dbh ) = @_;
1387 0 0       0 croak( ( ref( $_[0] ) ? ref( $_[0] ) : $_[0] ) . " must implement avail_tables" );
1388             }
1389              
1390             # ====== DataSource ============================================================
1391              
1392             package DBI::DBD::SqlEngine::DataSource;
1393              
1394 48     48   247 use strict;
  48         67  
  48         1391  
1395 48     48   190 use warnings;
  48         69  
  48         1090  
1396              
1397 48     48   188 use Carp;
  48         69  
  48         7315  
1398              
1399             sub complete_table_name ($$;$)
1400             {
1401 0     0   0 my ( $self, $meta, $table, $respect_case ) = @_;
1402 0 0       0 croak( ( ref( $_[0] ) ? ref( $_[0] ) : $_[0] ) . " must implement complete_table_name" );
1403             }
1404              
1405             sub open_data ($)
1406             {
1407 0     0   0 my ( $self, $meta, $attrs, $flags ) = @_;
1408 0 0       0 croak( ( ref( $_[0] ) ? ref( $_[0] ) : $_[0] ) . " must implement open_data" );
1409             }
1410              
1411             # ====== SQL::STATEMENT ========================================================
1412              
1413             package DBI::DBD::SqlEngine::Statement;
1414              
1415 48     48   234 use strict;
  48         68  
  48         1258  
1416 48     48   182 use warnings;
  48         62  
  48         1108  
1417              
1418 48     48   229 use Carp;
  48         89  
  48         11400  
1419              
1420             @DBI::DBD::SqlEngine::Statement::ISA = qw(DBI::SQL::Nano::Statement);
1421              
1422             sub open_table ($$$$$)
1423             {
1424 476     476   763 my ( $self, $data, $table, $createMode, $lockMode ) = @_;
1425              
1426 476         920 my $class = ref $self;
1427 476         1980 $class =~ s/::Statement/::Table/;
1428              
1429 476         1665 my $flags = {
1430             createMode => $createMode,
1431             lockMode => $lockMode,
1432             };
1433 476 100       1616 $self->{command} eq "DROP" and $flags->{dropMode} = 1;
1434              
1435             # because column name mapping is initialized in constructor ...
1436             # and therefore specific opening operations might be done before
1437             # reaching DBI::DBD::SqlEngine::Table->new(), we need to intercept
1438             # ReadOnly here
1439 476   66     3224 my $write_op = $createMode || $lockMode || $flags->{dropMode};
1440 476 100       1191 if ($write_op)
1441             {
1442 376 50       2184 my ( $tblnm, $table_meta ) = $class->get_table_meta( $data->{Database}, $table, 1 )
1443             or croak "Cannot find appropriate file for table '$table'";
1444 376 50       6087 $table_meta->{readonly}
    100          
1445             and croak "Table '$table' is marked readonly - "
1446             . $self->{command}
1447             . ( $lockMode ? " with locking" : "" )
1448             . " command forbidden";
1449             }
1450              
1451 452         2397 return $class->new( $data, { table => $table }, $flags );
1452             } # open_table
1453              
1454             # ====== SQL::TABLE ============================================================
1455              
1456             package DBI::DBD::SqlEngine::Table;
1457              
1458 48     48   254 use strict;
  48         83  
  48         1263  
1459 48     48   182 use warnings;
  48         82  
  48         1122  
1460              
1461 48     48   198 use Carp;
  48         58  
  48         41681  
1462              
1463             @DBI::DBD::SqlEngine::Table::ISA = qw(DBI::SQL::Nano::Table);
1464              
1465             sub bootstrap_table_meta
1466             {
1467 508     508   747 my ( $self, $dbh, $meta, $table ) = @_;
1468              
1469 508 100 100     1644 defined $dbh->{ReadOnly}
1470             and !defined( $meta->{readonly} )
1471             and $meta->{readonly} = $dbh->{ReadOnly};
1472 508 100       1637 defined $meta->{sql_identifier_case}
1473             or $meta->{sql_identifier_case} = $dbh->{sql_identifier_case};
1474              
1475 508 100       1537 exists $meta->{sql_data_source} or $meta->{sql_data_source} = $dbh->{sql_data_source};
1476              
1477 508         847 $meta;
1478             }
1479              
1480             sub init_table_meta
1481             {
1482 340     340   356 my ( $self, $dbh, $meta, $table ) = @_ if (0);
1483              
1484 340         612 return;
1485             } # init_table_meta
1486              
1487             sub get_table_meta ($$$;$)
1488             {
1489 1096     1096   1936 my ( $self, $dbh, $table, $respect_case, @other ) = @_;
1490 1096 100       2391 unless ( defined $respect_case )
1491             {
1492 920         934 $respect_case = 0;
1493 920 50       2175 $table =~ s/^\"// and $respect_case = 1; # handle quoted identifiers
1494 920         1419 $table =~ s/\"$//;
1495             }
1496              
1497 1096 50       2020 unless ($respect_case)
1498             {
1499 1096 100       2993 defined $dbh->{sql_meta_map}{$table} and $table = $dbh->{sql_meta_map}{$table};
1500             }
1501              
1502 1096         1548 my $meta = {};
1503 1096 100       3003 defined $dbh->{sql_meta}{$table} and $meta = $dbh->{sql_meta}{$table};
1504              
1505             do_initialize:
1506 1128 100       2592 unless ( $meta->{initialized} )
1507             {
1508 500         2108 $self->bootstrap_table_meta( $dbh, $meta, $table, @other );
1509 500 100       6553 $meta->{sql_data_source}->complete_table_name( $meta, $table, $respect_case, @other )
1510             or return;
1511              
1512 372 100 66     2084 if ( defined $meta->{table_name} and $table ne $meta->{table_name} )
1513             {
1514 168         496 $dbh->{sql_meta_map}{$table} = $meta->{table_name};
1515 168         313 $table = $meta->{table_name};
1516             }
1517              
1518             # now we know a bit more - let's check if user can't use consequent spelling
1519             # XXX add know issue about reset sql_identifier_case here ...
1520 372 100       1265 if ( defined $dbh->{sql_meta}{$table} )
1521             {
1522 64         174 $meta = delete $dbh->{sql_meta}{$table}; # avoid endless loop
1523 64 100       543 $meta->{initialized}
1524             or goto do_initialize;
1525             #or $meta->{sql_data_source}->complete_table_name( $meta, $table, $respect_case, @other )
1526             #or return;
1527             }
1528              
1529 340 50       1335 unless ( $dbh->{sql_meta}{$table}{initialized} )
1530             {
1531 340         1912 $self->init_table_meta( $dbh, $meta, $table );
1532 340         613 $meta->{initialized} = 1;
1533 340         678 $dbh->{sql_meta}{$table} = $meta;
1534             }
1535             }
1536              
1537 968         2365 return ( $table, $meta );
1538             } # get_table_meta
1539              
1540             my %reset_on_modify = ();
1541             my %compat_map = ();
1542              
1543             sub register_reset_on_modify
1544             {
1545 80     80   455 my ( $proto, $extra_resets ) = @_;
1546 80         635 foreach my $cv ( keys %$extra_resets )
1547             {
1548             #%reset_on_modify = ( %reset_on_modify, %$extra_resets );
1549 304         951 push @{ $reset_on_modify{$cv} },
  96         196  
1550 304 100       288 ref $extra_resets->{$cv} ? @{ $extra_resets->{$cv} } : ( $extra_resets->{$cv} );
1551             }
1552 80         256 return;
1553             } # register_reset_on_modify
1554              
1555             sub register_compat_map
1556             {
1557 80     80   134 my ( $proto, $extra_compat_map ) = @_;
1558 80         524 %compat_map = ( %compat_map, %$extra_compat_map );
1559 80         185 return;
1560             } # register_compat_map
1561              
1562             sub get_table_meta_attr
1563             {
1564 76     76   112 my ( $class, $meta, $attrib ) = @_;
1565 76 50       189 exists $compat_map{$attrib}
1566             and $attrib = $compat_map{$attrib};
1567 76 50       615 exists $meta->{$attrib}
1568             and return $meta->{$attrib};
1569 0         0 return;
1570             } # get_table_meta_attr
1571              
1572             sub set_table_meta_attr
1573             {
1574 12     12   28 my ( $class, $meta, $attrib, $value ) = @_;
1575 12 100       77 exists $compat_map{$attrib}
1576             and $attrib = $compat_map{$attrib};
1577 12         61 $class->table_meta_attr_changed( $meta, $attrib, $value );
1578 12         30 $meta->{$attrib} = $value;
1579             } # set_table_meta_attr
1580              
1581             sub table_meta_attr_changed
1582             {
1583 12     12   22 my ( $class, $meta, $attrib, $value ) = @_;
1584 12         81 defined $reset_on_modify{$attrib}
1585 12 50 33     60 and delete @$meta{ @{ $reset_on_modify{$attrib} } }
1586             and $meta->{initialized} = 0;
1587             } # table_meta_attr_changed
1588              
1589             sub open_data
1590             {
1591 452     452   673 my ( $self, $meta, $attrs, $flags ) = @_;
1592              
1593 452 50       1149 $meta->{sql_data_source}
1594             or croak "Table " . $meta->{table_name} . " not completely initialized";
1595 452         2519 $meta->{sql_data_source}->open_data( $meta, $attrs, $flags );
1596              
1597 404         835 return;
1598             } # open_data
1599              
1600             # ====== SQL::Eval API =========================================================
1601              
1602             sub new
1603             {
1604 452     452   745 my ( $className, $data, $attrs, $flags ) = @_;
1605 452         826 my $dbh = $data->{Database};
1606              
1607 452 50       1492 my ( $tblnm, $meta ) = $className->get_table_meta( $dbh, $attrs->{table}, 1 )
1608             or croak "Cannot find appropriate table '$attrs->{table}'";
1609 452         861 $attrs->{table} = $tblnm;
1610              
1611             # Being a bit dirty here, as SQL::Statement::Structure does not offer
1612             # me an interface to the data I want
1613 452 50 66     2366 $flags->{createMode} && $data->{sql_stmt}{table_defs}
1614             and $meta->{table_defs} = $data->{sql_stmt}{table_defs};
1615              
1616             # open_file must be called before inherited new is invoked
1617             # because column name mapping is initialized in constructor ...
1618 452         1739 $className->open_data( $meta, $attrs, $flags );
1619              
1620 404         3010 my $tbl = {
1621 404   100     580 %{$attrs},
1622             meta => $meta,
1623             col_names => $meta->{col_names} || [],
1624             };
1625 404         3050 return $className->SUPER::new($tbl);
1626             } # new
1627              
1628             1;
1629              
1630             =pod
1631              
1632             =head1 NAME
1633              
1634             DBI::DBD::SqlEngine - Base class for DBI drivers without their own SQL engine
1635              
1636             =head1 SYNOPSIS
1637              
1638             package DBD::myDriver;
1639              
1640             use base qw(DBI::DBD::SqlEngine);
1641              
1642             sub driver
1643             {
1644             ...
1645             my $drh = $proto->SUPER::driver($attr);
1646             ...
1647             return $drh->{class};
1648             }
1649              
1650             package DBD::myDriver::dr;
1651              
1652             @ISA = qw(DBI::DBD::SqlEngine::dr);
1653              
1654             sub data_sources { ... }
1655             ...
1656              
1657             package DBD::myDriver::db;
1658              
1659             @ISA = qw(DBI::DBD::SqlEngine::db);
1660              
1661             sub init_valid_attributes { ... }
1662             sub init_default_attributes { ... }
1663             sub set_versions { ... }
1664             sub validate_STORE_attr { my ($dbh, $attrib, $value) = @_; ... }
1665             sub validate_FETCH_attr { my ($dbh, $attrib) = @_; ... }
1666             sub get_myd_versions { ... }
1667             sub get_avail_tables { ... }
1668              
1669             package DBD::myDriver::st;
1670              
1671             @ISA = qw(DBI::DBD::SqlEngine::st);
1672              
1673             sub FETCH { ... }
1674             sub STORE { ... }
1675              
1676             package DBD::myDriver::Statement;
1677              
1678             @ISA = qw(DBI::DBD::SqlEngine::Statement);
1679              
1680             sub open_table { ... }
1681              
1682             package DBD::myDriver::Table;
1683              
1684             @ISA = qw(DBI::DBD::SqlEngine::Table);
1685              
1686             sub new { ... }
1687              
1688             =head1 DESCRIPTION
1689              
1690             DBI::DBD::SqlEngine abstracts the usage of SQL engines from the
1691             DBD. DBD authors can concentrate on the data retrieval they want to
1692             provide.
1693              
1694             It is strongly recommended that you read L and
1695             L, because many of the DBD::File API is provided
1696             by DBI::DBD::SqlEngine.
1697              
1698             Currently the API of DBI::DBD::SqlEngine is experimental and will
1699             likely change in the near future to provide the table meta data basics
1700             like DBD::File.
1701              
1702             DBI::DBD::SqlEngine expects that any driver in inheritance chain has
1703             a L.
1704              
1705             =head2 Metadata
1706              
1707             The following attributes are handled by DBI itself and not by
1708             DBI::DBD::SqlEngine, thus they all work as expected:
1709              
1710             Active
1711             ActiveKids
1712             CachedKids
1713             CompatMode (Not used)
1714             InactiveDestroy
1715             AutoInactiveDestroy
1716             Kids
1717             PrintError
1718             RaiseError
1719             Warn (Not used)
1720              
1721             =head3 The following DBI attributes are handled by DBI::DBD::SqlEngine:
1722              
1723             =head4 AutoCommit
1724              
1725             Always on.
1726              
1727             =head4 ChopBlanks
1728              
1729             Works.
1730              
1731             =head4 NUM_OF_FIELDS
1732              
1733             Valid after C<< $sth->execute >>.
1734              
1735             =head4 NUM_OF_PARAMS
1736              
1737             Valid after C<< $sth->prepare >>.
1738              
1739             =head4 NAME
1740              
1741             Valid after C<< $sth->execute >>; probably undef for Non-Select statements.
1742              
1743             =head4 NULLABLE
1744              
1745             Not really working, always returns an array ref of ones, as DBD::CSV
1746             does not verify input data. Valid after C<< $sth->execute >>; undef for
1747             non-select statements.
1748              
1749             =head3 The following DBI attributes and methods are not supported:
1750              
1751             =over 4
1752              
1753             =item bind_param_inout
1754              
1755             =item CursorName
1756              
1757             =item LongReadLen
1758              
1759             =item LongTruncOk
1760              
1761             =back
1762              
1763             =head3 DBI::DBD::SqlEngine specific attributes
1764              
1765             In addition to the DBI attributes, you can use the following dbh
1766             attributes:
1767              
1768             =head4 sql_engine_version
1769              
1770             Contains the module version of this driver (B)
1771              
1772             =head4 sql_nano_version
1773              
1774             Contains the module version of DBI::SQL::Nano (B)
1775              
1776             =head4 sql_statement_version
1777              
1778             Contains the module version of SQL::Statement, if available (B)
1779              
1780             =head4 sql_handler
1781              
1782             Contains the SQL Statement engine, either DBI::SQL::Nano or SQL::Statement
1783             (B).
1784              
1785             =head4 sql_parser_object
1786              
1787             Contains an instantiated instance of SQL::Parser (B).
1788             This is filled when used first time (only when used with SQL::Statement).
1789              
1790             =head4 sql_sponge_driver
1791              
1792             Contains an internally used DBD::Sponge handle (B).
1793              
1794             =head4 sql_valid_attrs
1795              
1796             Contains the list of valid attributes for each DBI::DBD::SqlEngine based
1797             driver (B).
1798              
1799             =head4 sql_readonly_attrs
1800              
1801             Contains the list of those attributes which are readonly (B).
1802              
1803             =head4 sql_identifier_case
1804              
1805             Contains how DBI::DBD::SqlEngine deals with non-quoted SQL identifiers:
1806              
1807             * SQL_IC_UPPER (1) means all identifiers are internally converted
1808             into upper-cased pendants
1809             * SQL_IC_LOWER (2) means all identifiers are internally converted
1810             into lower-cased pendants
1811             * SQL_IC_MIXED (4) means all identifiers are taken as they are
1812              
1813             These conversions happen if (and only if) no existing identifier matches.
1814             Once existing identifier is used as known.
1815              
1816             The SQL statement execution classes doesn't have to care, so don't expect
1817             C affects column names in statements like
1818              
1819             SELECT * FROM foo
1820              
1821             =head4 sql_quoted_identifier_case
1822              
1823             Contains how DBI::DBD::SqlEngine deals with quoted SQL identifiers
1824             (B). It's fixated to SQL_IC_SENSITIVE (3), which is interpreted
1825             as SQL_IC_MIXED.
1826              
1827             =head4 sql_flags
1828              
1829             Contains additional flags to instantiate an SQL::Parser. Because an
1830             SQL::Parser is instantiated only once, it's recommended to set this flag
1831             before any statement is executed.
1832              
1833             =head4 sql_dialect
1834              
1835             Controls the dialect understood by SQL::Parser. Possible values (delivery
1836             state of SQL::Statement):
1837              
1838             * ANSI
1839             * CSV
1840             * AnyData
1841              
1842             Defaults to "CSV". Because an SQL::Parser is instantiated only once and
1843             SQL::Parser doesn't allow to modify the dialect once instantiated,
1844             it's strongly recommended to set this flag before any statement is
1845             executed (best place is connect attribute hash).
1846              
1847             =head4 sql_engine_in_gofer
1848              
1849             This value has a true value in case of this driver is operated via
1850             L. The impact of being operated via Gofer is a read-only
1851             driver (not read-only databases!), so you cannot modify any attributes
1852             later - neither any table settings. B you won't get an error in
1853             cases you modify table attributes, so please carefully watch
1854             C.
1855              
1856             =head4 sql_meta
1857              
1858             Private data area which contains information about the tables this
1859             module handles. Table meta data might not be available until the
1860             table has been accessed for the first time e.g., by issuing a select
1861             on it however it is possible to pre-initialize attributes for each table
1862             you use.
1863              
1864             DBI::DBD::SqlEngine recognizes the (public) attributes C,
1865             C, C, C and C.
1866             Be very careful when modifying attributes you do not know, the consequence
1867             might be a destroyed or corrupted table.
1868              
1869             While C is a private and readonly attribute (which means, you
1870             cannot modify it's values), derived drivers might provide restricted
1871             write access through another attribute. Well known accessors are
1872             C for L, C for L and
1873             C for L.
1874              
1875             =head4 sql_table_source
1876              
1877             Controls the class which will be used for fetching available tables.
1878              
1879             See L for details.
1880              
1881             =head4 sql_data_source
1882              
1883             Contains the class name to be used for opening tables.
1884              
1885             See L for details.
1886              
1887             =head2 Driver private methods
1888              
1889             =head3 Default DBI methods
1890              
1891             =head4 data_sources
1892              
1893             The C method returns a list of subdirectories of the current
1894             directory in the form "dbi:CSV:f_dir=$dirname".
1895              
1896             If you want to read the subdirectories of another directory, use
1897              
1898             my ($drh) = DBI->install_driver ("CSV");
1899             my (@list) = $drh->data_sources (f_dir => "/usr/local/csv_data");
1900              
1901             =head4 list_tables
1902              
1903             This method returns a list of file names inside $dbh->{f_dir}.
1904             Example:
1905              
1906             my ($dbh) = DBI->connect ("dbi:CSV:f_dir=/usr/local/csv_data");
1907             my (@list) = $dbh->func ("list_tables");
1908              
1909             Note that the list includes all files contained in the directory, even
1910             those that have non-valid table names, from the view of SQL.
1911              
1912             =head3 Additional methods
1913              
1914             The following methods are only available via their documented name when
1915             DBI::DBD::SQlEngine is used directly. Because this is only reasonable for
1916             testing purposes, the real names must be used instead. Those names can be
1917             computed by replacing the C in the method name with the driver prefix.
1918              
1919             =head4 sql_versions
1920              
1921             Signature:
1922              
1923             sub sql_versions (;$) {
1924             my ($table_name) = @_;
1925             $table_name ||= ".";
1926             ...
1927             }
1928              
1929             Returns the versions of the driver, including the DBI version, the Perl
1930             version, DBI::PurePerl version (if DBI::PurePerl is active) and the version
1931             of the SQL engine in use.
1932              
1933             my $dbh = DBI->connect ("dbi:File:");
1934             my $sql_versions = $dbh->func( "sql_versions" );
1935             print "$sql_versions\n";
1936             __END__
1937             # DBI::DBD::SqlEngine 0.05 using SQL::Statement 1.402
1938             # DBI 1.623
1939             # OS netbsd (6.99.12)
1940             # Perl 5.016002 (x86_64-netbsd-thread-multi)
1941              
1942             Called in list context, sql_versions will return an array containing each
1943             line as single entry.
1944              
1945             Some drivers might use the optional (table name) argument and modify
1946             version information related to the table (e.g. DBD::DBM provides storage
1947             backend information for the requested table, when it has a table name).
1948              
1949             =head4 sql_get_meta
1950              
1951             Signature:
1952              
1953             sub sql_get_meta ($$)
1954             {
1955             my ($table_name, $attrib) = @_;
1956             ...
1957             }
1958              
1959             Returns the value of a meta attribute set for a specific table, if any.
1960             See L for the possible attributes.
1961              
1962             A table name of C<"."> (single dot) is interpreted as the default table.
1963             This will retrieve the appropriate attribute globally from the dbh.
1964             This has the same restrictions as C<< $dbh->{$attrib} >>.
1965              
1966             =head4 sql_set_meta
1967              
1968             Signature:
1969              
1970             sub sql_set_meta ($$$)
1971             {
1972             my ($table_name, $attrib, $value) = @_;
1973             ...
1974             }
1975              
1976             Sets the value of a meta attribute set for a specific table.
1977             See L for the possible attributes.
1978              
1979             A table name of C<"."> (single dot) is interpreted as the default table
1980             which will set the specified attribute globally for the dbh.
1981             This has the same restrictions as C<< $dbh->{$attrib} = $value >>.
1982              
1983             =head4 sql_clear_meta
1984              
1985             Signature:
1986              
1987             sub sql_clear_meta ($)
1988             {
1989             my ($table_name) = @_;
1990             ...
1991             }
1992              
1993             Clears the table specific meta information in the private storage of the
1994             dbh.
1995              
1996             =head2 Extensibility
1997              
1998             =head3 DBI::DBD::SqlEngine::TableSource
1999              
2000             Provides data sources and table information on database driver and database
2001             handle level.
2002              
2003             package DBI::DBD::SqlEngine::TableSource;
2004              
2005             sub data_sources ($;$)
2006             {
2007             my ( $class, $drh, $attrs ) = @_;
2008             ...
2009             }
2010              
2011             sub avail_tables
2012             {
2013             my ( $class, $drh ) = @_;
2014             ...
2015             }
2016              
2017             The C method is called when the user invokes any of the
2018             following:
2019              
2020             @ary = DBI->data_sources($driver);
2021             @ary = DBI->data_sources($driver, \%attr);
2022            
2023             @ary = $dbh->data_sources();
2024             @ary = $dbh->data_sources(\%attr);
2025              
2026             The C method is called when the user invokes any of the
2027             following:
2028              
2029             @names = $dbh->tables( $catalog, $schema, $table, $type );
2030            
2031             $sth = $dbh->table_info( $catalog, $schema, $table, $type );
2032             $sth = $dbh->table_info( $catalog, $schema, $table, $type, \%attr );
2033              
2034             $dbh->func( "list_tables" );
2035              
2036             Every time where an C<\%attr> argument can be specified, this C<\%attr>
2037             object's C attribute is preferred over the C<$dbh>
2038             attribute or the driver default, eg.
2039              
2040             @ary = DBI->data_sources("dbi:CSV:", {
2041             f_dir => "/your/csv/tables",
2042             # note: this class doesn't comes with DBI
2043             sql_table_source => "DBD::File::Archive::Tar::TableSource",
2044             # scan tarballs instead of directories
2045             });
2046              
2047             When you're going to implement such a DBD::File::Archive::Tar::TableSource
2048             class, remember to add correct attributes (including C
2049             and C) to the returned DSN's.
2050              
2051             =head3 DBI::DBD::SqlEngine::DataSource
2052              
2053             Provides base functionality for dealing with tables. It is primarily
2054             designed for allowing transparent access to files on disk or already
2055             opened (file-)streams (eg. for DBD::CSV).
2056              
2057             Derived classes shall be restricted to similar functionality, too (eg.
2058             opening streams from an archive, transparently compress/uncompress
2059             log files before parsing them,
2060              
2061             package DBI::DBD::SqlEngine::DataSource;
2062              
2063             sub complete_table_name ($$;$)
2064             {
2065             my ( $self, $meta, $table, $respect_case ) = @_;
2066             ...
2067             }
2068              
2069             The method C is called when first setting up the
2070             I for a table:
2071              
2072             "SELECT user.id, user.name, user.shell FROM user WHERE ..."
2073              
2074             results in opening the table C. First step of the table open
2075             process is completing the name. Let's imagine you're having a L
2076             handle with following settings:
2077              
2078             $dbh->{sql_identifier_case} = SQL_IC_LOWER;
2079             $dbh->{f_ext} = '.lst';
2080             $dbh->{f_dir} = '/data/web/adrmgr';
2081              
2082             Those settings will result in looking for files matching
2083             C<[Uu][Ss][Ee][Rr](\.lst)?$> in C. The scanning of the
2084             directory C and the pattern match check will be done
2085             in C by the C method.
2086              
2087             If you intend to provide other sources of data streams than files, in
2088             addition to provide an appropriate C method, a method
2089             to open the resource is required:
2090              
2091             package DBI::DBD::SqlEngine::DataSource;
2092              
2093             sub open_data ($)
2094             {
2095             my ( $self, $meta, $attrs, $flags ) = @_;
2096             ...
2097             }
2098              
2099             After the method C has been run successfully, the table's meta
2100             information are in a state which allowes the table's data accessor methods
2101             will be able to fetch/store row information. Implementation details heavily
2102             depends on the table implementation, whereby the most famous is surely
2103             L.
2104              
2105             =head1 SQL ENGINES
2106              
2107             DBI::DBD::SqlEngine currently supports two SQL engines:
2108             L and
2109             L. DBI::SQL::Nano supports a
2110             I limited subset of SQL statements, but it might be faster for some
2111             very simple tasks. SQL::Statement in contrast supports a much larger subset
2112             of ANSI SQL.
2113              
2114             To use SQL::Statement, you need at least version 1.401 of
2115             SQL::Statement and the environment variable C must not
2116             be set to a true value.
2117              
2118             =head1 SUPPORT
2119              
2120             You can find documentation for this module with the perldoc command.
2121              
2122             perldoc DBI::DBD::SqlEngine
2123              
2124             You can also look for information at:
2125              
2126             =over 4
2127              
2128             =item * RT: CPAN's request tracker
2129              
2130             L
2131             L
2132              
2133             =item * AnnoCPAN: Annotated CPAN documentation
2134              
2135             L
2136             L
2137              
2138             =item * CPAN Ratings
2139              
2140             L
2141              
2142             =item * Search CPAN
2143              
2144             L
2145              
2146             =back
2147              
2148             =head2 Where can I go for more help?
2149              
2150             For questions about installation or usage, please ask on the
2151             dbi-dev@perl.org mailing list.
2152              
2153             If you have a bug report, patch or suggestion, please open
2154             a new report ticket on CPAN, if there is not already one for
2155             the issue you want to report. Of course, you can mail any of the
2156             module maintainers, but it is less likely to be missed if
2157             it is reported on RT.
2158              
2159             Report tickets should contain a detailed description of the bug or
2160             enhancement request you want to report and at least an easy way to
2161             verify/reproduce the issue and any supplied fix. Patches are always
2162             welcome, too.
2163              
2164             =head1 ACKNOWLEDGEMENTS
2165              
2166             Thanks to Tim Bunce, Martin Evans and H.Merijn Brand for their continued
2167             support while developing DBD::File, DBD::DBM and DBD::AnyData.
2168             Their support, hints and feedback helped to design and implement this
2169             module.
2170              
2171             =head1 AUTHOR
2172              
2173             This module is currently maintained by
2174              
2175             H.Merijn Brand < h.m.brand at xs4all.nl > and
2176             Jens Rehsack < rehsack at googlemail.com >
2177              
2178             The original authors are Jochen Wiedmann and Jeff Zucker.
2179              
2180             =head1 COPYRIGHT AND LICENSE
2181              
2182             Copyright (C) 2009-2013 by H.Merijn Brand & Jens Rehsack
2183             Copyright (C) 2004-2009 by Jeff Zucker
2184             Copyright (C) 1998-2004 by Jochen Wiedmann
2185              
2186             All rights reserved.
2187              
2188             You may freely distribute and/or modify this module under the terms of
2189             either the GNU General Public License (GPL) or the Artistic License, as
2190             specified in the Perl README file.
2191              
2192             =head1 SEE ALSO
2193              
2194             L, L, L and L.
2195              
2196             =cut