8000 dbi/doc/DBI.man at atoi-strlen · bluhm/dbi · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
6425 lines (4793 loc) · 273 KB

DBI.man

File metadata and controls

6425 lines (4793 loc) · 273 KB
DBI(3) User Contributed Perl Documentation DBI(3)
NAME
DBI - Database independent interface for Perl
SYNOPSIS
use DBI;
@driver_names = DBI->available_drivers;
%drivers = DBI->installed_drivers;
@data_sources = DBI->data_sources($driver_name, \%attr);
$dbh = DBI->connect($data_source, $username, $auth, \%attr);
$rv = $dbh->do($statement);
$rv = $dbh->do($statement, \%attr);
$rv = $dbh->do($statement, \%attr, @bind_values);
$ary_ref = $dbh->selectall_arrayref($statement);
$hash_ref = $dbh->selectall_hashref($statement, $key_field);
$ary_ref = $dbh->selectcol_arrayref($statement);
$ary_ref = $dbh->selectcol_arrayref($statement, \%attr);
@row_ary = $dbh->selectrow_array($statement);
$ary_ref = $dbh->selectrow_arrayref($statement);
$hash_ref = $dbh->selectrow_hashref($statement);
$sth = $dbh->prepare($statement);
$sth = $dbh->prepare_cached($statement);
$rc = $sth->bind_param($p_num, $bind_value);
$rc = $sth->bind_param($p_num, $bind_value, $bind_type);
$rc = $sth->bind_param($p_num, $bind_value, \%attr);
$rv = $sth->execute;
$rv = $sth->execute(@bind_values);
$rv = $sth->execute_array(\%attr, ...);
$rc = $sth->bind_col($col_num, \$col_variable);
$rc = $sth->bind_columns(@list_of_refs_to_vars_to_bind);
@row_ary = $sth->fetchrow_array;
$ary_ref = $sth->fetchrow_arrayref;
$hash_ref = $sth->fetchrow_hashref;
$ary_ref = $sth->fetchall_arrayref;
$ary_ref = $sth->fetchall_arrayref( $slice, $max_rows );
$hash_ref = $sth->fetchall_hashref( $key_field );
$rv = $sth->rows;
$rc = $dbh->begin_work;
$rc = $dbh->commit;
$rc = $dbh->rollback;
$quoted_string = $dbh->quote($string);
$rc = $h->err;
$str = $h->errstr;
$rv = $h->state;
$rc = $dbh->disconnect;
The synopsis above only lists the major methods and parameters.
GETTING HELP
General
Before asking any questions, reread this document, consult the archives
and read the DBI FAQ. The archives are listed at the end of this
document and on the DBI home page <http://dbi.perl.org/support/>
You might also like to read the Advanced DBI Tutorial at
<http://www.slideshare.net/Tim.Bunce/dbi-advanced-tutorial-2007>
To help you make the best use of the dbi-users mailing list, and any
other lists or forums you may use, I recommend that you read "Getting
Answers" by Mike Ash: <http://mikeash.com/getting_answers.html>.
Mailing Lists
If you have questions about DBI, or DBD driver modules, you can get
help from the dbi-users@perl.org 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 dbi-users-help@perl.org.
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 dbi-users mailing list has lots of experienced people
who should be able to help you if you need it. If you do email Tim he
is very likely to just forward it to the mailing list.
IRC
DBI IRC Channel: #dbi on irc.perl.org (<irc://irc.perl.org/#dbi>)
Online
StackOverflow has a DBI tag
<https://stackoverflow.com/questions/tagged/dbi> with over 800
questions.
The DBI home page at <https://dbi.perl.org/> might be worth a visit.
It includes links to other resources, but is rather out-dated.
Reporting a Bug
If you think you've found a bug then please read "How to Report Bugs
Effectively" by Simon Tatham:
<https://www.chiark.greenend.org.uk/~sgtatham/bugs.html>.
If you think you've found a memory leak then read "Memory Leaks".
Your problem is most likely related to the specific DBD driver module
you're using. If that's the case then click on the 'Bugs' link on the
<https://metacpan.org> page for your driver. Only submit a bug report
against the DBI itself if you're sure that your issue isn't related to
the driver you're using.
NOTES
This is the DBI specification that corresponds to DBI version 1.653
(see DBI::Changes for details).
The DBI is evolving at a steady pace, so it's good to check that you
have the latest copy.
The significant user-visible changes in each release are documented in
the DBI::Changes module so you can read them by executing "perldoc
DBI::Changes".
Some DBI changes require changes in the drivers, but the drivers can
take some time to catch up. Newer versions of the DBI have added
features that may not yet be supported by the drivers you use. Talk to
the authors of your drivers if you need a new feature that is not yet
supported.
Features added after DBI 1.21 (February 2002) are marked in the text
with the version number of the DBI release they first appeared in.
Extensions to the DBI API often use the "DBIx::*" namespace. See
"Naming Conventions and Name Space". DBI extension modules can be found
at <https://metacpan.org/search?q=DBIx>. And all modules related to
the DBI can be found at <https://metacpan.org/search?q=DBI>.
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.
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
driver 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.
This document often uses terms like references, objects, methods. 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.
Architecture of a DBI Application
|<- Scope of DBI ->|
.-. .--------------. .-------------.
.-------. | |---| XYZ Driver |---| XYZ Engine |
| Perl | | | `--------------' `-------------'
| script| |A| |D| .--------------. .-------------.
| using |--|P|--|B|---|Oracle Driver |---|Oracle Engine|
| DBI | |I| |I| `--------------' `-------------'
| API | | |...
|methods| | |... Other drivers
`-------' | |...
`-'
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.
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.
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.
Notation and Conventions
The following conventions are used in this document:
$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
\%attr Reference to a hash of attribute values passed to methods
Note that Perl will automatically destroy database and statement handle
objects if all references to them are deleted.
Outline Usage
To use DBI, first you need to load the DBI module:
use DBI;
use strict;
(The "use strict;" isn't required but is strongly recommended.)
Then you need to "connect" to your data source and get a handle for
that connection:
$dbh = DBI->connect($dsn, $user, $password,
{ RaiseError => 1, AutoCommit => 0 });
Since connecting can be expensive, you generally just connect at the
start of your program and disconnect at the end.
Explicitly defining the required "AutoCommit" 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.
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 $sth in our
examples.
The typical method call sequence for a "SELECT" statement is:
prepare,
execute, fetch, fetch, ...
execute, fetch, fetch, ...
execute, fetch, fetch, ...
for example:
$sth = $dbh->prepare("SELECT foo, bar FROM table WHERE baz=?");
$sth->execute( $baz );
while ( @row = $sth->fetchrow_array ) {
print "@row\n";
}
For queries that are not executed many times at once, it is often
cleaner to use the higher level select wrappers:
$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);
The typical method call sequence for a non-"SELECT" statement is:
prepare,
execute,
execute,
execute.
for example:
$sth = $dbh->prepare("INSERT INTO table(foo,bar,baz) VALUES (?,?,?)");
while(<CSV>) {
chomp;
my ($foo,$bar,$baz) = split /,/;
$sth->execute( $foo, $bar, $baz );
}
The do() method is a wrapper of prepare and execute that can be simpler
for non repeated non-"SELECT" statements (or with drivers that don't
support placeholders):
$rows_affected = $dbh->do("UPDATE your_table SET foo = foo + 1");
$rows_affected = $dbh->do("DELETE FROM table WHERE foo = ?", undef, $foo);
To commit your changes to the database (when "AutoCommit" is off):
$dbh->commit; # or call $dbh->rollback; to undo changes
Finally, when you have finished working with the data source, you
should "disconnect" from it:
$dbh->disconnect;
General Interface Rules & Caveats
The DBI does not have a concept of a "current session". Every session
has a handle object (i.e., a $dbh) returned from the "connect" method.
That handle object is used to invoke database related methods.
Most data is returned to the Perl script as strings. (Null values are
returned as "undef".) 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.
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.
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.
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.
Multiple SQL statements may not be combined in a single statement
handle ($sth), although some databases and drivers do support this
(notably Sybase and SQL Server).
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.
Positioned updates and deletes are not directly supported by the DBI.
See the description of the "CursorName" attribute for an alternative.
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 func() method. Private
driver attributes are accessed just like standard attributes.
Many methods have an optional "\%attr" parameter which can be used to
pass information to the driver implementing the method. Except where
specifically documented, the "\%attr" parameter can only be used to
pass driver specific hints. In general, you can ignore "\%attr"
parameters or pass it as "undef".
Naming Conventions and Name Space
The DBI package and all packages below it ("DBI::*") are reserved for
use by the DBI. Extensions and related modules use the "DBIx::"
namespace (see <http://www.perl.com/CPAN/modules/by-module/DBIx/>).
Package names beginning with "DBD::" are reserved for use by DBI
database drivers. All environment variables used by the DBI or by
individual DBDs begin with ""DBI_"" or ""DBD_"".
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.
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)
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., ""ora_"" for Oracle, ""ing_"" for Ingres, etc).
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.
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.
For an interesting diversion on the real history of RDBMS and SQL, from
the people who made it happen, see:
http://www.mcjones.org/System_R/SQL_Reunion_95/sqlr95.html
Follow the "Full Contents" then "Intergalactic dataspeak" links for the
SQL history.
Placeholders and Bind Values
Some drivers support placeholders and bind values. Placeholders, 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:
INSERT INTO sales (product_code, qty, price) VALUES (?, ?, ?)
or the following, to select the description for a product:
SELECT description FROM products WHERE product_code = ?
The "?" characters are the placeholders. The association of actual
values with placeholders is known as binding, and the values are
referred to as bind values. Note that the "?" is not enclosed in
quotation marks, even when the placeholder represents a string.
Some drivers also allow placeholders like ":"name and ":"N (e.g., ":1",
":2", and so on) in addition to "?", but their use is not portable.
If the ":"N 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.
Some drivers allow you to prevent the recognition of a placeholder by
placing a single backslash character ("\") 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.
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:
"SELECT name, age FROM ?" # wrong (will probably fail)
"SELECT name, ? FROM people" # wrong (but may not 'fail')
Also, placeholders can only represent single scalar values. For
example, the following statement won't work as expected for more than
one value:
"SELECT name, age FROM people WHERE name IN (?)" # wrong
"SELECT name, age FROM people WHERE name IN (?,?)" # two names
When using placeholders with the SQL "LIKE" qualifier, you must
remember that the placeholder substitutes for the whole string. So you
should use ""... LIKE ? ..."" and include any wildcard characters in
the value that you bind to the placeholder.
NULL Values
Undefined values, or "undef", 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 "age" with a NULL
value:
$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");
However, care must be taken when trying to use NULL values in a "WHERE"
clause. Consider:
SELECT fullname FROM people WHERE age = ?
Binding an "undef" (NULL) to the placeholder will not select rows which
have a NULL "age"! 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 ""WHERE age IS NULL"".
A common issue is to have a code fragment handle a value that could be
either "defined" or "undef" (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:
$sql_clause = defined $age? "age = ?" : "age IS NULL";
$sth = $dbh->prepare(qq{
SELECT fullname FROM people WHERE $sql_clause
});
$sth->execute(defined $age ? $age : ());
The following technique illustrates qualifying a "WHERE" clause with
several columns, whose associated values ("defined" or "undef") are in
a hash %h:
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);
The techniques above call prepare for the SQL statement with each call
to execute. Because calls to prepare() can be expensive, performance
can suffer when an application iterates many times over statements like
the above.
A better solution is a single "WHERE" 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 "WHERE" 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 "?" placeholder in a
statement.
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)
Statements formed with the above "WHERE" clauses require execute
statements as follows. The arguments are required, whether their
values are "defined" or "undef".
0,1,2,3) $sth->execute($age);
4,5) $sth->execute($age, $age);
6) $sth->execute($age, defined($age) ? 0 : 1);
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 "OR"
expression.
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.
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.
Example 6, the least simple, is probably the most portable, i.e., it
should work with most, if not all, database engines.
Here is a table that indicates which examples above are known to work
on various database engines:
-----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
* Works only because Example 0 works.
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 ex/ subdirectory of the DBI source distribution, or here:
<https://github.com/perl5-dbi/dbi/blob/master/ex/perl_dbi_nulls_test.pl>
Please use the script to help us fill-in and maintain this table.
Performance
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 "execute" 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:
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;
See "execute" and "bind_param" for more details.
The "q{...}" style quoting used in this example avoids clashing with
quotes that may be used in the SQL statement. Use the double-quote like
"qq{...}" operator if you want to interpolate variables into the
string. See "Quote and Quote-like Operators" in perlop for more
details.
See also the "bind_columns" method, which is used to associate Perl
variables with the output columns of a "SELECT" statement.
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.
DBI Constants
Constants representing the values of the SQL standard types can be
imported individually by name, or all together by importing the special
":sql_types" tag.
The names and values of all the defined SQL standard types can be
produced like this:
foreach (@{ $DBI::EXPORT_TAGS{sql_types} }) {
printf "%s=%d\n", $_, &{"DBI::$_"};
}
These constants are defined by SQL/CLI, ODBC or both. "SQL_BIGINT" has
conflicting codes in SQL/CLI and ODBC, DBI uses the ODBC one.
See the "type_info", "type_info_all", and "bind_param" methods for
possible uses.
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.
DBI Class Methods
The following methods are provided by the DBI class:
"parse_dsn"
($scheme, $driver, $attr_string, $attr_hash, $driver_dsn) = DBI->parse_dsn($dsn)
or die "Can't parse DBI DSN '$dsn'";
Breaks apart a DBI Data Source Name (DSN) and returns the individual
parts. If $dsn doesn't contain a valid DSN then parse_dsn() returns an
empty list.
$scheme is the first part of the DSN and is currently always 'dbi'.
$driver is the driver name, possibly defaulted to $ENV{DBI_DRIVER}, and
may be undefined. $attr_string is the contents of the optional
attribute string, which may be undefined. If $attr_string is not empty
then $attr_hash is a reference to a hash containing the parsed
attribute names and values. $driver_dsn is the last part of the DBI
DSN string. For example:
($scheme, $driver, $attr_string, $attr_hash, $driver_dsn)
= DBI->parse_dsn("dbi:MyDriver(RaiseError=>1):db=test;port=42");
$scheme = 'dbi';
$driver = 'MyDriver';
$attr_string = 'RaiseError=>1';
$attr_hash = { 'RaiseError' => '1' };
$driver_dsn = 'db=test;port=42';
The parse_dsn() method was added in DBI 1.43.
"connect"
$dbh = DBI->connect($data_source, $username, $password)
or die $DBI::errstr;
$dbh = DBI->connect($data_source, $username, $password, \%attr)
or die $DBI::errstr;
Establishes a database connection, or session, to the requested
$data_source. Returns a database handle object if the connection
succeeds. Use "$dbh->disconnect" to terminate the connection.
If the connect fails (see below), it returns "undef" and sets both
$DBI::err and $DBI::errstr. (It does not explicitly set $!.) You should
generally test the return status of "connect" and "print $DBI::errstr"
if it has failed.
Multiple simultaneous connections to multiple databases through
multiple drivers can be made via the DBI. Simply make one "connect"
call for each database and keep a copy of each returned database
handle.
The $data_source value must begin with ""dbi:"driver_name":"". The
driver_name specifies the driver that will be used to make the
connection. (Letter case is significant.)
As a convenience, if the $data_source parameter is undefined or empty,
the DBI will substitute the value of the environment variable
"DBI_DSN". If just the driver_name part is empty (i.e., the
$data_source prefix is ""dbi::""), the environment variable
"DBI_DRIVER" is used. If neither variable is set, then "connect" dies.
Examples of $data_source values are:
dbi:DriverName:database_name
dbi:DriverName:database_name@hostname:port
dbi:DriverName:database=database_name;host=hostname;port=port
There is no standard 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.
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 '"host"', '"port"', and '"database"' (plus '"db"' as an
alias for "database"). This simplifies automatic construction of basic
DSNs: "dbi:$driver:database=$db;host=$host;port=$port". 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.
If the environment variable "DBI_AUTOPROXY" is defined (and the driver
in $data_source is not ""Proxy"") then the connect request will
automatically be changed to:
$ENV{DBI_AUTOPROXY};dsn=$data_source
"DBI_AUTOPROXY" is typically set as
""dbi:Proxy:hostname=...;port=..."". If $ENV{DBI_AUTOPROXY} doesn't
begin with '"dbi:"' then "dbi:Proxy:" will be prepended to it first.
See the DBD::Proxy documentation for more details.
If $username or $password are undefined (rather than just empty), then
the DBI will substitute the values of the "DBI_USER" and "DBI_PASS"
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.
"DBI->connect" automatically installs the driver if it has not been
installed yet. Driver installation either returns a valid driver
handle, or it dies with an error message that includes the string
""install_driver"" and the underlying problem. So "DBI->connect" will
die on a driver installation failure and will only return "undef" on a
connect failure, in which case $DBI::errstr will hold the error
message. Use "eval" if you need to catch the ""install_driver"" error.
The $data_source argument (with the ""dbi:...:"" prefix removed) and
the $username and $password 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 $data_source,
$username, and $password 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
$data_source is specified.)
The "AutoCommit" and "PrintError" attributes for each connection
default to "on". (See "AutoCommit" and "PrintError" for more
information.) However, it is strongly recommended that you explicitly
define "AutoCommit" rather than rely on the default. The "PrintWarn"
attribute defaults to true. The "RaiseWarn" attribute defaults to
false.
The "\%attr" parameter can be used to alter the default settings of
"PrintError", "RaiseError", "AutoCommit", and other attributes. For
example:
$dbh = DBI->connect($data_source, $user, $pass, {
PrintError => 0,
AutoCommit => 0
});
The username and password can also be specified using the attributes
"Username" and "Password", in which case they take precedence over the
$username and $password parameters.
You can also define connection attribute values within the $data_source
parameter. For example:
dbi:DriverName(PrintWarn=>0,PrintError=>0,Taint=>1):...
Individual attributes values specified in this way take precedence over
any conflicting values specified via the "\%attr" parameter to
"connect".
The "dbi_connect_method" 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).
Where possible, each session ($dbh) 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.
For compatibility with old DBI scripts, the driver can be specified by
passing its name as the fourth argument to "connect" (instead of
"\%attr"):
$dbh = DBI->connect($data_source, $user, $pass, $driver);
In this "old-style" form of "connect", the $data_source should not
start with ""dbi:driver_name:"". (If it does, the embedded driver_name
will be ignored). Also note that in this older form of "connect", the
"$dbh->{AutoCommit}" attribute is undefined, the "$dbh->{PrintError}"
attribute is off, and the old "DBI_DBNAME" environment variable is
checked if "DBI_DSN" is not defined. Beware that this "old-style"
"connect" will soon be withdrawn in a future version of DBI.
"connect_cached"
$dbh = DBI->connect_cached($data_source, $username, $password)
or die $DBI::errstr;
$dbh = DBI->connect_cached($data_source, $username, $password, \%attr)
or die $DBI::errstr;
"connect_cached" 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 "connect_cached" with the same parameter
values, then the corresponding cached $dbh 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 "ping" method fails.
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 "connect_cached" will use it.
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 connect_cached() because it will affect other code
that may be using the same handle. When connect_cached() returns a
handle the attributes will be reset to their initial values. This can
cause problems, especially with the "AutoCommit" attribute.
Also, to ensure that the attributes passed are always the same, avoid
passing references inline. For example, the "Callbacks" attribute is
specified as a hash reference. Be sure to declare it external to the
call to connect_cached(), such that the hash reference is not
re-created on every call. A package-level lexical works well:
package MyDBH;
my $cb = {
'connect_cached.reused' => sub { delete $_[4]->{AutoCommit} },
};
sub dbh {
DBI->connect_cached( $dsn, $username, $auth, { Callbacks => $cb });
}
Where multiple separate parts of a program are using connect_cached()
to connect to the same database with the same (initial) attributes it
is a good idea to add a private attribute to the connect_cached() call
to effectively limit the scope of the caching. For example:
DBI->connect_cached(..., { private_foo_cachekey => "Bar", ... });
Handles returned from that connect_cached() call will only be returned
by other connect_cached() call elsewhere in the code if those other
calls also pass in the same attribute values, including the private
one. (I've used "private_foo_cachekey" here as an example, you can use
any attribute name with a "private_" prefix.)
Taking that one step further, you can limit a particular
connect_cached() call to return handles unique to that one place in the
code by setting the private attribute to a unique value for that place:
DBI->connect_cached(..., { private_foo_cachekey => __FILE__.__LINE__, ... });
By using a private attribute you still get connection caching for the
individual calls to connect_cached() 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.
The cache can be accessed (and cleared) via the "CachedKids" attribute:
my $CachedKids_hashref = $dbh->{Driver}->{CachedKids};
%$CachedKids_hashref = () if $CachedKids_hashref;
"available_drivers"
@ary = DBI->available_drivers;
@ary = DBI->available_drivers($quiet);
Returns a list of all available drivers by searching for "DBD::*"
modules through the directories in @INC. 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 $quiet will inhibit the warning.
"installed_drivers"
%drivers = DBI->installed_drivers();
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.
To get a list of all drivers available in your perl installation you
can use "available_drivers".
Added in DBI 1.49.
"installed_versions"
DBI->installed_versions;
@ary = DBI->installed_versions;
$hash = DBI->installed_versions;
Calls available_drivers() and attempts to load each of them in turn
using install_driver(). 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.
When called in array context the list of successfully loaded drivers is
returned (without the 'DBD::' prefix).
When called in scalar context an extra entry for the "DBI" is added
(and "DBI::PurePerl" if appropriate) and a reference to the hash is
returned.
When called in a void context the installed_versions() 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.
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
available_drivers() instead.
The installed_versions() method is primarily intended as a quick way to
see from the command line what's installed. For example:
perl -MDBI -e 'DBI->installed_versions'
The installed_versions() method was added in DBI 1.38.
"data_sources"
@ary = DBI->data_sources($driver);
@ary = DBI->data_sources($driver, \%attr);
Returns a list of data sources (databases) available via the named
driver. If $driver is empty or "undef", then the value of the
"DBI_DRIVER" environment variable is used.
The driver will be loaded if it hasn't been already. Note that if the
driver loading fails then data_sources() dies with an error message
that includes the string ""install_driver"" and the underlying problem.
Data sources are returned in a form suitable for passing to the
"connect" method (that is, they will include the ""dbi:$driver:""
prefix).
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.
There is also a data_sources() method defined for database handles.
"trace"
DBI->trace($trace_setting)
DBI->trace($trace_setting, $trace_filename)
DBI->trace($trace_setting, $trace_filehandle)
$trace_setting = DBI->trace;
The "DBI->trace" method sets the global default trace settings and
returns the previous trace settings. It can also be used to change
where the trace output is sent.
There's a similar method, "$h->trace", which sets the trace settings
for the specific handle it's called on.
See the "TRACING" section for full details about the DBI's powerful
tracing facilities.
"visit_handles"
DBI->visit_handles( $coderef );
DBI->visit_handles( $coderef, $info );
Where $coderef is a reference to a subroutine and $info is an arbitrary
value which, if undefined, defaults to a reference to an empty hash.
Returns $info.
For each installed driver handle, if any, $coderef is invoked as:
$coderef->($driver_handle, $info);
If the execution of $coderef returns a true value then
"visit_child_handles" is called on that child handle and passed the
returned value as $info.
For example:
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
});
See also "visit_child_handles".
DBI Utility Functions
In addition to the DBI methods listed in the previous section, the DBI
package also provides several utility functions.
These can be imported into your code by listing them in the "use"
statement. For example:
use DBI qw(neat data_diff);
0