-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathDBI.pm
8679 lines (6498 loc) · 313 KB
/
DBI.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# $Id$
# vim: ts=8:sw=4:et
#
# Copyright (c) 2024-2025 DBI Team
# Copyright (c) 1994-2024 Tim Bunce Ireland
#
# See COPYRIGHT section in pod text below for usage and distribution rights.
#
package DBI;
require 5.008001;
use strict;
use warnings;
our ($XS_VERSION, $VERSION);
BEGIN {
$VERSION = "1.646"; # ==> ALSO update the version in the pod text below!
$XS_VERSION = $VERSION;
$VERSION =~ tr/_//d;
}
=head1 NAME
DBI - Database independent interface for Perl
=head1 SYNOPSIS
use DBI;
@driver_names = DBI->available_drivers;
%drivers = DBI->installed_drivers;
@data_sources = DBI->data_sources($driver_name, \%attr);
$dbh = DBI->connect($data_source, $username, $auth, \%attr);
$rv = $dbh->do($statement);
$rv = $dbh->do($statement, \%attr);
$rv = $dbh->do($statement, \%attr, @bind_values);
$ary_ref = $dbh->selectall_arrayref($statement);
$hash_ref = $dbh->selectall_hashref($statement, $key_field);
$ary_ref = $dbh->selectcol_arrayref($statement);
$ary_ref = $dbh->selectcol_arrayref($statement, \%attr);
@row_ary = $dbh->selectrow_array($statement);
$ary_ref = $dbh->selectrow_arrayref($statement);
$hash_ref = $dbh->selectrow_hashref($statement);
$sth = $dbh->prepare($statement);
$sth = $dbh->prepare_cached($statement);
$rc = $sth->bind_param($p_num, $bind_value);
$rc = $sth->bind_param($p_num, $bind_value, $bind_type);
$rc = $sth->bind_param($p_num, $bind_value, \%attr);
$rv = $sth->execute;
$rv = $sth->execute(@bind_values);
$rv = $sth->execute_array(\%attr, ...);
$rc = $sth->bind_col($col_num, \$col_variable);
$rc = $sth->bind_columns(@list_of_refs_to_vars_to_bind);
@row_ary = $sth->fetchrow_array;
$ary_ref = $sth->fetchrow_arrayref;
$hash_ref = $sth->fetchrow_hashref;
$ary_ref = $sth->fetchall_arrayref;
$ary_ref = $sth->fetchall_arrayref( $slice, $max_rows );
$hash_ref = $sth->fetchall_hashref( $key_field );
$rv = $sth->rows;
$rc = $dbh->begin_work;
$rc = $dbh->commit;
$rc = $dbh->rollback;
$quoted_string = $dbh->quote($string);
$rc = $h->err;
$str = $h->errstr;
$rv = $h->state;
$rc = $dbh->disconnect;
I<The synopsis above only lists the major methods and parameters.>
=head2 GETTING HELP
=head3 General
Before asking any questions, reread this document, consult the archives and
read the DBI FAQ. The archives are listed at the end of this document and on
the DBI home page L<http://dbi.perl.org/support/>
You might also like to read the Advanced DBI Tutorial at
L<http://www.slideshare.net/Tim.Bunce/dbi-advanced-tutorial-2007>
To help you make the best use of the dbi-users mailing list,
and any other lists or forums you may use, I recommend that you read
"Getting Answers" by Mike Ash: L<http://mikeash.com/getting_answers.html>.
=head3 Mailing Lists
If you have questions about DBI, or DBD driver modules, you can get
help from the I<[email protected]> mailing list. This is the best way to get
help. You don't have to subscribe to the list in order to post, though I'd
recommend it. You can get help on subscribing and using the list by emailing
Please note that Tim Bunce does not maintain the mailing lists or the
web pages (generous volunteers do that). So please don't send mail
directly to him; he just doesn't have the time to answer questions
personally. The I<dbi-users> mailing list has lots of experienced
people who should be able to help you if you need it. If you do email
Tim he is very likely to just forward it to the mailing list.
=head3 IRC
DBI IRC Channel: #dbi on irc.perl.org (L<irc://irc.perl.org/#dbi>)
=for html <a href="http://chat.mibbit.com/#[email protected]">(click for instant chatroom login)</a>
=head3 Online
StackOverflow has a DBI tag L<https://stackoverflow.com/questions/tagged/dbi>
with over 800 questions.
The DBI home page at L<https://dbi.perl.org/> might be worth a visit.
It includes links to other resources, but I<is rather out-dated>.
=head3 Reporting a Bug
If you think you've found a bug then please read
"How to Report Bugs Effectively" by Simon Tatham:
L<https://www.chiark.greenend.org.uk/~sgtatham/bugs.html>.
If you think you've found a memory leak then read L</Memory Leaks>.
Your problem is most likely related to the specific DBD driver module you're
using. If that's the case then click on the 'Bugs' link on the L<https://metacpan.org>
page for your driver. Only submit a bug report against the DBI itself if you're
sure that your issue isn't related to the driver you're using.
=head2 NOTES
This is the DBI specification that corresponds to DBI version 1.646
(see L<DBI::Changes> for details).
The DBI is evolving at a steady pace, so it's good to check that
you have the latest copy.
The significant user-visible changes in each release are documented
in the L<DBI::Changes> module so you can read them by executing
C<perldoc DBI::Changes>.
Some DBI changes require changes in the drivers, but the drivers
can take some time to catch up. Newer versions of the DBI have
added features that may not yet be supported by the drivers you
use. Talk to the authors of your drivers if you need a new feature
that is not yet supported.
Features added after DBI 1.21 (February 2002) are marked in the
text with the version number of the DBI release they first appeared in.
Extensions to the DBI API often use the C<DBIx::*> namespace.
See L</Naming Conventions and Name Space>. DBI extension modules
can be found at L<https://metacpan.org/search?q=DBIx>. And all modules
related to the DBI can be found at L<https://metacpan.org/search?q=DBI>.
=cut
# The POD text continues at the end of the file.
use Scalar::Util ();
use Carp();
use XSLoader ();
use Exporter ();
our (@ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
BEGIN {
@ISA = qw(Exporter);
# Make some utility functions available if asked for
@EXPORT = (); # we export nothing by default
@EXPORT_OK = qw(%DBI %DBI_methods hash); # also populated by export_ok_tags:
%EXPORT_TAGS = (
sql_types => [ qw(
SQL_GUID
SQL_WLONGVARCHAR
SQL_WVARCHAR
SQL_WCHAR
SQL_BIGINT
SQL_BIT
SQL_TINYINT
SQL_LONGVARBINARY
SQL_VARBINARY
SQL_BINARY
SQL_LONGVARCHAR
SQL_UNKNOWN_TYPE
SQL_ALL_TYPES
SQL_CHAR
SQL_NUMERIC
SQL_DECIMAL
SQL_INTEGER
SQL_SMALLINT
SQL_FLOAT
SQL_REAL
SQL_DOUBLE
SQL_DATETIME
SQL_DATE
SQL_INTERVAL
SQL_TIME
SQL_TIMESTAMP
SQL_VARCHAR
SQL_BOOLEAN
SQL_UDT
SQL_UDT_LOCATOR
SQL_ROW
SQL_REF
SQL_BLOB
SQL_BLOB_LOCATOR
SQL_CLOB
SQL_CLOB_LOCATOR
SQL_ARRAY
SQL_ARRAY_LOCATOR
SQL_MULTISET
SQL_MULTISET_LOCATOR
SQL_TYPE_DATE
SQL_TYPE_TIME
SQL_TYPE_TIMESTAMP
SQL_TYPE_TIME_WITH_TIMEZONE
SQL_TYPE_TIMESTAMP_WITH_TIMEZONE
SQL_INTERVAL_YEAR
SQL_INTERVAL_MONTH
SQL_INTERVAL_DAY
SQL_INTERVAL_HOUR
SQL_INTERVAL_MINUTE
SQL_INTERVAL_SECOND
SQL_INTERVAL_YEAR_TO_MONTH
SQL_INTERVAL_DAY_TO_HOUR
SQL_INTERVAL_DAY_TO_MINUTE
SQL_INTERVAL_DAY_TO_SECOND
SQL_INTERVAL_HOUR_TO_MINUTE
SQL_INTERVAL_HOUR_TO_SECOND
SQL_INTERVAL_MINUTE_TO_SECOND
) ],
sql_cursor_types => [ qw(
SQL_CURSOR_FORWARD_ONLY
SQL_CURSOR_KEYSET_DRIVEN
SQL_CURSOR_DYNAMIC
SQL_CURSOR_STATIC
SQL_CURSOR_TYPE_DEFAULT
) ], # for ODBC cursor types
utils => [ qw(
neat neat_list $neat_maxlen dump_results looks_like_number
data_string_diff data_string_desc data_diff sql_type_cast
DBIstcf_DISCARD_STRING
DBIstcf_STRICT
) ],
profile => [ qw(
dbi_profile dbi_profile_merge dbi_profile_merge_nodes dbi_time
) ], # notionally "in" DBI::Profile and normally imported from there
);
$DBI::dbi_debug = 0; # mixture of bit fields and int sub-fields
$DBI::neat_maxlen = 1000;
$DBI::stderr = 2_000_000_000; # a very round number below 2**31
# If you get an error here like "Can't find loadable object ..."
# then you haven't installed the DBI correctly. Read the README
# then install it again.
if ( $ENV{DBI_PUREPERL} ) {
eval { XSLoader::load('DBI', $XS_VERSION) } if $ENV{DBI_PUREPERL} == 1;
require DBI::PurePerl if $@ or $ENV{DBI_PUREPERL} >= 2;
$DBI::PurePerl ||= 0; # just to silence "only used once" warnings
}
else {
XSLoader::load( 'DBI', $XS_VERSION);
}
$EXPORT_TAGS{preparse_flags} = [ grep { /^DBIpp_\w\w_/ } keys %DBI:: ];
Exporter::export_ok_tags(keys %EXPORT_TAGS);
}
# Alias some handle methods to also be DBI class methods
for (qw(trace_msg set_err parse_trace_flag parse_trace_flags)) {
no strict;
*$_ = \&{"DBD::_::common::$_"};
}
DBI->trace(split /=/, $ENV{DBI_TRACE}, 2) if $ENV{DBI_TRACE};
$DBI::connect_via ||= "connect";
# check if user wants a persistent database connection ( Apache + mod_perl )
if ($INC{'Apache/DBI.pm'} && $ENV{MOD_PERL}) {
$DBI::connect_via = "Apache::DBI::connect";
DBI->trace_msg("DBI connect via $DBI::connect_via in $INC{'Apache/DBI.pm'}\n");
}
%DBI::installed_drh = (); # maps driver names to installed driver handles
sub installed_drivers { %DBI::installed_drh }
%DBI::installed_methods = (); # XXX undocumented, may change
sub installed_methods { %DBI::installed_methods }
# Setup special DBI dynamic variables. See DBI::var::FETCH for details.
# These are dynamically associated with the last handle used.
tie $DBI::err, 'DBI::var', '*err'; # special case: referenced via IHA list
tie $DBI::state, 'DBI::var', '"state'; # special case: referenced via IHA list
tie $DBI::lasth, 'DBI::var', '!lasth'; # special case: return boolean
tie $DBI::errstr, 'DBI::var', '&errstr'; # call &errstr in last used pkg
tie $DBI::rows, 'DBI::var', '&rows'; # call &rows in last used pkg
sub DBI::var::TIESCALAR{ my $var = $_[1]; bless \$var, 'DBI::var'; }
sub DBI::var::STORE { Carp::croak("Can't modify \$DBI::${$_[0]} special variable") }
# --- Driver Specific Prefix Registry ---
my $dbd_prefix_registry = {
ad_ => { class => 'DBD::AnyData', },
ad2_ => { class => 'DBD::AnyData2', },
ado_ => { class => 'DBD::ADO', },
amzn_ => { class => 'DBD::Amazon', },
best_ => { class => 'DBD::BestWins', },
csv_ => { class => 'DBD::CSV', },
cubrid_ => { class => 'DBD::cubrid', },
db2_ => { class => 'DBD::DB2', },
dbi_ => { class => 'DBI', },
dbm_ => { class => 'DBD::DBM', },
df_ => { class => 'DBD::DF', },
examplep_ => { class => 'DBD::ExampleP', },
f_ => { class => 'DBD::File', },
file_ => { class => 'DBD::TextFile', },
go_ => { class => 'DBD::Gofer', },
ib_ => { class => 'DBD::InterBase', },
ing_ => { class => 'DBD::Ingres', },
ix_ => { class => 'DBD::Informix', },
jdbc_ => { class => 'DBD::JDBC', },
mariadb_ => { class => 'DBD::MariaDB', },
mem_ => { class => 'DBD::Mem', },
mo_ => { class => 'DBD::MO', },
monetdb_ => { class => 'DBD::monetdb', },
msql_ => { class => 'DBD::mSQL', },
mvsftp_ => { class => 'DBD::MVS_FTPSQL', },
mysql_ => { class => 'DBD::mysql', },
multi_ => { class => 'DBD::Multi' },
mx_ => { class => 'DBD::Multiplex', },
neo_ => { class => 'DBD::Neo4p', },
nullp_ => { class => 'DBD::NullP', },
odbc_ => { class => 'DBD::ODBC', },
ora_ => { class => 'DBD::Oracle', },
pg_ => { class => 'DBD::Pg', },
pgpp_ => { class => 'DBD::PgPP', },
plb_ => { class => 'DBD::Plibdata', },
po_ => { class => 'DBD::PO', },
proxy_ => { class => 'DBD::Proxy', },
ram_ => { class => 'DBD::RAM', },
rdb_ => { class => 'DBD::RDB', },
sapdb_ => { class => 'DBD::SAP_DB', },
snmp_ => { class => 'DBD::SNMP', },
solid_ => { class => 'DBD::Solid', },
spatialite_ => { class => 'DBD::Spatialite', },
sponge_ => { class => 'DBD::Sponge', },
sql_ => { class => 'DBI::DBD::SqlEngine', },
sqlite_ => { class => 'DBD::SQLite', },
syb_ => { class => 'DBD::Sybase', },
sys_ => { class => 'DBD::Sys', },
tdat_ => { class => 'DBD::Teradata', },
tmpl_ => { class => 'DBD::Template', },
tmplss_ => { class => 'DBD::TemplateSS', },
tree_ => { class => 'DBD::TreeData', },
tuber_ => { class => 'DBD::Tuber', },
uni_ => { class => 'DBD::Unify', },
vt_ => { class => 'DBD::Vt', },
wmi_ => { class => 'DBD::WMI', },
x_ => { }, # for private use
xbase_ => { class => 'DBD::XBase', },
xmlsimple_ => { class => 'DBD::XMLSimple', },
xl_ => { class => 'DBD::Excel', },
yaswi_ => { class => 'DBD::Yaswi', },
};
my %dbd_class_registry = map { $dbd_prefix_registry->{$_}->{class} => { prefix => $_ } }
grep { exists $dbd_prefix_registry->{$_}->{class} }
keys %{$dbd_prefix_registry};
sub dump_dbd_registry {
require Data::Dumper;
local $Data::Dumper::Sortkeys=1;
local $Data::Dumper::Indent=1;
print Data::Dumper->Dump([$dbd_prefix_registry], [qw($dbd_prefix_registry)]);
}
# --- Dynamically create the DBI Standard Interface
my $keeperr = { O=>0x0004 };
%DBI::DBI_methods = ( # Define the DBI interface methods per class:
common => { # Interface methods common to all DBI handle classes
'DESTROY' => { O=>0x004|0x10000 },
'CLEAR' => $keeperr,
'EXISTS' => $keeperr,
'FETCH' => { O=>0x0404 },
'FETCH_many' => { O=>0x0404 },
'FIRSTKEY' => $keeperr,
'NEXTKEY' => $keeperr,
'STORE' => { O=>0x0418 | 0x4 },
'DELETE' => { O=>0x0404 },
can => { O=>0x0100 }, # special case, see dispatch
debug => { U =>[1,2,'[$debug_level]'], O=>0x0004 }, # old name for trace
dump_handle => { U =>[1,3,'[$message [, $level]]'], O=>0x0004 },
err => $keeperr,
errstr => $keeperr,
state => $keeperr,
func => { O=>0x0006 },
parse_trace_flag => { U =>[2,2,'$name'], O=>0x0404, T=>8 },
parse_trace_flags => { U =>[2,2,'$flags'], O=>0x0404, T=>8 },
private_data => { U =>[1,1], O=>0x0004 },
set_err => { U =>[3,6,'$err, $errmsg [, $state, $method, $rv]'], O=>0x0010 },
trace => { U =>[1,3,'[$trace_level, [$filename]]'], O=>0x0004 },
trace_msg => { U =>[2,3,'$message_text [, $min_level ]' ], O=>0x0004, T=>8 },
swap_inner_handle => { U =>[2,3,'$h [, $allow_reparent ]'] },
private_attribute_info => { },
visit_child_handles => { U => [2,3,'$coderef [, $info ]'], O=>0x0404, T=>4 },
},
dr => { # Database Driver Interface
'connect' => { U =>[1,5,'[$db [,$user [,$passwd [,\%attr]]]]'], H=>3, O=>0x8000, T=>0x200 },
'connect_cached'=>{U=>[1,5,'[$db [,$user [,$passwd [,\%attr]]]]'], H=>3, O=>0x8000, T=>0x200 },
'disconnect_all'=>{ U =>[1,1], O=>0x0800, T=>0x200 },
data_sources => { U =>[1,2,'[\%attr]' ], O=>0x0800, T=>0x200 },
default_user => { U =>[3,4,'$user, $pass [, \%attr]' ], T=>0x200 },
dbixs_revision => $keeperr,
},
db => { # Database Session Class Interface
data_sources => { U =>[1,2,'[\%attr]' ], O=>0x0200 },
take_imp_data => { U =>[1,1], O=>0x10000 },
clone => { U =>[1,2,'[\%attr]'], T=>0x200 },
connected => { U =>[1,0], O => 0x0004, T=>0x200, H=>3 },
begin_work => { U =>[1,2,'[ \%attr ]'], O=>0x0400, T=>0x1000 },
commit => { U =>[1,1], O=>0x0480|0x0800, T=>0x1000 },
rollback => { U =>[1,1], O=>0x0480|0x0800, T=>0x1000 },
'do' => { U =>[2,0,'$statement [, \%attr [, @bind_params ] ]'], O=>0x3200 },
last_insert_id => { U =>[1,6,'[$catalog [,$schema [,$table_name [,$field_name [, \%attr ]]]]]'], O=>0x2800 },
preparse => { }, # XXX
prepare => { U =>[2,3,'$statement [, \%attr]'], O=>0xA200 },
prepare_cached => { U =>[2,4,'$statement [, \%attr [, $if_active ] ]'], O=>0xA200 },
selectrow_array => { U =>[2,0,'$statement [, \%attr [, @bind_params ] ]'], O=>0x2000 },
selectrow_arrayref=>{U =>[2,0,'$statement [, \%attr [, @bind_params ] ]'], O=>0x2000 },
selectrow_hashref=>{ U =>[2,0,'$statement [, \%attr [, @bind_params ] ]'], O=>0x2000 },
selectall_arrayref=>{U =>[2,0,'$statement [, \%attr [, @bind_params ] ]'], O=>0x2000 },
selectall_array =>{U =>[2,0,'$statement [, \%attr [, @bind_params ] ]'], O=>0x2000 },
selectall_hashref=>{ U =>[3,0,'$statement, $keyfield [, \%attr [, @bind_params ] ]'], O=>0x2000 },
selectcol_arrayref=>{U =>[2,0,'$statement [, \%attr [, @bind_params ] ]'], O=>0x2000 },
ping => { U =>[1,1], O=>0x0404 },
disconnect => { U =>[1,1], O=>0x0400|0x0800|0x10000, T=>0x200 },
quote => { U =>[2,3, '$string [, $data_type ]' ], O=>0x0430, T=>2 },
quote_identifier=> { U =>[2,6, '$name [, ...] [, \%attr ]' ], O=>0x0430, T=>2 },
rows => $keeperr,
tables => { U =>[1,6,'$catalog, $schema, $table, $type [, \%attr ]' ], O=>0x2200 },
table_info => { U =>[1,6,'$catalog, $schema, $table, $type [, \%attr ]' ], O=>0x2200|0x8800 },
column_info => { U =>[5,6,'$catalog, $schema, $table, $column [, \%attr ]'],O=>0x2200|0x8800 },
primary_key_info=> { U =>[4,5,'$catalog, $schema, $table [, \%attr ]' ], O=>0x2200|0x8800 },
primary_key => { U =>[4,5,'$catalog, $schema, $table [, \%attr ]' ], O=>0x2200 },
foreign_key_info=> { U =>[7,8,'$pk_catalog, $pk_schema, $pk_table, $fk_catalog, $fk_schema, $fk_table [, \%attr ]' ], O=>0x2200|0x8800 },
statistics_info => { U =>[6,7,'$catalog, $schema, $table, $unique_only, $quick, [, \%attr ]' ], O=>0x2200|0x8800 },
type_info_all => { U =>[1,1], O=>0x2200|0x0800 },
type_info => { U =>[1,2,'$data_type'], O=>0x2200 },
get_info => { U =>[2,2,'$info_type'], O=>0x2200|0x0800 },
},
st => { # Statement Class Interface
bind_col => { U =>[3,4,'$column, \\$var [, \%attr]'] },
bind_columns => { U =>[2,0,'\\$var1 [, \\$var2, ...]'] },
bind_param => { U =>[3,4,'$parameter, $var [, \%attr]'] },
bind_param_inout=> { U =>[4,5,'$parameter, \\$var, $maxlen, [, \%attr]'] },
execute => { U =>[1,0,'[@args]'], O=>0x1040 },
last_insert_id => { U =>[1,6,'[$catalog [,$schema [,$table_name [,$field_name [, \%attr ]]]]]'], O=>0x2800 },
bind_param_array => { U =>[3,4,'$parameter, $var [, \%attr]'] },
bind_param_inout_array => { U =>[4,5,'$parameter, \\@var, $maxlen, [, \%attr]'] },
execute_array => { U =>[2,0,'\\%attribs [, @args]'], O=>0x1040|0x4000 },
execute_for_fetch => { U =>[2,3,'$fetch_sub [, $tuple_status]'], O=>0x1040|0x4000 },
fetch => undef, # alias for fetchrow_arrayref
fetchrow_arrayref => undef,
fetchrow_hashref => undef,
fetchrow_array => undef,
fetchrow => undef, # old alias for fetchrow_array
fetchall_arrayref => { U =>[1,3, '[ $slice [, $max_rows]]'] },
fetchall_hashref => { U =>[2,2,'$key_field'] },
blob_read => { U =>[4,5,'$field, $offset, $len [, \\$buf [, $bufoffset]]'] },
blob_copy_to_file => { U =>[3,3,'$field, $filename_or_handleref'] },
dump_results => { U =>[1,5,'$maxfieldlen, $linesep, $fieldsep, $filehandle'] },
more_results => { U =>[1,1] },
finish => { U =>[1,1] },
cancel => { U =>[1,1], O=>0x0800 },
rows => $keeperr,
_get_fbav => undef,
_set_fbav => { T=>6 },
},
);
while ( my ($class, $meths) = each %DBI::DBI_methods ) {
my $ima_trace = 0+($ENV{DBI_IMA_TRACE}||0);
while ( my ($method, $info) = each %$meths ) {
my $fullmeth = "DBI::${class}::$method";
if (($DBI::dbi_debug & 0xF) == 15) { # quick hack to list DBI methods
# and optionally filter by IMA flags
my $O = $info->{O}||0;
printf "0x%04x %-20s\n", $O, $fullmeth
unless $ima_trace && !($O & $ima_trace);
}
DBI->_install_method($fullmeth, 'DBI.pm', $info);
}
}
{
package DBI::common;
@DBI::dr::ISA = ('DBI::common');
@DBI::db::ISA = ('DBI::common');
@DBI::st::ISA = ('DBI::common');
}
# End of init code
END {
return unless defined &DBI::trace_msg; # return unless bootstrap'd ok
local ($!,$?);
DBI->trace_msg(sprintf(" -- DBI::END (\$\@: %s, \$!: %s)\n", $@||'', $!||''), 2);
# Let drivers know why we are calling disconnect_all:
$DBI::PERL_ENDING = $DBI::PERL_ENDING = 1; # avoid typo warning
DBI->disconnect_all() if %DBI::installed_drh;
}
sub CLONE {
_clone_dbis() unless $DBI::PurePerl; # clone the DBIS structure
DBI->trace_msg("CLONE DBI for new thread\n");
while ( my ($driver, $drh) = each %DBI::installed_drh) {
no strict 'refs';
next if defined &{"DBD::${driver}::CLONE"};
warn("$driver has no driver CLONE() function so is unsafe threaded\n");
}
%DBI::installed_drh = (); # clear loaded drivers so they have a chance to reinitialize
}
sub parse_dsn {
my ($class, $dsn) = @_;
$dsn =~ s/^(dbi):(\w*?)(?:\((.*?)\))?://i or return;
my ($scheme, $driver, $attr, $attr_hash) = (lc($1), $2, $3);
$driver ||= $ENV{DBI_DRIVER} || '';
$attr_hash = { split /\s*=>?\s*|\s*,\s*/, $attr, -1 } if $attr;
return ($scheme, $driver, $attr, $attr_hash, $dsn);
}
sub visit_handles {
my ($class, $code, $outer_info) = @_;
$outer_info = {} if not defined $outer_info;
my %drh = DBI->installed_drivers;
for my $h (values %drh) {
my $child_info = $code->($h, $outer_info)
or next;
$h->visit_child_handles($code, $child_info);
}
return $outer_info;
}
# --- The DBI->connect Front Door methods
sub connect_cached {
# For library code using connect_cached() with mod_perl
# we redirect those calls to Apache::DBI::connect() as well
my ($class, $dsn, $user, $pass, $attr) = @_;
my $dbi_connect_method = ($DBI::connect_via eq "Apache::DBI::connect")
? 'Apache::DBI::connect' : 'connect_cached';
$attr = {
$attr ? %$attr : (), # clone, don't modify callers data
dbi_connect_method => $dbi_connect_method,
};
return $class->connect($dsn, $user, $pass, $attr);
}
sub connect {
my $class = shift;
my ($dsn, $user, $pass, $attr, $old_driver) = my @orig_args = @_;
my $driver;
if ($attr and !ref($attr)) { # switch $old_driver<->$attr if called in old style
Carp::carp("DBI->connect using 'old-style' syntax is deprecated and will be an error in future versions");
($old_driver, $attr) = ($attr, $old_driver);
}
my $connect_meth = $attr->{dbi_connect_method};
$connect_meth ||= $DBI::connect_via; # fallback to default
$dsn ||= $ENV{DBI_DSN} || $ENV{DBI_DBNAME} || '' unless $old_driver;
if ($DBI::dbi_debug) {
no warnings;
pop @_ if $connect_meth ne 'connect';
my @args = @_; $args[2] = '****'; # hide password
DBI->trace_msg(" -> $class->$connect_meth(".join(", ",@args).")\n");
}
Carp::croak('Usage: $class->connect([$dsn [,$user [,$passwd [,\%attr]]]])')
if (ref $old_driver or ($attr and not ref $attr) or
(ref $pass and not defined Scalar::Util::blessed($pass)));
# extract dbi:driver prefix from $dsn into $1
my $orig_dsn = $dsn;
$dsn =~ s/^dbi:(\w*?)(?:\((.*?)\))?://i
or '' =~ /()/; # ensure $1 etc are empty if match fails
my $driver_attrib_spec = $2 || '';
# Set $driver. Old style driver, if specified, overrides new dsn style.
$driver = $old_driver || $1 || $ENV{DBI_DRIVER}
or Carp::croak("Can't connect to data source '$orig_dsn' "
."because I can't work out what driver to use "
."(it doesn't seem to contain a 'dbi:driver:' prefix "
."and the DBI_DRIVER env var is not set)");
my $proxy;
if ($ENV{DBI_AUTOPROXY} && $driver ne 'Proxy' && $driver ne 'Sponge' && $driver ne 'Switch') {
my $dbi_autoproxy = $ENV{DBI_AUTOPROXY};
$proxy = 'Proxy';
if ($dbi_autoproxy =~ s/^dbi:(\w*?)(?:\((.*?)\))?://i) {
$proxy = $1;
$driver_attrib_spec = join ",",
($driver_attrib_spec) ? $driver_attrib_spec : (),
($2 ) ? $2 : ();
}
$dsn = "$dbi_autoproxy;dsn=dbi:$driver:$dsn";
$driver = $proxy;
DBI->trace_msg(" DBI_AUTOPROXY: dbi:$driver($driver_attrib_spec):$dsn\n");
}
# avoid recursion if proxy calls DBI->connect itself
local $ENV{DBI_AUTOPROXY} if $ENV{DBI_AUTOPROXY};
my %attributes; # take a copy we can delete from
if ($old_driver) {
%attributes = %$attr if $attr;
}
else { # new-style connect so new default semantics
%attributes = (
PrintError => 1,
AutoCommit => 1,
ref $attr ? %$attr : (),
# attributes in DSN take precedence over \%attr connect parameter
$driver_attrib_spec ? (split /\s*=>?\s*|\s*,\s*/, $driver_attrib_spec, -1) : (),
);
}
$attr = \%attributes; # now set $attr to refer to our local copy
my $drh = $DBI::installed_drh{$driver} || $class->install_driver($driver)
or die "panic: $class->install_driver($driver) failed";
# attributes in DSN take precedence over \%attr connect parameter
$user = $attr->{Username} if defined $attr->{Username};
$pass = $attr->{Password} if defined $attr->{Password};
delete $attr->{Password}; # always delete Password as closure stores it securely
if ( !(defined $user && defined $pass) ) {
($user, $pass) = $drh->default_user($user, $pass, $attr);
}
$attr->{Username} = $user; # force the Username to be the actual one used
my $connect_closure = sub {
my ($old_dbh, $override_attr) = @_;
#use Data::Dumper;
#warn "connect_closure: ".Data::Dumper::Dumper([$attr,\%attributes, $override_attr]);
my $dbh;
unless ($dbh = $drh->$connect_meth($dsn, $user, $pass, $attr)) {
$user = '' if !defined $user;
$dsn = '' if !defined $dsn;
# $drh->errstr isn't safe here because $dbh->DESTROY may not have
# been called yet and so the dbh errstr would not have been copied
# up to the drh errstr. Certainly true for connect_cached!
my $errstr = $DBI::errstr;
# Getting '(no error string)' here is a symptom of a ref loop
$errstr = '(no error string)' if !defined $errstr;
my $msg = "$class connect('$dsn','$user',...) failed: $errstr";
DBI->trace_msg(" $msg\n");
# XXX HandleWarn
unless ($attr->{HandleError} && $attr->{HandleError}->($msg, $drh, $dbh)) {
Carp::croak($msg) if $attr->{RaiseError};
Carp::carp ($msg) if $attr->{PrintError};
}
$! = 0; # for the daft people who do DBI->connect(...) || die "$!";
return $dbh; # normally undef, but HandleError could change it
}
# merge any attribute overrides but don't change $attr itself (for closure)
my $apply = { ($override_attr) ? (%$attr, %$override_attr ) : %$attr };
# handle basic RootClass subclassing:
my $rebless_class = $apply->{RootClass} || ($class ne 'DBI' ? $class : '');
if ($rebless_class) {
no strict 'refs';
if ($apply->{RootClass}) { # explicit attribute (ie not static method call class)
delete $apply->{RootClass};
DBI::_load_class($rebless_class, 0);
}
unless (@{"$rebless_class\::db::ISA"} && @{"$rebless_class\::st::ISA"}) {
Carp::carp("DBI subclasses '$rebless_class\::db' and ::st are not setup, RootClass ignored");
$rebless_class = undef;
$class = 'DBI';
}
else {
$dbh->{RootClass} = $rebless_class; # $dbh->STORE called via plain DBI::db
DBI::_set_isa([$rebless_class], 'DBI'); # sets up both '::db' and '::st'
DBI::_rebless($dbh, $rebless_class); # appends '::db'
}
}
if (%$apply) {
if ($apply->{DbTypeSubclass}) {
my $DbTypeSubclass = delete $apply->{DbTypeSubclass};
DBI::_rebless_dbtype_subclass($dbh, $rebless_class||$class, $DbTypeSubclass);
}
my $a;
foreach $a (qw(Profile RaiseError PrintError AutoCommit)) { # do these first
next unless exists $apply->{$a};
$dbh->{$a} = delete $apply->{$a};
}
while ( my ($a, $v) = each %$apply) {
eval { $dbh->{$a} = $v }; # assign in void context to avoid re-FETCH
warn $@ if $@;
}
}
# confirm to driver (ie if subclassed) that we've connected successfully
# and finished the attribute setup. pass in the original arguments
$dbh->connected(@orig_args); #if ref $dbh ne 'DBI::db' or $proxy;
DBI->trace_msg(" <- connect= $dbh\n") if $DBI::dbi_debug & 0xF;
return $dbh;
};
my $dbh = &$connect_closure(undef, undef);
$dbh->{dbi_connect_closure} = $connect_closure if $dbh;
return $dbh;
}
sub disconnect_all {
keys %DBI::installed_drh; # reset iterator
while ( my ($name, $drh) = each %DBI::installed_drh ) {
$drh->disconnect_all() if ref $drh;
}
}
sub disconnect { # a regular beginners bug
Carp::croak("DBI->disconnect is not a DBI method (read the DBI manual)");
}
sub install_driver { # croaks on failure
my $class = shift;
my($driver, $attr) = @_;
my $drh;
$driver ||= $ENV{DBI_DRIVER} || '';
# allow driver to be specified as a 'dbi:driver:' string
$driver = $1 if $driver =~ s/^DBI:(.*?)://i;
Carp::croak("usage: $class->install_driver(\$driver [, \%attr])")
unless ($driver and @_<=3);
# already installed
return $drh if $drh = $DBI::installed_drh{$driver};
$class->trace_msg(" -> $class->install_driver($driver"
.") for $^O perl=$] pid=$$ ruid=$< euid=$>\n")
if $DBI::dbi_debug & 0xF;
# --- load the code
my $driver_class = "DBD::$driver";
eval qq{package # hide from PAUSE
DBI::_firesafe; # just in case
require $driver_class; # load the driver
};
if ($@) {
my $err = $@;
my $advice = "";
if ($err =~ /Can't find loadable object/) {
$advice = "Perhaps DBD::$driver was statically linked into a new perl binary."
."\nIn which case you need to use that new perl binary."
."\nOr perhaps only the .pm file was installed but not the shared object file."
}
elsif ($err =~ /Can't locate.*?DBD\/$driver\.pm in \@INC/) {
my @drv = $class->available_drivers(1);
$advice = "Perhaps the DBD::$driver perl module hasn't been fully installed,\n"
."or perhaps the capitalisation of '$driver' isn't right.\n"
."Available drivers: ".join(", ", @drv).".";
}
elsif ($err =~ /Can't load .*? for module DBD::/) {
$advice = "Perhaps a required shared library or dll isn't installed where expected";
}
elsif ($err =~ /Can't locate .*? in \@INC/) {
$advice = "Perhaps a module that DBD::$driver requires hasn't been fully installed";
}
Carp::croak("install_driver($driver) failed: $err$advice\n");
}
if ($DBI::dbi_debug & 0xF) {
no strict 'refs';
(my $driver_file = $driver_class) =~ s/::/\//g;
my $dbd_ver = ${"$driver_class\::VERSION"} || "undef";
$class->trace_msg(" install_driver: $driver_class version $dbd_ver"
." loaded from $INC{qq($driver_file.pm)}\n");
}
# --- do some behind-the-scenes checks and setups on the driver
$class->setup_driver($driver_class);
# --- run the driver function
$drh = eval { $driver_class->driver($attr || {}) };
unless ($drh && ref $drh && !$@) {
my $advice = "";
$@ ||= "$driver_class->driver didn't return a handle";
# catch people on case in-sensitive systems using the wrong case
$advice = "\nPerhaps the capitalisation of DBD '$driver' isn't right."
if $@ =~ /locate object method/;
Carp::croak("$driver_class initialisation failed: $@$advice");
}
$DBI::installed_drh{$driver} = $drh;
$class->trace_msg(" <- install_driver= $drh\n") if $DBI::dbi_debug & 0xF;
$drh;
}
*driver = \&install_driver; # currently an alias, may change
sub setup_driver {
my ($class, $driver_class) = @_;
my $h_type;
foreach $h_type (qw(dr db st)){
my $h_class = $driver_class."::$h_type";
no strict 'refs';
push @{"${h_class}::ISA"}, "DBD::_::$h_type"
unless UNIVERSAL::isa($h_class, "DBD::_::$h_type");
# The _mem class stuff is (IIRC) a crufty hack for global destruction
# timing issues in early versions of perl5 and possibly no longer needed.
my $mem_class = "DBD::_mem::$h_type";
push @{"${h_class}_mem::ISA"}, $mem_class
unless UNIVERSAL::isa("${h_class}_mem", $mem_class)
or $DBI::PurePerl;
}
}
sub _rebless {
my $dbh = shift;
my ($outer, $inner) = DBI::_handles($dbh);
my $class = shift(@_).'::db';
bless $inner => $class;
bless $outer => $class; # outer last for return
}
sub _set_isa {
my ($classes, $topclass) = @_;
my $trace = DBI->trace_msg(" _set_isa([@$classes])\n");
foreach my $suffix ('::db','::st') {
my $previous = $topclass || 'DBI'; # trees are rooted here
foreach my $class (@$classes) {
my $base_class = $previous.$suffix;
my $sub_class = $class.$suffix;
my $sub_class_isa = "${sub_class}::ISA";
no strict 'refs';
if (@$sub_class_isa) {
DBI->trace_msg(" $sub_class_isa skipped (already set to @$sub_class_isa)\n")
if $trace;
}
else {
@$sub_class_isa = ($base_class) unless @$sub_class_isa;
DBI->trace_msg(" $sub_class_isa = $base_class\n")
if $trace;
}
$previous = $class;
}
}
}
sub _rebless_dbtype_subclass {
my ($dbh, $rootclass, $DbTypeSubclass) = @_;
# determine the db type names for class hierarchy
my @hierarchy = DBI::_dbtype_names($dbh, $DbTypeSubclass);
# add the rootclass prefix to each ('DBI::' or 'MyDBI::' etc)
$_ = $rootclass.'::'.$_ foreach (@hierarchy);
# load the modules from the 'top down'
DBI::_load_class($_, 1) foreach (reverse @hierarchy);
# setup class hierarchy if needed, does both '::db' and '::st'
DBI::_set_isa(\@hierarchy, $rootclass);
# finally bless the handle into the subclass
DBI::_rebless($dbh, $hierarchy[0]);
}
sub _dbtype_names { # list dbtypes for hierarchy, ie Informix=>ADO=>ODBC
my ($dbh, $DbTypeSubclass) = @_;
if ($DbTypeSubclass && $DbTypeSubclass ne '1' && ref $DbTypeSubclass ne 'CODE') {
# treat $DbTypeSubclass as a comma separated list of names
my @dbtypes = split /\s*,\s*/, $DbTypeSubclass;
$dbh->trace_msg(" DbTypeSubclass($DbTypeSubclass)=@dbtypes (explicit)\n");
return @dbtypes;
}
# XXX will call $dbh->get_info(17) (=SQL_DBMS_NAME) in future?
my $driver = $dbh->{Driver}->{Name};
if ( $driver eq 'Proxy' ) {
# XXX Looking into the internals of DBD::Proxy is questionable!
($driver) = $dbh->{proxy_client}->{application} =~ /^DBI:(.+?):/i
or die "Can't determine driver name from proxy";
}
my @dbtypes = (ucfirst($driver));
if ($driver eq 'ODBC' || $driver eq 'ADO') {
# XXX will move these out and make extensible later:
my $_dbtype_name_regexp = 'Oracle'; # eg 'Oracle|Foo|Bar'
my %_dbtype_name_map = (
'Microsoft SQL Server' => 'MSSQL',
'SQL Server' => 'Sybase',
'Adaptive Server Anywhere' => 'ASAny',
'ADABAS D' => 'AdabasD',
);
my $name;
$name = $dbh->func(17, 'GetInfo') # SQL_DBMS_NAME
if $driver eq 'ODBC';
$name = $dbh->{ado_conn}->Properties->Item('DBMS Name')->Value
if $driver eq 'ADO';
die "Can't determine driver name! ($DBI::errstr)\n"
unless $name;
my $dbtype;
if ($_dbtype_name_map{$name}) {
$dbtype = $_dbtype_name_map{$name};
}
else {
if ($name =~ /($_dbtype_name_regexp)/) {
$dbtype = lc($1);
}
else { # generic mangling for other names:
$dbtype = lc($name);
}
$dbtype =~ s/\b(\w)/\U$1/g;
$dbtype =~ s/\W+/_/g;
}
# add ODBC 'behind' ADO
push @dbtypes, 'ODBC' if $driver eq 'ADO';
# add discovered dbtype in front of ADO/ODBC
unshift @dbtypes, $dbtype;
}
@dbtypes = &$DbTypeSubclass($dbh, \@dbtypes)
if (ref $DbTypeSubclass eq 'CODE');
$dbh->trace_msg(" DbTypeSubclass($DbTypeSubclass)=@dbtypes\n");
return @dbtypes;
}
sub _load_class {
my ($load_class, $missing_ok) = @_;
DBI->trace_msg(" _load_class($load_class, $missing_ok)\n", 2);
no strict 'refs';
return 1 if @{"$load_class\::ISA"}; # already loaded/exists
(my $module = $load_class) =~ s!::!/!g;
DBI->trace_msg(" _load_class require $module\n", 2);
eval { require "$module.pm"; };
return 1 unless $@;
return 0 if $missing_ok && $@ =~ /^Can't locate \Q$module.pm\E/;
die $@;
}
sub init_rootclass { # deprecated
return 1;
}
*internal = \&DBD::Switch::dr::driver;