1
- # $Id: DBI.pm,v 11.40 2003/11/27 23:29:06 timbo Exp $
1
+ # $Id: DBI.pm,v 11.42 2004/01/08 14:03:46 timbo Exp $
2
2
# vim: ts=8:sw=4
3
3
#
4
- # Copyright (c) 1994-2003 Tim Bunce Ireland
4
+ # Copyright (c) 1994-2004 Tim Bunce Ireland
5
5
#
6
6
# See COPYRIGHT section in pod text below for usage and distribution rights.
7
7
#
8
8
9
9
require 5.006_00;
10
10
11
11
BEGIN {
12
- $DBI::VERSION = " 1.39 " ; # ==> ALSO update the version in the pod text below!
12
+ $DBI::VERSION = " 1.40 " ; # ==> ALSO update the version in the pod text below!
13
13
}
14
14
15
15
=head1 NAME
@@ -84,10 +84,9 @@ I<The synopsis above only lists the major methods and parameters.>
84
84
85
85
=head2 GETTING HELP
86
86
87
- If you have questions about DBI, you can get help from
88
- the I<[email protected] > mailing list.
89
- You can get help on subscribing and using the list by emailing
90
-
87
+ If you have questions about DBI, or DBD driver modules, you can get
88
+ help from the I<[email protected] > mailing list. You can get help
89
+ on subscribing and using the list by emailing I<[email protected] > .
91
90
92
91
(To help you make the best use of the dbi-users mailing list,
93
92
and any other lists or forums you may use, I I<strongly >
@@ -119,8 +118,8 @@ Tim he's very likely to just forward it to the mailing list.
119
118
120
119
=head2 NOTES
121
120
122
- This is the DBI specification that corresponds to the DBI version 1.39
123
- (C<$Date: 2003/11/27 23:29:06 $ > ).
121
+ This is the DBI specification that corresponds to the DBI version 1.40
122
+ (C<$Date: 2004/01/08 14:03:46 $ > ).
124
123
125
124
The DBI is evolving at a steady pace, so it's good to check that
126
125
you have the latest copy.
@@ -151,7 +150,7 @@ L<"http://search.cpan.org/search?query=DBI&mode=all">.
151
150
152
151
package DBI ;
153
152
154
- my $Revision = substr (q$ Revision: 11.40 $ , 10);
153
+ my $Revision = substr (q$ Revision: 11.42 $ , 10);
155
154
156
155
use Carp();
157
156
use DynaLoader ();
@@ -394,7 +393,7 @@ my @Common_IF = ( # Interface functions common to all DBI classes
394
393
last_insert_id => { U => [3,4,' $table_name, $field_name [, \%attr ]' ], O => 0x0100 },
395
394
preparse => { }, # XXX
396
395
prepare => { U => [2,3,' $statement [, \%attr]' ], O => 0x0200 },
397
- prepare_cached => { U => [2,4,' $statement [, \%attr [, $allow_active ] ]' ] },
396
+ prepare_cached => { U => [2,4,' $statement [, \%attr [, $if_active ] ]' ] },
398
397
selectrow_array => { U => [2,0,' $statement [, \%attr [, @bind_params ] ]' ] },
399
398
selectrow_arrayref => {U => [2,0,' $statement [, \%attr [, @bind_params ] ]' ] },
400
399
selectrow_hashref => { U => [2,0,' $statement [, \%attr [, @bind_params ] ]' ] },
@@ -479,18 +478,26 @@ END {
479
478
sub CLONE {
480
479
my $olddbis = $DBI::_dbistate ;
481
480
_clone_dbis() unless $DBI::PurePerl ; # clone the DBIS structure
482
- %DBI::installed_drh = (); # clear loaded drivers so they have a chance to reinitialize
483
481
DBI-> trace_msg(sprintf " CLONE DBI for new thread %s \n " ,
484
482
$DBI::PurePerl ? " " : sprintf (" (dbis %x -> %x )" ,$olddbis , $DBI::_dbistate ));
483
+ while ( my ($driver , $drh ) = each %DBI::installed_drh ) {
484
+ no strict ' refs' ;
485
+ next if defined &{" DBD::${driver} ::CLONE" };
486
+ warn (" $driver has no driver CLONE() function so is unsafe threaded\n " );
487
+ }
488
+ %DBI::installed_drh = (); # clear loaded drivers so they have a chance to reinitialize
485
489
}
486
490
487
491
488
492
# --- The DBI->connect Front Door methods
489
493
490
494
sub connect_cached {
491
- # XXX we expect Apache::DBI users to still call connect()
495
+ # For library code using connect_cached() with mod_perl
496
+ # we redirect those calls to Apache::DBI::connect() as well
492
497
my ($class , $dsn , $user , $pass , $attr ) = @_ ;
493
- ($attr ||= {})-> {dbi_connect_method } = ' connect_cached' ;
498
+ ($attr ||= {})-> {dbi_connect_method } =
499
+ ($DBI::connect_via eq " Apache::DBI::connect" )
500
+ ? ' Apache::DBI::connect' : ' connect_cached' ;
494
501
return $class -> connect ($dsn , $user , $pass , $attr );
495
502
}
496
503
@@ -1234,16 +1241,8 @@ sub _new_sth { # called by DBD::<drivername>::db::prepare)
1234
1241
1235
1242
sub default_user {
1236
1243
my ($drh , $user , $pass , $attr ) = @_ ;
1237
- unless (defined $user ) {
1238
- $user = $ENV {DBI_USER };
1239
- Carp::carp(" DBI connect: user not defined and DBI_USER env var not set" )
1240
- if 0 && !defined $user && $drh -> {Warn }; # XXX enable later
1241
- }
1242
- unless (defined $pass ) {
1243
- $pass = $ENV {DBI_PASS };
1244
- Carp::carp(" DBI connect: password not defined and DBI_PASS env var not set" )
1245
- if 0 && !defined $pass && $drh -> {Warn }; # XXX enable later
1246
- }
1244
+ $user = $ENV {DBI_USER } unless defined $user ;
1245
+ $pass = $ENV {DBI_PASS } unless defined $pass ;
1247
1246
return ($user , $pass );
1248
1247
}
1249
1248
@@ -1452,7 +1451,7 @@ sub _new_sth { # called by DBD::<drivername>::db::prepare)
1452
1451
}
1453
1452
1454
1453
sub prepare_cached {
1455
- my ($dbh , $statement , $attr , $allow_active ) = @_ ;
1454
+ my ($dbh , $statement , $attr , $if_active ) = @_ ;
1456
1455
# Needs support at dbh level to clear cache before complaining about
1457
1456
# active children. The XS template code does this. Drivers not using
1458
1457
# the template must handle clearing the cache themselves.
@@ -1462,12 +1461,11 @@ sub _new_sth { # called by DBD::<drivername>::db::prepare)
1462
1461
my $key = ($attr ) ? join (" ~~" , $statement , @attr_keys , @{$attr }{@attr_keys }) : $statement ;
1463
1462
my $sth = $cache -> {$key };
1464
1463
if ($sth ) {
1465
- if ($sth -> FETCH(' Active' ) && ($allow_active ||0) != 2) {
1466
- Carp::carp(" prepare_cached($statement ) statement handle $sth was still active" )
1467
- if !$allow_active ;
1468
- $sth -> finish;
1469
- }
1470
- return $sth ;
1464
+ return $sth unless $sth -> FETCH(' Active' );
1465
+ Carp::carp(" prepare_cached($statement ) statement handle $sth still active" )
1466
+ unless ($if_active ||= 0);
1467
+ $sth -> finish if $if_active <= 1;
1468
+ return $sth if $if_active <= 2;
1471
1469
}
1472
1470
$sth = $dbh -> prepare($statement , $attr );
1473
1471
$cache -> {$key } = $sth if $sth ;
@@ -2286,8 +2284,7 @@ variables if no C<$data_source> is specified.)
2286
2284
The C<AutoCommit > and C<PrintError > attributes for each connection default to
2287
2285
"on". (See L</AutoCommit> and L</PrintError> for more information.)
2288
2286
However, it is strongly recommended that you explicitly define C<AutoCommit >
2289
- rather than rely on the default. Future versions of
2290
- the DBI may issue a warning if C<AutoCommit > is not explicitly defined.
2287
+ rather than rely on the default.
2291
2288
2292
2289
The C<\%attr > parameter can be used to alter the default settings of
2293
2290
C<PrintError > , C<RaiseError > , C<AutoCommit > , and other attributes. For example:
@@ -3434,14 +3431,36 @@ be used with the DBI.
3434
3431
3435
3432
$sth = $dbh->prepare_cached($statement)
3436
3433
$sth = $dbh->prepare_cached($statement, \%attr)
3437
- $sth = $dbh->prepare_cached($statement, \%attr, $allow_active )
3434
+ $sth = $dbh->prepare_cached($statement, \%attr, $if_active )
3438
3435
3439
3436
Like L</prepare> except that the statement handle returned will be
3440
3437
stored in a hash associated with the C<$dbh > . If another call is made to
3441
3438
C<prepare_cached > with the same C<$statement > and C<%attr > values, then the
3442
3439
corresponding cached C<$sth > will be returned without contacting the
3443
3440
database server.
3444
3441
3442
+ The C<$if_active > parameter lets you adjust the behaviour if an
3443
+ already cached statement handle is still Active. There are several
3444
+ alternatives:
3445
+
3446
+ =over 4
3447
+
3448
+ B<0 > : A warning will be generated, and finish() will be called on
3449
+ the statement handle before it is returned. This is the default
3450
+ behaviour if $if_active is not passed.
3451
+
3452
+ B<1 > : finish() will be called on the statement handle, but the
3453
+ warning is suppressed.
3454
+
3455
+ B<2 > : Disables any checking.
3456
+
3457
+ B<3 > : The existing active statement handle will be removed from the
3458
+ cache and a new statement handle prepared and cached in its place.
3459
+ This is the safest option because it doesn't affect the state of the
3460
+ old handle, it just removes it from the cache. [Added in DBI 1.40]
3461
+
3462
+ =back
3463
+
3445
3464
Here are some examples of C<prepare_cached > :
3446
3465
3447
3466
sub insert_hash {
@@ -3469,10 +3488,12 @@ but it can also cause problems and should be used with care. Here
3469
3488
is a contrived case where caching would cause a significant problem:
3470
3489
3471
3490
my $sth = $dbh->prepare_cached('SELECT * FROM foo WHERE bar=?');
3472
- $sth->execute($bar );
3491
+ $sth->execute(... );
3473
3492
while (my $data = $sth->fetchrow_hashref) {
3493
+
3494
+ # later, in some other code called within the loop...
3474
3495
my $sth2 = $dbh->prepare_cached('SELECT * FROM foo WHERE bar=?');
3475
- $sth2->execute($data->{bar} );
3496
+ $sth2->execute(... );
3476
3497
while (my $data2 = $sth2->fetchrow_arrayref) {
3477
3498
do_stuff(...);
3478
3499
}
@@ -3485,30 +3506,14 @@ Typically the the inner fetch loop will work normally, fetching all
3485
3506
the records and terminating when there are no more, but now $sth
3486
3507
is the same as $sth2 the outer fetch loop will also terminate.
3487
3508
3488
- The C<$allow_active > parameter lets you adjust DBI's behaviour when
3489
- prepare_cached is returning a statement handle that is still active.
3490
- There are three settings:
3491
-
3492
- =over 4
3493
-
3494
- B<0 > : A warning will be generated, and C<finish > will be called on
3495
- the statement handle before it is returned. This is the default
3496
- behaviour if C<$allow_active > is not passed.
3497
-
3498
- B<1 > : C<finish > will be called on the statement handle, but the
3499
- warning is suppressed.
3500
-
3501
- B<2 > : DBI will not touch the statement handle before returning it.
3502
- You will need to check C<$sth-E<gt> {Active}> on the returned
3503
- statement handle and deal with it in your own code.
3504
-
3505
- =back
3509
+ You'll know if you run into this problem because prepare_cached()
3510
+ will generate a warning by default (when $if_active is false).
3506
3511
3507
- Because the cache used by prepare_cached() is keyed by all the
3508
- parameters, including any attributes passed, you can also avoid
3509
- this issue by doing something like:
3512
+ The cache used by prepare_cached() is keyed by both the statement
3513
+ and any attributes so you can also avoid this issue by doing something
3514
+ like:
3510
3515
3511
- my $sth = $dbh->prepare_cached("...", { dbi_dummy => __FILE__.__LINE__ });
3516
+ $sth = $dbh->prepare_cached("...", { dbi_dummy => __FILE__.__LINE__ });
3512
3517
3513
3518
which will ensure that prepare_cached only returns statements cached
3514
3519
by that line of code in that source file.
@@ -4008,7 +4013,8 @@ See L</table_info> for a description of the parameters.
4008
4013
If C<$dbh-E<gt> get_info(29)> returns true (29 is SQL_IDENTIFIER_QUOTE_CHAR)
4009
4014
then the table names are constructed and quoted by L</quote_identifier>
4010
4015
to ensure they are usable even if they contain whitespace or reserved
4011
- words etc.
4016
+ words etc. This means that the table names returned will include
4017
+ quote characters.
4012
4018
4013
4019
=item C<type_info_all >
4014
4020
@@ -4998,6 +5004,7 @@ value to be used for the key for the returned hash. For example:
4998
5004
4999
5005
$dbh->{FetchHashKeyName} = 'NAME_lc';
5000
5006
$sth = $dbh->prepare("SELECT FOO, BAR, ID, NAME, BAZ FROM TABLE");
5007
+ $sth->execute;
5001
5008
$hash_ref = $sth->fetchall_hashref('id');
5002
5009
print "Name for id 42 is $hash_ref->{42}->{name}\n";
5003
5010
@@ -5611,6 +5618,8 @@ can cause problems. You have been warned.
5611
5618
Using DBI with perl threads is not yet recommended for production
5612
5619
environments.
5613
5620
5621
+ Note: There is a bug in perl 5.8.2 when configured with threads
5622
+ and debugging enabled (bug #24463) which causes a DBI test to fail.
5614
5623
5615
5624
=head2 Signal Handling and Canceling Operations
5616
5625
@@ -5962,18 +5971,19 @@ Some of these are rather dated now but may still be useful.
5962
5971
http://www.altavista.com/query?q=sql+tutorial
5963
5972
5964
5973
5965
- =head2 Books and Journals
5974
+ =head2 Books and Articles
5966
5975
5967
- Programming the Perl DBI, by Alligator Descartes and Tim Bunce.
5976
+ Programming the Perl DBI, by Alligator Descartes and Tim Bunce.
5977
+ L<http://books.perl.org/book/154>
5968
5978
5969
- Programming Perl 3rd Ed. by Larry Wall, Tom Christiansen & Jon Orwant.
5979
+ Programming Perl 3rd Ed. by Larry Wall, Tom Christiansen & Jon Orwant.
5980
+ L<http://books.perl.org/book/134>
5970
5981
5971
- Learning Perl by Randal Schwartz.
5972
-
5973
- Dr Dobb's Journal, November 1996.
5974
-
5975
- The Perl Journal, April 1997.
5982
+ Learning Perl by Randal Schwartz.
5983
+ L<http://books.perl.org/book/101>
5976
5984
5985
+ Details of many other books related to perl can be found at L<http://books.perl.org>
5986
+
5977
5987
=head2 Perl Modules
5978
5988
5979
5989
Index of DBI related modules available from CPAN:
@@ -6096,14 +6106,60 @@ Leffler, Jeff Urlwin, Michael Peppler, Henrik Tougaard, Edwin Pratomo,
6096
6106
Davide Migliavacca, Jan Pazdziora, Peter Haworth, Edmund Mergl, Steve
6097
6107
Williams, Thomas Lowery, and Phlip Plumlee. Without them, the DBI would
6098
6108
not be the practical reality it is today. I'm also especially grateful
6099
- to Alligator Descartes for starting work on the "Programming the Perl
6100
- DBI" book and letting me jump on board.
6109
+ to Alligator Descartes for starting work on the first edition of the
6110
+ "Programming the Perl DBI" book and letting me jump on board.
6101
6111
6102
- Much of the DBI and DBD::Oracle was developed while I was Technical
6112
+ The DBI and DBD::Oracle were originally developed while I was Technical
6103
6113
Director (CTO) of the Paul Ingram Group (www.ig.co.uk). So I'd
6104
6114
especially like to thank Paul for his generosity and vision in
6105
6115
supporting this work for many years.
6106
6116
6117
+ =head1 CONTRIBUTING
6118
+
6119
+ As you can see above, many people have contributed to the DBI and
6120
+ drivers in many ways over many years.
6121
+
6122
+ If you'd like the DBI to do something new or different the best way
6123
+ to make that happen is to do it yourself and send me a patch to the
6124
+ source code that shows the changes.
6125
+
6126
+ =head2 How to create a patch
6127
+
6128
+ Unpack a fresh copy of the distribution:
6129
+
6130
+ tar xfz DBI-1.40.tar.gz
6131
+
6132
+ Rename the newly created top level directory:
6133
+
6134
+ mv DBI-1.40 DBI-1.40.your_foo
6135
+
6136
+ Edit the contents of DBI-1.40.your_foo/* till it does what you want.
6137
+
6138
+ Test your changes and then remove all temporary files:
6139
+
6140
+ make test && make distclean
6141
+
6142
+ Go back to the directory you originally unpacked the distribution:
6143
+
6144
+ cd ..
6145
+
6146
+ Unpack I<another > copy of the original distribution you started with:
6147
+
6148
+ tar xfz DBI-1.40.tar.gz
6149
+
6150
+ Then create a patch file by performing a recursive C<diff > on the two
6151
+ top level directories:
6152
+
6153
+ diff -r -u DBI-1.40 DBI-1.40.your_foo > DBI-1.40.your_foo.patch
6154
+
6155
+ =head2 Speak before you patch
6156
+
6157
+ For anything non-trivial or possibly controversial it's a good idea
6158
+ to discuss (on [email protected] ) the changes you propose before
6159
+ actually spending time working on them. Otherwise you run the risk
6160
+ of them being rejected because they don't fit into some larger plans
6161
+ you may not be aware of.
6162
+
6107
6163
=head1 TRANSLATIONS
6108
6164
6109
6165
A German translation of this manual (possibly slightly out of date) is
0 commit comments