.\" -*- mode: troff; coding: utf-8 -*-
.\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45)
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
.ie n \{\
. ds C` ""
. ds C' ""
'br\}
.el\{\
. ds C`
. ds C'
'br\}
.\"
.\" Escape single quotes in literal strings from groff's Unicode transform.
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.\"
.\" If the F register is >0, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
.\" entries marked with X<> in POD. Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.\"
.\" Avoid warning from groff about undefined register 'F'.
.de IX
..
.nr rF 0
.if \n(.g .if rF .nr rF 1
.if (\n(rF:(\n(.g==0)) \{\
. if \nF \{\
. de IX
. tm Index:\\$1\t\\n%\t"\\$2"
..
. if !\nF==2 \{\
. nr % 0
. nr F 2
. \}
. \}
.\}
.rr rF
.\" ========================================================================
.\"
.IX Title "DBI 3"
.TH DBI 3 2024-09-13 "perl v5.40.0" "User Contributed Perl Documentation"
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.if n .ad l
.nh
.SH NAME
DBI \- Database independent interface for Perl
.SH SYNOPSIS
.IX Header "SYNOPSIS"
.Vb 1
\& use DBI;
\&
\& @driver_names = DBI\->available_drivers;
\& %drivers = DBI\->installed_drivers;
\& @data_sources = DBI\->data_sources($driver_name, \e%attr);
\&
\& $dbh = DBI\->connect($data_source, $username, $auth, \e%attr);
\&
\& $rv = $dbh\->do($statement);
\& $rv = $dbh\->do($statement, \e%attr);
\& $rv = $dbh\->do($statement, \e%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, \e%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, \e%attr);
\&
\& $rv = $sth\->execute;
\& $rv = $sth\->execute(@bind_values);
\& $rv = $sth\->execute_array(\e%attr, ...);
\&
\& $rc = $sth\->bind_col($col_num, \e$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;
.Ve
.PP
\&\fIThe synopsis above only lists the major methods and parameters.\fR
.SS "GETTING HELP"
.IX Subsection "GETTING HELP"
\fIGeneral\fR
.IX Subsection "General"
.PP
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
.PP
You might also like to read the Advanced DBI Tutorial at
.PP
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: .
.PP
\fIMailing Lists\fR
.IX Subsection "Mailing Lists"
.PP
If you have questions about DBI, or DBD driver modules, you can get
help from the \fIdbi\-users@perl.org\fR 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
\&\fIdbi\-users\-help@perl.org\fR.
.PP
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 \fIdbi-users\fR 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.
.PP
\fIIRC\fR
.IX Subsection "IRC"
.PP
DBI IRC Channel: #dbi on irc.perl.org ()
.PP
\fIOnline\fR
.IX Subsection "Online"
.PP
StackOverflow has a DBI tag
with over 800 questions.
.PP
The DBI home page at might be worth a visit.
It includes links to other resources, but \fIis rather out-dated\fR.
.PP
\fIReporting a Bug\fR
.IX Subsection "Reporting a Bug"
.PP
If you think you've found a bug then please read
"How to Report Bugs Effectively" by Simon Tatham:
.
.PP
If you think you've found a memory leak then read "Memory Leaks".
.PP
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
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.
.SS NOTES
.IX Subsection "NOTES"
This is the DBI specification that corresponds to DBI version 1.645
(see DBI::Changes for details).
.PP
The DBI is evolving at a steady pace, so it's good to check that
you have the latest copy.
.PP
The significant user-visible changes in each release are documented
in the DBI::Changes module so you can read them by executing
\&\f(CW\*(C`perldoc DBI::Changes\*(C'\fR.
.PP
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.
.PP
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.
.PP
Extensions to the DBI API often use the \f(CW\*(C`DBIx::*\*(C'\fR namespace.
See "Naming Conventions and Name Space". DBI extension modules
can be found at . And all modules
related to the DBI can be found at .
.SH DESCRIPTION
.IX Header "DESCRIPTION"
The DBI is a database access module for the Perl programming language. It defines
a set of methods, variables, and conventions that provide a consistent
database interface, independent of the actual database being used.
.PP
It is important to remember that the DBI is just an interface.
The DBI is a layer
of "glue" between an application and one or more database \fIdriver\fR
modules. It is the driver modules which do most of the real work. The DBI
provides a standard interface and framework for the drivers to operate
within.
.PP
This document often uses terms like \fIreferences\fR, \fIobjects\fR,
\&\fImethods\fR. If you're not familiar with those terms then it would
be a good idea to read at least the following perl manuals first:
perlreftut, perldsc, perllol, and perlboot.
.SS "Architecture of a DBI Application"
.IX Subsection "Architecture of a DBI Application"
.Vb 11
\& |<\- Scope of DBI \->|
\& .\-. .\-\-\-\-\-\-\-\-\-\-\-\-\-\-. .\-\-\-\-\-\-\-\-\-\-\-\-\-.
\& .\-\-\-\-\-\-\-. | |\-\-\-| XYZ Driver |\-\-\-| XYZ Engine |
\& | Perl | | | \`\-\-\-\-\-\-\-\-\-\-\-\-\-\-\*(Aq \`\-\-\-\-\-\-\-\-\-\-\-\-\-\*(Aq
\& | script| |A| |D| .\-\-\-\-\-\-\-\-\-\-\-\-\-\-. .\-\-\-\-\-\-\-\-\-\-\-\-\-.
\& | using |\-\-|P|\-\-|B|\-\-\-|Oracle Driver |\-\-\-|Oracle Engine|
\& | DBI | |I| |I| \`\-\-\-\-\-\-\-\-\-\-\-\-\-\-\*(Aq \`\-\-\-\-\-\-\-\-\-\-\-\-\-\*(Aq
\& | API | | |...
\& |methods| | |... Other drivers
\& \`\-\-\-\-\-\-\-\*(Aq | |...
\& \`\-\*(Aq
.Ve
.PP
The API, or Application Programming Interface, defines the
call interface and variables for Perl scripts to use. The API
is implemented by the Perl DBI extension.
.PP
The DBI "dispatches" the method calls to the appropriate driver for
actual execution. The DBI is also responsible for the dynamic loading
of drivers, error checking and handling, providing default
implementations for methods, and many other non-database specific duties.
.PP
Each driver
contains implementations of the DBI methods using the
private interface functions of the corresponding database engine. Only authors
of sophisticated/multi\-database applications or generic library
functions need be concerned with drivers.
.SS "Notation and Conventions"
.IX Subsection "Notation and Conventions"
The following conventions are used in this document:
.PP
.Vb 11
\& $dbh Database handle object
\& $sth Statement handle object
\& $drh Driver handle object (rarely seen or used in applications)
\& $h Any of the handle types above ($dbh, $sth, or $drh)
\& $rc General Return Code (boolean: true=ok, false=error)
\& $rv General Return Value (typically an integer)
\& @ary List of values returned from the database, typically a row of data
\& $rows Number of rows processed (if available, else \-1)
\& $fh A filehandle
\& undef NULL values are represented by undefined values in Perl
\& \e%attr Reference to a hash of attribute values passed to methods
.Ve
.PP
Note that Perl will automatically destroy database and statement handle objects
if all references to them are deleted.
.SS "Outline Usage"
.IX Subsection "Outline Usage"
To use DBI,
first you need to load the DBI module:
.PP
.Vb 2
\& use DBI;
\& use strict;
.Ve
.PP
(The \f(CW\*(C`use strict;\*(C'\fR isn't required but is strongly recommended.)
.PP
Then you need to "connect" to your data source and get a \fIhandle\fR for that
connection:
.PP
.Vb 2
\& $dbh = DBI\->connect($dsn, $user, $password,
\& { RaiseError => 1, AutoCommit => 0 });
.Ve
.PP
Since connecting can be expensive, you generally just connect at the
start of your program and disconnect at the end.
.PP
Explicitly defining the required \f(CW\*(C`AutoCommit\*(C'\fR behaviour is strongly
recommended and may become mandatory in a later version. This
determines whether changes are automatically committed to the
database when executed, or need to be explicitly committed later.
.PP
The DBI allows an application to "prepare" statements for later
execution. A prepared statement is identified by a statement handle
held in a Perl variable.
We'll call the Perl variable \f(CW$sth\fR in our examples.
.PP
The typical method call sequence for a \f(CW\*(C`SELECT\*(C'\fR statement is:
.PP
.Vb 4
\& prepare,
\& execute, fetch, fetch, ...
\& execute, fetch, fetch, ...
\& execute, fetch, fetch, ...
.Ve
.PP
for example:
.PP
.Vb 1
\& $sth = $dbh\->prepare("SELECT foo, bar FROM table WHERE baz=?");
\&
\& $sth\->execute( $baz );
\&
\& while ( @row = $sth\->fetchrow_array ) {
\& print "@row\en";
\& }
.Ve
.PP
For queries that are not executed many times at once, it is often cleaner
to use the higher level select wrappers:
.PP
.Vb 1
\& $row_hashref = $dbh\->selectrow_hashref("SELECT foo, bar FROM table WHERE baz=?", undef, $baz);
\&
\& $arrayref_of_row_hashrefs = $dbh\->selectall_arrayref(
\& "SELECT foo, bar FROM table WHERE baz BETWEEN ? AND ?",
\& { Slice => {} }, $baz_min, $baz_max);
.Ve
.PP
The typical method call sequence for a \fInon\fR\-\f(CW\*(C`SELECT\*(C'\fR statement is:
.PP
.Vb 4
\& prepare,
\& execute,
\& execute,
\& execute.
.Ve
.PP
for example:
.PP
.Vb 1
\& $sth = $dbh\->prepare("INSERT INTO table(foo,bar,baz) VALUES (?,?,?)");
\&
\& while() {
\& chomp;
\& my ($foo,$bar,$baz) = split /,/;
\& $sth\->execute( $foo, $bar, $baz );
\& }
.Ve
.PP
The \f(CWdo()\fR method is a wrapper of prepare and execute that can be simpler
for non repeated \fInon\fR\-\f(CW\*(C`SELECT\*(C'\fR statements (or with drivers that don't
support placeholders):
.PP
.Vb 1
\& $rows_affected = $dbh\->do("UPDATE your_table SET foo = foo + 1");
\&
\& $rows_affected = $dbh\->do("DELETE FROM table WHERE foo = ?", undef, $foo);
.Ve
.PP
To commit your changes to the database (when "AutoCommit" is off):
.PP
.Vb 1
\& $dbh\->commit; # or call $dbh\->rollback; to undo changes
.Ve
.PP
Finally, when you have finished working with the data source, you should
"disconnect" from it:
.PP
.Vb 1
\& $dbh\->disconnect;
.Ve
.SS "General Interface Rules & Caveats"
.IX Subsection "General Interface Rules & Caveats"
The DBI does not have a concept of a "current session". Every session
has a handle object (i.e., a \f(CW$dbh\fR) returned from the \f(CW\*(C`connect\*(C'\fR method.
That handle object is used to invoke database related methods.
.PP
Most data is returned to the Perl script as strings. (Null values are
returned as \f(CW\*(C`undef\*(C'\fR.) This allows arbitrary precision numeric data to be
handled without loss of accuracy. Beware that Perl may not preserve
the same accuracy when the string is used as a number.
.PP
Dates and times are returned as character strings in the current
default format of the corresponding database engine. Time zone effects
are database/driver dependent.
.PP
Perl supports binary data in Perl strings, and the DBI will pass binary
data to and from the driver without change. It is up to the driver
implementors to decide how they wish to handle such binary data.
.PP
Perl supports two kinds of strings: Unicode (utf8 internally) and non-Unicode
(defaults to iso\-8859\-1 if forced to assume an encoding). Drivers should
accept both kinds of strings and, if required, convert them to the character
set of the database being used. Similarly, when fetching from the database
character data that isn't iso\-8859\-1 the driver should convert it into utf8.
.PP
Multiple SQL statements may not be combined in a single statement
handle (\f(CW$sth\fR), although some databases and drivers do support this
(notably Sybase and SQL Server).
.PP
Non-sequential record reads are not supported in this version of the DBI.
In other words, records can only be fetched in the order that the
database returned them, and once fetched they are forgotten.
.PP
Positioned updates and deletes are not directly supported by the DBI.
See the description of the \f(CW\*(C`CursorName\*(C'\fR attribute for an alternative.
.PP
Individual driver implementors are free to provide any private
functions and/or handle attributes that they feel are useful.
Private driver functions can be invoked using the DBI \f(CWfunc()\fR method.
Private driver attributes are accessed just like standard attributes.
.PP
Many methods have an optional \f(CW\*(C`\e%attr\*(C'\fR parameter which can be used to
pass information to the driver implementing the method. Except where
specifically documented, the \f(CW\*(C`\e%attr\*(C'\fR parameter can only be used to pass
driver specific hints. In general, you can ignore \f(CW\*(C`\e%attr\*(C'\fR parameters
or pass it as \f(CW\*(C`undef\*(C'\fR.
.SS "Naming Conventions and Name Space"
.IX Subsection "Naming Conventions and Name Space"
The DBI package and all packages below it (\f(CW\*(C`DBI::*\*(C'\fR) are reserved for
use by the DBI. Extensions and related modules use the \f(CW\*(C`DBIx::\*(C'\fR
namespace (see ).
Package names beginning with \f(CW\*(C`DBD::\*(C'\fR are reserved for use
by DBI database drivers. All environment variables used by the DBI
or by individual DBDs begin with "\f(CW\*(C`DBI_\*(C'\fR" or "\f(CW\*(C`DBD_\*(C'\fR".
.PP
The letter case used for attribute names is significant and plays an
important part in the portability of DBI scripts. The case of the
attribute name is used to signify who defined the meaning of that name
and its values.
.PP
.Vb 5
\& Case of name Has a meaning defined by
\& \-\-\-\-\-\-\-\-\-\-\-\- \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
\& UPPER_CASE Standards, e.g., X/Open, ISO SQL92 etc (portable)
\& MixedCase DBI API (portable), underscores are not used.
\& lower_case Driver or database engine specific (non\-portable)
.Ve
.PP
It is of the utmost importance that Driver developers only use
lowercase attribute names when defining private attributes. Private
attribute names must be prefixed with the driver name or suitable
abbreviation (e.g., "\f(CW\*(C`ora_\*(C'\fR" for Oracle, "\f(CW\*(C`ing_\*(C'\fR" for Ingres, etc).
.SS "SQL \- A Query Language"
.IX Subsection "SQL - A Query Language"
Most DBI drivers require applications to use a dialect of SQL
(Structured Query Language) to interact with the database engine.
The "Standards Reference Information" section provides links
to useful information about SQL.
.PP
The DBI itself does not mandate or require any particular language to
be used; it is language independent. In ODBC terms, the DBI is in
"pass-thru" mode, although individual drivers might not be. The only requirement
is that queries and other statements must be expressed as a single
string of characters passed as the first argument to the "prepare" or
"do" methods.
.PP
For an interesting diversion on the \fIreal\fR history of RDBMS and SQL,
from the people who made it happen, see:
.PP
.Vb 1
\& http://www.mcjones.org/System_R/SQL_Reunion_95/sqlr95.html
.Ve
.PP
Follow the "Full Contents" then "Intergalactic dataspeak" links for the
SQL history.
.SS "Placeholders and Bind Values"
.IX Subsection "Placeholders and Bind Values"
Some drivers support placeholders and bind values.
\&\fIPlaceholders\fR, also called parameter markers, are used to indicate
values in a database statement that will be supplied later,
before the prepared statement is executed. For example, an application
might use the following to insert a row of data into the SALES table:
.PP
.Vb 1
\& INSERT INTO sales (product_code, qty, price) VALUES (?, ?, ?)
.Ve
.PP
or the following, to select the description for a product:
.PP
.Vb 1
\& SELECT description FROM products WHERE product_code = ?
.Ve
.PP
The \f(CW\*(C`?\*(C'\fR characters are the placeholders. The association of actual
values with placeholders is known as \fIbinding\fR, and the values are
referred to as \fIbind values\fR.
Note that the \f(CW\*(C`?\*(C'\fR is not enclosed in quotation marks, even when the
placeholder represents a string.
.PP
Some drivers also allow placeholders like \f(CW\*(C`:\*(C'\fR\fIname\fR and \f(CW\*(C`:\*(C'\fR\fIN\fR (e.g.,
\&\f(CW\*(C`:1\*(C'\fR, \f(CW\*(C`:2\*(C'\fR, and so on) in addition to \f(CW\*(C`?\*(C'\fR, but their use is not portable.
.PP
If the \f(CW\*(C`:\*(C'\fR\fIN\fR form of placeholder is supported by the driver you're using,
then you should be able to use either "bind_param" or "execute" to bind
values. Check your driver documentation.
.PP
Some drivers allow you to prevent the recognition of a placeholder by placing a
single backslash character (\f(CW\*(C`\e\*(C'\fR) immediately before it. The driver will remove
the backslash character and ignore the placeholder, passing it unchanged to the
backend. If the driver supports this then "get_info"(9000) will return true.
.PP
With most drivers, placeholders can't be used for any element of a
statement that would prevent the database server from validating the
statement and creating a query execution plan for it. For example:
.PP
.Vb 2
\& "SELECT name, age FROM ?" # wrong (will probably fail)
\& "SELECT name, ? FROM people" # wrong (but may not \*(Aqfail\*(Aq)
.Ve
.PP
Also, placeholders can only represent single scalar values.
For example, the following
statement won't work as expected for more than one value:
.PP
.Vb 2
\& "SELECT name, age FROM people WHERE name IN (?)" # wrong
\& "SELECT name, age FROM people WHERE name IN (?,?)" # two names
.Ve
.PP
When using placeholders with the SQL \f(CW\*(C`LIKE\*(C'\fR qualifier, you must
remember that the placeholder substitutes for the whole string.
So you should use "\f(CW\*(C`... LIKE ? ...\*(C'\fR" and include any wildcard
characters in the value that you bind to the placeholder.
.PP
\&\fBNULL Values\fR
.PP
Undefined values, or \f(CW\*(C`undef\*(C'\fR, are used to indicate NULL values.
You can insert and update columns with a NULL value as you would a
non-NULL value. These examples insert and update the column
\&\f(CW\*(C`age\*(C'\fR with a NULL value:
.PP
.Vb 4
\& $sth = $dbh\->prepare(qq{
\& INSERT INTO people (fullname, age) VALUES (?, ?)
\& });
\& $sth\->execute("Joe Bloggs", undef);
\&
\& $sth = $dbh\->prepare(qq{
\& UPDATE people SET age = ? WHERE fullname = ?
\& });
\& $sth\->execute(undef, "Joe Bloggs");
.Ve
.PP
However, care must be taken when trying to use NULL values in a
\&\f(CW\*(C`WHERE\*(C'\fR clause. Consider:
.PP
.Vb 1
\& SELECT fullname FROM people WHERE age = ?
.Ve
.PP
Binding an \f(CW\*(C`undef\*(C'\fR (NULL) to the placeholder will \fInot\fR select rows
which have a NULL \f(CW\*(C`age\*(C'\fR! At least for database engines that
conform to the SQL standard. Refer to the SQL manual for your database
engine or any SQL book for the reasons for this. To explicitly select
NULLs you have to say "\f(CW\*(C`WHERE age IS NULL\*(C'\fR".
.PP
A common issue is to have a code fragment handle a value that could be
either \f(CW\*(C`defined\*(C'\fR or \f(CW\*(C`undef\*(C'\fR (non-NULL or NULL) at runtime.
A simple technique is to prepare the appropriate statement as needed,
and substitute the placeholder for non-NULL cases:
.PP
.Vb 5
\& $sql_clause = defined $age? "age = ?" : "age IS NULL";
\& $sth = $dbh\->prepare(qq{
\& SELECT fullname FROM people WHERE $sql_clause
\& });
\& $sth\->execute(defined $age ? $age : ());
.Ve
.PP
The following technique illustrates qualifying a \f(CW\*(C`WHERE\*(C'\fR clause with
several columns, whose associated values (\f(CW\*(C`defined\*(C'\fR or \f(CW\*(C`undef\*(C'\fR) are
in a hash \f(CW%h:\fR
.PP
.Vb 10
\& for my $col ("age", "phone", "email") {
\& if (defined $h{$col}) {
\& push @sql_qual, "$col = ?";
\& push @sql_bind, $h{$col};
\& }
\& else {
\& push @sql_qual, "$col IS NULL";
\& }
\& }
\& $sql_clause = join(" AND ", @sql_qual);
\& $sth = $dbh\->prepare(qq{
\& SELECT fullname FROM people WHERE $sql_clause
\& });
\& $sth\->execute(@sql_bind);
.Ve
.PP
The techniques above call prepare for the SQL statement with each call to
execute. Because calls to \fBprepare()\fR can be expensive, performance
can suffer when an application iterates many times over statements
like the above.
.PP
A better solution is a single \f(CW\*(C`WHERE\*(C'\fR clause that supports both
NULL and non-NULL comparisons. Its SQL statement would need to be
prepared only once for all cases, thus improving performance.
Several examples of \f(CW\*(C`WHERE\*(C'\fR clauses that support this are presented
below. But each example lacks portability, robustness, or simplicity.
Whether an example is supported on your database engine depends on
what SQL extensions it provides, and where it supports the \f(CW\*(C`?\*(C'\fR
placeholder in a statement.
.PP
.Vb 7
\& 0) age = ?
\& 1) NVL(age, xx) = NVL(?, xx)
\& 2) ISNULL(age, xx) = ISNULL(?, xx)
\& 3) DECODE(age, ?, 1, 0) = 1
\& 4) age = ? OR (age IS NULL AND ? IS NULL)
\& 5) age = ? OR (age IS NULL AND SP_ISNULL(?) = 1)
\& 6) age = ? OR (age IS NULL AND ? = 1)
.Ve
.PP
Statements formed with the above \f(CW\*(C`WHERE\*(C'\fR clauses require execute
statements as follows. The arguments are required, whether their
values are \f(CW\*(C`defined\*(C'\fR or \f(CW\*(C`undef\*(C'\fR.
.PP
.Vb 3
\& 0,1,2,3) $sth\->execute($age);
\& 4,5) $sth\->execute($age, $age);
\& 6) $sth\->execute($age, defined($age) ? 0 : 1);
.Ve
.PP
Example 0 should not work (as mentioned earlier), but may work on
a few database engines anyway (e.g. Sybase). Example 0 is part
of examples 4, 5, and 6, so if example 0 works, these other
examples may work, even if the engine does not properly support
the right hand side of the \f(CW\*(C`OR\*(C'\fR expression.
.PP
Examples 1 and 2 are not robust: they require that you provide a
valid column value xx (e.g. '~') which is not present in any row.
That means you must have some notion of what data won't be stored
in the column, and expect clients to adhere to that.
.PP
Example 5 requires that you provide a stored procedure (SP_ISNULL
in this example) that acts as a function: it checks whether a value
is null, and returns 1 if it is, or 0 if not.
.PP
Example 6, the least simple, is probably the most portable, i.e., it
should work with most, if not all, database engines.
.PP
Here is a table that indicates which examples above are known to
work on various database engines:
.PP
.Vb 10
\& \-\-\-\-\-Examples\-\-\-\-\-\-
\& 0 1 2 3 4 5 6
\& \- \- \- \- \- \- \-
\& Oracle 9 N Y N Y Y ? Y
\& Informix IDS 9 N N N Y N Y Y
\& MS SQL N N Y N Y ? Y
\& Sybase Y N N N N N Y
\& AnyData,DBM,CSV Y N N N Y Y* Y
\& SQLite 3.3 N N N N Y N N
\& MSAccess N N N N Y N Y
.Ve
.PP
* Works only because Example 0 works.
.PP
DBI provides a sample perl script that will test the examples above
on your database engine and tell you which ones work. It is located
in the \fIex/\fR subdirectory of the DBI source distribution, or here:
Please use the script to help us fill-in and maintain this table.
.PP
\&\fBPerformance\fR
.PP
Without using placeholders, the insert statement shown previously would have to
contain the literal values to be inserted and would have to be
re-prepared and re-executed for each row. With placeholders, the insert
statement only needs to be prepared once. The bind values for each row
can be given to the \f(CW\*(C`execute\*(C'\fR method each time it's called. By avoiding
the need to re-prepare the statement for each row, the application
typically runs many times faster. Here's an example:
.PP
.Vb 9
\& my $sth = $dbh\->prepare(q{
\& INSERT INTO sales (product_code, qty, price) VALUES (?, ?, ?)
\& }) or die $dbh\->errstr;
\& while (<>) {
\& chomp;
\& my ($product_code, $qty, $price) = split /,/;
\& $sth\->execute($product_code, $qty, $price) or die $dbh\->errstr;
\& }
\& $dbh\->commit or die $dbh\->errstr;
.Ve
.PP
See "execute" and "bind_param" for more details.
.PP
The \f(CW\*(C`q{...}\*(C'\fR style quoting used in this example avoids clashing with
quotes that may be used in the SQL statement. Use the double-quote like
\&\f(CW\*(C`qq{...}\*(C'\fR operator if you want to interpolate variables into the string.
See "Quote and Quote-like Operators" in perlop for more details.
.PP
See also the "bind_columns" method, which is used to associate Perl
variables with the output columns of a \f(CW\*(C`SELECT\*(C'\fR statement.
.SH "THE DBI PACKAGE AND CLASS"
.IX Header "THE DBI PACKAGE AND CLASS"
In this section, we cover the DBI class methods, utility functions,
and the dynamic attributes associated with generic DBI handles.
.SS "DBI Constants"
.IX Subsection "DBI Constants"
Constants representing the values of the SQL standard types can be
imported individually by name, or all together by importing the
special \f(CW\*(C`:sql_types\*(C'\fR tag.
.PP
The names and values of all the defined SQL standard types can be
produced like this:
.PP
.Vb 3
\& foreach (@{ $DBI::EXPORT_TAGS{sql_types} }) {
\& printf "%s=%d\en", $_, &{"DBI::$_"};
\& }
.Ve
.PP
These constants are defined by SQL/CLI, ODBC or both.
\&\f(CW\*(C`SQL_BIGINT\*(C'\fR has conflicting codes in SQL/CLI and ODBC,
DBI uses the ODBC one.
.PP
See the "type_info", "type_info_all", and "bind_param" methods
for possible uses.
.PP
Note that just because the DBI defines a named constant for a given
data type doesn't mean that drivers will support that data type.
.SS "DBI Class Methods"
.IX Subsection "DBI Class Methods"
The following methods are provided by the DBI class:
.PP
\fR\f(CI\*(C`parse_dsn\*(C'\fR\fI\fR
.IX Subsection "parse_dsn"
.PP
.Vb 2
\& ($scheme, $driver, $attr_string, $attr_hash, $driver_dsn) = DBI\->parse_dsn($dsn)
\& or die "Can\*(Aqt parse DBI DSN \*(Aq$dsn\*(Aq";
.Ve
.PP
Breaks apart a DBI Data Source Name (DSN) and returns the individual
parts. If \f(CW$dsn\fR doesn't contain a valid DSN then \fBparse_dsn()\fR returns
an empty list.
.PP
\&\f(CW$scheme\fR is the first part of the DSN and is currently always 'dbi'.
\&\f(CW$driver\fR is the driver name, possibly defaulted to \f(CW$ENV\fR{DBI_DRIVER},
and may be undefined. \f(CW$attr_string\fR is the contents of the optional attribute
string, which may be undefined. If \f(CW$attr_string\fR is not empty then \f(CW$attr_hash\fR
is a reference to a hash containing the parsed attribute names and values.
\&\f(CW$driver_dsn\fR is the last part of the DBI DSN string. For example:
.PP
.Vb 7
\& ($scheme, $driver, $attr_string, $attr_hash, $driver_dsn)
\& = DBI\->parse_dsn("dbi:MyDriver(RaiseError=>1):db=test;port=42");
\& $scheme = \*(Aqdbi\*(Aq;
\& $driver = \*(AqMyDriver\*(Aq;
\& $attr_string = \*(AqRaiseError=>1\*(Aq;
\& $attr_hash = { \*(AqRaiseError\*(Aq => \*(Aq1\*(Aq };
\& $driver_dsn = \*(Aqdb=test;port=42\*(Aq;
.Ve
.PP
The \fBparse_dsn()\fR method was added in DBI 1.43.
.PP
\fR\f(CI\*(C`connect\*(C'\fR\fI\fR
.IX Subsection "connect"
.PP
.Vb 4
\& $dbh = DBI\->connect($data_source, $username, $password)
\& or die $DBI::errstr;
\& $dbh = DBI\->connect($data_source, $username, $password, \e%attr)
\& or die $DBI::errstr;
.Ve
.PP
Establishes a database connection, or session, to the requested \f(CW$data_source\fR.
Returns a database handle object if the connection succeeds. Use
\&\f(CW\*(C`$dbh\->disconnect\*(C'\fR to terminate the connection.
.PP
If the connect fails (see below), it returns \f(CW\*(C`undef\*(C'\fR and sets both \f(CW$DBI::err\fR
and \f(CW$DBI::errstr\fR. (It does \fInot\fR explicitly set \f(CW$!\fR.) You should generally
test the return status of \f(CW\*(C`connect\*(C'\fR and \f(CW\*(C`print $DBI::errstr\*(C'\fR if it has failed.
.PP
Multiple simultaneous connections to multiple databases through multiple
drivers can be made via the DBI. Simply make one \f(CW\*(C`connect\*(C'\fR call for each
database and keep a copy of each returned database handle.
.PP
The \f(CW$data_source\fR value must begin with "\f(CW\*(C`dbi:\*(C'\fR\fIdriver_name\fR\f(CW\*(C`:\*(C'\fR".
The \fIdriver_name\fR specifies the driver that will be used to make the
connection. (Letter case is significant.)
.PP
As a convenience, if the \f(CW$data_source\fR parameter is undefined or empty,
the DBI will substitute the value of the environment variable \f(CW\*(C`DBI_DSN\*(C'\fR.
If just the \fIdriver_name\fR part is empty (i.e., the \f(CW$data_source\fR
prefix is "\f(CW\*(C`dbi::\*(C'\fR"), the environment variable \f(CW\*(C`DBI_DRIVER\*(C'\fR is
used. If neither variable is set, then \f(CW\*(C`connect\*(C'\fR dies.
.PP
Examples of \f(CW$data_source\fR values are:
.PP
.Vb 3
\& dbi:DriverName:database_name
\& dbi:DriverName:database_name@hostname:port
\& dbi:DriverName:database=database_name;host=hostname;port=port
.Ve
.PP
There is \fIno standard\fR for the text following the driver name. Each
driver is free to use whatever syntax it wants. The only requirement the
DBI makes is that all the information is supplied in a single string.
You must consult the documentation for the drivers you are using for a
description of the syntax they require.
.PP
It is recommended that drivers support the ODBC style, shown in the
last example above. It is also recommended that they support the
three common names '\f(CW\*(C`host\*(C'\fR', '\f(CW\*(C`port\*(C'\fR', and '\f(CW\*(C`database\*(C'\fR' (plus '\f(CW\*(C`db\*(C'\fR'
as an alias for \f(CW\*(C`database\*(C'\fR). This simplifies automatic construction
of basic DSNs: \f(CW"dbi:$driver:database=$db;host=$host;port=$port"\fR.
Drivers should aim to 'do something reasonable' when given a DSN
in this form, but if any part is meaningless for that driver (such
as 'port' for Informix) it should generate an error if that part
is not empty.
.PP
If the environment variable \f(CW\*(C`DBI_AUTOPROXY\*(C'\fR is defined (and the
driver in \f(CW$data_source\fR is not "\f(CW\*(C`Proxy\*(C'\fR") then the connect request
will automatically be changed to:
.PP
.Vb 1
\& $ENV{DBI_AUTOPROXY};dsn=$data_source
.Ve
.PP
\&\f(CW\*(C`DBI_AUTOPROXY\*(C'\fR is typically set as "\f(CW\*(C`dbi:Proxy:hostname=...;port=...\*(C'\fR".
If \f(CW$ENV\fR{DBI_AUTOPROXY} doesn't begin with '\f(CW\*(C`dbi:\*(C'\fR' then "dbi:Proxy:"
will be prepended to it first. See the DBD::Proxy documentation
for more details.
.PP
If \f(CW$username\fR or \f(CW$password\fR are undefined (rather than just empty),
then the DBI will substitute the values of the \f(CW\*(C`DBI_USER\*(C'\fR and \f(CW\*(C`DBI_PASS\*(C'\fR
environment variables, respectively. The DBI will warn if the
environment variables are not defined. However, the everyday use
of these environment variables is not recommended for security
reasons. The mechanism is primarily intended to simplify testing.
See below for alternative way to specify the username and password.
.PP
\&\f(CW\*(C`DBI\->connect\*(C'\fR automatically installs the driver if it has not been
installed yet. Driver installation either returns a valid driver
handle, or it \fIdies\fR with an error message that includes the string
"\f(CW\*(C`install_driver\*(C'\fR" and the underlying problem. So \f(CW\*(C`DBI\->connect\*(C'\fR
will die
on a driver installation failure and will only return \f(CW\*(C`undef\*(C'\fR on a
connect failure, in which case \f(CW$DBI::errstr\fR will hold the error message.
Use \f(CW\*(C`eval\*(C'\fR if you need to catch the "\f(CW\*(C`install_driver\*(C'\fR" error.
.PP
The \f(CW$data_source\fR argument (with the "\f(CW\*(C`dbi:...:\*(C'\fR" prefix removed) and the
\&\f(CW$username\fR and \f(CW$password\fR arguments are then passed to the driver for
processing. The DBI does not define any interpretation for the
contents of these fields. The driver is free to interpret the
\&\f(CW$data_source\fR, \f(CW$username\fR, and \f(CW$password\fR fields in any way, and supply
whatever defaults are appropriate for the engine being accessed.
(Oracle, for example, uses the ORACLE_SID and TWO_TASK environment
variables if no \f(CW$data_source\fR is specified.)
.PP
The \f(CW\*(C`AutoCommit\*(C'\fR and \f(CW\*(C`PrintError\*(C'\fR attributes for each connection
default to "on". (See "AutoCommit" and "PrintError" for more information.)
However, it is strongly recommended that you explicitly define \f(CW\*(C`AutoCommit\*(C'\fR
rather than rely on the default. The \f(CW\*(C`PrintWarn\*(C'\fR attribute defaults to true.
The \f(CW\*(C`RaiseWarn\*(C'\fR attribute defaults to false.
.PP
The \f(CW\*(C`\e%attr\*(C'\fR parameter can be used to alter the default settings of
\&\f(CW\*(C`PrintError\*(C'\fR, \f(CW\*(C`RaiseError\*(C'\fR, \f(CW\*(C`AutoCommit\*(C'\fR, and other attributes. For example:
.PP
.Vb 4
\& $dbh = DBI\->connect($data_source, $user, $pass, {
\& PrintError => 0,
\& AutoCommit => 0
\& });
.Ve
.PP
The username and password can also be specified using the attributes
\&\f(CW\*(C`Username\*(C'\fR and \f(CW\*(C`Password\*(C'\fR, in which case they take precedence
over the \f(CW$username\fR and \f(CW$password\fR parameters.
.PP
You can also define connection attribute values within the \f(CW$data_source\fR
parameter. For example:
.PP
.Vb 1
\& dbi:DriverName(PrintWarn=>0,PrintError=>0,Taint=>1):...
.Ve
.PP
Individual attributes values specified in this way take precedence over
any conflicting values specified via the \f(CW\*(C`\e%attr\*(C'\fR parameter to \f(CW\*(C`connect\*(C'\fR.
.PP
The \f(CW\*(C`dbi_connect_method\*(C'\fR attribute can be used to specify which driver
method should be called to establish the connection. The only useful
values are 'connect', 'connect_cached', or some specialized case like
\&'Apache::DBI::connect' (which is automatically the default when running
within Apache).
.PP
Where possible, each session (\f(CW$dbh\fR) is independent from the transactions
in other sessions. This is useful when you need to hold cursors open
across transactions\-\-for example, if you use one session for your long lifespan
cursors (typically read-only) and another for your short update
transactions.
.PP
For compatibility with old DBI scripts, the driver can be specified by
passing its name as the fourth argument to \f(CW\*(C`connect\*(C'\fR (instead of \f(CW\*(C`\e%attr\*(C'\fR):
.PP
.Vb 1
\& $dbh = DBI\->connect($data_source, $user, $pass, $driver);
.Ve
.PP
In this "old-style" form of \f(CW\*(C`connect\*(C'\fR, the \f(CW$data_source\fR should not start
with "\f(CW\*(C`dbi:driver_name:\*(C'\fR". (If it does, the embedded driver_name
will be ignored). Also note that in this older form of \f(CW\*(C`connect\*(C'\fR,
the \f(CW\*(C`$dbh\->{AutoCommit}\*(C'\fR attribute is \fIundefined\fR, the
\&\f(CW\*(C`$dbh\->{PrintError}\*(C'\fR attribute is off, and the old \f(CW\*(C`DBI_DBNAME\*(C'\fR
environment variable is
checked if \f(CW\*(C`DBI_DSN\*(C'\fR is not defined. Beware that this "old-style"
\&\f(CW\*(C`connect\*(C'\fR will soon be withdrawn in a future version of DBI.
.PP
\fR\f(CI\*(C`connect_cached\*(C'\fR\fI\fR
.IX Subsection "connect_cached"
.PP
.Vb 4
\& $dbh = DBI\->connect_cached($data_source, $username, $password)
\& or die $DBI::errstr;
\& $dbh = DBI\->connect_cached($data_source, $username, $password, \e%attr)
\& or die $DBI::errstr;
.Ve
.PP
\&\f(CW\*(C`connect_cached\*(C'\fR is like "connect", except that the database handle
returned is also
stored in a hash associated with the given parameters. If another call
is made to \f(CW\*(C`connect_cached\*(C'\fR with the same parameter values, then the
corresponding cached \f(CW$dbh\fR will be returned if it is still valid.
The cached database handle is replaced with a new connection if it
has been disconnected or if the \f(CW\*(C`ping\*(C'\fR method fails.
.PP
Note that the behaviour of this method differs in several respects from the
behaviour of persistent connections implemented by Apache::DBI.
However, if Apache::DBI is loaded then \f(CW\*(C`connect_cached\*(C'\fR will use it.
.PP
Caching connections can be useful in some applications, but it can
also cause problems, such as too many connections, and so should
be used with care. In particular, avoid changing the attributes of
a database handle created via \fBconnect_cached()\fR because it will affect
other code that may be using the same handle. When \fBconnect_cached()\fR
returns a handle the attributes will be reset to their initial values.
This can cause problems, especially with the \f(CW\*(C`AutoCommit\*(C'\fR attribute.
.PP
Also, to ensure that the attributes passed are always the same, avoid passing
references inline. For example, the \f(CW\*(C`Callbacks\*(C'\fR attribute is specified as a
hash reference. Be sure to declare it external to the call to
\&\fBconnect_cached()\fR, such that the hash reference is not re-created on every
call. A package-level lexical works well:
.PP
.Vb 4
\& package MyDBH;
\& my $cb = {
\& \*(Aqconnect_cached.reused\*(Aq => sub { delete $_[4]\->{AutoCommit} },
\& };
\&
\& sub dbh {
\& DBI\->connect_cached( $dsn, $username, $auth, { Callbacks => $cb });
\& }
.Ve
.PP
Where multiple separate parts of a program are using \fBconnect_cached()\fR
to connect to the same database with the same (initial) attributes
it is a good idea to add a private attribute to the \fBconnect_cached()\fR
call to effectively limit the scope of the caching. For example:
.PP
.Vb 1
\& DBI\->connect_cached(..., { private_foo_cachekey => "Bar", ... });
.Ve
.PP
Handles returned from that \fBconnect_cached()\fR call will only be returned
by other \fBconnect_cached()\fR call elsewhere in the code if those other
calls also pass in the same attribute values, including the private one.
(I've used \f(CW\*(C`private_foo_cachekey\*(C'\fR here as an example, you can use
any attribute name with a \f(CW\*(C`private_\*(C'\fR prefix.)
.PP
Taking that one step further, you can limit a particular \fBconnect_cached()\fR
call to return handles unique to that one place in the code by setting the
private attribute to a unique value for that place:
.PP
.Vb 1
\& DBI\->connect_cached(..., { private_foo_cachekey => _\|_FILE_\|_._\|_LINE_\|_, ... });
.Ve
.PP
By using a private attribute you still get connection caching for
the individual calls to \fBconnect_cached()\fR but, by making separate
database connections for separate parts of the code, the database
handles are isolated from any attribute changes made to other handles.
.PP
The cache can be accessed (and cleared) via the "CachedKids" attribute:
.PP
.Vb 2
\& my $CachedKids_hashref = $dbh\->{Driver}\->{CachedKids};
\& %$CachedKids_hashref = () if $CachedKids_hashref;
.Ve
.PP
\fR\f(CI\*(C`available_drivers\*(C'\fR\fI\fR
.IX Subsection "available_drivers"
.PP
.Vb 2
\& @ary = DBI\->available_drivers;
\& @ary = DBI\->available_drivers($quiet);
.Ve
.PP
Returns a list of all available drivers by searching for \f(CW\*(C`DBD::*\*(C'\fR modules
through the directories in \f(CW@INC\fR. By default, a warning is given if
some drivers are hidden by others of the same name in earlier
directories. Passing a true value for \f(CW$quiet\fR will inhibit the warning.
.PP
\fR\f(CI\*(C`installed_drivers\*(C'\fR\fI\fR
.IX Subsection "installed_drivers"
.PP
.Vb 1
\& %drivers = DBI\->installed_drivers();
.Ve
.PP
Returns a list of driver name and driver handle pairs for all drivers
\&'installed' (loaded) into the current process. The driver name does not
include the 'DBD::' prefix.
.PP
To get a list of all drivers available in your perl installation you can use
"available_drivers".
.PP
Added in DBI 1.49.
.PP
\fR\f(CI\*(C`installed_versions\*(C'\fR\fI\fR
.IX Subsection "installed_versions"
.PP
.Vb 3
\& DBI\->installed_versions;
\& @ary = DBI\->installed_versions;
\& $hash = DBI\->installed_versions;
.Ve
.PP
Calls \fBavailable_drivers()\fR and attempts to load each of them in turn
using \fBinstall_driver()\fR. For each load that succeeds the driver
name and version number are added to a hash. When running under
DBI::PurePerl drivers which appear not be pure-perl are ignored.
.PP
When called in array context the list of successfully loaded drivers
is returned (without the 'DBD::' prefix).
.PP
When called in scalar context an extra entry for the \f(CW\*(C`DBI\*(C'\fR is added (and
\&\f(CW\*(C`DBI::PurePerl\*(C'\fR if appropriate) and a reference to the hash is returned.
.PP
When called in a void context the \fBinstalled_versions()\fR method will
print out a formatted list of the hash contents, one per line, along with some
other information about the DBI version and OS.
.PP
Due to the potentially high memory cost and unknown risks of loading
in an unknown number of drivers that just happen to be installed
on the system, this method is not recommended for general use.
Use \fBavailable_drivers()\fR instead.
.PP
The \fBinstalled_versions()\fR method is primarily intended as a quick
way to see from the command line what's installed. For example:
.PP
.Vb 1
\& perl \-MDBI \-e \*(AqDBI\->installed_versions\*(Aq
.Ve
.PP
The \fBinstalled_versions()\fR method was added in DBI 1.38.
.PP
\fR\f(CI\*(C`data_sources\*(C'\fR\fI\fR
.IX Subsection "data_sources"
.PP
.Vb 2
\& @ary = DBI\->data_sources($driver);
\& @ary = DBI\->data_sources($driver, \e%attr);
.Ve
.PP
Returns a list of data sources (databases) available via the named
driver. If \f(CW$driver\fR is empty or \f(CW\*(C`undef\*(C'\fR, then the value of the
\&\f(CW\*(C`DBI_DRIVER\*(C'\fR environment variable is used.
.PP
The driver will be loaded if it hasn't been already. Note that if the
driver loading fails then \fBdata_sources()\fR \fIdies\fR with an error message
that includes the string "\f(CW\*(C`install_driver\*(C'\fR" and the underlying problem.
.PP
Data sources are returned in a form suitable for passing to the
"connect" method (that is, they will include the "\f(CW\*(C`dbi:$driver:\*(C'\fR" prefix).
.PP
Note that many drivers have no way of knowing what data sources might
be available for it. These drivers return an empty or incomplete list
or may require driver-specific attributes.
.PP
There is also a \fBdata_sources()\fR method defined for database handles.
.PP
\fR\f(CI\*(C`trace\*(C'\fR\fI\fR
.IX Subsection "trace"
.PP
.Vb 4
\& DBI\->trace($trace_setting)
\& DBI\->trace($trace_setting, $trace_filename)
\& DBI\->trace($trace_setting, $trace_filehandle)
\& $trace_setting = DBI\->trace;
.Ve
.PP
The \f(CW\*(C`DBI\->trace\*(C'\fR method sets the \fIglobal default\fR trace
settings and returns the \fIprevious\fR trace settings. It can also
be used to change where the trace output is sent.
.PP
There's a similar method, \f(CW\*(C`$h\->trace\*(C'\fR, which sets the trace
settings for the specific handle it's called on.
.PP
See the "TRACING" section for full details about the DBI's powerful
tracing facilities.
.PP
\fR\f(CI\*(C`visit_handles\*(C'\fR\fI\fR
.IX Subsection "visit_handles"
.PP
.Vb 2
\& DBI\->visit_handles( $coderef );
\& DBI\->visit_handles( $coderef, $info );
.Ve
.PP
Where \f(CW$coderef\fR is a reference to a subroutine and \f(CW$info\fR is an arbitrary value
which, if undefined, defaults to a reference to an empty hash. Returns \f(CW$info\fR.
.PP
For each installed driver handle, if any, \f(CW$coderef\fR is invoked as:
.PP
.Vb 1
\& $coderef\->($driver_handle, $info);
.Ve
.PP
If the execution of \f(CW$coderef\fR returns a true value then "visit_child_handles"
is called on that child handle and passed the returned value as \f(CW$info\fR.
.PP
For example:
.PP
.Vb 5
\& my $info = $dbh\->{Driver}\->visit_child_handles(sub {
\& my ($h, $info) = @_;
\& ++$info\->{ $h\->{Type} }; # count types of handles (dr/db/st)
\& return $info; # visit kids
\& });
.Ve
.PP
See also "visit_child_handles".
.SS "DBI Utility Functions"
.IX Subsection "DBI Utility Functions"
In addition to the DBI methods listed in the previous section,
the DBI package also provides several utility functions.
.PP
These can be imported into your code by listing them in
the \f(CW\*(C`use\*(C'\fR statement. For example:
.PP
.Vb 1
\& use DBI qw(neat data_diff);
.Ve
.PP
Alternatively, all these utility functions (except hash) can be
imported using the \f(CW\*(C`:utils\*(C'\fR import tag. For example:
.PP
.Vb 1
\& use DBI qw(:utils);
.Ve
.PP
\fR\f(CI\*(C`data_string_desc\*(C'\fR\fI\fR
.IX Subsection "data_string_desc"
.PP
.Vb 1
\& $description = data_string_desc($string);
.Ve
.PP
Returns an informal description of the string. For example:
.PP
.Vb 5
\& UTF8 off, ASCII, 42 characters 42 bytes
\& UTF8 off, non\-ASCII, 42 characters 42 bytes
\& UTF8 on, non\-ASCII, 4 characters 6 bytes
\& UTF8 on but INVALID encoding, non\-ASCII, 4 characters 6 bytes
\& UTF8 off, undef
.Ve
.PP
The initial \f(CW\*(C`UTF8\*(C'\fR on/off refers to Perl's internal SvUTF8 flag.
If \f(CW$string\fR has the SvUTF8 flag set but the sequence of bytes it
contains are not a valid UTF\-8 encoding then \fBdata_string_desc()\fR
will report \f(CW\*(C`UTF8 on but INVALID encoding\*(C'\fR.
.PP
The \f(CW\*(C`ASCII\*(C'\fR vs \f(CW\*(C`non\-ASCII\*(C'\fR portion shows \f(CW\*(C`ASCII\*(C'\fR if \fIall\fR the
characters in the string are ASCII (have code points <= 127).
.PP
The \fBdata_string_desc()\fR function was added in DBI 1.46.
.PP
\fR\f(CI\*(C`data_string_diff\*(C'\fR\fI\fR
.IX Subsection "data_string_diff"
.PP
.Vb 1
\& $diff = data_string_diff($a, $b);
.Ve
.PP
Returns an informal description of the first character difference
between the strings. If both \f(CW$a\fR and \f(CW$b\fR contain the same sequence
of characters then \fBdata_string_diff()\fR returns an empty string.
For example:
.PP
.Vb 6
\& Params a & b Result
\& \-\-\-\-\-\-\-\-\-\-\-\- \-\-\-\-\-\-
\& \*(Aqaaa\*(Aq, \*(Aqaaa\*(Aq \*(Aq\*(Aq
\& \*(Aqaaa\*(Aq, \*(Aqabc\*(Aq \*(AqStrings differ at index 2: a[2]=a, b[2]=b\*(Aq
\& \*(Aqaaa\*(Aq, undef \*(AqString b is undef, string a has 3 characters\*(Aq
\& \*(Aqaaa\*(Aq, \*(Aqaa\*(Aq \*(AqString b truncated after 2 characters\*(Aq
.Ve
.PP
Unicode characters are reported in \f(CW\*(C`\ex{XXXX}\*(C'\fR format. Unicode
code points in the range U+0800 to U+08FF are unassigned and most
likely to occur due to double-encoding. Characters in this range
are reported as \f(CW\*(C`\ex{08XX}=\*(AqC\*(Aq\*(C'\fR where \f(CW\*(C`C\*(C'\fR is the corresponding
latin\-1 character.
.PP
The \fBdata_string_diff()\fR function only considers logical \fIcharacters\fR
and not the underlying encoding. See "data_diff" for an alternative.
.PP
The \fBdata_string_diff()\fR function was added in DBI 1.46.
.PP
\fR\f(CI\*(C`data_diff\*(C'\fR\fI\fR
.IX Subsection "data_diff"
.PP
.Vb 2
\& $diff = data_diff($a, $b);
\& $diff = data_diff($a, $b, $logical);
.Ve
.PP
Returns an informal description of the difference between two strings.
It calls "data_string_desc" and "data_string_diff"
and returns the combined results as a multi-line string.
.PP
For example, \f(CW\*(C`data_diff("abc", "ab\ex{263a}")\*(C'\fR will return:
.PP
.Vb 3
\& a: UTF8 off, ASCII, 3 characters 3 bytes
\& b: UTF8 on, non\-ASCII, 3 characters 5 bytes
\& Strings differ at index 2: a[2]=c, b[2]=\ex{263A}
.Ve
.PP
If \f(CW$a\fR and \f(CW$b\fR are identical in both the characters they contain \fIand\fR
their physical encoding then \fBdata_diff()\fR returns an empty string.
If \f(CW$logical\fR is true then physical encoding differences are ignored
(but are still reported if there is a difference in the characters).
.PP
The \fBdata_diff()\fR function was added in DBI 1.46.
.PP
\fR\f(CI\*(C`neat\*(C'\fR\fI\fR
.IX Subsection "neat"
.PP
.Vb 2
\& $str = neat($value);
\& $str = neat($value, $maxlen);
.Ve
.PP
Return a string containing a neat (and tidy) representation of the
supplied value.
.PP
Strings will be quoted, although internal quotes will \fInot\fR be escaped.
Values known to be numeric will be unquoted. Undefined (NULL) values
will be shown as \f(CW\*(C`undef\*(C'\fR (without quotes).
.PP
If the string is flagged internally as utf8 then double quotes will
be used, otherwise single quotes are used and unprintable characters
will be replaced by dot (.).
.PP
For result strings longer than \f(CW$maxlen\fR the result string will be
truncated to \f(CW\*(C`$maxlen\-4\*(C'\fR and "\f(CW\*(C`...\*(Aq\*(C'\fR" will be appended. If \f(CW$maxlen\fR is 0
or \f(CW\*(C`undef\*(C'\fR, it defaults to \f(CW$DBI::neat_maxlen\fR which, in turn, defaults to 400.
.PP
This function is designed to format values for human consumption.
It is used internally by the DBI for "trace" output. It should
typically \fInot\fR be used for formatting values for database use.
(See also "quote".)
.PP
\fR\f(CI\*(C`neat_list\*(C'\fR\fI\fR
.IX Subsection "neat_list"
.PP
.Vb 1
\& $str = neat_list(\e@listref, $maxlen, $field_sep);
.Ve
.PP
Calls \f(CW\*(C`neat\*(C'\fR on each element of the list and returns a string
containing the results joined with \f(CW$field_sep\fR. \f(CW$field_sep\fR defaults
to \f(CW", "\fR.
.PP
\fR\f(CI\*(C`looks_like_number\*(C'\fR\fI\fR
.IX Subsection "looks_like_number"
.PP
.Vb 1
\& @bool = looks_like_number(@array);
.Ve
.PP
Returns true for each element that looks like a number.
Returns false for each element that does not look like a number.
Returns \f(CW\*(C`undef\*(C'\fR for each element that is undefined or empty.
.PP
\fR\f(CI\*(C`hash\*(C'\fR\fI\fR
.IX Subsection "hash"
.PP
.Vb 1
\& $hash_value = DBI::hash($buffer, $type);
.Ve
.PP
Return a 32\-bit integer 'hash' value corresponding to the contents of \f(CW$buffer\fR.
The \f(CW$type\fR parameter selects which kind of hash algorithm should be used.
.PP
For the technically curious, type 0 (which is the default if \f(CW$type\fR
isn't specified) is based on the Perl 5.1 hash except that the value
is forced to be negative (for obscure historical reasons).
Type 1 is the better "Fowler / Noll / Vo" (FNV) hash. See
for more information.
Both types are implemented in C and are very fast.
.PP
This function doesn't have much to do with databases, except that
it can sometimes be handy to store such values in a database.
It also doesn't have much to do with perl hashes, like \f(CW%foo\fR.
.PP
\fR\f(CI\*(C`sql_type_cast\*(C'\fR\fI\fR
.IX Subsection "sql_type_cast"
.PP
.Vb 1
\& $sts = DBI::sql_type_cast($sv, $sql_type, $flags);
.Ve
.PP
sql_type_cast attempts to cast \f(CW$sv\fR to the SQL type (see DBI
Constants) specified in \f(CW$sql_type\fR. At present only the SQL types
\&\f(CW\*(C`SQL_INTEGER\*(C'\fR, \f(CW\*(C`SQL_DOUBLE\*(C'\fR and \f(CW\*(C`SQL_NUMERIC\*(C'\fR are supported.
.PP
For \f(CW\*(C`SQL_INTEGER\*(C'\fR the effect is similar to using the value in an expression
that requires an integer. It gives the perl scalar an 'integer aspect'.
(Technically the value gains an IV, or possibly a UV or NV if the value is too
large for an IV.)
.PP
For \f(CW\*(C`SQL_DOUBLE\*(C'\fR the effect is similar to using the value in an expression
that requires a general numeric value. It gives the perl scalar a 'numeric
aspect'. (Technically the value gains an NV.)
.PP
\&\f(CW\*(C`SQL_NUMERIC\*(C'\fR is similar to \f(CW\*(C`SQL_INTEGER\*(C'\fR or \f(CW\*(C`SQL_DOUBLE\*(C'\fR but more
general and more cautious. It will look at the string first and if it
looks like an integer (that will fit in an IV or UV) it will act like
\&\f(CW\*(C`SQL_INTEGER\*(C'\fR, if it looks like a floating point value it will act
like \f(CW\*(C`SQL_DOUBLE\*(C'\fR, if it looks like neither then it will do nothing \-
and thereby avoid the warnings that would be generated by
\&\f(CW\*(C`SQL_INTEGER\*(C'\fR and \f(CW\*(C`SQL_DOUBLE\*(C'\fR when given non-numeric data.
.PP
\&\f(CW$flags\fR may be:
.ie n .IP """DBIstcf_DISCARD_STRING""" 4
.el .IP \f(CWDBIstcf_DISCARD_STRING\fR 4
.IX Item "DBIstcf_DISCARD_STRING"
If this flag is specified then when the driver successfully casts the
bound perl scalar to a non-string type then the string portion of the
scalar will be discarded.
.ie n .IP """DBIstcf_STRICT""" 4
.el .IP \f(CWDBIstcf_STRICT\fR 4
.IX Item "DBIstcf_STRICT"
If \f(CW$sv\fR cannot be cast to the requested \f(CW$sql_type\fR then by default
it is left untouched and no error is generated. If you specify
\&\f(CW\*(C`DBIstcf_STRICT\*(C'\fR and the cast fails, this will generate an error.
.PP
The returned \f(CW$sts\fR value is:
.PP
.Vb 5
\& \-2 sql_type is not handled
\& \-1 sv is undef so unchanged
\& 0 sv could not be cast cleanly and DBIstcf_STRICT was used
\& 1 sv could not be cast and DBIstcf_STRICT was not used
\& 2 sv was cast successfully
.Ve
.PP
This method is exported by the :utils tag and was introduced in DBI
1.611.
.SS "DBI Dynamic Attributes"
.IX Subsection "DBI Dynamic Attributes"
Dynamic attributes are always associated with the \fIlast handle used\fR
(that handle is represented by \f(CW$h\fR in the descriptions below).
.PP
Where an attribute is equivalent to a method call, then refer to
the method call for all related documentation.
.PP
Warning: these attributes are provided as a convenience but they
do have limitations. Specifically, they have a short lifespan:
because they are associated with
the last handle used, they should only be used \fIimmediately\fR after
calling the method that "sets" them.
If in any doubt, use the corresponding method call.
.PP
\fR\f(CI$DBI::err\fR\fI\fR
.IX Subsection "$DBI::err"
.PP
Equivalent to \f(CW\*(C`$h\->err\*(C'\fR.
.PP
\fR\f(CI$DBI::errstr\fR\fI\fR
.IX Subsection "$DBI::errstr"
.PP
Equivalent to \f(CW\*(C`$h\->errstr\*(C'\fR.
.PP
\fR\f(CI$DBI::state\fR\fI\fR
.IX Subsection "$DBI::state"
.PP
Equivalent to \f(CW\*(C`$h\->state\*(C'\fR.
.PP
\fR\f(CI$DBI::rows\fR\fI\fR
.IX Subsection "$DBI::rows"
.PP
Equivalent to \f(CW\*(C`$h\->rows\*(C'\fR. Please refer to the documentation
for the "rows" method.
.PP
\fR\f(CI$DBI::lasth\fR\fI\fR
.IX Subsection "$DBI::lasth"
.PP
Returns the DBI object handle used for the most recent DBI method call.
If the last DBI method call was a DESTROY then \f(CW$DBI::lasth\fR will return
the handle of the parent of the destroyed handle, if there is one.
.SH "METHODS COMMON TO ALL HANDLES"
.IX Header "METHODS COMMON TO ALL HANDLES"
The following methods can be used by all types of DBI handles.
.PP
\fR\f(CI\*(C`err\*(C'\fR\fI\fR
.IX Subsection "err"
.PP
.Vb 1
\& $rv = $h\->err;
.Ve
.PP
Returns the \fInative\fR database engine error code from the last driver
method called. The code is typically an integer but you should not
assume that.
.PP
The DBI resets \f(CW$h\fR\->err to undef before almost all DBI method calls, so the
value only has a short lifespan. Also, for most drivers, the statement
handles share the same error variable as the parent database handle,
so calling a method on one handle may reset the error on the
related handles.
.PP
(Methods which don't reset err before being called include \fBerr()\fR and \fBerrstr()\fR,
obviously, \fBstate()\fR, \fBrows()\fR, \fBfunc()\fR, \fBtrace()\fR, \fBtrace_msg()\fR, \fBping()\fR, and the
tied hash attribute \fBFETCH()\fR and \fBSTORE()\fR methods.)
.PP
If you need to test for specific error conditions \fIand\fR have your program be
portable to different database engines, then you'll need to determine what the
corresponding error codes are for all those engines and test for all of them.
.PP
The DBI uses the value of \f(CW$DBI::stderr\fR as the \f(CW\*(C`err\*(C'\fR value for internal errors.
Drivers should also do likewise. The default value for \f(CW$DBI::stderr\fR is 2000000000.
.PP
A driver may return \f(CW0\fR from \fBerr()\fR to indicate a warning condition
after a method call. Similarly, a driver may return an empty string
to indicate a 'success with information' condition. In both these
cases the value is false but not undef. The \fBerrstr()\fR and \fBstate()\fR
methods may be used to retrieve extra information in these cases.
.PP
See "set_err" for more information.
.PP
\fR\f(CI\*(C`errstr\*(C'\fR\fI\fR
.IX Subsection "errstr"
.PP
.Vb 1
\& $str = $h\->errstr;
.Ve
.PP
Returns the native database engine error message from the last DBI
method called. This has the same lifespan issues as the "err" method
described above.
.PP
The returned string may contain multiple messages separated by
newline characters.
.PP
The \fBerrstr()\fR method should not be used to test for errors, use \fBerr()\fR
for that, because drivers may return 'success with information' or
warning messages via \fBerrstr()\fR for methods that have not 'failed'.
.PP
See "set_err" for more information.
.PP
\fR\f(CI\*(C`state\*(C'\fR\fI\fR
.IX Subsection "state"
.PP
.Vb 1
\& $str = $h\->state;
.Ve
.PP
Returns a state code in the standard SQLSTATE five character format.
Note that the specific success code \f(CW00000\fR is translated to any empty string
(false). If the driver does not support SQLSTATE (and most don't),
then \fBstate()\fR will return \f(CW\*(C`S1000\*(C'\fR (General Error) for all errors.
.PP
The driver is free to return any value via \f(CW\*(C`state\*(C'\fR, e.g., warning
codes, even if it has not declared an error by returning a true value
via the "err" method described above.
.PP
The \fBstate()\fR method should not be used to test for errors, use \fBerr()\fR
for that, because drivers may return a 'success with information' or
warning state code via \fBstate()\fR for methods that have not 'failed'.
.PP
\fR\f(CI\*(C`set_err\*(C'\fR\fI\fR
.IX Subsection "set_err"
.PP
.Vb 4
\& $rv = $h\->set_err($err, $errstr);
\& $rv = $h\->set_err($err, $errstr, $state);
\& $rv = $h\->set_err($err, $errstr, $state, $method);
\& $rv = $h\->set_err($err, $errstr, $state, $method, $rv);
.Ve
.PP
Set the \f(CW\*(C`err\*(C'\fR, \f(CW\*(C`errstr\*(C'\fR, and \f(CW\*(C`state\*(C'\fR values for the handle.
This method is typically only used by DBI drivers and DBI subclasses.
.PP
If the "HandleSetErr" attribute holds a reference to a subroutine
it is called first. The subroutine can alter the \f(CW$err\fR, \f(CW$errstr\fR, \f(CW$state\fR,
and \f(CW$method\fR values. See "HandleSetErr" for full details.
If the subroutine returns a true value then the handle \f(CW\*(C`err\*(C'\fR,
\&\f(CW\*(C`errstr\*(C'\fR, and \f(CW\*(C`state\*(C'\fR values are not altered and \fBset_err()\fR returns
an empty list (it normally returns \f(CW$rv\fR which defaults to undef, see below).
.PP
Setting \f(CW\*(C`err\*(C'\fR to a \fItrue\fR value indicates an error and will trigger
the normal DBI error handling mechanisms, such as \f(CW\*(C`RaiseError\*(C'\fR and
\&\f(CW\*(C`HandleError\*(C'\fR, if they are enabled, when execution returns from
the DBI back to the application.
.PP
Setting \f(CW\*(C`err\*(C'\fR to \f(CW""\fR indicates an 'information' state, and setting
it to \f(CW"0"\fR indicates a 'warning' state. Setting \f(CW\*(C`err\*(C'\fR to \f(CW\*(C`undef\*(C'\fR
also sets \f(CW\*(C`errstr\*(C'\fR to undef, and \f(CW\*(C`state\*(C'\fR to \f(CW""\fR, irrespective
of the values of the \f(CW$errstr\fR and \f(CW$state\fR parameters.
.PP
The \f(CW$method\fR parameter provides an alternate method name for the
\&\f(CW\*(C`RaiseError\*(C'\fR/\f(CW\*(C`PrintError\*(C'\fR/\f(CW\*(C`RaiseWarn\*(C'\fR/\f(CW\*(C`PrintWarn\*(C'\fR error string instead of
the fairly unhelpful '\f(CW\*(C`set_err\*(C'\fR'.
.PP
The \f(CW\*(C`set_err\*(C'\fR method normally returns undef. The \f(CW$rv\fR parameter
provides an alternate return value.
.PP
Some special rules apply if the \f(CW\*(C`err\*(C'\fR or \f(CW\*(C`errstr\*(C'\fR
values for the handle are \fIalready\fR set...
.PP
If \f(CW\*(C`errstr\*(C'\fR is true then: "\f(CW\*(C` [err was %s now %s]\*(C'\fR" is appended if \f(CW$err\fR is
true and \f(CW\*(C`err\*(C'\fR is already true and the new err value differs from the original
one. Similarly "\f(CW\*(C` [state was %s now %s]\*(C'\fR" is appended if \f(CW$state\fR is true and \f(CW\*(C`state\*(C'\fR is
already true and the new state value differs from the original one. Finally
"\f(CW\*(C`\en\*(C'\fR" and the new \f(CW$errstr\fR are appended if \f(CW$errstr\fR differs from the existing
errstr value. Obviously the \f(CW%s\fR's above are replaced by the corresponding values.
.PP
The handle \f(CW\*(C`err\*(C'\fR value is set to \f(CW$err\fR if: \f(CW$err\fR is true; or handle
\&\f(CW\*(C`err\*(C'\fR value is undef; or \f(CW$err\fR is defined and the length is greater
than the handle \f(CW\*(C`err\*(C'\fR length. The effect is that an 'information'
state only overrides undef; a 'warning' overrides undef or 'information',
and an 'error' state overrides anything.
.PP
The handle \f(CW\*(C`state\*(C'\fR value is set to \f(CW$state\fR if \f(CW$state\fR is true and
the handle \f(CW\*(C`err\*(C'\fR value was set (by the rules above).
.PP
Support for warning and information states was added in DBI 1.41.
.PP
\fR\f(CI\*(C`trace\*(C'\fR\fI\fR
.IX Subsection "trace"
.PP
.Vb 3
\& $h\->trace($trace_settings);
\& $h\->trace($trace_settings, $trace_filename);
\& $trace_settings = $h\->trace;
.Ve
.PP
The \fBtrace()\fR method is used to alter the trace settings for a handle
(and any future children of that handle). It can also be used to
change where the trace output is sent.
.PP
There's a similar method, \f(CW\*(C`DBI\->trace\*(C'\fR, which sets the global
default trace settings.
.PP
See the "TRACING" section for full details about the DBI's powerful
tracing facilities.
.PP
\fR\f(CI\*(C`trace_msg\*(C'\fR\fI\fR
.IX Subsection "trace_msg"
.PP
.Vb 2
\& $h\->trace_msg($message_text);
\& $h\->trace_msg($message_text, $min_level);
.Ve
.PP
Writes \f(CW$message_text\fR to the trace file if the trace level is
greater than or equal to \f(CW$min_level\fR (which defaults to 1).
Can also be called as \f(CW\*(C`DBI\->trace_msg($msg)\*(C'\fR.
.PP
See "TRACING" for more details.
.PP
\fR\f(CI\*(C`func\*(C'\fR\fI\fR
.IX Subsection "func"
.PP
.Vb 1
\& $h\->func(@func_arguments, $func_name) or die ...;
.Ve
.PP
The \f(CW\*(C`func\*(C'\fR method can be used to call private non-standard and
non-portable methods implemented by the driver. Note that the function
name is given as the \fIlast\fR argument.
.PP
It's also important to note that the \fBfunc()\fR method does not clear
a previous error ($DBI::err etc.) and it does not trigger automatic
error detection (RaiseError etc.) so you must check the return
status and/or \f(CW$h\fR\->err to detect errors.
.PP
(This method is not directly related to calling stored procedures.
Calling stored procedures is currently not defined by the DBI.
Some drivers, such as DBD::Oracle, support it in non-portable ways.
See driver documentation for more details.)
.PP
See also \fBinstall_method()\fR in DBI::DBD for how you can avoid needing to
use \fBfunc()\fR and gain direct access to driver-private methods.
.PP
\fR\f(CI\*(C`can\*(C'\fR\fI\fR
.IX Subsection "can"
.PP
.Vb 1
\& $is_implemented = $h\->can($method_name);
.Ve
.PP
Returns true if \f(CW$method_name\fR is implemented by the driver or a
default method is provided by the DBI's driver base class.
It returns false where a driver hasn't implemented a method and the
default method is provided by the DBI's driver base class is just an empty stub.
.PP
\fR\f(CI\*(C`parse_trace_flags\*(C'\fR\fI\fR
.IX Subsection "parse_trace_flags"
.PP
.Vb 1
\& $trace_settings_integer = $h\->parse_trace_flags($trace_settings);
.Ve
.PP
Parses a string containing trace settings and returns the corresponding
integer value used internally by the DBI and drivers.
.PP
The \f(CW$trace_settings\fR argument is a string containing a trace level
between 0 and 15 and/or trace flag names separated by vertical bar
("\f(CW\*(C`|\*(C'\fR") or comma ("\f(CW\*(C`,\*(C'\fR") characters. For example: \f(CW"SQL|3|foo"\fR.
.PP
It uses the \fBparse_trace_flag()\fR method, described below, to process
the individual trace flag names.
.PP
The \fBparse_trace_flags()\fR method was added in DBI 1.42.
.PP
\fR\f(CI\*(C`parse_trace_flag\*(C'\fR\fI\fR
.IX Subsection "parse_trace_flag"
.PP
.Vb 1
\& $bit_flag = $h\->parse_trace_flag($trace_flag_name);
.Ve
.PP
Returns the bit flag corresponding to the trace flag name in
\&\f(CW$trace_flag_name\fR. Drivers are expected to override this method and
check if \f(CW$trace_flag_name\fR is a driver specific trace flags and, if
not, then call the DBI's default \fBparse_trace_flag()\fR.
.PP
The \fBparse_trace_flag()\fR method was added in DBI 1.42.
.PP
\fR\f(CI\*(C`private_attribute_info\*(C'\fR\fI\fR
.IX Subsection "private_attribute_info"
.PP
.Vb 1
\& $hash_ref = $h\->private_attribute_info();
.Ve
.PP
Returns a reference to a hash whose keys are the names of driver-private
handle attributes available for the kind of handle (driver, database, statement)
that the method was called on.
.PP
For example, the return value when called with a DBD::Sybase \f(CW$dbh\fR could look like this:
.PP
.Vb 6
\& {
\& syb_dynamic_supported => undef,
\& syb_oc_version => undef,
\& syb_server_version => undef,
\& syb_server_version_string => undef,
\& }
.Ve
.PP
and when called with a DBD::Sybase \f(CW$sth\fR they could look like this:
.PP
.Vb 5
\& {
\& syb_types => undef,
\& syb_proc_status => undef,
\& syb_result_type => undef,
\& }
.Ve
.PP
The values should be undef. Meanings may be assigned to particular values in future.
.PP
\fR\f(CI\*(C`swap_inner_handle\*(C'\fR\fI\fR
.IX Subsection "swap_inner_handle"
.PP
.Vb 2
\& $rc = $h1\->swap_inner_handle( $h2 );
\& $rc = $h1\->swap_inner_handle( $h2, $allow_reparent );
.Ve
.PP
Brain transplants for handles. You don't need to know about this
unless you want to become a handle surgeon.
.PP
A DBI handle is a reference to a tied hash. A tied hash has an
\&\fIinner\fR hash that actually holds the contents. The \fBswap_inner_handle()\fR
method swaps the inner hashes between two handles. The \f(CW$h1\fR and \f(CW$h2\fR
handles still point to the same tied hashes, but what those hashes
are tied to has been swapped. In effect \f(CW$h1\fR \fIbecomes\fR \f(CW$h2\fR and
vice-versa. This is powerful stuff, expect problems. Use with care.
.PP
As a small safety measure, the two handles, \f(CW$h1\fR and \f(CW$h2\fR, have to
share the same parent unless \f(CW$allow_reparent\fR is true.
.PP
The \fBswap_inner_handle()\fR method was added in DBI 1.44.
.PP
Here's a quick kind of 'diagram' as a worked example to help think about what's
happening:
.PP
.Vb 4
\& Original state:
\& dbh1o \-> dbh1i
\& sthAo \-> sthAi(dbh1i)
\& dbh2o \-> dbh2i
\&
\& swap_inner_handle dbh1o with dbh2o:
\& dbh2o \-> dbh1i
\& sthAo \-> sthAi(dbh1i)
\& dbh1o \-> dbh2i
\&
\& create new sth from dbh1o:
\& dbh2o \-> dbh1i
\& sthAo \-> sthAi(dbh1i)
\& dbh1o \-> dbh2i
\& sthBo \-> sthBi(dbh2i)
\&
\& swap_inner_handle sthAo with sthBo:
\& dbh2o \-> dbh1i
\& sthBo \-> sthAi(dbh1i)
\& dbh1o \-> dbh2i
\& sthAo \-> sthBi(dbh2i)
.Ve
.PP
\fR\f(CI\*(C`visit_child_handles\*(C'\fR\fI\fR
.IX Subsection "visit_child_handles"
.PP
.Vb 2
\& $h\->visit_child_handles( $coderef );
\& $h\->visit_child_handles( $coderef, $info );
.Ve
.PP
Where \f(CW$coderef\fR is a reference to a subroutine and \f(CW$info\fR is an arbitrary value
which, if undefined, defaults to a reference to an empty hash. Returns \f(CW$info\fR.
.PP
For each child handle of \f(CW$h\fR, if any, \f(CW$coderef\fR is invoked as:
.PP
.Vb 1
\& $coderef\->($child_handle, $info);
.Ve
.PP
If the execution of \f(CW$coderef\fR returns a true value then \f(CW\*(C`visit_child_handles\*(C'\fR
is called on that child handle and passed the returned value as \f(CW$info\fR.
.PP
For example:
.PP
.Vb 7
\& # count database connections with names (DSN) matching a pattern
\& my $connections = 0;
\& $dbh\->{Driver}\->visit_child_handles(sub {
\& my ($h, $info) = @_;
\& ++$connections if $h\->{Name} =~ /foo/;
\& return 0; # don\*(Aqt visit kids
\& })
.Ve
.PP
See also "visit_handles".
.SH "ATTRIBUTES COMMON TO ALL HANDLES"
.IX Header "ATTRIBUTES COMMON TO ALL HANDLES"
These attributes are common to all types of DBI handles.
.PP
Some attributes are inherited by child handles. That is, the value
of an inherited attribute in a newly created statement handle is the
same as the value in the parent database handle. Changes to attributes
in the new statement handle do not affect the parent database handle
and changes to the database handle do not affect existing statement
handles, only future ones.
.PP
Attempting to set or get the value of an unknown attribute generates a warning,
except for private driver specific attributes (which all have names
starting with a lowercase letter).
.PP
Example:
.PP
.Vb 2
\& $h\->{AttributeName} = ...; # set/write
\& ... = $h\->{AttributeName}; # get/read
.Ve
.PP
\fR\f(CI\*(C`Warn\*(C'\fR\fI\fR
.IX Subsection "Warn"
.PP
Type: boolean, inherited
.PP
The \f(CW\*(C`Warn\*(C'\fR attribute enables useful warnings for certain bad
practices. It is enabled by default and should only be disabled in
rare circumstances. Since warnings are generated using the Perl
\&\f(CW\*(C`warn\*(C'\fR function, they can be intercepted using the Perl \f(CW$SIG{_\|_WARN_\|_}\fR
hook.
.PP
The \f(CW\*(C`Warn\*(C'\fR attribute is not related to the \f(CW\*(C`PrintWarn\*(C'\fR attribute.
.PP
\fR\f(CI\*(C`Active\*(C'\fR\fI\fR
.IX Subsection "Active"
.PP
Type: boolean, read-only
.PP
The \f(CW\*(C`Active\*(C'\fR attribute is true if the handle object is "active". This is rarely used in
applications. The exact meaning of active is somewhat vague at the
moment. For a database handle it typically means that the handle is
connected to a database (\f(CW\*(C`$dbh\->disconnect\*(C'\fR sets \f(CW\*(C`Active\*(C'\fR off). For
a statement handle it typically means that the handle is a \f(CW\*(C`SELECT\*(C'\fR
that may have more data to fetch. (Fetching all the data or calling \f(CW\*(C`$sth\->finish\*(C'\fR
sets \f(CW\*(C`Active\*(C'\fR off.)
.PP
\fR\f(CI\*(C`Executed\*(C'\fR\fI\fR
.IX Subsection "Executed"
.PP
Type: boolean
.PP
The \f(CW\*(C`Executed\*(C'\fR attribute is true if the handle object has been "executed".
Currently only the \f(CW$dbh\fR \fBdo()\fR method and the \f(CW$sth\fR \fBexecute()\fR, \fBexecute_array()\fR,
and \fBexecute_for_fetch()\fR methods set the \f(CW\*(C`Executed\*(C'\fR attribute.
.PP
When it's set on a handle it is also set on the parent handle at the
same time. So calling \fBexecute()\fR on a \f(CW$sth\fR also sets the \f(CW\*(C`Executed\*(C'\fR
attribute on the parent \f(CW$dbh\fR.
.PP
The \f(CW\*(C`Executed\*(C'\fR attribute for a database handle is cleared by the \fBcommit()\fR and
\&\fBrollback()\fR methods (even if they fail). The \f(CW\*(C`Executed\*(C'\fR attribute of a
statement handle is not cleared by the DBI under any circumstances and so acts
as a permanent record of whether the statement handle was ever used.
.PP
The \f(CW\*(C`Executed\*(C'\fR attribute was added in DBI 1.41.
.PP
\fR\f(CI\*(C`Kids\*(C'\fR\fI\fR
.IX Subsection "Kids"
.PP
Type: integer, read-only
.PP
For a driver handle, \f(CW\*(C`Kids\*(C'\fR is the number of currently existing database
handles that were created from that driver handle. For a database
handle, \f(CW\*(C`Kids\*(C'\fR is the number of currently existing statement handles that
were created from that database handle.
For a statement handle, the value is zero.
.PP
\fR\f(CI\*(C`ActiveKids\*(C'\fR\fI\fR
.IX Subsection "ActiveKids"
.PP
Type: integer, read-only
.PP
Like \f(CW\*(C`Kids\*(C'\fR, but only counting those that are \f(CW\*(C`Active\*(C'\fR (as above).
.PP
\fR\f(CI\*(C`CachedKids\*(C'\fR\fI\fR
.IX Subsection "CachedKids"
.PP
Type: hash ref
.PP
For a database handle, \f(CW\*(C`CachedKids\*(C'\fR returns a reference to the cache (hash) of
statement handles created by the "prepare_cached" method. For a
driver handle, returns a reference to the cache (hash) of
database handles created by the "connect_cached" method.
.PP
\fR\f(CI\*(C`Type\*(C'\fR\fI\fR
.IX Subsection "Type"
.PP
Type: scalar, read-only
.PP
The \f(CW\*(C`Type\*(C'\fR attribute identifies the type of a DBI handle. Returns
"dr" for driver handles, "db" for database handles and "st" for
statement handles.
.PP
\fR\f(CI\*(C`ChildHandles\*(C'\fR\fI\fR
.IX Subsection "ChildHandles"
.PP
Type: array ref
.PP
The ChildHandles attribute contains a reference to an array of all the
handles created by this handle which are still accessible. The
contents of the array are weak-refs and will become undef when the
handle goes out of scope. (They're cleared out occasionally.)
.PP
\&\f(CW\*(C`ChildHandles\*(C'\fR returns undef if your perl version does not support weak
references (check the Scalar::Util module). The referenced
array returned should be treated as read-only.
.PP
For example, to enumerate all driver handles, database handles and
statement handles:
.PP
.Vb 6
\& sub show_child_handles {
\& my ($h, $level) = @_;
\& printf "%sh %s %s\en", $h\->{Type}, "\et" x $level, $h;
\& show_child_handles($_, $level + 1)
\& for (grep { defined } @{$h\->{ChildHandles}});
\& }
\&
\& my %drivers = DBI\->installed_drivers();
\& show_child_handles($_, 0) for (values %drivers);
.Ve
.PP
\fR\f(CI\*(C`CompatMode\*(C'\fR\fI\fR
.IX Subsection "CompatMode"
.PP
Type: boolean, inherited
.PP
The \f(CW\*(C`CompatMode\*(C'\fR attribute is used by emulation layers (such as
Oraperl) to enable compatible behaviour in the underlying driver
(e.g., DBD::Oracle) for this handle. Not normally set by application code.
.PP
It also has the effect of disabling the 'quick FETCH' of attribute
values from the handles attribute cache. So all attribute values
are handled by the drivers own FETCH method. This makes them slightly
slower but is useful for special-purpose drivers like DBD::Multiplex.
.PP
\fR\f(CI\*(C`InactiveDestroy\*(C'\fR\fI\fR
.IX Subsection "InactiveDestroy"
.PP
Type: boolean
.PP
The default value, false, means a handle will be fully destroyed
as normal when the last reference to it is removed, just as you'd expect.
.PP
If set true then the handle will be treated by the DESTROY as if it was no
longer Active, and so the \fIdatabase engine\fR related effects of DESTROYing a
handle will be skipped. Think of the name as meaning 'treat the handle as
not-Active in the DESTROY method'.
.PP
For a database handle, this attribute does not disable an \fIexplicit\fR
call to the disconnect method, only the implicit call from DESTROY
that happens if the handle is still marked as \f(CW\*(C`Active\*(C'\fR.
.PP
This attribute is specifically designed for use in Unix applications
that "fork" child processes. For some drivers, when the child process exits
the destruction of inherited handles cause the corresponding handles in the
parent process to cease working.
.PP
Either the parent or the child process, but not both, should set
\&\f(CW\*(C`InactiveDestroy\*(C'\fR true on all their shared handles. Alternatively, and
preferably, the "AutoInactiveDestroy" can be set in the parent on connect.
.PP
To help tracing applications using fork the process id is shown in
the trace log whenever a DBI or handle \fBtrace()\fR method is called.
The process id also shown for \fIevery\fR method call if the DBI trace
level (not handle trace level) is set high enough to show the trace
from the DBI's method dispatcher, e.g. >= 9.
.PP
\fR\f(CI\*(C`AutoInactiveDestroy\*(C'\fR\fI\fR
.IX Subsection "AutoInactiveDestroy"
.PP
Type: boolean, inherited
.PP
The "InactiveDestroy" attribute, described above, needs to be explicitly set
in the child process after a \fBfork()\fR, on every active database and statement handle.
This is a problem if the code that performs the \fBfork()\fR is not under your
control, perhaps in a third-party module. Use \f(CW\*(C`AutoInactiveDestroy\*(C'\fR to get
around this situation.
.PP
If set true, the DESTROY method will check the process id of the handle and, if
different from the current process id, it will set the \fIInactiveDestroy\fR attribute.
It is strongly recommended that \f(CW\*(C`AutoInactiveDestroy\*(C'\fR is enabled on all new code
(it's only not enabled by default to avoid backwards compatibility problems).
.PP
This is the example it's designed to deal with:
.PP
.Vb 4
\& my $dbh = DBI\->connect(...);
\& some_code_that_forks(); # Perhaps without your knowledge
\& # Child process dies, destroying the inherited dbh
\& $dbh\->do(...); # Breaks because parent $dbh is now broken
.Ve
.PP
The \f(CW\*(C`AutoInactiveDestroy\*(C'\fR attribute was added in DBI 1.614.
.PP
\fR\f(CI\*(C`PrintWarn\*(C'\fR\fI\fR
.IX Subsection "PrintWarn"
.PP
Type: boolean, inherited
.PP
The \f(CW\*(C`PrintWarn\*(C'\fR attribute controls the printing of warnings recorded
by the driver. When set to a true value (the default) the DBI will check method
calls to see if a warning condition has been set. If so, the DBI
will effectively do a \f(CW\*(C`warn("$class $method warning: $DBI::errstr")\*(C'\fR
where \f(CW$class\fR is the driver class and \f(CW$method\fR is the name of
the method which failed. E.g.,
.PP
.Vb 1
\& DBD::Oracle::db execute warning: ... warning text here ...
.Ve
.PP
If desired, the warnings can be caught and processed using a \f(CW$SIG{_\|_WARN_\|_}\fR
handler or modules like CGI::Carp and CGI::ErrorWrap.
.PP
See also "set_err" for how warnings are recorded and "HandleSetErr"
for how to influence it.
.PP
Fetching the full details of warnings can require an extra round-trip
to the database server for some drivers. In which case the driver
may opt to only fetch the full details of warnings if the \f(CW\*(C`PrintWarn\*(C'\fR
attribute is true. If \f(CW\*(C`PrintWarn\*(C'\fR is false then these drivers should
still indicate the fact that there were warnings by setting the
warning string to, for example: "3 warnings".
.PP
\fR\f(CI\*(C`PrintError\*(C'\fR\fI\fR
.IX Subsection "PrintError"
.PP
Type: boolean, inherited
.PP
The \f(CW\*(C`PrintError\*(C'\fR attribute can be used to force errors to generate warnings (using
\&\f(CW\*(C`warn\*(C'\fR) in addition to returning error codes in the normal way. When set
"on", any method which results in an error occurring will cause the DBI to
effectively do a \f(CW\*(C`warn("$class $method failed: $DBI::errstr")\*(C'\fR where \f(CW$class\fR
is the driver class and \f(CW$method\fR is the name of the method which failed. E.g.,
.PP
.Vb 1
\& DBD::Oracle::db prepare failed: ... error text here ...
.Ve
.PP
By default, \f(CW\*(C`DBI\->connect\*(C'\fR sets \f(CW\*(C`PrintError\*(C'\fR "on".
.PP
If desired, the warnings can be caught and processed using a \f(CW$SIG{_\|_WARN_\|_}\fR
handler or modules like CGI::Carp and CGI::ErrorWrap.
.PP
\fR\f(CI\*(C`RaiseWarn\*(C'\fR\fI\fR
.IX Subsection "RaiseWarn"
.PP
Type: boolean, inherited
.PP
The \f(CW\*(C`RaiseWarn\*(C'\fR attribute can be used to force warnings to raise exceptions rather
then simply printing them. It is "off" by default.
When set "on", any method which sets warning condition will cause
the DBI to effectively do a \f(CW\*(C`die("$class $method warning: $DBI::errstr")\*(C'\fR,
where \f(CW$class\fR is the driver class and \f(CW$method\fR is the name of the method
that sets warning condition. E.g.,
.PP
.Vb 1
\& DBD::Oracle::db execute warning: ... warning text here ...
.Ve
.PP
If you turn \f(CW\*(C`RaiseWarn\*(C'\fR on then you'd normally turn \f(CW\*(C`PrintWarn\*(C'\fR off.
If \f(CW\*(C`PrintWarn\*(C'\fR is also on, then the \f(CW\*(C`PrintWarn\*(C'\fR is done first (naturally).
.PP
This attribute was added in DBI 1.643.
.PP
\fR\f(CI\*(C`RaiseError\*(C'\fR\fI\fR
.IX Subsection "RaiseError"
.PP
Type: boolean, inherited
.PP
The \f(CW\*(C`RaiseError\*(C'\fR attribute can be used to force errors to raise exceptions rather
than simply return error codes in the normal way. It is "off" by default.
When set "on", any method which results in an error will cause
the DBI to effectively do a \f(CW\*(C`die("$class $method failed: $DBI::errstr")\*(C'\fR,
where \f(CW$class\fR is the driver class and \f(CW$method\fR is the name of the method
that failed. E.g.,
.PP
.Vb 1
\& DBD::Oracle::db prepare failed: ... error text here ...
.Ve
.PP
If you turn \f(CW\*(C`RaiseError\*(C'\fR on then you'd normally turn \f(CW\*(C`PrintError\*(C'\fR off.
If \f(CW\*(C`PrintError\*(C'\fR is also on, then the \f(CW\*(C`PrintError\*(C'\fR is done first (naturally).
.PP
Typically \f(CW\*(C`RaiseError\*(C'\fR is used in conjunction with \f(CW\*(C`eval\*(C'\fR,
or a module like Try::Tiny or TryCatch,
to catch the exception that's been thrown and handle it.
For example:
.PP
.Vb 1
\& use Try::Tiny;
\&
\& try {
\& ...
\& $sth\->execute();
\& ...
\& } catch {
\& # $sth\->err and $DBI::err will be true if error was from DBI
\& warn $_; # print the error (which Try::Tiny puts into $_)
\& ... # do whatever you need to deal with the error
\& };
.Ve
.PP
In the catch block the \f(CW$DBI::lasth\fR variable can be useful for
diagnosis and reporting if you can't be sure which handle triggered
the error. For example, \f(CW$DBI::lasth\fR\->{Type} and \f(CW$DBI::lasth\fR\->{Statement}.
.PP
See also "Transactions".
.PP
If you want to temporarily turn \f(CW\*(C`RaiseError\*(C'\fR off (inside a library function
that is likely to fail, for example), the recommended way is like this:
.PP
.Vb 4
\& {
\& local $h\->{RaiseError}; # localize and turn off for this block
\& ...
\& }
.Ve
.PP
The original value will automatically and reliably be restored by Perl,
regardless of how the block is exited.
The same logic applies to other attributes, including \f(CW\*(C`PrintError\*(C'\fR.
.PP
\fR\f(CI\*(C`HandleError\*(C'\fR\fI\fR
.IX Subsection "HandleError"
.PP
Type: code ref, inherited
.PP
The \f(CW\*(C`HandleError\*(C'\fR attribute can be used to provide your own alternative behaviour
in case of errors. If set to a reference to a subroutine then that
subroutine is called when an error is detected (at the same point that
\&\f(CW\*(C`RaiseError\*(C'\fR and \f(CW\*(C`PrintError\*(C'\fR are handled). It is called also when
\&\f(CW\*(C`RaiseWarn\*(C'\fR is enabled and a warning is detected.
.PP
The subroutine is called with three parameters: the error message
string that \f(CW\*(C`RaiseError\*(C'\fR, \f(CW\*(C`RaiseWarn\*(C'\fR or \f(CW\*(C`PrintError\*(C'\fR would use,
the DBI handle being used, and the first value being returned by
the method that failed (typically undef).
.PP
If the subroutine returns a false value then the \f(CW\*(C`RaiseError\*(C'\fR, \f(CW\*(C`RaiseWarn\*(C'\fR
and/or \f(CW\*(C`PrintError\*(C'\fR attributes are checked and acted upon as normal.
.PP
For example, to \f(CW\*(C`die\*(C'\fR with a full stack trace for any error:
.PP
.Vb 2
\& use Carp;
\& $h\->{HandleError} = sub { confess(shift) };
.Ve
.PP
Or to turn errors into exceptions:
.PP
.Vb 2
\& use Exception; # or your own favourite exception module
\& $h\->{HandleError} = sub { Exception\->new(\*(AqDBI\*(Aq)\->raise($_[0]) };
.Ve
.PP
It is possible to 'stack' multiple HandleError handlers by using
closures:
.PP
.Vb 7
\& sub your_subroutine {
\& my $previous_handler = $h\->{HandleError};
\& $h\->{HandleError} = sub {
\& return 1 if $previous_handler and &$previous_handler(@_);
\& ... your code here ...
\& };
\& }
.Ve
.PP
Using a \f(CW\*(C`my\*(C'\fR inside a subroutine to store the previous \f(CW\*(C`HandleError\*(C'\fR
value is important. See perlsub and perlref for more information
about \fIclosures\fR.
.PP
It is possible for \f(CW\*(C`HandleError\*(C'\fR to alter the error message that
will be used by \f(CW\*(C`RaiseError\*(C'\fR, \f(CW\*(C`RaiseWarn\*(C'\fR and \f(CW\*(C`PrintError\*(C'\fR if it returns false.
It can do that by altering the value of \f(CW$_\fR[0]. This example appends
a stack trace to all errors and, unlike the previous example using
Carp::confess, this will work \f(CW\*(C`PrintError\*(C'\fR as well as \f(CW\*(C`RaiseError\*(C'\fR:
.PP
.Vb 1
\& $h\->{HandleError} = sub { $_[0]=Carp::longmess($_[0]); 0; };
.Ve
.PP
It is also possible for \f(CW\*(C`HandleError\*(C'\fR to hide an error, to a limited
degree, by using "set_err" to reset \f(CW$DBI::err\fR and \f(CW$DBI::errstr\fR,
and altering the return value of the failed method. For example:
.PP
.Vb 7
\& $h\->{HandleError} = sub {
\& return 0 unless $_[0] =~ /^\eS+ fetchrow_arrayref failed:/;
\& return 0 unless $_[1]\->err == 1234; # the error to \*(Aqhide\*(Aq
\& $h\->set_err(undef,undef); # turn off the error
\& $_[2] = [ ... ]; # supply alternative return value
\& return 1;
\& };
.Ve
.PP
This only works for methods which return a single value and is hard
to make reliable (avoiding infinite loops, for example) and so isn't
recommended for general use! If you find a \fIgood\fR use for it then
please let me know.
.PP
\fR\f(CI\*(C`HandleSetErr\*(C'\fR\fI\fR
.IX Subsection "HandleSetErr"
.PP
Type: code ref, inherited
.PP
The \f(CW\*(C`HandleSetErr\*(C'\fR attribute can be used to intercept
the setting of handle \f(CW\*(C`err\*(C'\fR, \f(CW\*(C`errstr\*(C'\fR, and \f(CW\*(C`state\*(C'\fR values.
If set to a reference to a subroutine then that subroutine is called
whenever \fBset_err()\fR is called, typically by the driver or a subclass.
.PP
The subroutine is called with five arguments, the first five that
were passed to \fBset_err()\fR: the handle, the \f(CW\*(C`err\*(C'\fR, \f(CW\*(C`errstr\*(C'\fR, and
\&\f(CW\*(C`state\*(C'\fR values being set, and the method name. These can be altered
by changing the values in the \f(CW@_\fR array. The return value affects
\&\fBset_err()\fR behaviour, see "set_err" for details.
.PP
It is possible to 'stack' multiple HandleSetErr handlers by using
closures. See "HandleError" for an example.
.PP
The \f(CW\*(C`HandleSetErr\*(C'\fR and \f(CW\*(C`HandleError\*(C'\fR subroutines differ in subtle
but significant ways. HandleError is only invoked at the point where
the DBI is about to return to the application with \f(CW\*(C`err\*(C'\fR set true.
It's not invoked by the failure of a method that's been called by
another DBI method. HandleSetErr, on the other hand, is called
whenever \fBset_err()\fR is called with a defined \f(CW\*(C`err\*(C'\fR value, even if false.
So it's not just for errors, despite the name, but also warn and info states.
The \fBset_err()\fR method, and thus HandleSetErr, may be called multiple
times within a method and is usually invoked from deep within driver code.
.PP
In theory a driver can use the return value from HandleSetErr via
\&\fBset_err()\fR to decide whether to continue or not. If \fBset_err()\fR returns
an empty list, indicating that the HandleSetErr code has 'handled'
the 'error', the driver could then continue instead of failing (if
that's a reasonable thing to do). This isn't excepted to be
common and any such cases should be clearly marked in the driver
documentation and discussed on the dbi-dev mailing list.
.PP
The \f(CW\*(C`HandleSetErr\*(C'\fR attribute was added in DBI 1.41.
.PP
\fR\f(CI\*(C`ErrCount\*(C'\fR\fI\fR
.IX Subsection "ErrCount"
.PP
Type: unsigned integer
.PP
The \f(CW\*(C`ErrCount\*(C'\fR attribute is incremented whenever the \fBset_err()\fR
method records an error. It isn't incremented by warnings or
information states. It is not reset by the DBI at any time.
.PP
The \f(CW\*(C`ErrCount\*(C'\fR attribute was added in DBI 1.41. Older drivers may
not have been updated to use \fBset_err()\fR to record errors and so this
attribute may not be incremented when using them.
.PP
\fR\f(CI\*(C`ShowErrorStatement\*(C'\fR\fI\fR
.IX Subsection "ShowErrorStatement"
.PP
Type: boolean, inherited
.PP
The \f(CW\*(C`ShowErrorStatement\*(C'\fR attribute can be used to cause the relevant
Statement text to be appended to the error messages generated by
the \f(CW\*(C`RaiseError\*(C'\fR, \f(CW\*(C`PrintError\*(C'\fR, \f(CW\*(C`RaiseWarn\*(C'\fR and \f(CW\*(C`PrintWarn\*(C'\fR attributes.
Only applies to errors on statement handles
plus the \fBprepare()\fR, \fBdo()\fR, and the various \f(CW\*(C`select*()\*(C'\fR database handle methods.
(The exact format of the appended text is subject to change.)
.PP
If \f(CW\*(C`$h\->{ParamValues}\*(C'\fR returns a hash reference of parameter
(placeholder) values then those are formatted and appended to the
end of the Statement text in the error message.
.PP
\fR\f(CI\*(C`TraceLevel\*(C'\fR\fI\fR
.IX Subsection "TraceLevel"
.PP
Type: integer, inherited
.PP
The \f(CW\*(C`TraceLevel\*(C'\fR attribute can be used as an alternative to the
"trace" method to set the DBI trace level and trace flags for a
specific handle. See "TRACING" for more details.
.PP
The \f(CW\*(C`TraceLevel\*(C'\fR attribute is especially useful combined with
\&\f(CW\*(C`local\*(C'\fR to alter the trace settings for just a single block of code.
.PP
\fR\f(CI\*(C`FetchHashKeyName\*(C'\fR\fI\fR
.IX Subsection "FetchHashKeyName"
.PP
Type: string, inherited
.PP
The \f(CW\*(C`FetchHashKeyName\*(C'\fR attribute is used to specify whether the \fBfetchrow_hashref()\fR
method should perform case conversion on the field names used for
the hash keys. For historical reasons it defaults to '\f(CW\*(C`NAME\*(C'\fR' but
it is recommended to set it to '\f(CW\*(C`NAME_lc\*(C'\fR' (convert to lower case)
or '\f(CW\*(C`NAME_uc\*(C'\fR' (convert to upper case) according to your preference.
It can only be set for driver and database handles. For statement
handles the value is frozen when \fBprepare()\fR is called.
.PP
\fR\f(CI\*(C`ChopBlanks\*(C'\fR\fI\fR
.IX Subsection "ChopBlanks"
.PP
Type: boolean, inherited
.PP
The \f(CW\*(C`ChopBlanks\*(C'\fR attribute can be used to control the trimming of trailing space
characters from fixed width character (CHAR) fields. No other field
types are affected, even where field values have trailing spaces.
.PP
The default is false (although it is possible that the default may change).
Applications that need specific behaviour should set the attribute as
needed.
.PP
Drivers are not required to support this attribute, but any driver which
does not support it must arrange to return \f(CW\*(C`undef\*(C'\fR as the attribute value.
.PP
\fR\f(CI\*(C`LongReadLen\*(C'\fR\fI\fR
.IX Subsection "LongReadLen"
.PP
Type: unsigned integer, inherited
.PP
The \f(CW\*(C`LongReadLen\*(C'\fR attribute may be used to control the maximum
length of 'long' type fields (LONG, BLOB, CLOB, MEMO, etc.) which the driver will
read from the database automatically when it fetches each row of data.
.PP
The \f(CW\*(C`LongReadLen\*(C'\fR attribute only relates to fetching and reading
long values; it is not involved in inserting or updating them.
.PP
A value of 0 means not to automatically fetch any long data.
Drivers may return undef or an empty string for long fields when
\&\f(CW\*(C`LongReadLen\*(C'\fR is 0.
.PP
The default is typically 0 (zero) or 80 bytes but may vary between drivers.
Applications fetching long fields should set this value to slightly
larger than the longest long field value to be fetched.
.PP
Some databases return some long types encoded as pairs of hex digits.
For these types, \f(CW\*(C`LongReadLen\*(C'\fR relates to the underlying data
length and not the doubled-up length of the encoded string.
.PP
Changing the value of \f(CW\*(C`LongReadLen\*(C'\fR for a statement handle after it
has been \f(CW\*(C`prepare\*(C'\fR'd will typically have no effect, so it's common to
set \f(CW\*(C`LongReadLen\*(C'\fR on the \f(CW$dbh\fR before calling \f(CW\*(C`prepare\*(C'\fR.
.PP
For most drivers the value used here has a direct effect on the
memory used by the statement handle while it's active, so don't be
too generous. If you can't be sure what value to use you could
execute an extra select statement to determine the longest value.
For example:
.PP
.Vb 7
\& $dbh\->{LongReadLen} = $dbh\->selectrow_array(qq{
\& SELECT MAX(OCTET_LENGTH(long_column_name))
\& FROM table WHERE ...
\& });
\& $sth = $dbh\->prepare(qq{
\& SELECT long_column_name, ... FROM table WHERE ...
\& });
.Ve
.PP
You may need to take extra care if the table can be modified between
the first select and the second being executed. You may also need to
use a different function if \fBOCTET_LENGTH()\fR does not work for long
types in your database. For example, for Sybase use \fBDATALENGTH()\fR and
for Oracle use \fBLENGTHB()\fR.
.PP
See also "LongTruncOk" for information on truncation of long types.
.PP
\fR\f(CI\*(C`LongTruncOk\*(C'\fR\fI\fR
.IX Subsection "LongTruncOk"
.PP
Type: boolean, inherited
.PP
The \f(CW\*(C`LongTruncOk\*(C'\fR attribute may be used to control the effect of
fetching a long field value which has been truncated (typically
because it's longer than the value of the \f(CW\*(C`LongReadLen\*(C'\fR attribute).
.PP
By default, \f(CW\*(C`LongTruncOk\*(C'\fR is false and so fetching a long value that
needs to be truncated will cause the fetch to fail.
(Applications should always be sure to
check for errors after a fetch loop in case an error, such as a divide
by zero or long field truncation, caused the fetch to terminate
prematurely.)
.PP
If a fetch fails due to a long field truncation when \f(CW\*(C`LongTruncOk\*(C'\fR is
false, many drivers will allow you to continue fetching further rows.
.PP
See also "LongReadLen".
.PP
\fR\f(CI\*(C`TaintIn\*(C'\fR\fI\fR
.IX Subsection "TaintIn"
.PP
Type: boolean, inherited
.PP
If the \f(CW\*(C`TaintIn\*(C'\fR attribute is set to a true value \fIand\fR Perl is running in
taint mode (e.g., started with the \f(CW\*(C`\-T\*(C'\fR option), then all the arguments
to most DBI method calls are checked for being tainted. \fIThis may change.\fR
.PP
The attribute defaults to off, even if Perl is in taint mode.
See perlsec for more about taint mode. If Perl is not
running in taint mode, this attribute has no effect.
.PP
When fetching data that you trust you can turn off the TaintIn attribute,
for that statement handle, for the duration of the fetch loop.
.PP
The \f(CW\*(C`TaintIn\*(C'\fR attribute was added in DBI 1.31.
.PP
\fR\f(CI\*(C`TaintOut\*(C'\fR\fI\fR
.IX Subsection "TaintOut"
.PP
Type: boolean, inherited
.PP
If the \f(CW\*(C`TaintOut\*(C'\fR attribute is set to a true value \fIand\fR Perl is running in
taint mode (e.g., started with the \f(CW\*(C`\-T\*(C'\fR option), then most data fetched
from the database is considered tainted. \fIThis may change.\fR
.PP
The attribute defaults to off, even if Perl is in taint mode.
See perlsec for more about taint mode. If Perl is not
running in taint mode, this attribute has no effect.
.PP
When fetching data that you trust you can turn off the TaintOut attribute,
for that statement handle, for the duration of the fetch loop.
.PP
Currently only fetched data is tainted. It is possible that the results
of other DBI method calls, and the value of fetched attributes, may
also be tainted in future versions. That change may well break your
applications unless you take great care now. If you use DBI Taint mode,
please report your experience and any suggestions for changes.
.PP
The \f(CW\*(C`TaintOut\*(C'\fR attribute was added in DBI 1.31.
.PP
\fR\f(CI\*(C`Taint\*(C'\fR\fI\fR
.IX Subsection "Taint"
.PP
Type: boolean, inherited
.PP
The \f(CW\*(C`Taint\*(C'\fR attribute is a shortcut for "TaintIn" and "TaintOut" (it is also present
for backwards compatibility).
.PP
Setting this attribute sets both "TaintIn" and "TaintOut", and retrieving
it returns a true value if and only if "TaintIn" and "TaintOut" are
both set to true values.
.PP
\fR\f(CI\*(C`Profile\*(C'\fR\fI\fR
.IX Subsection "Profile"
.PP
Type: inherited
.PP
The \f(CW\*(C`Profile\*(C'\fR attribute enables the collection and reporting of
method call timing statistics. See the DBI::Profile module
documentation for \fImuch\fR more detail.
.PP
The \f(CW\*(C`Profile\*(C'\fR attribute was added in DBI 1.24.
.PP
\fR\f(CI\*(C`ReadOnly\*(C'\fR\fI\fR
.IX Subsection "ReadOnly"
.PP
Type: boolean, inherited
.PP
An application can set the \f(CW\*(C`ReadOnly\*(C'\fR attribute of a handle to a true value to
indicate that it will not be attempting to make any changes using that handle
or any children of it.
.PP
Note that the exact definition of 'read only' is rather fuzzy.
For more details see the documentation for the driver you're using.
.PP
If the driver can make the handle truly read-only then it should
(unless doing so would have unpleasant side effect, like changing the
consistency level from per-statement to per-session).
Otherwise the attribute is simply advisory.
.PP
A driver can set the \f(CW\*(C`ReadOnly\*(C'\fR attribute itself to indicate that the data it
is connected to cannot be changed for some reason.
.PP
If the driver cannot ensure the \f(CW\*(C`ReadOnly\*(C'\fR attribute is adhered to it
will record a warning. In this case reading the \f(CW\*(C`ReadOnly\*(C'\fR attribute
back after it is set true will return true even if the underlying
driver cannot ensure this (so any application knows the application
declared itself ReadOnly).
.PP
Library modules and proxy drivers can use the attribute to influence
their behavior. For example, the DBD::Gofer driver considers the
\&\f(CW\*(C`ReadOnly\*(C'\fR attribute when making a decision about whether to retry an
operation that failed.
.PP
The attribute should be set to 1 or 0 (or undef). Other values are reserved.
.PP
\fR\f(CI\*(C`Callbacks\*(C'\fR\fI\fR
.IX Subsection "Callbacks"
.PP
Type: hash ref
.PP
The DBI callback mechanism lets you intercept, and optionally replace, any
method call on a DBI handle. At the extreme, it lets you become a puppet
master, deceiving the application in any way you want.
.PP
The \f(CW\*(C`Callbacks\*(C'\fR attribute is a hash reference where the keys are DBI method
names and the values are code references. For each key naming a method, the
DBI will execute the associated code reference before executing the method.
.PP
The arguments to the code reference will be the same as to the method,
including the invocant (a database handle or statement handle). For example,
say that to callback to some code on a call to \f(CWprepare()\fR:
.PP
.Vb 6
\& $dbh\->{Callbacks} = {
\& prepare => sub {
\& my ($dbh, $query, $attrs) = @_;
\& print "Preparing q{$query}\en"
\& },
\& };
.Ve
.PP
The callback would then be executed when you called the \f(CWprepare()\fR method:
.PP
.Vb 1
\& $dbh\->prepare(\*(AqSELECT 1\*(Aq);
.Ve
.PP
And the output of course would be:
.PP
.Vb 1
\& Preparing q{SELECT 1}
.Ve
.PP
Because callbacks are executed \fIbefore\fR the methods
they're associated with, you can modify the arguments before they're passed on
to the method call. For example, to make sure that all calls to \f(CWprepare()\fR
are immediately prepared by DBD::Pg, add a callback that makes sure that
the \f(CW\*(C`pg_prepare_now\*(C'\fR attribute is always set:
.PP
.Vb 9
\& my $dbh = DBI\->connect($dsn, $username, $auth, {
\& Callbacks => {
\& prepare => sub {
\& $_[2] ||= {};
\& $_[2]\->{pg_prepare_now} = 1;
\& return; # must return nothing
\& },
\& }
\& });
.Ve
.PP
Note that we are editing the contents of \f(CW@_\fR directly. In this case we've
created the attributes hash if it's not passed to the \f(CW\*(C`prepare\*(C'\fR call.
.PP
You can also prevent the associated method from ever executing. While a
callback executes, \f(CW$_\fR holds the method name. (This allows multiple callbacks
to share the same code reference and still know what method was called.)
To prevent the method from
executing, simply \f(CW\*(C`undef $_\*(C'\fR. For example, if you wanted to disable calls to
\&\f(CWping()\fR, you could do this:
.PP
.Vb 8
\& $dbh\->{Callbacks} = {
\& ping => sub {
\& # tell dispatch to not call the method:
\& undef $_;
\& # return this value instead:
\& return "42 bells";
\& }
\& };
.Ve
.PP
As with other attributes, Callbacks can be specified on a handle or via the
attributes to \f(CWconnect()\fR. Callbacks can also be applied to a statement
methods on a statement handle. For example:
.PP
.Vb 5
\& $sth\->{Callbacks} = {
\& execute => sub {
\& print "Executing ", shift\->{Statement}, "\en";
\& }
\& };
.Ve
.PP
The \f(CW\*(C`Callbacks\*(C'\fR attribute of a database handle isn't copied to any statement
handles it creates. So setting callbacks for a statement handle requires you to
set the \f(CW\*(C`Callbacks\*(C'\fR attribute on the statement handle yourself, as in the
example above, or use the special \f(CW\*(C`ChildCallbacks\*(C'\fR key described below.
.PP
\&\fBSpecial Keys in Callbacks Attribute\fR
.PP
In addition to DBI handle method names, the \f(CW\*(C`Callbacks\*(C'\fR hash reference
supports four additional keys.
.PP
The first is the \f(CW\*(C`ChildCallbacks\*(C'\fR key. When a statement handle is created from
a database handle the \f(CW\*(C`ChildCallbacks\*(C'\fR key of the database handle's
\&\f(CW\*(C`Callbacks\*(C'\fR attribute, if any, becomes the new \f(CW\*(C`Callbacks\*(C'\fR attribute of the
statement handle.
This allows you to define callbacks for all statement handles created from a
database handle. For example, if you wanted to count how many times \f(CW\*(C`execute\*(C'\fR
was called in your application, you could write:
.PP
.Vb 8
\& my $exec_count = 0;
\& my $dbh = DBI\->connect( $dsn, $username, $auth, {
\& Callbacks => {
\& ChildCallbacks => {
\& execute => sub { $exec_count++; return; }
\& }
\& }
\& });
\&
\& END {
\& print "The execute method was called $exec_count times\en";
\& }
.Ve
.PP
The other three special keys are \f(CW\*(C`connect_cached.new\*(C'\fR,
\&\f(CW\*(C`connect_cached.connected\*(C'\fR, and \f(CW\*(C`connect_cached.reused\*(C'\fR. These keys define
callbacks that are called when \f(CWconnect_cached()\fR is called, but allow
different behaviors depending on whether a new handle is created or a handle
is returned. The callback is invoked with these arguments:
\&\f(CW\*(C`$dbh, $dsn, $user, $auth, $attr\*(C'\fR.
.PP
For example, some applications uses \f(CWconnect_cached()\fR to connect with
\&\f(CW\*(C`AutoCommit\*(C'\fR enabled and then disable \f(CW\*(C`AutoCommit\*(C'\fR temporarily for
transactions. If \f(CWconnect_cached()\fR is called during a transaction, perhaps in
a utility method, then it might select the same cached handle and then force
\&\f(CW\*(C`AutoCommit\*(C'\fR on, forcing a commit of the transaction. See the "connect_cached"
documentation for one way to deal with that. Here we'll describe an alternative
approach using a callback.
.PP
Because the \f(CW\*(C`connect_cached.new\*(C'\fR and \f(CW\*(C`connect_cached.reused\*(C'\fR callbacks are
invoked before \f(CWconnect_cached()\fR has applied the connect attributes, you can
use them to edit the attributes that will be applied. To prevent a cached
handle from having its transactions committed before it's returned, you can
eliminate the \f(CW\*(C`AutoCommit\*(C'\fR attribute in a \f(CW\*(C`connect_cached.reused\*(C'\fR callback,
like so:
.PP
.Vb 3
\& my $cb = {
\& \*(Aqconnect_cached.reused\*(Aq => sub { delete $_[4]\->{AutoCommit} },
\& };
\&
\& sub dbh {
\& my $self = shift;
\& DBI\->connect_cached( $dsn, $username, $auth, {
\& PrintError => 0,
\& RaiseError => 1,
\& AutoCommit => 1,
\& Callbacks => $cb,
\& });
\& }
.Ve
.PP
The upshot is that new database handles are created with \f(CW\*(C`AutoCommit\*(C'\fR
enabled, while cached database handles are left in whatever transaction state
they happened to be in when retrieved from the cache.
.PP
Note that we've also used a lexical for the callbacks hash reference. This is
because \f(CWconnect_cached()\fR returns a new database handle if any of the
attributes passed to is have changed. If we used an inline hash reference,
\&\f(CWconnect_cached()\fR would return a new database handle every time. Which would
rather defeat the purpose.
.PP
A more common application for callbacks is setting connection state only when
a new connection is made (by \fBconnect()\fR or \fBconnect_cached()\fR). Adding a callback
to the connected method (when using \f(CW\*(C`connect\*(C'\fR) or via
\&\f(CW\*(C`connect_cached.connected\*(C'\fR (when useing \fBconnect_cached()\fR>) makes this easy.
The \fBconnected()\fR method is a no-op by default (unless you subclass the DBI and
change it). The DBI calls it to indicate that a new connection has been made
and the connection attributes have all been set. You can give it a bit of
added functionality by applying a callback to it. For example, to make sure
that MySQL understands your application's ANSI-compliant SQL, set it up like
so:
.PP
.Vb 10
\& my $dbh = DBI\->connect($dsn, $username, $auth, {
\& Callbacks => {
\& connected => sub {
\& shift\->do(q{
\& SET SESSION sql_mode=\*(Aqansi,strict_trans_tables,no_auto_value_on_zero\*(Aq;
\& });
\& return;
\& },
\& }
\& });
.Ve
.PP
If you're using \f(CWconnect_cached()\fR, use the \f(CW\*(C`connect_cached.connected\*(C'\fR
callback, instead. This is because \f(CWconnected()\fR is called for both new and
reused database handles, but you want to execute a callback only the when a
new database handle is returned. For example, to set the time zone on
connection to a PostgreSQL database, try this:
.PP
.Vb 5
\& my $cb = {
\& \*(Aqconnect_cached.connected\*(Aq => sub {
\& shift\->do(\*(AqSET timezone = UTC\*(Aq);
\& }
\& };
\&
\& sub dbh {
\& my $self = shift;
\& DBI\->connect_cached( $dsn, $username, $auth, { Callbacks => $cb });
\& }
.Ve
.PP
One significant limitation with callbacks is that there can only be one per
method per handle. This means it's easy for one use of callbacks to interfere
with, or typically simply overwrite, another use of callbacks. For this reason
modules using callbacks should document the fact clearly so application authors
can tell if use of callbacks by the module will clash with use of callbacks by
the application.
.PP
You might be able to work around this issue by taking a copy of the original
callback and calling it within your own. For example:
.PP
.Vb 8
\& my $prev_cb = $h\->{Callbacks}{method_name};
\& $h\->{Callbacks}{method_name} = sub {
\& if ($prev_cb) {
\& my @result = $prev_cb\->(@_);
\& return @result if not $_; # $prev_cb vetoed call
\& }
\& ... your callback logic here ...
\& };
.Ve
.PP
\fR\f(CI\*(C`private_your_module_name_*\*(C'\fR\fI\fR
.IX Subsection "private_your_module_name_*"
.PP
The DBI provides a way to store extra information in a DBI handle as
"private" attributes. The DBI will allow you to store and retrieve any
attribute which has a name starting with "\f(CW\*(C`private_\*(C'\fR".
.PP
It is \fIstrongly\fR recommended that you use just \fIone\fR private
attribute (e.g., use a hash ref) \fIand\fR give it a long and unambiguous
name that includes the module or application name that the attribute
relates to (e.g., "\f(CW\*(C`private_YourFullModuleName_thingy\*(C'\fR").
.PP
Because of the way the Perl tie mechanism works you cannot reliably
use the \f(CW\*(C`||=\*(C'\fR operator directly to initialise the attribute, like this:
.PP
.Vb 1
\& my $foo = $dbh\->{private_yourmodname_foo} ||= { ... }; # WRONG
.Ve
.PP
you should use a two step approach like this:
.PP
.Vb 2
\& my $foo = $dbh\->{private_yourmodname_foo};
\& $foo ||= $dbh\->{private_yourmodname_foo} = { ... };
.Ve
.PP
This attribute is primarily of interest to people sub-classing DBI,
or for applications to piggy-back extra information onto DBI handles.
.SH "DBI DATABASE HANDLE OBJECTS"
.IX Header "DBI DATABASE HANDLE OBJECTS"
This section covers the methods and attributes associated with
database handles.
.SS "Database Handle Methods"
.IX Subsection "Database Handle Methods"
The following methods are specified for DBI database handles:
.PP
\fR\f(CI\*(C`clone\*(C'\fR\fI\fR
.IX Subsection "clone"
.PP
.Vb 1
\& $new_dbh = $dbh\->clone(\e%attr);
.Ve
.PP
The \f(CW\*(C`clone\*(C'\fR method duplicates the \f(CW$dbh\fR connection by connecting
with the same parameters ($dsn, \f(CW$user\fR, \f(CW$password\fR) as originally used.
.PP
The attributes for the cloned connect are the same as those used
for the \fIoriginal\fR connect, with any other attributes in \f(CW\*(C`\e%attr\*(C'\fR
merged over them. Effectively the same as doing:
.PP
.Vb 1
\& %attributes_used = ( %original_attributes, %attr );
.Ve
.PP
If \e%attr is not given then it defaults to a hash containing all
the attributes in the attribute cache of \f(CW$dbh\fR excluding any non-code
references, plus the main boolean attributes (RaiseError, PrintError,
AutoCommit, etc.). \fIThis behaviour is unreliable and so use of clone without
an argument is deprecated and may cause a warning in a future release.\fR
.PP
The clone method can be used even if the database handle is disconnected.
.PP
The \f(CW\*(C`clone\*(C'\fR method was added in DBI 1.33.
.PP
\fR\f(CI\*(C`data_sources\*(C'\fR\fI\fR
.IX Subsection "data_sources"
.PP
.Vb 2
\& @ary = $dbh\->data_sources();
\& @ary = $dbh\->data_sources(\e%attr);
.Ve
.PP
Returns a list of data sources (databases) available via the \f(CW$dbh\fR
driver's \fBdata_sources()\fR method, plus any extra data sources that
the driver can discover via the connected \f(CW$dbh\fR. Typically the extra
data sources are other databases managed by the same server process
that the \f(CW$dbh\fR is connected to.
.PP
Data sources are returned in a form suitable for passing to the
"connect" method (that is, they will include the "\f(CW\*(C`dbi:$driver:\*(C'\fR" prefix).
.PP
The \fBdata_sources()\fR method, for a \f(CW$dbh\fR, was added in DBI 1.38.
.PP
\fR\f(CI\*(C`do\*(C'\fR\fI\fR
.IX Subsection "do"
.PP
.Vb 3
\& $rows = $dbh\->do($statement) or die $dbh\->errstr;
\& $rows = $dbh\->do($statement, \e%attr) or die $dbh\->errstr;
\& $rows = $dbh\->do($statement, \e%attr, @bind_values) or die ...
.Ve
.PP
Prepare and execute a single statement. Returns the number of rows
affected or \f(CW\*(C`undef\*(C'\fR on error. A return value of \f(CW\-1\fR means the
number of rows is not known, not applicable, or not available.
.PP
This method is typically most useful for \fInon\fR\-\f(CW\*(C`SELECT\*(C'\fR statements that
either cannot be prepared in advance (due to a limitation of the
driver) or do not need to be executed repeatedly. It should not
be used for \f(CW\*(C`SELECT\*(C'\fR statements because it does not return a statement
handle (so you can't fetch any data).
.PP
The default \f(CW\*(C`do\*(C'\fR method is logically similar to:
.PP
.Vb 7
\& sub do {
\& my($dbh, $statement, $attr, @bind_values) = @_;
\& my $sth = $dbh\->prepare($statement, $attr) or return undef;
\& $sth\->execute(@bind_values) or return undef;
\& my $rows = $sth\->rows;
\& ($rows == 0) ? "0E0" : $rows; # always return true if no error
\& }
.Ve
.PP
For example:
.PP
.Vb 4
\& my $rows_deleted = $dbh\->do(q{
\& DELETE FROM table
\& WHERE status = ?
\& }, undef, \*(AqDONE\*(Aq) or die $dbh\->errstr;
.Ve
.PP
Using placeholders and \f(CW@bind_values\fR with the \f(CW\*(C`do\*(C'\fR method can be
useful because it avoids the need to correctly quote any variables
in the \f(CW$statement\fR. But if you'll be executing the statement many
times then it's more efficient to \f(CW\*(C`prepare\*(C'\fR it once and call
\&\f(CW\*(C`execute\*(C'\fR many times instead.
.PP
The \f(CW\*(C`q{...}\*(C'\fR style quoting used in this example avoids clashing with
quotes that may be used in the SQL statement. Use the double-quote-like
\&\f(CW\*(C`qq{...}\*(C'\fR operator if you want to interpolate variables into the string.
See "Quote and Quote-like Operators" in perlop for more details.
.PP
Note drivers are free to avoid the overhead of creating an DBI
statement handle for \fBdo()\fR, especially if there are no parameters. In
this case error handlers, if invoked during \fBdo()\fR, will be passed the
database handle.
.PP
\fR\f(CI\*(C`last_insert_id\*(C'\fR\fI\fR
.IX Subsection "last_insert_id"
.PP
.Vb 3
\& $rv = $dbh\->last_insert_id();
\& $rv = $dbh\->last_insert_id($catalog, $schema, $table, $field);
\& $rv = $dbh\->last_insert_id($catalog, $schema, $table, $field, \e%attr);
.Ve
.PP
Returns a value 'identifying' the row just inserted, if possible.
Typically this would be a value assigned by the database server
to a column with an \fIauto_increment\fR or \fIserial\fR type.
Returns undef if the driver does not support the method or can't
determine the value.
.PP
The \f(CW$catalog\fR, \f(CW$schema\fR, \f(CW$table\fR, and \f(CW$field\fR parameters may be required
for some drivers (see below). If you don't know the parameter values
and your driver does not need them, then use \f(CW\*(C`undef\*(C'\fR for each.
.PP
There are several caveats to be aware of with this method if you want
to use it for portable applications:
.PP
\&\fB*\fR For some drivers the value may only be available immediately after
the insert statement has executed (e.g., mysql, Informix).
.PP
\&\fB*\fR For some drivers the \f(CW$catalog\fR, \f(CW$schema\fR, \f(CW$table\fR, and \f(CW$field\fR parameters
are required, for others they are ignored (e.g., mysql).
.PP
\&\fB*\fR Drivers may return an indeterminate value if no insert has
been performed yet.
.PP
\&\fB*\fR For some drivers the value may only be available if placeholders
have \fInot\fR been used (e.g., Sybase, MS SQL). In this case the value
returned would be from the last non-placeholder insert statement.
.PP
\&\fB*\fR Some drivers may need driver-specific hints about how to get
the value. For example, being told the name of the database 'sequence'
object that holds the value. Any such hints are passed as driver-specific
attributes in the \e%attr parameter.
.PP
\&\fB*\fR If the underlying database offers nothing better, then some
drivers may attempt to implement this method by executing
"\f(CW\*(C`select max($field) from $table\*(C'\fR". Drivers using any approach
like this should issue a warning if \f(CW\*(C`AutoCommit\*(C'\fR is true because
it is generally unsafe \- another process may have modified the table
between your insert and the select. For situations where you know
it is safe, such as when you have locked the table, you can silence
the warning by passing \f(CW\*(C`Warn\*(C'\fR => 0 in \e%attr.
.PP
\&\fB*\fR If no insert has been performed yet, or the last insert failed,
then the value is implementation defined.
.PP
Given all the caveats above, it's clear that this method must be
used with care.
.PP
The \f(CW\*(C`last_insert_id\*(C'\fR method was added in DBI 1.38.
.PP
\fR\f(CI\*(C`selectrow_array\*(C'\fR\fI\fR
.IX Subsection "selectrow_array"
.PP
.Vb 3
\& @row_ary = $dbh\->selectrow_array($statement);
\& @row_ary = $dbh\->selectrow_array($statement, \e%attr);
\& @row_ary = $dbh\->selectrow_array($statement, \e%attr, @bind_values);
.Ve
.PP
This utility method combines "prepare", "execute" and
"fetchrow_array" into a single call. If called in a list context, it
returns the first row of data from the statement. The \f(CW$statement\fR
parameter can be a previously prepared statement handle, in which case
the \f(CW\*(C`prepare\*(C'\fR is skipped.
.PP
If any method fails, and "RaiseError" is not set, \f(CW\*(C`selectrow_array\*(C'\fR
will return an empty list.
.PP
If called in a scalar context for a statement handle that has more
than one column, it is undefined whether the driver will return
the value of the first column or the last. So don't do that.
Also, in a scalar context, an \f(CW\*(C`undef\*(C'\fR is returned if there are no
more rows or if an error occurred. That \f(CW\*(C`undef\*(C'\fR can't be distinguished
from an \f(CW\*(C`undef\*(C'\fR returned because the first field value was NULL.
For these reasons you should exercise some caution if you use
\&\f(CW\*(C`selectrow_array\*(C'\fR in a scalar context, or just don't do that.
.PP
\fR\f(CI\*(C`selectrow_arrayref\*(C'\fR\fI\fR
.IX Subsection "selectrow_arrayref"
.PP
.Vb 3
\& $ary_ref = $dbh\->selectrow_arrayref($statement);
\& $ary_ref = $dbh\->selectrow_arrayref($statement, \e%attr);
\& $ary_ref = $dbh\->selectrow_arrayref($statement, \e%attr, @bind_values);
.Ve
.PP
This utility method combines "prepare", "execute" and
"fetchrow_arrayref" into a single call. It returns the first row of
data from the statement. The \f(CW$statement\fR parameter can be a previously
prepared statement handle, in which case the \f(CW\*(C`prepare\*(C'\fR is skipped.
.PP
If any method fails, and "RaiseError" is not set, \f(CW\*(C`selectrow_arrayref\*(C'\fR
will return undef.
.PP
\fR\f(CI\*(C`selectrow_hashref\*(C'\fR\fI\fR
.IX Subsection "selectrow_hashref"
.PP
.Vb 3
\& $hash_ref = $dbh\->selectrow_hashref($statement);
\& $hash_ref = $dbh\->selectrow_hashref($statement, \e%attr);
\& $hash_ref = $dbh\->selectrow_hashref($statement, \e%attr, @bind_values);
.Ve
.PP
This utility method combines "prepare", "execute" and
"fetchrow_hashref" into a single call. It returns the first row of
data from the statement. The \f(CW$statement\fR parameter can be a previously
prepared statement handle, in which case the \f(CW\*(C`prepare\*(C'\fR is skipped.
.PP
If any method fails, and "RaiseError" is not set, \f(CW\*(C`selectrow_hashref\*(C'\fR
will return undef.
.PP
\fR\f(CI\*(C`selectall_arrayref\*(C'\fR\fI\fR
.IX Subsection "selectall_arrayref"
.PP
.Vb 3
\& $ary_ref = $dbh\->selectall_arrayref($statement);
\& $ary_ref = $dbh\->selectall_arrayref($statement, \e%attr);
\& $ary_ref = $dbh\->selectall_arrayref($statement, \e%attr, @bind_values);
.Ve
.PP
This utility method combines "prepare", "execute" and
"fetchall_arrayref" into a single call. It returns a reference to an
array containing a reference to an array (or hash, see below) for each row of
data fetched.
.PP
The \f(CW$statement\fR parameter can be a previously prepared statement handle,
in which case the \f(CW\*(C`prepare\*(C'\fR is skipped. This is recommended if the
statement is going to be executed many times.
.PP
If "RaiseError" is not set and any method except \f(CW\*(C`fetchall_arrayref\*(C'\fR
fails then \f(CW\*(C`selectall_arrayref\*(C'\fR will return \f(CW\*(C`undef\*(C'\fR; if
\&\f(CW\*(C`fetchall_arrayref\*(C'\fR fails then it will return with whatever data
has been fetched thus far. You should check \f(CW\*(C`$dbh\->err\*(C'\fR
afterwards (or use the \f(CW\*(C`RaiseError\*(C'\fR attribute) to discover if the data is
complete or was truncated due to an error.
.PP
The "fetchall_arrayref" method called by \f(CW\*(C`selectall_arrayref\*(C'\fR
supports a \f(CW$max_rows\fR parameter. You can specify a value for \f(CW$max_rows\fR
by including a '\f(CW\*(C`MaxRows\*(C'\fR' attribute in \e%attr. In which case \fBfinish()\fR
is called for you after \fBfetchall_arrayref()\fR returns.
.PP
The "fetchall_arrayref" method called by \f(CW\*(C`selectall_arrayref\*(C'\fR
also supports a \f(CW$slice\fR parameter. You can specify a value for \f(CW$slice\fR by
including a '\f(CW\*(C`Slice\*(C'\fR' or '\f(CW\*(C`Columns\*(C'\fR' attribute in \e%attr. The only
difference between the two is that if \f(CW\*(C`Slice\*(C'\fR is not defined and
\&\f(CW\*(C`Columns\*(C'\fR is an array ref, then the array is assumed to contain column
index values (which count from 1), rather than perl array index values.
In which case the array is copied and each value decremented before
passing to \f(CW\*(C`/fetchall_arrayref\*(C'\fR.
.PP
You may often want to fetch an array of rows where each row is stored as a
hash. That can be done simply using:
.PP
.Vb 7
\& my $emps = $dbh\->selectall_arrayref(
\& "SELECT ename FROM emp ORDER BY ename",
\& { Slice => {} }
\& );
\& foreach my $emp ( @$emps ) {
\& print "Employee: $emp\->{ename}\en";
\& }
.Ve
.PP
Or, to fetch into an array instead of an array ref:
.PP
.Vb 1
\& @result = @{ $dbh\->selectall_arrayref($sql, { Slice => {} }) };
.Ve
.PP
See "fetchall_arrayref" method for more details.
.PP
\fR\f(CI\*(C`selectall_array\*(C'\fR\fI\fR
.IX Subsection "selectall_array"
.PP
.Vb 3
\& @ary = $dbh\->selectall_array($statement);
\& @ary = $dbh\->selectall_array($statement, \e%attr);
\& @ary = $dbh\->selectall_array($statement, \e%attr, @bind_values);
.Ve
.PP
This is a convenience wrapper around "selectall_arrayref" that returns
the rows directly as a list, rather than a reference to an array of rows.
.PP
Note that if "RaiseError" is not set then you can't tell the difference
between returning no rows and an error. Using RaiseError is best practice.
.PP
The \f(CW\*(C`selectall_array\*(C'\fR method was added in DBI 1.635.
.PP
\fR\f(CI\*(C`selectall_hashref\*(C'\fR\fI\fR
.IX Subsection "selectall_hashref"
.PP
.Vb 3
\& $hash_ref = $dbh\->selectall_hashref($statement, $key_field);
\& $hash_ref = $dbh\->selectall_hashref($statement, $key_field, \e%attr);
\& $hash_ref = $dbh\->selectall_hashref($statement, $key_field, \e%attr, @bind_values);
.Ve
.PP
This utility method combines "prepare", "execute" and
"fetchall_hashref" into a single call. It returns a reference to a
hash containing one entry, at most, for each row, as returned by \fBfetchall_hashref()\fR.
.PP
The \f(CW$statement\fR parameter can be a previously prepared statement handle,
in which case the \f(CW\*(C`prepare\*(C'\fR is skipped. This is recommended if the
statement is going to be executed many times.
.PP
The \f(CW$key_field\fR parameter defines which column, or columns, are used as keys
in the returned hash. It can either be the name of a single field, or a
reference to an array containing multiple field names. Using multiple names
yields a tree of nested hashes.
.PP
If a row has the same key as an earlier row then it replaces the earlier row.
.PP
If any method except \f(CW\*(C`fetchall_hashref\*(C'\fR fails, and "RaiseError" is not set,
\&\f(CW\*(C`selectall_hashref\*(C'\fR will return \f(CW\*(C`undef\*(C'\fR. If \f(CW\*(C`fetchall_hashref\*(C'\fR fails and
"RaiseError" is not set, then it will return with whatever data it
has fetched thus far. \f(CW$DBI::err\fR should be checked to catch that.
.PP
See \fBfetchall_hashref()\fR for more details.
.PP
\fR\f(CI\*(C`selectcol_arrayref\*(C'\fR\fI\fR
.IX Subsection "selectcol_arrayref"
.PP
.Vb 3
\& $ary_ref = $dbh\->selectcol_arrayref($statement);
\& $ary_ref = $dbh\->selectcol_arrayref($statement, \e%attr);
\& $ary_ref = $dbh\->selectcol_arrayref($statement, \e%attr, @bind_values);
.Ve
.PP
This utility method combines "prepare", "execute", and fetching one
column from all the rows, into a single call. It returns a reference to
an array containing the values of the first column from each row.
.PP
The \f(CW$statement\fR parameter can be a previously prepared statement handle,
in which case the \f(CW\*(C`prepare\*(C'\fR is skipped. This is recommended if the
statement is going to be executed many times.
.PP
If any method except \f(CW\*(C`fetch\*(C'\fR fails, and "RaiseError" is not set,
\&\f(CW\*(C`selectcol_arrayref\*(C'\fR will return \f(CW\*(C`undef\*(C'\fR. If \f(CW\*(C`fetch\*(C'\fR fails and
"RaiseError" is not set, then it will return with whatever data it
has fetched thus far. \f(CW$DBI::err\fR should be checked to catch that.
.PP
The \f(CW\*(C`selectcol_arrayref\*(C'\fR method defaults to pushing a single column
value (the first) from each row into the result array. However, it can
also push another column, or even multiple columns per row, into the
result array. This behaviour can be specified via a '\f(CW\*(C`Columns\*(C'\fR'
attribute which must be a ref to an array containing the column number
or numbers to use. For example:
.PP
.Vb 3
\& # get array of id and name pairs:
\& my $ary_ref = $dbh\->selectcol_arrayref("select id, name from table", { Columns=>[1,2] });
\& my %hash = @$ary_ref; # build hash from key\-value pairs so $hash{$id} => name
.Ve
.PP
You can specify a maximum number of rows to fetch by including a
\&'\f(CW\*(C`MaxRows\*(C'\fR' attribute in \e%attr.
.PP
\fR\f(CI\*(C`prepare\*(C'\fR\fI\fR
.IX Subsection "prepare"
.PP
.Vb 2
\& $sth = $dbh\->prepare($statement) or die $dbh\->errstr;
\& $sth = $dbh\->prepare($statement, \e%attr) or die $dbh\->errstr;
.Ve
.PP
Prepares a statement for later execution by the database
engine and returns a reference to a statement handle object.
.PP
The returned statement handle can be used to get attributes of the
statement and invoke the "execute" method. See "Statement Handle Methods".
.PP
Drivers for engines without the concept of preparing a
statement will typically just store the statement in the returned
handle and process it when \f(CW\*(C`$sth\->execute\*(C'\fR is called. Such drivers are
unlikely to give much useful information about the
statement, such as \f(CW\*(C`$sth\->{NUM_OF_FIELDS}\*(C'\fR, until after \f(CW\*(C`$sth\->execute\*(C'\fR
has been called. Portable applications should take this into account.
.PP
In general, DBI drivers do not parse the contents of the statement
(other than simply counting any Placeholders).
The statement is
passed directly to the database engine, sometimes known as pass-thru
mode. This has advantages and disadvantages. On the plus side, you can
access all the functionality of the engine being used. On the downside,
you're limited if you're using a simple engine, and you need to take extra care if
writing applications intended to be portable between engines.
.PP
Portable applications should not assume that a new statement can be
prepared and/or executed while still fetching results from a previous
statement.
.PP
Some command-line SQL tools use statement terminators, like a semicolon,
to indicate the end of a statement. Such terminators should not normally
be used with the DBI.
.PP
\fR\f(CI\*(C`prepare_cached\*(C'\fR\fI\fR
.IX Subsection "prepare_cached"
.PP
.Vb 3
\& $sth = $dbh\->prepare_cached($statement)
\& $sth = $dbh\->prepare_cached($statement, \e%attr)
\& $sth = $dbh\->prepare_cached($statement, \e%attr, $if_active)
.Ve
.PP
Like "prepare" except that the statement handle returned will be
stored in a hash associated with the \f(CW$dbh\fR. If another call is made to
\&\f(CW\*(C`prepare_cached\*(C'\fR with the same \f(CW$statement\fR and \f(CW%attr\fR parameter values,
then the corresponding cached \f(CW$sth\fR will be returned without contacting the
database server. Be sure to understand the cautions and caveats noted below.
.PP
The \f(CW$if_active\fR parameter lets you adjust the behaviour if an
already cached statement handle is still Active. There are several
alternatives:
.ie n .IP "\fB0\fR: A warning will be generated, and \fBfinish()\fR will be called on the statement handle before it is returned. This is the default behaviour if $if_active is not passed." 4
.el .IP "\fB0\fR: A warning will be generated, and \fBfinish()\fR will be called on the statement handle before it is returned. This is the default behaviour if \f(CW$if_active\fR is not passed." 4
.IX Item "0: A warning will be generated, and finish() will be called on the statement handle before it is returned. This is the default behaviour if $if_active is not passed."
.PD 0
.IP "\fB1\fR: \fBfinish()\fR will be called on the statement handle, but the warning is suppressed." 4
.IX Item "1: finish() will be called on the statement handle, but the warning is suppressed."
.IP "\fB2\fR: Disables any checking." 4
.IX Item "2: Disables any checking."
.IP "\fB3\fR: The existing active statement handle will be removed from the cache and a new statement handle prepared and cached in its place. This is the safest option because it doesn't affect the state of the old handle, it just removes it from the cache. [Added in DBI 1.40]" 4
.IX Item "3: The existing active statement handle will be removed from the cache and a new statement handle prepared and cached in its place. This is the safest option because it doesn't affect the state of the old handle, it just removes it from the cache. [Added in DBI 1.40]"
.PD
.PP
Here are some examples of \f(CW\*(C`prepare_cached\*(C'\fR:
.PP
.Vb 10
\& sub insert_hash {
\& my ($table, $field_values) = @_;
\& # sort to keep field order, and thus sql, stable for prepare_cached
\& my @fields = sort keys %$field_values;
\& my @values = @{$field_values}{@fields};
\& my $sql = sprintf "insert into %s (%s) values (%s)",
\& $table, join(",", @fields), join(",", ("?")x@fields);
\& my $sth = $dbh\->prepare_cached($sql);
\& return $sth\->execute(@values);
\& }
\&
\& sub search_hash {
\& my ($table, $field_values) = @_;
\& # sort to keep field order, and thus sql, stable for prepare_cached
\& my @fields = sort keys %$field_values;
\& my @values = @{$field_values}{@fields};
\& my $qualifier = "";
\& $qualifier = "where ".join(" and ", map { "$_=?" } @fields) if @fields;
\& $sth = $dbh\->prepare_cached("SELECT * FROM $table $qualifier");
\& return $dbh\->selectall_arrayref($sth, {}, @values);
\& }
.Ve
.PP
\&\fICaveat emptor:\fR This caching can be useful in some applications,
but it can also cause problems and should be used with care. Here
is a contrived case where caching would cause a significant problem:
.PP
.Vb 3
\& my $sth = $dbh\->prepare_cached(\*(AqSELECT * FROM foo WHERE bar=?\*(Aq);
\& $sth\->execute(...);
\& while (my $data = $sth\->fetchrow_hashref) {
\&
\& # later, in some other code called within the loop...
\& my $sth2 = $dbh\->prepare_cached(\*(AqSELECT * FROM foo WHERE bar=?\*(Aq);
\& $sth2\->execute(...);
\& while (my $data2 = $sth2\->fetchrow_arrayref) {
\& do_stuff(...);
\& }
\& }
.Ve
.PP
In this example, since both handles are preparing the exact same statement,
\&\f(CW$sth2\fR will not be its own statement handle, but a duplicate of \f(CW$sth\fR
returned from the cache. The results will certainly not be what you expect.
Typically the inner fetch loop will work normally, fetching all
the records and terminating when there are no more, but now that \f(CW$sth\fR
is the same as \f(CW$sth2\fR the outer fetch loop will also terminate.
.PP
You'll know if you run into this problem because \fBprepare_cached()\fR
will generate a warning by default (when \f(CW$if_active\fR is false).
.PP
The cache used by \fBprepare_cached()\fR is keyed by both the statement
and any attributes so you can also avoid this issue by doing something
like:
.PP
.Vb 1
\& $sth = $dbh\->prepare_cached("...", { dbi_dummy => _\|_FILE_\|_._\|_LINE_\|_ });
.Ve
.PP
which will ensure that prepare_cached only returns statements cached
by that line of code in that source file.
.PP
Also, to ensure the attributes passed are always the same, avoid passing
references inline. For example, the Slice attribute is specified as a
reference. Be sure to declare it external to the call to \fBprepare_cached()\fR, such
that a new hash reference is not created on every call. See "connect_cached"
for more details and examples.
.PP
If you'd like the cache to managed intelligently, you can tie the
hashref returned by \f(CW\*(C`CachedKids\*(C'\fR to an appropriate caching module,
such as Tie::Cache::LRU:
.PP
.Vb 3
\& my $cache;
\& tie %$cache, \*(AqTie::Cache::LRU\*(Aq, 500;
\& $dbh\->{CachedKids} = $cache;
.Ve
.PP
\fR\f(CI\*(C`commit\*(C'\fR\fI\fR
.IX Subsection "commit"
.PP
.Vb 1
\& $rc = $dbh\->commit or die $dbh\->errstr;
.Ve
.PP
Commit (make permanent) the most recent series of database changes
if the database supports transactions and AutoCommit is off.
.PP
If \f(CW\*(C`AutoCommit\*(C'\fR is on, then calling
\&\f(CW\*(C`commit\*(C'\fR will issue a "commit ineffective with AutoCommit" warning.
.PP
See also "Transactions" in the "FURTHER INFORMATION" section below.
.PP
\fR\f(CI\*(C`rollback\*(C'\fR\fI\fR
.IX Subsection "rollback"
.PP
.Vb 1
\& $rc = $dbh\->rollback or die $dbh\->errstr;
.Ve
.PP
Rollback (undo) the most recent series of uncommitted database
changes if the database supports transactions and AutoCommit is off.
.PP
If \f(CW\*(C`AutoCommit\*(C'\fR is on, then calling
\&\f(CW\*(C`rollback\*(C'\fR will issue a "rollback ineffective with AutoCommit" warning.
.PP
See also "Transactions" in the "FURTHER INFORMATION" section below.
.PP
\fR\f(CI\*(C`begin_work\*(C'\fR\fI\fR
.IX Subsection "begin_work"
.PP
.Vb 1
\& $rc = $dbh\->begin_work or die $dbh\->errstr;
.Ve
.PP
Enable transactions (by turning \f(CW\*(C`AutoCommit\*(C'\fR off) until the next call
to \f(CW\*(C`commit\*(C'\fR or \f(CW\*(C`rollback\*(C'\fR. After the next \f(CW\*(C`commit\*(C'\fR or \f(CW\*(C`rollback\*(C'\fR,
\&\f(CW\*(C`AutoCommit\*(C'\fR will automatically be turned on again.
.PP
If \f(CW\*(C`AutoCommit\*(C'\fR is already off when \f(CW\*(C`begin_work\*(C'\fR is called then
it does nothing except return an error. If the driver does not support
transactions then when \f(CW\*(C`begin_work\*(C'\fR attempts to set \f(CW\*(C`AutoCommit\*(C'\fR off
the driver will trigger a fatal error.
.PP
See also "Transactions" in the "FURTHER INFORMATION" section below.
.PP
\fR\f(CI\*(C`disconnect\*(C'\fR\fI\fR
.IX Subsection "disconnect"
.PP
.Vb 1
\& $rc = $dbh\->disconnect or warn $dbh\->errstr;
.Ve
.PP
Disconnects the database from the database handle. \f(CW\*(C`disconnect\*(C'\fR is typically only used
before exiting the program. The handle is of little use after disconnecting.
.PP
The transaction behaviour of the \f(CW\*(C`disconnect\*(C'\fR method is, sadly,
undefined. Some database systems (such as Oracle and Ingres) will
automatically commit any outstanding changes, but others (such as
Informix) will rollback any outstanding changes. Applications not
using \f(CW\*(C`AutoCommit\*(C'\fR should explicitly call \f(CW\*(C`commit\*(C'\fR or \f(CW\*(C`rollback\*(C'\fR before
calling \f(CW\*(C`disconnect\*(C'\fR.
.PP
The database is automatically disconnected by the \f(CW\*(C`DESTROY\*(C'\fR method if
still connected when there are no longer any references to the handle.
The \f(CW\*(C`DESTROY\*(C'\fR method for each driver should implicitly call \f(CW\*(C`rollback\*(C'\fR to
undo any uncommitted changes. This is vital behaviour to ensure that
incomplete transactions don't get committed simply because Perl calls
\&\f(CW\*(C`DESTROY\*(C'\fR on every object before exiting. Also, do not rely on the order
of object destruction during "global destruction", as it is undefined.
.PP
Generally, if you want your changes to be committed or rolled back when
you disconnect, then you should explicitly call "commit" or "rollback"
before disconnecting.
.PP
If you disconnect from a database while you still have active
statement handles (e.g., SELECT statement handles that may have
more data to fetch), you will get a warning. The warning may indicate
that a fetch loop terminated early, perhaps due to an uncaught error.
To avoid the warning call the \f(CW\*(C`finish\*(C'\fR method on the active handles.
.PP
\fR\f(CI\*(C`ping\*(C'\fR\fI\fR
.IX Subsection "ping"
.PP
.Vb 1
\& $rc = $dbh\->ping;
.Ve
.PP
Attempts to determine, in a reasonably efficient way, if the database
server is still running and the connection to it is still working.
Individual drivers should implement this function in the most suitable
manner for their database engine.
.PP
The current \fIdefault\fR implementation always returns true without
actually doing anything. Actually, it returns "\f(CW\*(C`0 but true\*(C'\fR" which is
true but zero. That way you can tell if the return value is genuine or
just the default. Drivers should override this method with one that
does the right thing for their type of database.
.PP
Few applications would have direct use for this method. See the specialized
Apache::DBI module for one example usage.
.PP
\fR\f(CI\*(C`get_info\*(C'\fR\fI\fR
.IX Subsection "get_info"
.PP
.Vb 1
\& $value = $dbh\->get_info( $info_type );
.Ve
.PP
Returns information about the implementation, i.e. driver and data
source capabilities, restrictions etc. It returns \f(CW\*(C`undef\*(C'\fR for
unknown or unimplemented information types. For example:
.PP
.Vb 2
\& $database_version = $dbh\->get_info( 18 ); # SQL_DBMS_VER
\& $max_select_tables = $dbh\->get_info( 106 ); # SQL_MAXIMUM_TABLES_IN_SELECT
.Ve
.PP
See "Standards Reference Information" for more detailed information
about the information types and their meanings and possible return values.
.PP
The DBI::Const::GetInfoType module exports a \f(CW%GetInfoType\fR hash that
can be used to map info type names to numbers. For example:
.PP
.Vb 1
\& $database_version = $dbh\->get_info( $GetInfoType{SQL_DBMS_VER} );
.Ve
.PP
The names are a merging of the ANSI and ODBC standards (which differ
in some cases). See DBI::Const::GetInfoType for more details.
.PP
Because some DBI methods make use of \fBget_info()\fR, drivers are strongly
encouraged to support \fIat least\fR the following very minimal set
of information types to ensure the DBI itself works properly:
.PP
.Vb 7
\& Type Name Example A Example B
\& \-\-\-\- \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \-\-\-\-\-\-\-\-\-\-\-\- \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
\& 17 SQL_DBMS_NAME \*(AqACCESS\*(Aq \*(AqOracle\*(Aq
\& 18 SQL_DBMS_VER \*(Aq03.50.0000\*(Aq \*(Aq08.01.0721 ...\*(Aq
\& 29 SQL_IDENTIFIER_QUOTE_CHAR \*(Aq\`\*(Aq \*(Aq"\*(Aq
\& 41 SQL_CATALOG_NAME_SEPARATOR \*(Aq.\*(Aq \*(Aq@\*(Aq
\& 114 SQL_CATALOG_LOCATION 1 2
.Ve
.PP
Values from 9000 to 9999 for get_info are officially reserved for use by Perl DBI.
Values in that range which have been assigned a meaning are defined here:
.PP
\&\f(CW9000\fR: true if a backslash character (\f(CW\*(C`\e\*(C'\fR) before placeholder-like text
(e.g. \f(CW\*(C`?\*(C'\fR, \f(CW\*(C`:foo\*(C'\fR) will prevent it being treated as a placeholder by the driver.
The backslash will be removed before the text is passed to the backend.
.PP
\fR\f(CI\*(C`table_info\*(C'\fR\fI\fR
.IX Subsection "table_info"
.PP
.Vb 2
\& $sth = $dbh\->table_info( $catalog, $schema, $table, $type );
\& $sth = $dbh\->table_info( $catalog, $schema, $table, $type, \e%attr );
\&
\& # then $sth\->fetchall_arrayref or $sth\->fetchall_hashref etc
.Ve
.PP
Returns an active statement handle that can be used to fetch
information about tables and views that exist in the database.
.PP
The arguments \f(CW$catalog\fR, \f(CW$schema\fR and \f(CW$table\fR may accept search patterns
according to the database/driver, for example: \f(CW$table\fR = '%FOO%';
Remember that the underscore character ('\f(CW\*(C`_\*(C'\fR') is a search pattern
that means match any character, so 'FOO_%' is the same as 'FOO%'
and 'FOO_BAR%' will match names like 'FOO1BAR'.
.PP
The value of \f(CW$type\fR is a comma-separated list of one or more types of
tables to be returned in the result set. Each value may optionally be
quoted, e.g.:
.PP
.Vb 2
\& $type = "TABLE";
\& $type = "\*(AqTABLE\*(Aq,\*(AqVIEW\*(Aq";
.Ve
.PP
In addition the following special cases may also be supported by some drivers:
.IP \(bu 4
If the value of \f(CW$catalog\fR is '%' and \f(CW$schema\fR and \f(CW$table\fR name
are empty strings, the result set contains a list of catalog names.
For example:
.Sp
.Vb 1
\& $sth = $dbh\->table_info(\*(Aq%\*(Aq, \*(Aq\*(Aq, \*(Aq\*(Aq);
.Ve
.IP \(bu 4
If the value of \f(CW$schema\fR is '%' and \f(CW$catalog\fR and \f(CW$table\fR are empty
strings, the result set contains a list of schema names.
.IP \(bu 4
If the value of \f(CW$type\fR is '%' and \f(CW$catalog\fR, \f(CW$schema\fR, and \f(CW$table\fR are all
empty strings, the result set contains a list of table types.
.PP
If your driver doesn't support one or more of the selection filter
parameters then you may get back more than you asked for and can
do the filtering yourself.
.PP
This method can be expensive, and can return a large amount of data.
(For example, small Oracle installation returns over 2000 rows.)
So it's a good idea to use the filters to limit the data as much as possible.
.PP
The statement handle returned has at least the following fields in the
order show below. Other fields, after these, may also be present.
.PP
\&\fBTABLE_CAT\fR: Table catalog identifier. This field is NULL (\f(CW\*(C`undef\*(C'\fR) if not
applicable to the data source, which is usually the case. This field
is empty if not applicable to the table.
.PP
\&\fBTABLE_SCHEM\fR: The name of the schema containing the TABLE_NAME value.
This field is NULL (\f(CW\*(C`undef\*(C'\fR) if not applicable to data source, and
empty if not applicable to the table.
.PP
\&\fBTABLE_NAME\fR: Name of the table (or view, synonym, etc).
.PP
\&\fBTABLE_TYPE\fR: One of the following: "TABLE", "VIEW", "SYSTEM TABLE",
"GLOBAL TEMPORARY", "LOCAL TEMPORARY", "ALIAS", "SYNONYM" or a type
identifier that is specific to the data
source.
.PP
\&\fBREMARKS\fR: A description of the table. May be NULL (\f(CW\*(C`undef\*(C'\fR).
.PP
Note that \f(CW\*(C`table_info\*(C'\fR might not return records for all tables.
Applications can use any valid table regardless of whether it's
returned by \f(CW\*(C`table_info\*(C'\fR.
.PP
See also "tables", "Catalog Methods" and
"Standards Reference Information".
.PP
\fR\f(CI\*(C`column_info\*(C'\fR\fI\fR
.IX Subsection "column_info"
.PP
.Vb 1
\& $sth = $dbh\->column_info( $catalog, $schema, $table, $column );
\&
\& # then $sth\->fetchall_arrayref or $sth\->fetchall_hashref etc
.Ve
.PP
Returns an active statement handle that can be used to fetch
information about columns in specified tables.
.PP
The arguments \f(CW$schema\fR, \f(CW$table\fR and \f(CW$column\fR may accept search patterns
according to the database/driver, for example: \f(CW$table\fR = '%FOO%';
.PP
Note: The support for the selection criteria is driver specific. If the
driver doesn't support one or more of them then you may get back more
than you asked for and can do the filtering yourself.
.PP
Note: If your driver does not support column_info an undef is
returned. This is distinct from asking for something which does not
exist in a driver which supports column_info as a valid statement
handle to an empty result-set will be returned in this case.
.PP
If the arguments don't match any tables then you'll still get a statement
handle, it'll just return no rows.
.PP
The statement handle returned has at least the following fields in the
order shown below. Other fields, after these, may also be present.
.PP
\&\fBTABLE_CAT\fR: The catalog identifier.
This field is NULL (\f(CW\*(C`undef\*(C'\fR) if not applicable to the data source,
which is often the case. This field is empty if not applicable to the
table.
.PP
\&\fBTABLE_SCHEM\fR: The schema identifier.
This field is NULL (\f(CW\*(C`undef\*(C'\fR) if not applicable to the data source,
and empty if not applicable to the table.
.PP
\&\fBTABLE_NAME\fR: The table identifier.
Note: A driver may provide column metadata not only for base tables, but
also for derived objects like SYNONYMS etc.
.PP
\&\fBCOLUMN_NAME\fR: The column identifier.
.PP
\&\fBDATA_TYPE\fR: The concise data type code.
.PP
\&\fBTYPE_NAME\fR: A data source dependent data type name.
.PP
\&\fBCOLUMN_SIZE\fR: The column size.
This is the maximum length in characters for character data types,
the number of digits or bits for numeric data types or the length
in the representation of temporal types.
See the relevant specifications for detailed information.
.PP
\&\fBBUFFER_LENGTH\fR: The length in bytes of transferred data.
.PP
\&\fBDECIMAL_DIGITS\fR: The total number of significant digits to the right of
the decimal point.
.PP
\&\fBNUM_PREC_RADIX\fR: The radix for numeric precision.
The value is 10 or 2 for numeric data types and NULL (\f(CW\*(C`undef\*(C'\fR) if not
applicable.
.PP
\&\fBNULLABLE\fR: Indicates if a column can accept NULLs.
The following values are defined:
.PP
.Vb 3
\& SQL_NO_NULLS 0
\& SQL_NULLABLE 1
\& SQL_NULLABLE_UNKNOWN 2
.Ve
.PP
\&\fBREMARKS\fR: A description of the column.
.PP
\&\fBCOLUMN_DEF\fR: The default value of the column, in a format that can be used
directly in an SQL statement.
.PP
Note that this may be an expression and not simply the text used for the
default value in the original CREATE TABLE statement. For example, given:
.PP
.Vb 2
\& col1 char(30) default current_user \-\- a \*(Aqfunction\*(Aq
\& col2 char(30) default \*(Aqstring\*(Aq \-\- a string literal
.Ve
.PP
where "current_user" is the name of a function, the corresponding \f(CW\*(C`COLUMN_DEF\*(C'\fR
values would be:
.PP
.Vb 5
\& Database col1 col2
\& \-\-\-\-\-\-\-\- \-\-\-\- \-\-\-\-
\& Oracle: current_user \*(Aqstring\*(Aq
\& Postgres: "current_user"() \*(Aqstring\*(Aq::text
\& MS SQL: (user_name()) (\*(Aqstring\*(Aq)
.Ve
.PP
\&\fBSQL_DATA_TYPE\fR: The SQL data type.
.PP
\&\fBSQL_DATETIME_SUB\fR: The subtype code for datetime and interval data types.
.PP
\&\fBCHAR_OCTET_LENGTH\fR: The maximum length in bytes of a character or binary
data type column.
.PP
\&\fBORDINAL_POSITION\fR: The column sequence number (starting with 1).
.PP
\&\fBIS_NULLABLE\fR: Indicates if the column can accept NULLs.
Possible values are: 'NO', 'YES' and ''.
.PP
SQL/CLI defines the following additional columns:
.PP
.Vb 10
\& CHAR_SET_CAT
\& CHAR_SET_SCHEM
\& CHAR_SET_NAME
\& COLLATION_CAT
\& COLLATION_SCHEM
\& COLLATION_NAME
\& UDT_CAT
\& UDT_SCHEM
\& UDT_NAME
\& DOMAIN_CAT
\& DOMAIN_SCHEM
\& DOMAIN_NAME
\& SCOPE_CAT
\& SCOPE_SCHEM
\& SCOPE_NAME
\& MAX_CARDINALITY
\& DTD_IDENTIFIER
\& IS_SELF_REF
.Ve
.PP
Drivers capable of supplying any of those values should do so in
the corresponding column and supply undef values for the others.
.PP
Drivers wishing to provide extra database/driver specific information
should do so in extra columns beyond all those listed above, and
use lowercase field names with the driver-specific prefix (i.e.,
\&'ora_...'). Applications accessing such fields should do so by name
and not by column number.
.PP
The result set is ordered by TABLE_CAT, TABLE_SCHEM, TABLE_NAME
and ORDINAL_POSITION.
.PP
Note: There is some overlap with statement handle attributes (in perl) and
SQLDescribeCol (in ODBC). However, SQLColumns provides more metadata.
.PP
See also "Catalog Methods" and "Standards Reference Information".
.PP
\fR\f(CI\*(C`primary_key_info\*(C'\fR\fI\fR
.IX Subsection "primary_key_info"
.PP
.Vb 1
\& $sth = $dbh\->primary_key_info( $catalog, $schema, $table );
\&
\& # then $sth\->fetchall_arrayref or $sth\->fetchall_hashref etc
.Ve
.PP
Returns an active statement handle that can be used to fetch information
about columns that make up the primary key for a table.
The arguments don't accept search patterns (unlike \fBtable_info()\fR).
.PP
The statement handle will return one row per column, ordered by
TABLE_CAT, TABLE_SCHEM, TABLE_NAME, and KEY_SEQ.
If there is no primary key then the statement handle will fetch no rows.
.PP
Note: The support for the selection criteria, such as \f(CW$catalog\fR, is
driver specific. If the driver doesn't support catalogs and/or
schemas, it may ignore these criteria.
.PP
The statement handle returned has at least the following fields in the
order shown below. Other fields, after these, may also be present.
.PP
\&\fBTABLE_CAT\fR: The catalog identifier.
This field is NULL (\f(CW\*(C`undef\*(C'\fR) if not applicable to the data source,
which is often the case. This field is empty if not applicable to the
table.
.PP
\&\fBTABLE_SCHEM\fR: The schema identifier.
This field is NULL (\f(CW\*(C`undef\*(C'\fR) if not applicable to the data source,
and empty if not applicable to the table.
.PP
\&\fBTABLE_NAME\fR: The table identifier.
.PP
\&\fBCOLUMN_NAME\fR: The column identifier.
.PP
\&\fBKEY_SEQ\fR: The column sequence number (starting with 1).
Note: This field is named \fBORDINAL_POSITION\fR in SQL/CLI.
.PP
\&\fBPK_NAME\fR: The primary key constraint identifier.
This field is NULL (\f(CW\*(C`undef\*(C'\fR) if not applicable to the data source.
.PP
See also "Catalog Methods" and "Standards Reference Information".
.PP
\fR\f(CI\*(C`primary_key\*(C'\fR\fI\fR
.IX Subsection "primary_key"
.PP
.Vb 1
\& @key_column_names = $dbh\->primary_key( $catalog, $schema, $table );
.Ve
.PP
Simple interface to the \fBprimary_key_info()\fR method. Returns a list of
the column names that comprise the primary key of the specified table.
The list is in primary key column sequence order.
If there is no primary key then an empty list is returned.
.PP
\fR\f(CI\*(C`foreign_key_info\*(C'\fR\fI\fR
.IX Subsection "foreign_key_info"
.PP
.Vb 2
\& $sth = $dbh\->foreign_key_info( $pk_catalog, $pk_schema, $pk_table
\& , $fk_catalog, $fk_schema, $fk_table );
\&
\& $sth = $dbh\->foreign_key_info( $pk_catalog, $pk_schema, $pk_table
\& , $fk_catalog, $fk_schema, $fk_table
\& , \e%attr );
\&
\& # then $sth\->fetchall_arrayref or $sth\->fetchall_hashref etc
.Ve
.PP
Returns an active statement handle that can be used to fetch information
about foreign keys in and/or referencing the specified table(s).
The arguments don't accept search patterns (unlike \fBtable_info()\fR).
.PP
\&\f(CW$pk_catalog\fR, \f(CW$pk_schema\fR, \f(CW$pk_table\fR
identify the primary (unique) key table (\fBPKT\fR).
.PP
\&\f(CW$fk_catalog\fR, \f(CW$fk_schema\fR, \f(CW$fk_table\fR
identify the foreign key table (\fBFKT\fR).
.PP
If both \fBPKT\fR and \fBFKT\fR are given, the function returns the foreign key, if
any, in table \fBFKT\fR that refers to the primary (unique) key of table \fBPKT\fR.
(Note: In SQL/CLI, the result is implementation-defined.)
.PP
If only \fBPKT\fR is given, then the result set contains the primary key
of that table and all foreign keys that refer to it.
.PP
If only \fBFKT\fR is given, then the result set contains all foreign keys
in that table and the primary keys to which they refer.
(Note: In SQL/CLI, the result includes unique keys too.)
.PP
For example:
.PP
.Vb 3
\& $sth = $dbh\->foreign_key_info( undef, $user, \*(Aqmaster\*(Aq);
\& $sth = $dbh\->foreign_key_info( undef, undef, undef , undef, $user, \*(Aqdetail\*(Aq);
\& $sth = $dbh\->foreign_key_info( undef, $user, \*(Aqmaster\*(Aq, undef, $user, \*(Aqdetail\*(Aq);
\&
\& # then $sth\->fetchall_arrayref or $sth\->fetchall_hashref etc
.Ve
.PP
Note: The support for the selection criteria, such as \f(CW$catalog\fR, is
driver specific. If the driver doesn't support catalogs and/or
schemas, it may ignore these criteria.
.PP
The statement handle returned has the following fields in the order shown below.
Because ODBC never includes unique keys, they define different columns in the
result set than SQL/CLI. SQL/CLI column names are shown in parentheses.
.PP
\&\fBPKTABLE_CAT ( UK_TABLE_CAT )\fR:
The primary (unique) key table catalog identifier.
This field is NULL (\f(CW\*(C`undef\*(C'\fR) if not applicable to the data source,
which is often the case. This field is empty if not applicable to the
table.
.PP
\&\fBPKTABLE_SCHEM ( UK_TABLE_SCHEM )\fR:
The primary (unique) key table schema identifier.
This field is NULL (\f(CW\*(C`undef\*(C'\fR) if not applicable to the data source,
and empty if not applicable to the table.
.PP
\&\fBPKTABLE_NAME ( UK_TABLE_NAME )\fR:
The primary (unique) key table identifier.
.PP
\&\fBPKCOLUMN_NAME (UK_COLUMN_NAME )\fR:
The primary (unique) key column identifier.
.PP
\&\fBFKTABLE_CAT ( FK_TABLE_CAT )\fR:
The foreign key table catalog identifier.
This field is NULL (\f(CW\*(C`undef\*(C'\fR) if not applicable to the data source,
which is often the case. This field is empty if not applicable to the
table.
.PP
\&\fBFKTABLE_SCHEM ( FK_TABLE_SCHEM )\fR:
The foreign key table schema identifier.
This field is NULL (\f(CW\*(C`undef\*(C'\fR) if not applicable to the data source,
and empty if not applicable to the table.
.PP
\&\fBFKTABLE_NAME ( FK_TABLE_NAME )\fR:
The foreign key table identifier.
.PP
\&\fBFKCOLUMN_NAME ( FK_COLUMN_NAME )\fR:
The foreign key column identifier.
.PP
\&\fBKEY_SEQ ( ORDINAL_POSITION )\fR:
The column sequence number (starting with 1).
.PP
\&\fBUPDATE_RULE ( UPDATE_RULE )\fR:
The referential action for the UPDATE rule.
The following codes are defined:
.PP
.Vb 5
\& CASCADE 0
\& RESTRICT 1
\& SET NULL 2
\& NO ACTION 3
\& SET DEFAULT 4
.Ve
.PP
\&\fBDELETE_RULE ( DELETE_RULE )\fR:
The referential action for the DELETE rule.
The codes are the same as for UPDATE_RULE.
.PP
\&\fBFK_NAME ( FK_NAME )\fR:
The foreign key name.
.PP
\&\fBPK_NAME ( UK_NAME )\fR:
The primary (unique) key name.
.PP
\&\fBDEFERRABILITY ( DEFERABILITY )\fR:
The deferrability of the foreign key constraint.
The following codes are defined:
.PP
.Vb 3
\& INITIALLY DEFERRED 5
\& INITIALLY IMMEDIATE 6
\& NOT DEFERRABLE 7
.Ve
.PP
\&\fB ( UNIQUE_OR_PRIMARY )\fR:
This column is necessary if a driver includes all candidate (i.e. primary and
alternate) keys in the result set (as specified by SQL/CLI).
The value of this column is UNIQUE if the foreign key references an alternate
key and PRIMARY if the foreign key references a primary key, or it
may be undefined if the driver doesn't have access to the information.
.PP
See also "Catalog Methods" and "Standards Reference Information".
.PP
\fR\f(CI\*(C`statistics_info\*(C'\fR\fI\fR
.IX Subsection "statistics_info"
.PP
\&\fBWarning:\fR This method is experimental and may change.
.PP
.Vb 1
\& $sth = $dbh\->statistics_info( $catalog, $schema, $table, $unique_only, $quick );
\&
\& # then $sth\->fetchall_arrayref or $sth\->fetchall_hashref etc
.Ve
.PP
Returns an active statement handle that can be used to fetch statistical
information about a table and its indexes.
.PP
The arguments don't accept search patterns (unlike "table_info").
.PP
If the boolean argument \f(CW$unique_only\fR is true, only UNIQUE indexes will be
returned in the result set, otherwise all indexes will be returned.
.PP
If the boolean argument \f(CW$quick\fR is set, the actual statistical information
columns (CARDINALITY and PAGES) will only be returned if they are readily
available from the server, and might not be current. Some databases may
return stale statistics or no statistics at all with this flag set.
.PP
The statement handle will return at most one row per column name per index,
plus at most one row for the entire table itself, ordered by NON_UNIQUE, TYPE,
INDEX_QUALIFIER, INDEX_NAME, and ORDINAL_POSITION.
.PP
Note: The support for the selection criteria, such as \f(CW$catalog\fR, is
driver specific. If the driver doesn't support catalogs and/or
schemas, it may ignore these criteria.
.PP
The statement handle returned has at least the following fields in the
order shown below. Other fields, after these, may also be present.
.PP
\&\fBTABLE_CAT\fR: The catalog identifier.
This field is NULL (\f(CW\*(C`undef\*(C'\fR) if not applicable to the data source,
which is often the case. This field is empty if not applicable to the
table.
.PP
\&\fBTABLE_SCHEM\fR: The schema identifier.
This field is NULL (\f(CW\*(C`undef\*(C'\fR) if not applicable to the data source,
and empty if not applicable to the table.
.PP
\&\fBTABLE_NAME\fR: The table identifier.
.PP
\&\fBNON_UNIQUE\fR: Unique index indicator.
Returns 0 for unique indexes, 1 for non-unique indexes
.PP
\&\fBINDEX_QUALIFIER\fR: Index qualifier identifier.
The identifier that is used to qualify the index name when doing a
\&\f(CW\*(C`DROP INDEX\*(C'\fR; NULL (\f(CW\*(C`undef\*(C'\fR) is returned if an index qualifier is not
supported by the data source.
If a non-NULL (defined) value is returned in this column, it must be used
to qualify the index name on a \f(CW\*(C`DROP INDEX\*(C'\fR statement; otherwise,
the TABLE_SCHEM should be used to qualify the index name.
.PP
\&\fBINDEX_NAME\fR: The index identifier.
.PP
\&\fBTYPE\fR: The type of information being returned. Can be any of the
following values: 'table', 'btree', 'clustered', 'content', 'hashed',
or 'other'.
.PP
In the case that this field is 'table', all fields
other than TABLE_CAT, TABLE_SCHEM, TABLE_NAME, TYPE,
CARDINALITY, and PAGES will be NULL (\f(CW\*(C`undef\*(C'\fR).
.PP
\&\fBORDINAL_POSITION\fR: Column sequence number (starting with 1).
.PP
\&\fBCOLUMN_NAME\fR: The column identifier.
.PP
\&\fBASC_OR_DESC\fR: Column sort sequence.
\&\f(CW\*(C`A\*(C'\fR for Ascending, \f(CW\*(C`D\*(C'\fR for Descending, or NULL (\f(CW\*(C`undef\*(C'\fR) if
not supported for this index.
.PP
\&\fBCARDINALITY\fR: Cardinality of the table or index.
For indexes, this is the number of unique values in the index.
For tables, this is the number of rows in the table.
If not supported, the value will be NULL (\f(CW\*(C`undef\*(C'\fR).
.PP
\&\fBPAGES\fR: Number of storage pages used by this table or index.
If not supported, the value will be NULL (\f(CW\*(C`undef\*(C'\fR).
.PP
\&\fBFILTER_CONDITION\fR: The index filter condition as a string.
If the index is not a filtered index, or it cannot be determined
whether the index is a filtered index, this value is NULL (\f(CW\*(C`undef\*(C'\fR).
If the index is a filtered index, but the filter condition
cannot be determined, this value is the empty string \f(CW\*(Aq\*(Aq\fR.
Otherwise it will be the literal filter condition as a string,
such as \f(CW\*(C`SALARY <= 4500\*(C'\fR.
.PP
See also "Catalog Methods" and "Standards Reference Information".
.PP
\fR\f(CI\*(C`tables\*(C'\fR\fI\fR
.IX Subsection "tables"
.PP
.Vb 2
\& @names = $dbh\->tables( $catalog, $schema, $table, $type );
\& @names = $dbh\->tables; # deprecated
.Ve
.PP
Simple interface to \fBtable_info()\fR. Returns a list of matching
table names, possibly including a catalog/schema prefix.
.PP
See "table_info" for a description of the parameters.
.PP
If \f(CW\*(C`$dbh\->get_info(29)\*(C'\fR returns true (29 is SQL_IDENTIFIER_QUOTE_CHAR)
then the table names are constructed and quoted by "quote_identifier"
to ensure they are usable even if they contain whitespace or reserved
words etc. This means that the table names returned will include
quote characters.
.PP
\fR\f(CI\*(C`type_info_all\*(C'\fR\fI\fR
.IX Subsection "type_info_all"
.PP
.Vb 1
\& $type_info_all = $dbh\->type_info_all;
.Ve
.PP
Returns a reference to an array which holds information about each data
type variant supported by the database and driver. The array and its
contents should be treated as read-only.
.PP
The first item is a reference to an 'index' hash of \f(CW\*(C`Name =\*(C'\fR> \f(CW\*(C`Index\*(C'\fR pairs.
The items following that are references to arrays, one per supported data
type variant. The leading index hash defines the names and order of the
fields within the arrays that follow it.
For example:
.PP
.Vb 10
\& $type_info_all = [
\& { TYPE_NAME => 0,
\& DATA_TYPE => 1,
\& COLUMN_SIZE => 2, # was PRECISION originally
\& LITERAL_PREFIX => 3,
\& LITERAL_SUFFIX => 4,
\& CREATE_PARAMS => 5,
\& NULLABLE => 6,
\& CASE_SENSITIVE => 7,
\& SEARCHABLE => 8,
\& UNSIGNED_ATTRIBUTE=> 9,
\& FIXED_PREC_SCALE => 10, # was MONEY originally
\& AUTO_UNIQUE_VALUE => 11, # was AUTO_INCREMENT originally
\& LOCAL_TYPE_NAME => 12,
\& MINIMUM_SCALE => 13,
\& MAXIMUM_SCALE => 14,
\& SQL_DATA_TYPE => 15,
\& SQL_DATETIME_SUB => 16,
\& NUM_PREC_RADIX => 17,
\& INTERVAL_PRECISION=> 18,
\& },
\& [ \*(AqVARCHAR\*(Aq, SQL_VARCHAR,
\& undef, "\*(Aq","\*(Aq", undef,0, 1,1,0,0,0,undef,1,255, undef
\& ],
\& [ \*(AqINTEGER\*(Aq, SQL_INTEGER,
\& undef, "", "", undef,0, 0,1,0,0,0,undef,0, 0, 10
\& ],
\& ];
.Ve
.PP
More than one row may have the same value in the \f(CW\*(C`DATA_TYPE\*(C'\fR
field if there are different ways to spell the type name and/or there
are variants of the type with different attributes (e.g., with and
without \f(CW\*(C`AUTO_UNIQUE_VALUE\*(C'\fR set, with and without \f(CW\*(C`UNSIGNED_ATTRIBUTE\*(C'\fR, etc).
.PP
The rows are ordered by \f(CW\*(C`DATA_TYPE\*(C'\fR first and then by how closely each
type maps to the corresponding ODBC SQL data type, closest first.
.PP
The meaning of the fields is described in the documentation for
the "type_info" method.
.PP
An 'index' hash is provided so you don't need to rely on index
values defined above. However, using DBD::ODBC with some old ODBC
drivers may return older names, shown as comments in the example above.
Another issue with the index hash is that the lettercase of the
keys is not defined. It is usually uppercase, as show here, but
drivers may return names with any lettercase.
.PP
Drivers are also free to return extra driver-specific columns of
information \- though it's recommended that they start at column
index 50 to leave room for expansion of the DBI/ODBC specification.
.PP
The \fBtype_info_all()\fR method is not normally used directly.
The "type_info" method provides a more usable and useful interface
to the data.
.PP
\fR\f(CI\*(C`type_info\*(C'\fR\fI\fR
.IX Subsection "type_info"
.PP
.Vb 1
\& @type_info = $dbh\->type_info($data_type);
.Ve
.PP
Returns a list of hash references holding information about one or more
variants of \f(CW$data_type\fR. The list is ordered by \f(CW\*(C`DATA_TYPE\*(C'\fR first and
then by how closely each type maps to the corresponding ODBC SQL data
type, closest first. If called in a scalar context then only the first
(best) element is returned.
.PP
If \f(CW$data_type\fR is undefined or \f(CW\*(C`SQL_ALL_TYPES\*(C'\fR, then the list will
contain hashes for all data type variants supported by the database and driver.
.PP
If \f(CW$data_type\fR is an array reference then \f(CW\*(C`type_info\*(C'\fR returns the
information for the \fIfirst\fR type in the array that has any matches.
.PP
The keys of the hash follow the same letter case conventions as the
rest of the DBI (see "Naming Conventions and Name Space"). The
following uppercase items should always exist, though may be undef:
.IP "TYPE_NAME (string)" 4
.IX Item "TYPE_NAME (string)"
Data type name for use in CREATE TABLE statements etc.
.IP "DATA_TYPE (integer)" 4
.IX Item "DATA_TYPE (integer)"
SQL data type number.
.IP "COLUMN_SIZE (integer)" 4
.IX Item "COLUMN_SIZE (integer)"
For numeric types, this is either the total number of digits (if the
NUM_PREC_RADIX value is 10) or the total number of bits allowed in the
column (if NUM_PREC_RADIX is 2).
.Sp
For string types, this is the maximum size of the string in characters.
.Sp
For date and interval types, this is the maximum number of characters
needed to display the value.
.IP "LITERAL_PREFIX (string)" 4
.IX Item "LITERAL_PREFIX (string)"
Characters used to prefix a literal. A typical prefix is "\f(CW\*(C`\*(Aq\*(C'\fR" for characters,
or possibly "\f(CW\*(C`0x\*(C'\fR" for binary values passed as hexadecimal. NULL (\f(CW\*(C`undef\*(C'\fR) is
returned for data types for which this is not applicable.
.IP "LITERAL_SUFFIX (string)" 4
.IX Item "LITERAL_SUFFIX (string)"
Characters used to suffix a literal. Typically "\f(CW\*(C`\*(Aq\*(C'\fR" for characters.
NULL (\f(CW\*(C`undef\*(C'\fR) is returned for data types where this is not applicable.
.IP "CREATE_PARAMS (string)" 4
.IX Item "CREATE_PARAMS (string)"
Parameter names for data type definition. For example, \f(CW\*(C`CREATE_PARAMS\*(C'\fR for a
\&\f(CW\*(C`DECIMAL\*(C'\fR would be "\f(CW\*(C`precision,scale\*(C'\fR" if the DECIMAL type should be
declared as \f(CW\*(C`DECIMAL(\*(C'\fR\fIprecision,scale\fR\f(CW\*(C`)\*(C'\fR where \fIprecision\fR and \fIscale\fR
are integer values. For a \f(CW\*(C`VARCHAR\*(C'\fR it would be "\f(CW\*(C`max length\*(C'\fR".
NULL (\f(CW\*(C`undef\*(C'\fR) is returned for data types for which this is not applicable.
.IP "NULLABLE (integer)" 4
.IX Item "NULLABLE (integer)"
Indicates whether the data type accepts a NULL value:
\&\f(CW0\fR or an empty string = no, \f(CW1\fR = yes, \f(CW2\fR = unknown.
.IP "CASE_SENSITIVE (boolean)" 4
.IX Item "CASE_SENSITIVE (boolean)"
Indicates whether the data type is case sensitive in collations and
comparisons.
.IP "SEARCHABLE (integer)" 4
.IX Item "SEARCHABLE (integer)"
Indicates how the data type can be used in a WHERE clause, as
follows:
.Sp
.Vb 4
\& 0 \- Cannot be used in a WHERE clause
\& 1 \- Only with a LIKE predicate
\& 2 \- All comparison operators except LIKE
\& 3 \- Can be used in a WHERE clause with any comparison operator
.Ve
.IP "UNSIGNED_ATTRIBUTE (boolean)" 4
.IX Item "UNSIGNED_ATTRIBUTE (boolean)"
Indicates whether the data type is unsigned. NULL (\f(CW\*(C`undef\*(C'\fR) is returned
for data types for which this is not applicable.
.IP "FIXED_PREC_SCALE (boolean)" 4
.IX Item "FIXED_PREC_SCALE (boolean)"
Indicates whether the data type always has the same precision and scale
(such as a money type). NULL (\f(CW\*(C`undef\*(C'\fR) is returned for data types
for which
this is not applicable.
.IP "AUTO_UNIQUE_VALUE (boolean)" 4
.IX Item "AUTO_UNIQUE_VALUE (boolean)"
Indicates whether a column of this data type is automatically set to a
unique value whenever a new row is inserted. NULL (\f(CW\*(C`undef\*(C'\fR) is returned
for data types for which this is not applicable.
.IP "LOCAL_TYPE_NAME (string)" 4
.IX Item "LOCAL_TYPE_NAME (string)"
Localized version of the \f(CW\*(C`TYPE_NAME\*(C'\fR for use in dialog with users.
NULL (\f(CW\*(C`undef\*(C'\fR) is returned if a localized name is not available (in which
case \f(CW\*(C`TYPE_NAME\*(C'\fR should be used).
.IP "MINIMUM_SCALE (integer)" 4
.IX Item "MINIMUM_SCALE (integer)"
The minimum scale of the data type. If a data type has a fixed scale,
then \f(CW\*(C`MAXIMUM_SCALE\*(C'\fR holds the same value. NULL (\f(CW\*(C`undef\*(C'\fR) is returned for
data types for which this is not applicable.
.IP "MAXIMUM_SCALE (integer)" 4
.IX Item "MAXIMUM_SCALE (integer)"
The maximum scale of the data type. If a data type has a fixed scale,
then \f(CW\*(C`MINIMUM_SCALE\*(C'\fR holds the same value. NULL (\f(CW\*(C`undef\*(C'\fR) is returned for
data types for which this is not applicable.
.IP "SQL_DATA_TYPE (integer)" 4
.IX Item "SQL_DATA_TYPE (integer)"
This column is the same as the \f(CW\*(C`DATA_TYPE\*(C'\fR column, except for interval
and datetime data types. For interval and datetime data types, the
\&\f(CW\*(C`SQL_DATA_TYPE\*(C'\fR field will return \f(CW\*(C`SQL_INTERVAL\*(C'\fR or \f(CW\*(C`SQL_DATETIME\*(C'\fR, and the
\&\f(CW\*(C`SQL_DATETIME_SUB\*(C'\fR field below will return the subcode for the specific
interval or datetime data type. If this field is NULL, then the driver
does not support or report on interval or datetime subtypes.
.IP "SQL_DATETIME_SUB (integer)" 4
.IX Item "SQL_DATETIME_SUB (integer)"
For interval or datetime data types, where the \f(CW\*(C`SQL_DATA_TYPE\*(C'\fR
field above is \f(CW\*(C`SQL_INTERVAL\*(C'\fR or \f(CW\*(C`SQL_DATETIME\*(C'\fR, this field will
hold the \fIsubcode\fR for the specific interval or datetime data type.
Otherwise it will be NULL (\f(CW\*(C`undef\*(C'\fR).
.Sp
Although not mentioned explicitly in the standards, it seems there
is a simple relationship between these values:
.Sp
.Vb 1
\& DATA_TYPE == (10 * SQL_DATA_TYPE) + SQL_DATETIME_SUB
.Ve
.IP "NUM_PREC_RADIX (integer)" 4
.IX Item "NUM_PREC_RADIX (integer)"
The radix value of the data type. For approximate numeric types,
\&\f(CW\*(C`NUM_PREC_RADIX\*(C'\fR
contains the value 2 and \f(CW\*(C`COLUMN_SIZE\*(C'\fR holds the number of bits. For
exact numeric types, \f(CW\*(C`NUM_PREC_RADIX\*(C'\fR contains the value 10 and \f(CW\*(C`COLUMN_SIZE\*(C'\fR holds
the number of decimal digits. NULL (\f(CW\*(C`undef\*(C'\fR) is returned either for data types
for which this is not applicable or if the driver cannot report this information.
.IP "INTERVAL_PRECISION (integer)" 4
.IX Item "INTERVAL_PRECISION (integer)"
The interval leading precision for interval types. NULL is returned
either for data types for which this is not applicable or if the driver
cannot report this information.
.PP
For example, to find the type name for the fields in a select statement
you can do:
.PP
.Vb 1
\& @names = map { scalar $dbh\->type_info($_)\->{TYPE_NAME} } @{ $sth\->{TYPE} }
.Ve
.PP
Since DBI and ODBC drivers vary in how they map their types into the
ISO standard types you may need to search for more than one type.
Here's an example looking for a usable type to store a date:
.PP
.Vb 1
\& $my_date_type = $dbh\->type_info( [ SQL_DATE, SQL_TIMESTAMP ] );
.Ve
.PP
Similarly, to more reliably find a type to store small integers, you could
use a list starting with \f(CW\*(C`SQL_SMALLINT\*(C'\fR, \f(CW\*(C`SQL_INTEGER\*(C'\fR, \f(CW\*(C`SQL_DECIMAL\*(C'\fR, etc.
.PP
See also "Standards Reference Information".
.PP
\fR\f(CI\*(C`quote\*(C'\fR\fI\fR
.IX Subsection "quote"
.PP
.Vb 2
\& $sql = $dbh\->quote($value);
\& $sql = $dbh\->quote($value, $data_type);
.Ve
.PP
Quote a string literal for use as a literal value in an SQL statement,
by escaping any special characters (such as quotation marks)
contained within the string and adding the required type of outer
quotation marks.
.PP
.Vb 2
\& $sql = sprintf "SELECT foo FROM bar WHERE baz = %s",
\& $dbh\->quote("Don\*(Aqt");
.Ve
.PP
For most database types, at least those that conform to SQL standards, quote
would return \f(CW\*(AqDon\*(Aq\*(Aqt\*(Aq\fR (including the outer quotation marks). For others it
may return something like \f(CW\*(AqDon\e\*(Aqt\*(Aq\fR
.PP
An undefined \f(CW$value\fR value will be returned as the string \f(CW\*(C`NULL\*(C'\fR (without
single quotation marks) to match how NULLs are represented in SQL.
.PP
If \f(CW$data_type\fR is supplied, it is used to try to determine the required
quoting behaviour by using the information returned by "type_info".
As a special case, the standard numeric types are optimized to return
\&\f(CW$value\fR without calling \f(CW\*(C`type_info\*(C'\fR.
.PP
Quote will probably \fInot\fR be able to deal with all possible input
(such as binary data or data containing newlines), and is not related in
any way with escaping or quoting shell meta-characters.
.PP
It is valid for the \fBquote()\fR method to return an SQL expression that
evaluates to the desired string. For example:
.PP
.Vb 1
\& $quoted = $dbh\->quote("one\entwo\e0three")
.Ve
.PP
may return something like:
.PP
.Vb 1
\& CONCAT(\*(Aqone\*(Aq, CHAR(12), \*(Aqtwo\*(Aq, CHAR(0), \*(Aqthree\*(Aq)
.Ve
.PP
The \fBquote()\fR method should \fInot\fR be used with "Placeholders and
Bind Values".
.PP
\fR\f(CI\*(C`quote_identifier\*(C'\fR\fI\fR
.IX Subsection "quote_identifier"
.PP
.Vb 2
\& $sql = $dbh\->quote_identifier( $name );
\& $sql = $dbh\->quote_identifier( $catalog, $schema, $table, \e%attr );
.Ve
.PP
Quote an identifier (table name etc.) for use in an SQL statement,
by escaping any special characters (such as double quotation marks)
it contains and adding the required type of outer quotation marks.
.PP
Undefined names are ignored and the remainder are quoted and then
joined together, typically with a dot (\f(CW\*(C`.\*(C'\fR) character. For example:
.PP
.Vb 1
\& $id = $dbh\->quote_identifier( undef, \*(AqHer schema\*(Aq, \*(AqMy table\*(Aq );
.Ve
.PP
would, for most database types, return \f(CW"Her schema"."My table"\fR
(including all the double quotation marks).
.PP
If three names are supplied then the first is assumed to be a
catalog name and special rules may be applied based on what "get_info"
returns for SQL_CATALOG_NAME_SEPARATOR (41) and SQL_CATALOG_LOCATION (114).
For example, for Oracle:
.PP
.Vb 1
\& $id = $dbh\->quote_identifier( \*(Aqlink\*(Aq, \*(Aqschema\*(Aq, \*(Aqtable\*(Aq );
.Ve
.PP
would return \f(CW"schema"."table"@"link"\fR.
.PP
\fR\f(CI\*(C`take_imp_data\*(C'\fR\fI\fR
.IX Subsection "take_imp_data"
.PP
.Vb 1
\& $imp_data = $dbh\->take_imp_data;
.Ve
.PP
Leaves the \f(CW$dbh\fR in an almost dead, zombie-like, state and returns
a binary string of raw implementation data from the driver which
describes the current database connection. Effectively it detaches
the underlying database API connection data from the DBI handle.
After calling \fBtake_imp_data()\fR, all other methods except \f(CW\*(C`DESTROY\*(C'\fR
will generate a warning and return undef.
.PP
Why would you want to do this? You don't, forget I even mentioned it.
Unless, that is, you're implementing something advanced like a
multi-threaded connection pool like \f(CW\*(C`DBI::Pool\*(C'\fR.
.PP
The returned \f(CW$imp_data\fR can be passed as a \f(CW\*(C`dbi_imp_data\*(C'\fR attribute
to a later \fBconnect()\fR call, even in a separate thread in the same
process, where the driver can use it to 'adopt' the existing
connection that the implementation data was taken from.
.PP
Some things to keep in mind...
.PP
\&\fB*\fR the \f(CW$imp_data\fR holds the only reference to the underlying
database API connection data. That connection is still 'live' and
won't be cleaned up properly unless the \f(CW$imp_data\fR is used to create
a new \f(CW$dbh\fR which is then allowed to \fBdisconnect()\fR normally.
.PP
\&\fB*\fR using the same \f(CW$imp_data\fR to create more than one other new
\&\f(CW$dbh\fR at a time may well lead to unpleasant problems. Don't do that.
.PP
Any child statement handles are effectively destroyed when \fBtake_imp_data()\fR is
called.
.PP
The \f(CW\*(C`take_imp_data\*(C'\fR method was added in DBI 1.36 but wasn't useful till 1.49.
.SS "Database Handle Attributes"
.IX Subsection "Database Handle Attributes"
This section describes attributes specific to database handles.
.PP
Changes to these database handle attributes do not affect any other
existing or future database handles.
.PP
Attempting to set or get the value of an unknown attribute generates a warning,
except for private driver-specific attributes (which all have names
starting with a lowercase letter).
.PP
Example:
.PP
.Vb 2
\& $h\->{AutoCommit} = ...; # set/write
\& ... = $h\->{AutoCommit}; # get/read
.Ve
.PP
\fR\f(CI\*(C`AutoCommit\*(C'\fR\fI\fR
.IX Subsection "AutoCommit"
.PP
Type: boolean
.PP
If true, then database changes cannot be rolled-back (undone). If false,
then database changes automatically occur within a "transaction", which
must either be committed or rolled back using the \f(CW\*(C`commit\*(C'\fR or \f(CW\*(C`rollback\*(C'\fR
methods.
.PP
Drivers should always default to \f(CW\*(C`AutoCommit\*(C'\fR mode (an unfortunate
choice largely forced on the DBI by ODBC and JDBC conventions.)
.PP
Attempting to set \f(CW\*(C`AutoCommit\*(C'\fR to an unsupported value is a fatal error.
This is an important feature of the DBI. Applications that need
full transaction behaviour can set \f(CW\*(C`$dbh\->{AutoCommit} = 0\*(C'\fR (or
set \f(CW\*(C`AutoCommit\*(C'\fR to 0 via "connect")
without having to check that the value was assigned successfully.
.PP
For the purposes of this description, we can divide databases into three
categories:
.PP
.Vb 3
\& Databases which don\*(Aqt support transactions at all.
\& Databases in which a transaction is always active.
\& Databases in which a transaction must be explicitly started (C<\*(AqBEGIN WORK\*(Aq>).
.Ve
.PP
\&\fB* Databases which don't support transactions at all\fR
.PP
For these databases, attempting to turn \f(CW\*(C`AutoCommit\*(C'\fR off is a fatal error.
\&\f(CW\*(C`commit\*(C'\fR and \f(CW\*(C`rollback\*(C'\fR both issue warnings about being ineffective while
\&\f(CW\*(C`AutoCommit\*(C'\fR is in effect.
.PP
\&\fB* Databases in which a transaction is always active\fR
.PP
These are typically mainstream commercial relational databases with
"ANSI standard" transaction behaviour.
If \f(CW\*(C`AutoCommit\*(C'\fR is off, then changes to the database won't have any
lasting effect unless "commit" is called (but see also
"disconnect"). If "rollback" is called then any changes since the
last commit are undone.
.PP
If \f(CW\*(C`AutoCommit\*(C'\fR is on, then the effect is the same as if the DBI
called \f(CW\*(C`commit\*(C'\fR automatically after every successful database
operation. So calling \f(CW\*(C`commit\*(C'\fR or \f(CW\*(C`rollback\*(C'\fR explicitly while
\&\f(CW\*(C`AutoCommit\*(C'\fR is on would be ineffective because the changes would
have already been committed.
.PP
Changing \f(CW\*(C`AutoCommit\*(C'\fR from off to on will trigger a "commit".
.PP
For databases which don't support a specific auto-commit mode, the
driver has to commit each statement automatically using an explicit
\&\f(CW\*(C`COMMIT\*(C'\fR after it completes successfully (and roll it back using an
explicit \f(CW\*(C`ROLLBACK\*(C'\fR if it fails). The error information reported to the
application will correspond to the statement which was executed, unless
it succeeded and the commit or rollback failed.
.PP
\&\fB* Databases in which a transaction must be explicitly started\fR
.PP
For these databases, the intention is to have them act like databases in
which a transaction is always active (as described above).
.PP
To do this, the driver will automatically begin an explicit transaction
when \f(CW\*(C`AutoCommit\*(C'\fR is turned off, or after a "commit" or
"rollback" (or when the application issues the next database
operation after one of those events).
.PP
In this way, the application does not have to treat these databases
as a special case.
.PP
See "commit", "disconnect" and "Transactions" for other important
notes about transactions.
.PP
\fR\f(CI\*(C`Driver\*(C'\fR\fI\fR
.IX Subsection "Driver"
.PP
Type: handle
.PP
Holds the handle of the parent driver. The only recommended use for this
is to find the name of the driver using:
.PP
.Vb 1
\& $dbh\->{Driver}\->{Name}
.Ve
.PP
\fR\f(CI\*(C`Name\*(C'\fR\fI\fR
.IX Subsection "Name"
.PP
Type: string
.PP
Holds the "name" of the database. Usually (and recommended to be) the
same as the "\f(CW\*(C`dbi:DriverName:...\*(C'\fR" string used to connect to the database,
but with the leading "\f(CW\*(C`dbi:DriverName:\*(C'\fR" removed.
.PP
\fR\f(CI\*(C`Statement\*(C'\fR\fI\fR
.IX Subsection "Statement"
.PP
Type: string, read-only
.PP
Returns the statement string passed to the most recent "prepare" or
"do" method called in this database handle, even if that method
failed. This is especially useful where \f(CW\*(C`RaiseError\*(C'\fR is enabled and
the exception handler checks $@ and sees that a 'prepare' method call
failed.
.PP
\fR\f(CI\*(C`RowCacheSize\*(C'\fR\fI\fR
.IX Subsection "RowCacheSize"
.PP
Type: integer
.PP
A hint to the driver indicating the size of the local row cache that the
application would like the driver to use for future \f(CW\*(C`SELECT\*(C'\fR statements.
If a row cache is not implemented, then setting \f(CW\*(C`RowCacheSize\*(C'\fR is ignored
and getting the value returns \f(CW\*(C`undef\*(C'\fR.
.PP
Some \f(CW\*(C`RowCacheSize\*(C'\fR values have special meaning, as follows:
.PP
.Vb 4
\& 0 \- Automatically determine a reasonable cache size for each C