man DBI () - Database independent interface for Perl
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
If you have questions about DBI, or DBD driver modules, you can get help from the dbi-users@perl.org mailing list. You can get help on subscribing and using the list by emailing dbi-users-help@perl.org.
(To help you make the best use of the dbi-users mailing list, and any other lists or forums you may use, I strongly recommend that you read How To Ask Questions The Smart Way by Eric Raymond: <http://www.catb.org/~esr/faqs/smart-questions.html>)
The DBI home page at <http://dbi.perl.org/> is always worth a visit and includes an FAQ and links to other resources.
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. An FAQ is installed as a DBI::FAQ module so you can read it by executing CWperldoc DBI::FAQ. However the DBI::FAQ module is currently (2004) outdated relative to the online FAQ on the DBI home page.
This document often uses terms like references, objects, methods. If you're not familar with those terms then it would be a good idea to read at least the following perl manuals first: perlreftut, perldsc, perllol, and perlboot.
Please note that Tim Bunce does not maintain the mailing lists or the web page (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's very likely to just forward it to the mailing list.
NOTES
This is the DBI specification that corresponds to the DBI version 1.50.
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 CWperldoc 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's 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 CWDBIx::* namespace. See Naming Conventions and Name Space. DBI extension modules can be found at <http://search.cpan.org/search?mode=module&query=DBIx>. And all modules related to the DBI can be found at <http://search.cpan.org/search?query=DBI&mode=all>.
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.
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 CWuse 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 CWAutoCommit 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 CW$sth in our examples.
The typical method call sequence for a CWSELECT 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"; }
The typical method call sequence for a non-CWSELECT 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 CWdo() method can be used for non repeated non-CWSELECT statement (or with drivers that don't support placeholders):
$rows_affected = $dbh->do("UPDATE your_table SET foo = foo + 1");
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 CW$dbh) returned from the CWconnect 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 CWundef.) 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.
Most databases that understand multiple character sets have a default global charset. Text stored in the database is, or should be, stored in that charset; if not, then that's the fault of either the database or the application that inserted the data. When text is fetched it should be automatically converted to the charset of the client, presumably based on the locale. If a driver needs to set a flag to get that behaviour, then it should do so; it should not require the application to do that.
Multiple SQL statements may not be combined in a single statement handle (CW$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 CWCursorName 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 CWfunc() method. Private driver attributes are accessed just like standard attributes.
Many methods have an optional CW\%attr parameter which can be used to pass information to the driver implementing the method. Except where specifically documented, the CW\%attr parameter can only be used to pass driver specific hints. In general, you can ignore CW\%attr parameters or pass it as CWundef.
Naming Conventions and Name Space
The DBI package and all packages below it (CWDBI::*) are reserved for use by the DBI. Extensions and related modules use the CWDBIx:: namespace (see <http://www.perl.com/CPAN/modules/by-module/DBIx/>). Package names beginning with CWDBD:: are reserved for use by DBI database drivers. All environment variables used by the DBI or by individual DBDs begin with "CWDBI_ or CWDBD_".
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., "CWora_ for Oracle, CWing_" 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://ftp.digital.com/pub/DEC/SRC/technical-notes/SRC-1997-018-html/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 CW? 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 CW? is not enclosed in quotation marks, even when the placeholder represents a string. Some drivers also allow placeholders like CW:name and CW:n (e.g., CW:1, CW:2, and so on) in addition to CW?, but their use is not portable.
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 CWLIKE qualifier, you must remember that the placeholder substitutes for the whole string. So you should use "CW... LIKE ? ..." and include any wildcard characters in the value that you bind to the placeholder.
NULL Values
Undefined values, or CWundef, 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 CWage 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 CWWHERE clause. Consider:
SELECT fullname FROM people WHERE age = ?
Binding an CWundef (NULL) to the placeholder will not select rows which have a NULL CWage! 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 "CWWHERE age IS NULL".
A common issue is to have a code fragment handle a value that could be either CWdefined or CWundef (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 CWWHERE clause with several columns, whose associated values (CWdefined or CWundef) are in a hash CW%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 CWWHERE 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 CWWHERE 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 CW? 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 CWWHERE clauses require execute statements as follows. The arguments are required, whether their values are CWdefined or CWundef.
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 CWOR 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 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
* 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: <http://svn.perl.org/modules/dbi/trunk/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 CWexecute 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 CWq{...} style quoting used in this example avoids clashing with quotes that may be used in the SQL statement. Use the double-quote like CWqq{...} 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 CWSELECT 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 CW: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. CWSQL_BIGINT is (currently) omitted, because SQL/CLI and ODBC provide conflicting codes.
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:
($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 CW$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'. CW$driver is the driver name, possibly defaulted to CW$ENV{DBI_DRIVER}, and may be undefined. CW$attr_string is the optional attribute string, which may be undefined. If CW$attr_string is true then CW$attr_hash is a reference to a hash containing the parsed attribute names and values. CW$driver_dsn is the last part of the DBI DSN string. The parse_dsn() method was added in DBI 1.43.
$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 CW$data_source. Returns a database handle object if the connection succeeds. Use CW$dbh->disconnect to terminate the connection. If the connect fails (see below), it returns CWundef and sets both CW$DBI::err and CW$DBI::errstr. (It does not explicitly set CW$!.) You should generally test the return status of CWconnect and CWprint $DBI::errstr if it has failed. Multiple simultaneous connections to multiple databases through multiple drivers can be made via the DBI. Simply make one CWconnect call for each database and keep a copy of each returned database handle. The CW$data_source value must begin with "CWdbi:driver_nameCW:". The driver_name specifies the driver that will be used to make the connection. (Letter case is significant.) As a convenience, if the CW$data_source parameter is undefined or empty, the DBI will substitute the value of the environment variable CWDBI_DSN. If just the driver_name part is empty (i.e., the CW$data_source prefix is "CWdbi::"), the environment variable CWDBI_DRIVER is used. If neither variable is set, then CWconnect dies. Examples of CW$data_source values are:
dbi:DriverName:database_name dbi:DriverName:database_name@hostname:port dbi:DriverName:database=database_name;host=hostname;port=portThere 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 that they support the three common names 'CWhost', 'CWport', and 'CWdatabase' (plus 'CWdb' as an alias for CWdatabase). This simplifies automatic construction of basic DSNs: CW"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 CWDBI_AUTOPROXY is defined (and the driver in CW$data_source is not "CWProxy") then the connect request will automatically be changed to:
$ENV{DBI_AUTOPROXY};dsn=$data_sourceCWDBI_AUTOPROXY is typically set as "CWdbi:Proxy:hostname=...;port=...". If CW$ENV{DBI_AUTOPROXY} doesn't begin with 'CWdbi:' then dbi:Proxy: will be prepended to it first. See the DBD::Proxy documentation for more details. If CW$username or CW$password are undefined (rather than just empty), then the DBI will substitute the values of the CWDBI_USER and CWDBI_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. CWDBI->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 "CWinstall_driver" and the underlying problem. So CWDBI->connect will die on a driver installation failure and will only return CWundef on a connect failure, in which case CW$DBI::errstr will hold the error message. Use CWeval { ... } if you need to catch the "CWinstall_driver" error. The CW$data_source argument (with the "CWdbi:...:" prefix removed) and the CW$username and CW$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 CW$data_source, CW$username, and CW$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 CW$data_source is specified.) The CWAutoCommit and CWPrintError attributes for each connection default to on. (See AutoCommit and PrintError for more information.) However, it is strongly recommended that you explicitly define CWAutoCommit rather than rely on the default. The CWPrintWarn attribute defaults to on if $^W is true, i.e., perl is running with warnings enabled. The CW\%attr parameter can be used to alter the default settings of CWPrintError, CWRaiseError, CWAutoCommit, 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 CWUsername and CWPassword, in which case they take precedence over the CW$username and CW$password parameters. You can also define connection attribute values within the CW$data_source parameter. For example:
dbi:DriverName(PrintWarn=>1,PrintError=>0,Taint=>1):...Individual attributes values specified in this way take precedence over any conflicting values specified via the CW\%attr parameter to CWconnect. The CWdbi_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 (CW$dbh) is independent from the transactions in other sessions. This is useful when you need to hold cursors open across transactionsfor 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 CWconnect (instead of CW\%attr):
$dbh = DBI->connect($data_source, $user, $pass, $driver);In this old-style form of CWconnect, the CW$data_source should not start with "CWdbi:driver_name:". (If it does, the embedded driver_name will be ignored). Also note that in this older form of CWconnect, the CW$dbh->{AutoCommit} attribute is undefined, the CW$dbh->{PrintError} attribute is off, and the old CWDBI_DBNAME environment variable is checked if CWDBI_DSN is not defined. Beware that this old-style CWconnect will soon be withdrawn in a future version of DBI.
$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;CWconnect_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 CWconnect_cached with the same parameter values, then the corresponding cached CW$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 CWping method fails. Note that the behaviour of this method differs in several respects from the behaviour of persistent connections implemented by Apache::DBI. 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. 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 CWprivate_foo_cachekey here as an example, you can use any attribute name with a CWprivate_ 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 conections 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;
@ary = DBI->available_drivers; @ary = DBI->available_drivers($quiet);Returns a list of all available drivers by searching for CWDBD::* modules through the directories in CW@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 CW$quiet will inhibit the warning.
%drivers = DBI->installed_drivers();Returns a list of driver name and driver handle pairs for all installed drivers. The driver name does not include the 'DBD::' prefix.
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 a reference to the hash is returned and the hash will also contain other entries for the CWDBI version, CWOS name, etc. When called in a void context the installed_versions() method will print out a formatted list of the hash contents, one per line. 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 nor 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.
@ary = DBI->data_sources($driver); @ary = DBI->data_sources($driver, \%attr);Returns a list of data sources (databases) available via the named driver. If CW$driver is empty or CWundef, then the value of the CWDBI_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 "CWinstall_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 "CWdbi:$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.
DBI->trace($trace_setting) DBI->trace($trace_setting, $trace_filename) $trace_setting = DBI->trace;The CWDBI->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, CW$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.
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 CWuse statement. For example:
use DBI qw(neat data_diff);
Alternatively, all these utility functions (except hash) can be imported using the CW:utils import tag. For example:
use DBI qw(:utils);
$description = data_string_desc($string);Returns an informal description of the string. For example:
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, undefThe initial CWUTF8 on/off refers to Perl's internal SvUTF8 flag. If CW$string has the SvUTF8 flag set but the sequence of bytes it contains are not a valid UTF-8 encoding then data_string_desc() will report CWUTF8 on but INVALID encoding. The CWASCII vs CWnon-ASCII portion shows CWASCII if all the characters in the string are ASCII (have code points <= 127). The data_string_desc() function was added in DBI 1.46.
$diff = data_string_diff($a, $b);Returns an informal description of the first character difference between the strings. If both CW$a and CW$b contain the same sequence of characters then data_string_diff() returns an empty string. For example:
Params a & b Result ------------ ------ 'aaa', 'aaa' '' 'aaa', 'abc' 'Strings differ at index 2: a[2]=a, b[2]=b' 'aaa', undef 'String b is undef, string a has 3 characters' 'aaa', 'aa' 'String b truncated after 2 characters'Unicode characters are reported in CW\x{XXXX} 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 CW\x{08XX}='C' where CWC is the corresponding latin-1 character. The data_string_diff() function only considers logical characters and not the underlying encoding. See data_diff for an alternative. The data_string_diff() function was added in DBI 1.46.
$diff = data_diff($a, $b); $diff = data_diff($a, $b, $logical);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. For example, CWdata_diff("abc", "ab\x{263a}") will return:
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]=\x{263A}If CW$a and CW$b are identical in both the characters they contain and their physical encoding then data_diff() returns an empty string. If CW$logical is true then physical encoding differences are ignored (but are still reported if there is a difference in the characters). The data_diff() function was added in DBI 1.46.
$str = neat($value); $str = neat($value, $maxlen);Return a string containing a neat (and tidy) representation of the supplied value. Strings will be quoted, although internal quotes will not be escaped. Values known to be numeric will be unquoted. Undefined (NULL) values will be shown as CWundef (without quotes). 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 (.). For result strings longer than CW$maxlen the result string will be truncated to CW$maxlen-4 and "CW...'" will be appended. If CW$maxlen is 0 or CWundef, it defaults to CW$DBI::neat_maxlen which, in turn, defaults to 400. This function is designed to format values for human consumption. It is used internally by the DBI for trace output. It should typically not be used for formatting values for database use. (See also quote.)
$str = neat_list(\@listref, $maxlen, $field_sep);Calls CWneat on each element of the list and returns a string containing the results joined with CW$field_sep. CW$field_sep defaults to CW", ".
@bool = looks_like_number(@array);Returns true for each element that looks like a number. Returns false for each element that does not look like a number. Returns CWundef for each element that is undefined or empty.
$hash_value = DBI::hash($buffer, $type);Return a 32-bit integer 'hash' value corresponding to the contents of CW$buffer. The CW$type parameter selects which kind of hash algorithm should be used. For the technically curious, type 0 (which is the default if CW$type 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 <http://www.isthe.com/chongo/tech/comp/fnv/> for more information. Both types are implemented in C and are very fast. This function doesn't have much to do with databases, except that it can be handy to store hash values in a database.
DBI Dynamic Attributes
Dynamic attributes are always associated with the last handle used (that handle is represented by CW$h in the descriptions below).
Where an attribute is equivalent to a method call, then refer to the method call for all related documentation.
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 immediately after calling the method that sets them. If in any doubt, use the corresponding method call. Equivalent to CW$h->err. Equivalent to CW$h->errstr. Equivalent to CW$h->state. Equivalent to CW$h->rows. Please refer to the documentation for the rows method. Returns the DBI object handle used for the most recent DBI method call. If the last DBI method call was a DESTROY then CW$DBI::lasth will return the handle of the parent of the destroyed handle, if there is one.
METHODS COMMON TO ALL HANDLES
The following methods can be used by all types of DBI handles.
$rv = $h->err;Returns the native database engine error code from the last driver method called. The code is typically an integer but you should not assume that. The DBI resets CW$h->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. (Methods which don't reset err before being called include err() and errstr(), obviously, state(), rows(), func(), trace(), trace_msg(), ping(), and the tied hash attribute FETCH() and STORE() methods.) If you need to test for specific error conditions and 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. A driver may return CW0 from err() 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 errstr() and state() methods may be used to retrieve extra information in these cases. See set_err for more information.
$str = $h->errstr;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. The returned string may contain multiple messages separated by newline characters. The errstr() method should not be used to test for errors, use err() for that, because drivers may return 'success with information' or warning messages via errstr() for methods that have not 'failed'. See set_err for more information.
$str = $h->state;Returns a state code in the standard SQLSTATE five character format. Note that the specific success code CW00000 is translated to any empty string (false). If the driver does not support SQLSTATE (and most don't), then state will return CWS1000 (General Error) for all errors. The driver is free to return any value via CWstate, e.g., warning codes, even if it has not declared an error by returning a true value via the err method described above. The state() method should not be used to test for errors, use err() for that, because drivers may return a 'success with information' or warning state code via errstr() for methods that have not 'failed'.
$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);Set the CWerr, CWerrstr, and CWstate values for the handle. This method is typically only used by DBI drivers and DBI subclasses. If the HandleSetErr attribute holds a reference to a subroutine it is called first. The subroutine can alter the CW$err, CW$errstr, CW$state, and CW$method values. See HandleSetErr for full details. If the subroutine returns a true value then the handle CWerr, CWerrstr, and CWstate values are not altered and set_err() returns an empty list (it normally returns CW$rv which defaults to undef, see below). Setting CWerr to a true value indicates an error and will trigger the normal DBI error handling mechanisms, such as CWRaiseError and CWHandleError, if they are enabled, when execution returns from the DBI back to the application. Setting CWerr to CW"" indicates an 'information' state, and setting it to CW"0" indicates a 'warning' state. Setting CWerr to CWundef also sets CWerrstr to undef, and CWstate to CW"", irrespective of the values of the CW$errstr and CW$state parameters. The CW$method parameter provides an alternate method name for the CWRaiseError/CWPrintError/CWPrintWarn error string instead of the fairly unhelpful 'CWset_err'. The CWset_err method normally returns undef. The CW$rv parameter provides an alternate return value. Some special rules apply if the CWerr or CWerrstr values for the handle are already set... If CWerrstr is true then: "CW [err was %s now %s]" is appended if CW$err is true and CWerr is already true; "CW [state was %s now %s]" is appended if CW$state is true and CWstate is already true; then "CW\n" and the new CW$errstr are appended. Obviously the CW%s's above are replaced by the corresponding values. The handle CWerr value is set to CW$err if: CW$err is true; or handle CWerr value is undef; or CW$err is defined and the length is greater than the handle CWerr length. The effect is that an 'information' state only overrides undef; a 'warning' overrides undef or 'information', and an 'error' state overrides anything. The handle CWstate value is set to CW$state if CW$state is true and the handle CWerr value was set (by the rules above). Support for warning and information states was added in DBI 1.41.
$h->trace($trace_settings); $h->trace($trace_settings, $trace_filename); $trace_settings = $h->trace;The trace() 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. There's a similar method, CWDBI->trace, which sets the global default trace settings. See the TRACING section for full details about the DBI's powerful tracing facilities.
$h->trace_msg($message_text); $h->trace_msg($message_text, $min_level);Writes CW$message_text to the trace file if the trace level is greater than or equal to CW$min_level (which defaults to 1). Can also be called as CWDBI->trace_msg($msg). See TRACING for more details.
$h->func(@func_arguments, $func_name) or die ...;The CWfunc 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 last argument. It's also important to note that the func() 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 CW$h->err to detect errors. (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.) See also install_method for how you can avoid needing to use func() and gain.
$is_implemented = $h->can($method_name);Returns true if CW$method_name is implemented by the driver or a default method is provided by the DBI. It returns false where a driver hasn't implemented a method and the default method is provided by the DBI is just an empty stub.
$trace_settings_integer = $h->parse_trace_flags($trace_settings);Parses a string containing trace settings and returns the corresponding integer value used internally by the DBI and drivers. The CW$trace_settings argument is a string containing a trace level between 0 and 15 and/or trace flag names separated by vertical bar ("CW|) or comma (CW,") characters. For example: CW"SQL|3|foo". It uses the parse_trace_flag() method, described below, to process the individual trage flag names. The parse_trace_flags() method was added in DBI 1.42.
$bit_flag = $h->parse_trace_flag($trace_flag_name);Returns the bit flag corresponding to the trace flag name in CW$trace_flag_name. Drivers are expected to override this method and check if CW$trace_flag_name is a driver specific trace flags and, if not, then call the DBIs default parse_trace_flag(). The parse_trace_flag() method was added in DBI 1.42.
$rc = $h1->swap_inner_handle( $h2 ); $rc = $h1->swap_inner_handle( $h2, $allow_reparent );Brain transplants for handles. You don't need to know about this unless you want to become a handle surgeon. A DBI handle is a reference to a tied hash. A tied hash has an inner hash that actually holds the contents. The swap_inner_handle() method swaps the inner hashes between two handles. The CW$h1 and CW$h2 handles still point to the same tied hashes, but what those hashes are tied to has been swapped. In effect CW$h1 becomes CW$h2 and vice-versa. This is powerful stuff. Use with care. As a small safety measure, the two handles, CW$h1 and CW$h2, have to share the same parent unless CW$allow_reparent is true. The swap_inner_handle() method was added in DBI 1.44.
ATTRIBUTES COMMON TO ALL HANDLES
These attributes are common to all types of DBI handles.
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.
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).
Example:
$h->{AttributeName} = ...; # set/write ... = $h->{AttributeName}; # get/readThe CWWarn 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 CWwarn function, they can be intercepted using the Perl CW$SIG{__WARN__} hook. The CWWarn attribute is not related to the CWPrintWarn attribute. The CWActive 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 (CW$dbh->disconnect sets CWActive off). For a statement handle it typically means that the handle is a CWSELECT that may have more data to fetch. (Fetching all the data or calling CW$sth->finish sets CWActive off.) The CWExecuted attribute is true if the handle object has been executed. Currently only the CW$dbh do() method and the CW$sth execute(), execute_array(), and execute_for_fetch() methods set the CWExecuted attribute. When it's set on a handle it is also set on the parent handle at the same time. So calling execute() on a CW$sth also sets the CWExecuted attribute on the parent CW$dbh. The CWExecuted attribute for a database handle is cleared by the commit() and rollback() methods. The CWExecuted 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. The CWExecuted attribute was added in DBI 1.41. For a driver handle, CWKids is the number of currently existing database handles that were created from that driver handle. For a database handle, CWKids is the number of currently existing statement handles that were created from that database handle. For a statement handle, the value is zero. Like CWKids, but only counting those that are CWActive (as above). For a database handle, CWCachedKids 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. The CWType attribute identifies the type of a DBI handle. Returns dr for driver handles, db for database handles and st for statement handles. 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. CWChildHandles 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. For example, to enumerate all driver handles, database handles and statement handles:
sub show_child_handles { my ($h, $level) = @_; $level ||= 0; printf "%sh %s %s\n", $h->{Type}, "\t" x $level, $h; show_child_handles($_, $level + 1) for (grep { defined } @{$h->{ChildHandles}}); }
my %drivers = DBI->installed_drivers(); show_child_handles($_) for (values %drivers);The CWCompatMode 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. 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. The CWInactiveDestroy attribute can be used to disable the database engine related effect of DESTROYing a handle (which would normally close a prepared statement or disconnect from the database etc). The default value, false, means a handle will be fully destroyed when it passes out of scope. For a database handle, this attribute does not disable an explicit call to the disconnect method, only the implicit call from DESTROY that happens if the handle is still marked as CWActive. Think of the name as meaning 'treat the handle as not-Active in the DESTROY method'. This attribute is specifically designed for use in Unix applications that fork child processes. Either the parent or the child process, but not both, should set CWInactiveDestroy on all their shared handles. Note that some databases, including Oracle, don't support passing a database connection across a fork. To help tracing applications using fork the process id is shown in the trace log whenever a DBI or handle trace() method is called. The process id also shown for every 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. The CWPrintWarn attribute controls the printing of warnings recorded by the driver. When set to a true value the DBI will check method calls to see if a warning condition has been set. If so, the DBI will effectively do a CWwarn("$class $method warning: $DBI::errstr") where CW$class is the driver class and CW$method is the name of the method which failed. E.g.,
DBD::Oracle::db execute warning: ... warning text here ...By default, CWDBI->connect sets CWPrintWarn on if $^W is true, i.e., perl is running with warnings enabled. If desired, the warnings can be caught and processed using a CW$SIG{__WARN__} handler or modules like CGI::Carp and CGI::ErrorWrap. See also set_err for how warnings are recorded and HandleSetErr for how to influence it. 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 CWPrintWarn attribute is true. If CWPrintWarn is false then these drivers should still indicate the fact that there were warnings by setting the warning string to, for example: 3 warnings. The CWPrintError attribute can be used to force errors to generate warnings (using CWwarn) in addition to returning error codes in the normal way. When set on, any method which results in an error occuring will cause the DBI to effectively do a CWwarn("$class $method failed: $DBI::errstr") where CW$class is the driver class and CW$method is the name of the method which failed. E.g.,
DBD::Oracle::db prepare failed: ... error text here ...By default, CWDBI->connect sets CWPrintError on. If desired, the warnings can be caught and processed using a CW$SIG{__WARN__} handler or modules like CGI::Carp and CGI::ErrorWrap. The CWRaiseError 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 CWdie("$class $method failed: $DBI::errstr"), where CW$class is the driver class and CW$method is the name of the method that failed. E.g.,
DBD::Oracle::db prepare failed: ... error text here ...If you turn CWRaiseError on then you'd normally turn CWPrintError off. If CWPrintError is also on, then the CWPrintError is done first (naturally). Typically CWRaiseError is used in conjunction with CWeval { ... } to catch the exception that's been thrown and followed by an CWif ($@) { ... } block to handle the caught exception. For example:
eval { ... $sth->execute(); ... }; if ($@) { # $sth->err and $DBI::err will be true if error was from DBI warn $@; # print the error ... # do whatever you need to deal with the error }In that eval block the CW$DBI::lasth variable can be useful for diagnosis and reporting if you can't be sure which handle triggered the error. For example, CW$DBI::lasth->{Type} and CW$DBI::lasth->{Statement}. See also Transactions. If you want to temporarily turn CWRaiseError off (inside a library function that is likely to fail, for example), the recommended way is like this:
{ local $h->{RaiseError}; # localize and turn off for this block ... }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 CWPrintError. The CWHandleError 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 CWRaiseError and CWPrintError are handled). The subroutine is called with three parameters: the error message string that CWRaiseError and CWPrintError would use, the DBI handle being used, and the first value being returned by the method that failed (typically undef). If the subroutine returns a false value then the CWRaiseError and/or CWPrintError attributes are checked and acted upon as normal. For example, to CWdie with a full stack trace for any error:
use Carp; $h->{HandleError} = sub { confess(shift) };Or to turn errors into exceptions:
use Exception; # or your own favourite exception module $h->{HandleError} = sub { Exception->new('DBI')->raise($_[0]) };It is possible to 'stack' multiple HandleError handlers by using closures:
sub your_subroutine { my $previous_handler = $h->{HandleError}; $h->{HandleError} = sub { return 1 if $previous_handler and &$previous_handler(@_); ... your code here ... }; }Using a CWmy inside a subroutine to store the previous CWHandleError value is important. See perlsub and perlref for more information about closures. It is possible for CWHandleError to alter the error message that will be used by CWRaiseError and CWPrintError if it returns false. It can do that by altering the value of CW$_[0]. This example appends a stack trace to all errors and, unlike the previous example using Carp::confess, this will work CWPrintError as well as CWRaiseError:
$h->{HandleError} = sub { $_[0]=Carp::longmess($_[0]); 0; };It is also possible for CWHandleError to hide an error, to a limited degree, by using set_err to reset CW$DBI::err and CW$DBI::errstr, and altering the return value of the failed method. For example:
$h->{HandleError} = sub { return 0 unless $_[0] =~ /^\S+ fetchrow_arrayref failed:/; return 0 unless $_[1]->err == 1234; # the error to 'hide' $h->set_err(undef,undef); # turn off the error $_[2] = [ ... ]; # supply alternative return value return 1; };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 good use for it then please let me know. The CWHandleSetErr attribute can be used to intercept the setting of handle CWerr, CWerrstr, and CWstate values. If set to a reference to a subroutine then that subroutine is called whenever set_err() is called, typically by the driver or a subclass. The subroutine is called with five arguments, the first five that were passed to set_err(): the handle, the CWerr, CWerrstr, and CWstate values being set, and the method name. These can be altered by changing the values in the CW@_ array. The return value affects set_err() behaviour, see set_err for details. It is possible to 'stack' multiple HandleSetErr handlers by using closures. See HandleError for an example. The CWHandleSetErr and CWHandleError 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 CWerr 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 set_err() is called with a defined CWerr value, even if false. So it's not just for errors, despite the name, but also warn and info states. The set_err() method, and thus HandleSetErr, may be called multiple times within a method and is usually invoked from deep within driver code. In theory a driver can use the return value from HandleSetErr via set_err() to decide whether to continue or not. If set_err() 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. The CWHandleSetErr attribute was added in DBI 1.41. The CWErrCount attribute is incremented whenever the set_err() method records an error. It isn't incremented by warnings or information states. It is not reset by the DBI at any time. The CWErrCount attribute was added in DBI 1.41. Older drivers may not have been updated to use set_err() to record errors and so this attribute may not be incremented when using them. The CWShowErrorStatement attribute can be used to cause the relevant Statement text to be appended to the error messages generated by the CWRaiseError, CWPrintError, and CWPrintWarn attributes. Only applies to errors on statement handles plus the prepare(), do(), and the various CWselect*() database handle methods. (The exact format of the appended text is subject to change.) If CW$h->{ParamValues} 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. The CWTraceLevel 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. The CWTraceLevel attribute is especially useful combined with CWlocal to alter the trace settings for just a single block of code. The CWFetchHashKeyName attribute is used to specify whether the fetchrow_hashref() method should perform case conversion on the field names used for the hash keys. For historical reasons it defaults to 'CWNAME' but it is recommended to set it to 'CWNAME_lc' (convert to lower case) or 'CWNAME_uc' (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 prepare() is called. The CWChopBlanks 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. The default is false (although it is possible that the default may change). Applications that need specific behaviour should set the attribute as needed. Drivers are not required to support this attribute, but any driver which does not support it must arrange to return CWundef as the attribute value. The CWLongReadLen 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. The CWLongReadLen attribute only relates to fetching and reading long values; it is not involved in inserting or updating them. A value of 0 means not to automatically fetch any long data. Drivers may return undef or an empty string for long fields when CWLongReadLen is 0. The default is typically 0 (zero) 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. Some databases return some long types encoded as pairs of hex digits. For these types, CWLongReadLen relates to the underlying data length and not the doubled-up length of the encoded string. Changing the value of CWLongReadLen for a statement handle after it has been CWprepare'd will typically have no effect, so it's common to set CWLongReadLen on the CW$dbh before calling CWprepare. 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:
$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 ... });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 OCTET_LENGTH() does not work for long types in your database. For example, for Sybase use DATALENGTH() and for Oracle use LENGTHB(). See also LongTruncOk for information on truncation of long types. The CWLongTruncOk 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 CWLongReadLen attribute). By default, CWLongTruncOk 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.) If a fetch fails due to a long field truncation when CWLongTruncOk is false, many drivers will allow you to continue fetching further rows. See also LongReadLen. If the CWTaintIn attribute is set to a true value and Perl is running in taint mode (e.g., started with the CW-T option), then all the arguments to most DBI method calls are checked for being tainted. This may change. 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. When fetching data that you trust you can turn off the TaintIn attribute, for that statement handle, for the duration of the fetch loop. The CWTaintIn attribute was added in DBI 1.31. If the CWTaintOut attribute is set to a true value and Perl is running in taint mode (e.g., started with the CW-T option), then most data fetched from the database is considered tainted. This may change. 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. When fetching data that you trust you can turn off the TaintOut attribute, for that statement handle, for the duration of the fetch loop. 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. The CWTaintOut attribute was added in DBI 1.31. The CWTaint attribute is a shortcut for TaintIn and TaintOut (it is also present for backwards compatibility). 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. The CWProfile attribute enables the collection and reporting of method call timing statistics. See the DBI::Profile module documentation for much more detail. The CWProfile attribute was added in DBI 1.24. 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 "CWprivate_". It is strongly recommended that you use just one private attribute (e.g., use a hash ref) and give it a long and unambiguous name that includes the module or application name that the attribute relates to (e.g., "CWprivate_YourFullModuleName_thingy"). Because of the way the Perl tie mechanism works you cannot reliably use the CW||= operator directly to initialise the attribute, like this:
my $foo = $dbh->{private_yourmodname_foo} ||= { ... }; # WRONGyou should use a two step approach like this:
my $foo = $dbh->{private_yourmodname_foo}; $foo ||= $dbh->{private_yourmodname_foo} = { ... };This attribute is primarily of interest to people sub-classing DBI.
DBI DATABASE HANDLE OBJECTS
This section covers the methods and attributes associated with database handles.
Database Handle Methods
The following methods are specified for DBI database handles:
$new_dbh = $dbh->clone(); $new_dbh = $dbh->clone(\%attr);The CWclone method duplicates the CW$dbh connection by connecting with the same parameters ($dsn, CW$user, CW$password) as originally used. The attributes for the cloned connect are the same as those used for the original connect, with some other attribute merged over them depending on the \%attr parameter. If \%attr is given then the attributes it contains are merged into the original attributes and override any with the same names. Effectively the same as doing:
%attribues_used = ( %original_attributes, %attr );If \%attr is not given then it defaults to a hash containing all the attributes in the attribute cache of CW$dbh excluding any non-code references, plus the main boolean attributes (RaiseError, PrintError, AutoCommit, etc.). This behaviour is subject to change. The clone method can be used even if the database handle is disconnected. The CWclone method was added in DBI 1.33. It is very new and likely to change.
@ary = $dbh->data_sources(); @ary = $dbh->data_sources(\%attr);Returns a list of data sources (databases) available via the CW$dbh driver's data_sources() method, plus any extra data sources that the driver can discover via the connected CW$dbh. Typically the extra data sources are other databases managed by the same server process that the CW$dbh is connected to. Data sources are returned in a form suitable for passing to the connect method (that is, they will include the "CWdbi:$driver:" prefix). The data_sources() method, for a CW$dbh, was added in DBI 1.38.
$rows = $dbh->do($statement) or die $dbh->errstr; $rows = $dbh->do($statement, \%attr) or die $dbh->errstr; $rows = $dbh->do($statement, \%attr, @bind_values) or die ...Prepare and execute a single statement. Returns the number of rows affected or CWundef on error. A return value of CW-1 means the number of rows is not known, not applicable, or not available. This method is typically most useful for non-CWSELECT 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 CWSELECT statements because it does not return a statement handle (so you can't fetch any data). The default CWdo method is logically similar to:
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 }For example:
my $rows_deleted = $dbh->do(q{ DELETE FROM table WHERE status = ? }, undef, 'DONE') or die $dbh->errstr;Using placeholders and CW@bind_values with the CWdo method can be useful because it avoids the need to correctly quote any variables in the CW$statement. But if you'll be executing the statement many times then it's more efficient to CWprepare it once and call CWexecute many times instead. The CWq{...} style quoting used in this example avoids clashing with quotes that may be used in the SQL statement. Use the double-quote-like CWqq{...} operator if you want to interpolate variables into the string. See Quote and Quote-like Operators in perlop for more details.
$rv = $dbh->last_insert_id($catalog, $schema, $table, $field); $rv = $dbh->last_insert_id($catalog, $schema, $table, $field, \%attr);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 auto_increment or serial type. Returns undef if the driver does not support the method or can't determine the value. The CW$catalog, CW$schema, CW$table, and CW$field 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 CWundef for each. There are several caveats to be aware of with this method if you want to use it for portable applications: * For some drivers the value may only available immediately after the insert statement has executed (e.g., mysql, Informix). * For some drivers the CW$catalog, CW$schema, CW$table, and CW$field parameters are required (e.g., Pg), for others they are ignored (e.g., mysql). * Drivers may return an indeterminate value if no insert has been performed yet. * For some drivers the value may only be available if placeholders have not been used (e.g., Sybase, MS SQL). In this case the value returned would be from the last non-placeholder insert statement. * 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 \%attr parameter. * If the underlying database offers nothing better, then some drivers may attempt to implement this method by executing "CWselect max($field) from $table". Drivers using any approach like this should issue a warning if CWAutoCommit 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 CWWarn => 0 in \%attr. * If no insert has been performed yet, or the last insert failed, then the value is implementation defined. Given all the caveats above, it's clear that this method must be used with care. The CWlast_insert_id method was added in DBI 1.38.
@row_ary = $dbh->selectrow_array($statement); @row_ary = $dbh->selectrow_array($statement, \%attr); @row_ary = $dbh->selectrow_array($statement, \%attr, @bind_values);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 CW$statement parameter can be a previously prepared statement handle, in which case the CWprepare is skipped. If any method fails, and RaiseError is not set, CWselectrow_array will return an empty list. 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 CWundef is returned if there are no more rows or if an error occurred. That CWundef can't be distinguished from an CWundef returned because the first field value was NULL. For these reasons you should exercise some caution if you use CWselectrow_array in a scalar context.
$ary_ref = $dbh->selectrow_arrayref($statement); $ary_ref = $dbh->selectrow_arrayref($statement, \%attr); $ary_ref = $dbh->selectrow_arrayref($statement, \%attr, @bind_values);This utility method combines prepare, execute and fetchrow_arrayref into a single call. It returns the first row of data from the statement. The CW$statement parameter can be a previously prepared statement handle, in which case the CWprepare is skipped. If any method fails, and RaiseError is not set, CWselectrow_array will return undef.
$hash_ref = $dbh->selectrow_hashref($statement); $hash_ref = $dbh->selectrow_hashref($statement, \%attr); $hash_ref = $dbh->selectrow_hashref($statement, \%attr, @bind_values);This utility method combines prepare, execute and fetchrow_hashref into a single call. It returns the first row of data from the statement. The CW$statement parameter can be a previously prepared statement handle, in which case the CWprepare is skipped. If any method fails, and RaiseError is not set, CWselectrow_hashref will return undef.
$ary_ref = $dbh->selectall_arrayref($statement); $ary_ref = $dbh->selectall_arrayref($statement, \%attr); $ary_ref = $dbh->selectall_arrayref($statement, \%attr, @bind_values);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 for each row of data fetched. The CW$statement parameter can be a previously prepared statement handle, in which case the CWprepare is skipped. This is recommended if the statement is going to be executed many times. If RaiseError is not set and any method except CWfetchall_arrayref fails then CWselectall_arrayref will return CWundef; if CWfetchall_arrayref fails then it will return with whatever data has been fetched thus far. You should check CW$sth->err afterwards (or use the CWRaiseError attribute) to discover if the data is complete or was truncated due to an error. The fetchall_arrayref method called by CWselectall_arrayref supports a CW$max_rows parameter. You can specify a value for CW$max_rows by including a 'CWMaxRows' attribute in \%attr. In which case finish() is called for you after fetchall_arrayref() returns. The fetchall_arrayref method called by CWselectall_arrayref also supports a CW$slice parameter. You can specify a value for CW$slice by including a 'CWSlice' or 'CWColumns' attribute in \%attr. The only difference between the two is that if CWSlice is not defined and CWColumns 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 CW/fetchall_arrayref.
$hash_ref = $dbh->selectall_hashref($statement, $key_field); $hash_ref = $dbh->selectall_hashref($statement, $key_field, \%attr); $hash_ref = $dbh->selectall_hashref($statement, $key_field, \%attr, @bind_values);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 fetchall_hashref(). The CW$key_field 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. See fetchall_hashref() for more details. The CW$statement parameter can be a previously prepared statement handle, in which case the CWprepare is skipped. This is recommended if the statement is going to be executed many times. If any method except CWfetchrow_hashref fails, and RaiseError is not set, CWselectall_hashref will return CWundef. If CWfetchrow_hashref fails and RaiseError is not set, then it will return with whatever data it has fetched thus far. CW$DBI::err should be checked to catch that.
$ary_ref = $dbh->selectcol_arrayref($statement); $ary_ref = $dbh->selectcol_arrayref($statement, \%attr); $ary_ref = $dbh->selectcol_arrayref($statement, \%attr, @bind_values);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. The CW$statement parameter can be a previously prepared statement handle, in which case the CWprepare is skipped. This is recommended if the statement is going to be executed many times. If any method except CWfetch fails, and RaiseError is not set, CWselectcol_arrayref will return CWundef. If CWfetch fails and RaiseError is not set, then it will return with whatever data it has fetched thus far. CW$DBI::err should be checked to catch that. The CWselectcol_arrayref 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 'CWColumns' attribute which must be a ref to an array containing the column number or numbers to use. For example:
# 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} => nameYou can specify a maximum number of rows to fetch by including a 'CWMaxRows' attribute in \%attr.
$sth = $dbh->prepare($statement) or die $dbh->errstr; $sth = $dbh->prepare($statement, \%attr) or die $dbh->errstr;Prepares a statement for later execution by the database engine and returns a reference to a statement handle object. The returned statement handle can be used to get attributes of the statement and invoke the execute method. See Statement Handle Methods. Drivers for engines without the concept of preparing a statement will typically just store the statement in the returned handle and process it when CW$sth->execute is called. Such drivers are unlikely to give much useful information about the statement, such as CW$sth->{NUM_OF_FIELDS}, until after CW$sth->execute has been called. Portable applications should take this into account. 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. Portable applications should not assume that a new statement can be prepared and/or executed while still fetching results from a previous statement. 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.
$sth = $dbh->prepare_cached($statement) $sth = $dbh->prepare_cached($statement, \%attr) $sth = $dbh->prepare_cached($statement, \%attr, $if_active)Like prepare except that the statement handle returned will be stored in a hash associated with the CW$dbh. If another call is made to CWprepare_cached with the same CW$statement and CW%attr parameter values, then the corresponding cached CW$sth will be returned without contacting the database server. The CW$if_active parameter lets you adjust the behaviour if an already cached statement handle is still Active. There are several alternatives:
- 1: finish() will be called on the statement handle, but the warning is suppressed.
- 2: Disables any checking.
- 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]
-
Here are some examples of CWprepare_cached:
sub insert_hash { my ($table, $field_values) = @_; my @fields = sort keys %$field_values; # sort required 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) = @_; my @fields = sort keys %$field_values; # sort required 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); }
Caveat emptor: 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:my $sth = $dbh->prepare_cached('SELECT * FROM foo WHERE bar=?'); $sth->execute(...); while (my $data = $sth->fetchrow_hashref) {
# later, in some other code called within the loop... my $sth2 = $dbh->prepare_cached('SELECT * FROM foo WHERE bar=?'); $sth2->execute(...); while (my $data2 = $sth2->fetchrow_arrayref) { do_stuff(...); } }
In this example, since both handles are preparing the exact same statement, CW$sth2 will not be its own statement handle, but a duplicate of CW$sth returned from the cache. The results will certainly not be what you expect. Typically the the inner fetch loop will work normally, fetching all the records and terminating when there are no more, but now CW$sth is the same as CW$sth2 the outer fetch loop will also terminate. You'll know if you run into this problem because prepare_cached() will generate a warning by default (when CW$if_active is false). The cache used by prepare_cached() is keyed by both the statement and any attributes so you can also avoid this issue by doing something like:$sth = $dbh->prepare_cached("...", { dbi_dummy => __FILE__.__LINE__ });
which will ensure that prepare_cached only returns statements cached by that line of code in that source file.$rc = $dbh->commit or die $dbh->errstr;
Commit (make permanent) the most recent series of database changes if the database supports transactions and AutoCommit is off. If CWAutoCommit is on, then calling CWcommit will issue a commit ineffective with AutoCommit warning. See also Transactions in the FURTHER INFORMATION section below.$rc = $dbh->rollback or die $dbh->errstr;
Rollback (undo) the most recent series of uncommitted database changes if the database supports transactions and AutoCommit is off. If CWAutoCommit is on, then calling CWrollback will issue a rollback ineffective with AutoCommit warning. See also Transactions in the FURTHER INFORMATION section below.$rc = $dbh->begin_work or die $dbh->errstr;
Enable transactions (by turning CWAutoCommit off) until the next call to CWcommit or CWrollback. After the next CWcommit or CWrollback, CWAutoCommit will automatically be turned on again. If CWAutoCommit is already off when CWbegin_work is called then it does nothing except return an error. If the driver does not support transactions then when CWbegin_work attempts to set CWAutoCommit off the driver will trigger a fatal error. See also Transactions in the FURTHER INFORMATION section below.$rc = $dbh->disconnect or warn $dbh->errstr;
Disconnects the database from the database handle. CWdisconnect is typically only used before exiting the program. The handle is of little use after disconnecting. The transaction behaviour of the CWdisconnect 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 CWAutoCommit should explicitly call CWcommit or CWrollback before calling CWdisconnect. The database is automatically disconnected by the CWDESTROY method if still connected when there are no longer any references to the handle. The CWDESTROY method for each driver should implicitly call CWrollback to undo any uncommitted changes. This is vital behaviour to ensure that incomplete transactions don't get committed simply because Perl calls CWDESTROY on every object before exiting. Also, do not rely on the order of object destruction during global destruction, as it is undefined. Generally, if you want your changes to be commited or rolled back when you disconnect, then you should explicitly call commit or rollback before disconnecting. 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 CWfinish method on the active handles.$rc = $dbh->ping;
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. The current default implementation always returns true without actually doing anything. Actually, it returns "CW0 but true" 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. Few applications would have direct use for this method. See the specialized Apache::DBI module for one example usage.$value = $dbh->get_info( $info_type );
Returns information about the implementation, i.e. driver and data source capabilities, restrictions etc. It returns CWundef for unknown or unimplemented information types. For example:$database_version = $dbh->get_info( 18 ); # SQL_DBMS_VER $max_select_tables = $dbh->get_info( 106 ); # SQL_MAXIMUM_TABLES_IN_SELECT
See Standards Reference Information for more detailed information about the information types and their meanings and possible return values. The DBI::Const::GetInfoType module exports a CW%GetInfoType hash that can be used to map info type names to numbers. For example:$database_version = $dbh->get_info( $GetInfoType{SQL_DBMS_VER} );
The names are a merging of the ANSI and ODBC standards (which differ in some cases). See DBI::Const::GetInfoType for more details. Because some DBI methods make use of get_info(), drivers are strongly encouraged to support at least the following very minimal set of information types to ensure the DBI itself works properly:Type Name Example A Example B ---- -------------------------- ------------ ---------------- 17 SQL_DBMS_NAME 'ACCESS' 'Oracle' 18 SQL_DBMS_VER '03.50.0000' '08.01.0721 ...' 29 SQL_IDENTIFIER_QUOTE_CHAR '`' '"' 41 SQL_CATALOG_NAME_SEPARATOR '.' '@' 114 SQL_CATALOG_LOCATION 1 2
$sth = $dbh->table_info( $catalog, $schema, $table, $type ); $sth = $dbh->table_info( $catalog, $schema, $table, $type, \%attr );
Returns an active statement handle that can be used to fetch information about tables and views that exist in the database. The arguments CW$catalog, CW$schema and CW$table may accept search patterns according to the database/driver, for example: CW$table = '%FOO%'; Remember that the underscore character ('CW_') is a search pattern that means match any character, so 'FOO_%' is the same as 'FOO%' and 'FOO_BAR%' will match names like 'FOO1BAR'. The value of CW$type 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.:$type = "TABLE"; $type = "'TABLE','VIEW'";
In addition the following special cases may also be supported by some drivers:$sth = $dbh->table_info('%', '', '');
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. 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. The statement handle returned has at least the following fields in the order show below. Other fields, after these, may also be present. TABLE_CAT: Table catalog identifier. This field is NULL (CWundef) if not applicable to the data source, which is usually the case. This field is empty if not applicable to the table. TABLE_SCHEM: The name of the schema containing the TABLE_NAME value. This field is NULL (CWundef) if not applicable to data source, and empty if not applicable to the table. TABLE_NAME: Name of the table (or view, synonym, etc). TABLE_TYPE: 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. REMARKS: A description of the table. May be NULL (CWundef). Note that CWtable_info might not return records for all tables. Applications can use any valid table regardless of whether it's returned by CWtable_info. See also tables, Catalog Methods and Standards Reference Information.$sth = $dbh->column_info( $catalog, $schema, $table, $column );
Returns an active statement handle that can be used to fetch information about columns in specified tables. The arguments CW$schema, CW$table and CW$column may accept search patterns according to the database/driver, for example: CW$table = '%FOO%'; 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. The statement handle returned has at least the following fields in the order shown below. Other fields, after these, may also be present. TABLE_CAT: The catalog identifier. This field is NULL (CWundef) if not applicable to the data source, which is often the case. This field is empty if not applicable to the table. TABLE_SCHEM: The schema identifier. This field is NULL (CWundef) if not applicable to the data source, and empty if not applicable to the table. TABLE_NAME: The table identifier. Note: A driver may provide column metadata not only for base tables, but also for derived objects like SYNONYMS etc. COLUMN_NAME: The column identifier. DATA_TYPE: The concise data type code. TYPE_NAME: A data source dependent data type name. COLUMN_SIZE: 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. BUFFER_LENGTH: The length in bytes of transferred data. DECIMAL_DIGITS: The total number of significant digits to the right of the decimal point. NUM_PREC_RADIX: The radix for numeric precision. The value is 10 or 2 for numeric data types and NULL (CWundef) if not applicable. NULLABLE: Indicates if a column can accept NULLs. The following values are defined:SQL_NO_NULLS 0 SQL_NULLABLE 1 SQL_NULLABLE_UNKNOWN 2
REMARKS: A description of the column. COLUMN_DEF: The default value of the column. SQL_DATA_TYPE: The SQL data type. SQL_DATETIME_SUB: The subtype code for datetime and interval data types. CHAR_OCTET_LENGTH: The maximum length in bytes of a character or binary data type column. ORDINAL_POSITION: The column sequence number (starting with 1). IS_NULLABLE: Indicates if the column can accept NULLs. Possible values are: 'NO', 'YES' and ''. SQL/CLI defines the following additional columns: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
Drivers capable of supplying any of those values should do so in the corresponding column and supply undef values for the others. 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. The result set is ordered by TABLE_CAT, TABLE_SCHEM, TABLE_NAME and ORDINAL_POSITION. Note: There is some overlap with statement attributes (in perl) and SQLDescribeCol (in ODBC). However, SQLColumns provides more metadata. See also Catalog Methods and Standards Reference Information.$sth = $dbh->primary_key_info( $catalog, $schema, $table );
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 table_info()). For example:$sth = $dbh->primary_key_info( undef, $user, 'foo' ); $data = $sth->fetchall_arrayref;
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. Note: The support for the selection criteria, such as CW$catalog, is driver specific. If the driver doesn't support catalogs and/or schemas, it may ignore these criteria. The statement handle returned has at least the following fields in the order shown below. Other fields, after these, may also be present. TABLE_CAT: The catalog identifier. This field is NULL (CWundef) if not applicable to the data source, which is often the case. This field is empty if not applicable to the table. TABLE_SCHEM: The schema identifier. This field is NULL (CWundef) if not applicable to the data source, and empty if not applicable to the table. TABLE_NAME: The table identifier. COLUMN_NAME: The column identifier. KEY_SEQ: The column sequence number (starting with 1). Note: This field is named ORDINAL_POSITION in SQL/CLI. PK_NAME: The primary key constraint identifier. This field is NULL (CWundef) if not applicable to the data source. See also Catalog Methods and Standards Reference Information.@key_column_names = $dbh->primary_key( $catalog, $schema, $table );
Simple interface to the primary_key_info() 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.$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 , \%attr );
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 table_info()). CW$pk_catalog, CW$pk_schema, CW$pk_table identify the primary (unique) key table (PKT). CW$fk_catalog, CW$fk_schema, CW$fk_table identify the foreign key table (FKT). If both PKT and FKT are given, the function returns the foreign key, if any, in table FKT that refers to the primary (unique) key of table PKT. (Note: In SQL/CLI, the result is implementation-defined.) If only PKT is given, then the result set contains the primary key of that table and all foreign keys that refer to it. If only FKT 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.) For example:$sth = $dbh->foreign_key_info( undef, $user, 'master'); $sth = $dbh->foreign_key_info( undef, undef, undef , undef, $user, 'detail'); $sth = $dbh->foreign_key_info( undef, $user, 'master', undef, $user, 'detail');
Note: The support for the selection criteria, such as CW$catalog, is driver specific. If the driver doesn't support catalogs and/or schemas, it may ignore these criteria. 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. PKTABLE_CAT ( UK_TABLE_CAT ): The primary (unique) key table catalog identifier. This field is NULL (CWundef) if not applicable to the data source, which is often the case. This field is empty if not applicable to the table. PKTABLE_SCHEM ( UK_TABLE_SCHEM ): The primary (unique) key table schema identifier. This field is NULL (CWundef) if not applicable to the data source, and empty if not applicable to the table. PKTABLE_NAME ( UK_TABLE_NAME ): The primary (unique) key table identifier. PKCOLUMN_NAME (UK_COLUMN_NAME ): The primary (unique) key column identifier. FKTABLE_CAT ( FK_TABLE_CAT ): The foreign key table catalog identifier. This field is NULL (CWundef) if not applicable to the data source, which is often the case. This field is empty if not applicable to the table. FKTABLE_SCHEM ( FK_TABLE_SCHEM ): The foreign key table schema identifier. This field is NULL (CWundef) if not applicable to the data source, and empty if not applicable to the table. FKTABLE_NAME ( FK_TABLE_NAME ): The foreign key table identifier. FKCOLUMN_NAME ( FK_COLUMN_NAME ): The foreign key column identifier. KEY_SEQ ( ORDINAL_POSITION ): The column sequence number (starting with 1). UPDATE_RULE ( UPDATE_RULE ): The referential action for the UPDATE rule. The following codes are defined:CASCADE 0 RESTRICT 1 SET NULL 2 NO ACTION 3 SET DEFAULT 4
DELETE_RULE ( DELETE_RULE ): The referential action for the DELETE rule. The codes are the same as for UPDATE_RULE. FK_NAME ( FK_NAME ): The foreign key name. PK_NAME ( UK_NAME ): The primary (unique) key name. DEFERRABILITY ( DEFERABILITY ): The deferrability of the foreign key constraint. The following codes are defined:INITIALLY DEFERRED 5 INITIALLY IMMEDIATE 6 NOT DEFERRABLE 7
( UNIQUE_OR_PRIMARY ): 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. See also Catalog Methods and Standards Reference Information.@names = $dbh->tables( $catalog, $schema, $table, $type ); @names = $dbh->tables; # deprecated
Simple interface to table_info(). Returns a list of matching table names, possibly including a catalog/schema prefix. See table_info for a description of the parameters. If CW$dbh->get_info(2) 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.$type_info_all = $dbh->type_info_all;
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. The first item is a reference to an 'index' hash of CWName => CWIndex 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:$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, }, [ 'VARCHAR', SQL_VARCHAR, undef, "'","'", undef,0, 1,1,0,0,0,undef,1,255, undef ], [ 'INTEGER', SQL_INTEGER, undef, "", "", undef,0, 0,1,0,0,0,undef,0, 0, 10 ], ];
More than one row may have the same value in the CWDATA_TYPE 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 CWAUTO_UNIQUE_VALUE set, with and without CWUNSIGNED_ATTRIBUTE, etc). The rows are ordered by CWDATA_TYPE first and then by how closely each type maps to the corresponding ODBC SQL data type, closest first. The meaning of the fields is described in the documentation for the type_info method. 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. 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. The type_info_all() method is not normally used directly. The type_info method provides a more usable and useful interface to the data.@type_info = $dbh->type_info($data_type);
Returns a list of hash references holding information about one or more variants of CW$data_type. The list is ordered by CWDATA_TYPE 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. If CW$data_type is undefined or CWSQL_ALL_TYPES, then the list will contain hashes for all data type variants supported by the database and driver. If CW$data_type is an array reference then CWtype_info returns the information for the first type in the array that has any matches. 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: - TYPE_NAME (string)
- Data type name for use in CREATE TABLE statements etc.
- DATA_TYPE (integer)
- SQL data type number.
- 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). For string types, this is the maximum size of the string in characters. For date and interval types, this is the maximum number of characters needed to display the value.
- LITERAL_PREFIX (string)
- Characters used to prefix a literal. A typical prefix is "CW' for characters, or possibly CW0x" for binary values passed as hexadecimal. NULL (CWundef) is returned for data types for which this is not applicable.
- LITERAL_SUFFIX (string)
- Characters used to suffix a literal. Typically "CW'" for characters. NULL (CWundef) is returned for data types where this is not applicable.
- CREATE_PARAMS (string)
- Parameter names for data type definition. For example, CWCREATE_PARAMS for a CWDECIMAL would be "CWprecision,scale" if the DECIMAL type should be declared as CWDECIMAL(precision,scaleCW) where precision and scale are integer values. For a CWVARCHAR it would be "CWmax length". NULL (CWundef) is returned for data types for which this is not applicable.
- NULLABLE (integer)
- Indicates whether the data type accepts a NULL value: CW0 or an empty string = no, CW1 = yes, CW2 = unknown.
- CASE_SENSITIVE (boolean)
- Indicates whether the data type is case sensitive in collations and comparisons.
- SEARCHABLE (integer)
-
Indicates how the data type can be used in a WHERE clause, as
follows:
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
- UNSIGNED_ATTRIBUTE (boolean)
- Indicates whether the data type is unsigned. NULL (CWundef) is returned for data types for which this is not applicable.
- FIXED_PREC_SCALE (boolean)
- Indicates whether the data type always has the same precision and scale (such as a money type). NULL (CWundef) is returned for data types for which this is not applicable.
- 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 (CWundef) is returned for data types for which this is not applicable.
- LOCAL_TYPE_NAME (string)
- Localized version of the CWTYPE_NAME for use in dialog with users. NULL (CWundef) is returned if a localized name is not available (in which case CWTYPE_NAME should be used).
- MINIMUM_SCALE (integer)
- The minimum scale of the data type. If a data type has a fixed scale, then CWMAXIMUM_SCALE holds the same value. NULL (CWundef) is returned for data types for which this is not applicable.
- MAXIMUM_SCALE (integer)
- The maximum scale of the data type. If a data type has a fixed scale, then CWMINIMUM_SCALE holds the same value. NULL (CWundef) is returned for data types for which this is not applicable.
- SQL_DATA_TYPE (integer)
- This column is the same as the CWDATA_TYPE column, except for interval and datetime data types. For interval and datetime data types, the CWSQL_DATA_TYPE field will return CWSQL_INTERVAL or CWSQL_DATETIME, and the CWSQL_DATETIME_SUB 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.
- SQL_DATETIME_SUB (integer)
-
For interval or datetime data types, where the CWSQL_DATA_TYPE
field above is CWSQL_INTERVAL or CWSQL_DATETIME, this field will
hold the subcode for the specific interval or datetime data type.
Otherwise it will be NULL (CWundef).
Although not mentioned explicitly in the standards, it seems there
is a simple relationship between these values:
DATA_TYPE == (10 * SQL_DATA_TYPE) + SQL_DATETIME_SUB
- NUM_PREC_RADIX (integer)
- The radix value of the data type. For approximate numeric types, CWNUM_PREC_RADIX contains the value 2 and CWCOLUMN_SIZE holds the number of bits. For exact numeric types, CWNUM_PREC_RADIX contains the value 10 and CWCOLUMN_SIZE holds the number of decimal digits. NULL (CWundef) is returned either for data types for which this is not applicable or if the driver cannot report this information.
- 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.
For example, to find the type name for the fields in a select statement
you can do:
@names = map { scalar $dbh->type_info($_)->{TYPE_NAME} } @{ $sth->{TYPE} }
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:$my_date_type = $dbh->type_info( [ SQL_DATE, SQL_TIMESTAMP ] );
Similarly, to more reliably find a type to store small integers, you could use a list starting with CWSQL_SMALLINT, CWSQL_INTEGER, CWSQL_DECIMAL, etc. See also Standards Reference Information.$sql = $dbh->quote($value); $sql = $dbh->quote($value, $data_type);
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.$sql = sprintf "SELECT foo FROM bar WHERE baz = %s", $dbh->quote("Don't");
For most database types, quote would return CW'Don''t' (including the outer quotation marks). An undefined CW$value value will be returned as the string CWNULL (without single quotation marks) to match how NULLs are represented in SQL. If CW$data_type 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 CW$value without calling CWtype_info. Quote will probably not 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. It is valid for the quote() method to return an SQL expression that evaluates to the desired string. For example:$quoted = $dbh->quote("one\ntwo\0three")
may return something like:CONCAT('one', CHAR(1), 'two', CHAR(0), 'three')
The quote() method should not be used with Placeholders and Bind Values.$sql = $dbh->quote_identifier( $name ); $sql = $dbh->quote_identifier( $catalog, $schema, $table, \%attr );
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. Undefined names are ignored and the remainder are quoted and then joined together, typically with a dot (CW.) character. For example:$id = $dbh->quote_identifier( undef, 'Her schema', 'My table' );
would, for most database types, return CW"Her schema"."My table" (including all the double quotation marks). 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:$id = $dbh->quote_identifier( 'link', 'schema', 'table' );
would return CW"schema"."table"@"link".$imp_data = $dbh->take_imp_data;
Leaves the CW$dbh 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 take_imp_data(), all other methods except CWDESTROY will generate a warning and return undef. 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. See DBI::Pool. The returned CW$imp_data can be passed as a CWdbi_imp_data attribute to a later connect() 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. Some things to keep in mind... * the CW$imp_data 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 CW$imp_data is used to create a new CW$dbh which is then allowed to disconnect() normally. * using the same CW$imp_data to create more than one other new CW$dbh at a time may well lead to unpleasant problems. Don't do that. Any child statement handles are effectively destroyed when take_imp_data() is called. The CWtake_imp_data method was added in DBI 1.36 but wasn't useful till 1.49.
Database Handle Attributes
This section describes attributes specific to database handles.
Changes to these database handle attributes do not affect any other existing or future database handles.
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).
Example:
$h->{AutoCommit} = ...; # set/write ... = $h->{AutoCommit}; # get/readIf 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 CWcommit or CWrollback methods. Drivers should always default to CWAutoCommit mode (an unfortunate choice largely forced on the DBI by ODBC and JDBC conventions.) Attempting to set CWAutoCommit to an unsupported value is a fatal error. This is an important feature of the DBI. Applications that need full transaction behaviour can set CW$dbh->{AutoCommit} = 0 (or set CWAutoCommit to 0 via connect) without having to check that the value was assigned successfully. For the purposes of this description, we can divide databases into three categories:
Databases which don't support transactions at all. Databases in which a transaction is always active. Databases in which a transaction must be explicitly started (C<'BEGIN WORK'>).* Databases which don't support transactions at all For these databases, attempting to turn CWAutoCommit off is a fatal error. CWcommit and CWrollback both issue warnings about being ineffective while CWAutoCommit is in effect. * Databases in which a transaction is always active These are typically mainstream commercial relational databases with ANSI standard transaction behaviour. If CWAutoCommit 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. If CWAutoCommit is on, then the effect is the same as if the DBI called CWcommit automatically after every successful database operation. So calling CWcommit or CWrollback explicitly while CWAutoCommit is on would be ineffective because the changes would have already been commited. Changing CWAutoCommit from off to on will trigger a commit. For databases which don't support a specific auto-commit mode, the driver has to commit each statement automatically using an explicit CWCOMMIT after it completes successfully (and roll it back using an explicit CWROLLBACK 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. * Databases in which a transaction must be explicitly started For these databases, the intention is to have them act like databases in which a transaction is always active (as described above). To do this, the driver will automatically begin an explicit transaction when CWAutoCommit is turned off, or after a commit or rollback (or when the application issues the next database operation after one of those events). In this way, the application does not have to treat these databases as a special case. See commit, disconnect and Transactions for other important notes about transactions. Holds the handle of the parent driver. The only recommended use for this is to find the name of the driver using:
$dbh->{Driver}->{Name}Holds the name of the database. Usually (and recommended to be) the same as the "CWdbi:DriverName:... string used to connect to the database, but with the leading CWdbi:DriverName:" removed. Returns the statement string passed to the most recent prepare method called in this database handle, even if that method failed. This is especially useful where CWRaiseError is enabled and the exception handler checks $@ and sees that a 'prepare' method call failed. A hint to the driver indicating the size of the local row cache that the application would like the driver to use for future CWSELECT statements. If a row cache is not implemented, then setting CWRowCacheSize is ignored and getting the value returns CWundef. Some CWRowCacheSize values have special meaning, as follows:
0 - Automatically determine a reasonable cache size for each C<SELECT> 1 - Disable the local row cache >1 - Cache this many rows <0 - Cache as many rows that will fit into this much memory for each C<SELECT>.Note that large cache sizes may require a very large amount of memory (cached rows * maximum size of row). Also, a large cache will cause a longer delay not only for the first fetch, but also whenever the cache needs refilling. See also the RowsInCache statement handle attribute. Returns the username used to connect to the database.
DBI STATEMENT HANDLE OBJECTS
This section lists the methods and attributes associated with DBI statement handles.
Statement Handle Methods
The DBI defines the following methods for use on DBI statement handles:
$sth->bind_param($p_num, $bind_value) $sth->bind_param($p_num, $bind_value, \%attr) $sth->bind_param($p_num, $bind_value, $bind_type)The CWbind_param method takes a copy of CW$bind_value and associates it (binds it) with a placeholder, identified by CW$p_num, embedded in the prepared statement. Placeholders are indicated with question mark character (CW?). For example:
$dbh->{RaiseError} = 1; # save having to check each method call $sth = $dbh->prepare("SELECT name, age FROM people WHERE name LIKE ?"); $sth->bind_param(1, "John%"); # placeholders are numbered from 1 $sth->execute; DBI::dump_results($sth);See Placeholders and Bind Values for more information. Data Types for Placeholders The CW\%attr parameter can be used to hint at the data type the placeholder should have. This is rarely needed. Typically, the driver is only interested in knowing if the placeholder should be bound as a number or a string.
$sth->bind_param(1, $value, { TYPE => SQL_INTEGER });As a short-cut for the common case, the data type can be passed directly, in place of the CW\%attr hash reference. This example is equivalent to the one above:
$sth->bind_param(1, $value, SQL_INTEGER);The CWTYPE value indicates the standard (non-driver-specific) type for this parameter. To specify the driver-specific type, the driver may support a driver-specific attribute, such as CW{ ora_type => 97 }. The SQL_INTEGER and other related constants can be imported using
use DBI qw(:sql_types);See DBI Constants for more information. The data type for a placeholder cannot be changed after the first CWbind_param call. In fact the whole \%attr parameter is 'sticky' in the sense that a driver only needs to consider the \%attr parameter for the first call, for a given CW$sth and parameter. After that the driver may ignore the \%attr parameter for that placeholder. Perl only has string and number scalar data types. All database types that aren't numbers are bound as strings and must be in a format the database will understand except where the bind_param() TYPE attribute specifies a type that implies a particular format. For example, given:
$sth->bind_param(1, $value, SQL_DATETIME);the driver should expect CW$value to be in the ODBC standard SQL_DATETIME format, which is 'YYYY-MM-DD HH:MM:SS'. Similarly for SQL_DATE, SQL_TIME etc. As an alternative to specifying the data type in the CWbind_param call, you can let the driver pass the value as the default type (CWVARCHAR). You can then use an SQL function to convert the type within the statement. For example:
INSERT INTO price(code, price) VALUES (?, CONVERT(MONEY,?))The CWCONVERT function used here is just an example. The actual function and syntax will vary between different databases and is non-portable. See also Placeholders and Bind Values for more information.
$rc = $sth->bind_param_inout($p_num, \$bind_value, $max_len) or die $sth->errstr; $rv = $sth->bind_param_inout($p_num, \$bind_value, $max_len, \%attr) or ... $rv = $sth->bind_param_inout($p_num, \$bind_value, $max_len, $bind_type) or ...This method acts like bind_param, but also enables values to be updated by the statement. The statement is typically a call to a stored procedure. The CW$bind_value must be passed as a reference to the actual value to be used. Note that unlike bind_param, the CW$bind_value variable is not copied when CWbind_param_inout is called. Instead, the value in the variable is read at the time execute is called. The additional CW$max_len parameter specifies the minimum amount of memory to allocate to CW$bind_value for the new value. If the value returned from the database is too big to fit, then the execution should fail. If unsure what value to use, pick a generous length, i.e., a length larger than the longest value that would ever be returned. The only cost of using a larger value than needed is wasted memory. Undefined values or CWundef are used to indicate null values. See also Placeholders and Bind Values for more information.
$rc = $sth->bind_param_array($p_num, $array_ref_or_value) $rc = $sth->bind_param_array($p_num, $array_ref_or_value, \%attr) $rc = $sth->bind_param_array($p_num, $array_ref_or_value, $bind_type)The CWbind_param_array method is used to bind an array of values to a placeholder embedded in the prepared statement which is to be executed with execute_array. For example:
$dbh->{RaiseError} = 1; # save having to check each method call $sth = $dbh->prepare("INSERT INTO staff (first_name, last_name, dept) VALUES(?, ?, ?)"); $sth->bind_param_array(1, [ 'John', 'Mary', 'Tim' ]); $sth->bind_param_array(2, [ 'Booth', 'Todd', 'Robinson' ]); $sth->bind_param_array(3, "SALES"); # scalar will be reused for each row $sth->execute_array( { ArrayTupleStatus => \my @tuple_status } );The CW%attr ($bind_type) argument is the same as defined for bind_param. Refer to bind_param for general details on using placeholders. (Note that bind_param_array() can not be used to expand a placeholder into a list of values for a statement like SELECT foo WHERE bar IN (?). A placeholder can only ever represent one value per execution.) Scalar values, including CWundef, may also be bound by CWbind_param_array. In which case the same value will be used for each execute call. Driver-specific implementations may behave differently, e.g., when binding to a stored procedure call, some databases may permit mixing scalars and arrays as arguments. The default implementation provided by DBI (for drivers that have not implemented array binding) is to iteratively call execute for each parameter tuple provided in the bound arrays. Drivers may provide more optimized implementations using whatever bulk operation support the database API provides. The default driver behaviour should match the default DBI behaviour, but always consult your driver documentation as there may be driver specific issues to consider. Note that the default implementation currently only supports non-data returning statements (INSERT, UPDATE, but not SELECT). Also, CWbind_param_array and bind_param cannot be mixed in the same statement execution, and CWbind_param_array must be used with execute_array; using CWbind_param_array will have no effect for execute. The CWbind_param_array method was added in DBI 1.22.
$rv = $sth->execute or die $sth->errstr; $rv = $sth->execute(@bind_values) or die $sth->errstr;Perform whatever processing is necessary to execute the prepared statement. An CWundef is returned if an error occurs. A successful CWexecute always returns true regardless of the number of rows affected, even if it's zero (see below). It is always important to check the return status of CWexecute (and most other DBI methods) for errors if you're not using RaiseError. For a non-CWSELECT statement, CWexecute returns the number of rows affected, if known. If no rows were affected, then CWexecute returns "CW0E0", which Perl will treat as 0 but will regard as true. Note that it is not an error for no rows to be affected by a statement. If the number of rows affected is not known, then CWexecute returns -1. For CWSELECT statements, execute simply starts the query within the database engine. Use one of the fetch methods to retrieve the data after calling CWexecute. The CWexecute method does not return the number of rows that will be returned by the query (because most databases can't tell in advance), it simply returns a true value. If any arguments are given, then CWexecute will effectively call bind_param for each value before executing the statement. Values bound in this way are usually treated as CWSQL_VARCHAR types unless the driver can determine the correct type (which is rare), or unless CWbind_param (or CWbind_param_inout) has already been used to specify the type. If execute() is called on a statement handle that's still active ($sth->{Active} is true) then it should effectively call finish() to tidy up the previous execution results before starting this new execution.
$rv = $sth->execute_array(\%attr) or die $sth->errstr; $rv = $sth->execute_array(\%attr, @bind_values) or die $sth->errstr;Execute the prepared statement once for each parameter tuple (group of values) provided either in the CW@bind_values, or by prior calls to bind_param_array, or via a reference passed in \%attr. The execute_array() method returns the number of tuples executed, or CWundef if an error occured. Like execute(), a successful execute_array() always returns true regardless of the number of tuples executed, even if it's zero. See the CWArrayTupleStatus attribute below for how to determine the execution status for each tuple. Bind values for the tuples to be executed may be supplied row-wise by an CWArrayTupleFetch attribute, or else column-wise in the CW@bind_values argument, or else column-wise by prior calls to bind_param_array. Where column-wise binding is used (via the CW@bind_values argument or calls to bind_param_array()) the maximum number of elements in any one of the bound value arrays determines the number of tuples executed. Placeholders with fewer values in their parameter arrays are treated as if padded with undef (NULL) values. If a scalar value is bound, instead of an array reference, it is treated as a variable length array with all elements having the same value. It's does not influence the number of tuples executed, so if all bound arrays have zero elements then zero tuples will be executed. If all bound values are scalars then one tuple will be executed, making execute_array() act just like execute(). The CWArrayTupleFetch attribute can be used to specify a reference to a subroutine that will be called to provide the bind values for each tuple execution. The subroutine should return an reference to an array which contains the appropriate number of bind values, or return an undef if there is no more data to execute. As a convienience, the CWArrayTupleFetch attribute can also be used to specify a statement handle. In which case the fetchrow_arrayref() method will be called on the given statement handle in order to provide the bind values for each tuple execution. The values specified via bind_param_array() or the CW@bind_values parameter may be either scalars, or arrayrefs. If any CW@bind_values are given, then CWexecute_array will effectively call bind_param_array for each value before executing the statement. Values bound in this way are usually treated as CWSQL_VARCHAR types unless the driver can determine the correct type (which is rare), or unless CWbind_param, CWbind_param_inout, CWbind_param_array, or CWbind_param_inout_array has already been used to specify the type. See bind_param_array for details. The mandatory CWArrayTupleStatus attribute is used to specify a reference to an array which will receive the execute status of each executed parameter tuple. For tuples which are successfully executed, the element at the same ordinal position in the status array is the resulting rowcount. If the execution of a tuple causes an error, then the corresponding status array element will be set to a reference to an array containing the error code and error string set by the failed execution. If any tuple execution returns an error, CWexecute_array will return CWundef. In that case, the application should inspect the status array to determine which parameter tuples failed. Some databases may not continue executing tuples beyond the first failure. In this case the status array will either hold fewer elements, or the elements beyond the failure will be undef. If all parameter tuples are successfully executed, CWexecute_array returns the number tuples executed. If no tuples were executed, then execute_array() returns "CW0E0", just like execute() does, which Perl will treat as 0 but will regard as true. For example:
$sth = $dbh->prepare("INSERT INTO staff (first_name, last_name) VALUES (?, ?)"); my $tuples = $sth->execute_array( { ArrayTupleStatus => \my @tuple_status }, \@first_names, \@last_names, ); if ($tuples) { print "Successfully inserted $tuples records\n"; } else { for my $tuple (0..@last_names-1) { my $status = $tuple_status[$tuple]; $status = [0, "Skipped"] unless defined $status; next unless ref $status; printf "Failed to insert (%s, %s): %s\n", $first_names[$tuple], $last_names[$tuple], $status->[1]; } }Support for data returning statements such as SELECT is driver-specific and subject to change. At present, the default implementation provided by DBI only supports non-data returning statements. Transaction semantics when using array binding are driver and database specific. If CWAutoCommit is on, the default DBI implementation will cause each parameter tuple to be inidividually committed (or rolled back in the event of an error). If CWAutoCommit is off, the application is responsible for explicitly committing the entire set of bound parameter tuples. Note that different drivers and databases may have different behaviours when some parameter tuples cause failures. In some cases, the driver or database may automatically rollback the effect of all prior parameter tuples that succeeded in the transaction; other drivers or databases may retain the effect of prior successfully executed parameter tuples. Be sure to check your driver and database for its specific behaviour. Note that, in general, performance will usually be better with CWAutoCommit turned off, and using explicit CWcommit after each CWexecute_array call. The CWexecute_array method was added in DBI 1.22, and ArrayTupleFetch was added in 1.36.
$rc = $sth->execute_for_fetch($fetch_tuple_sub); $rc = $sth->execute_for_fetch($fetch_tuple_sub, \@tuple_status);The execute_for_fetch() method is used to perform bulk operations and is most often used via the execute_array() method, not directly. The fetch subroutine, referenced by CW$fetch_tuple_sub, is expected to return a reference to an array (known as a 'tuple') or undef. The execute_for_fetch() method calls CW$fetch_tuple_sub, without any parameters, until it returns a false value. Each tuple returned is used to provide bind values for an CW$sth->execute(@$tuple) call. If there were any errors then CWundef is returned and the CW@tuple_status array can be used to discover which tuples failed and with what errors. If there were no errors then execute_for_fetch() returns the number of tuples executed. Like execute() and execute_array() a zero is returned as 0E0 so execute_for_fetch() is only false on error. If \@tuple_status is passed then the execute_for_fetch method uses it to return status information. The tuple_status array holds one element per tuple. If the corresponding execute() did not fail then the element holds the return value from execute(), which is typically a row count. If the execute() did fail then the element holds a reference to an array containing ($sth->err, CW$sth->errstr, CW$sth->state). Although each tuple returned by CW$fetch_tuple_sub is effectively used to call CW$sth->execute(@$tuple_array_ref) the exact timing may vary. Drivers are free to accumulate sets of tuples to pass to the database server in bulk group operations for more efficient execution. However, the CW$fetch_tuple_sub is specifically allowed to return the same array reference each time (which is what fetchrow_arrayref() usually does). For example:
my $sel = $dbh1->prepare("select foo, bar from table1"); $sel->execute;
my $ins = $dbh2->prepare("insert into table2 (foo, bar) values (?,?)"); my $fetch_tuple_sub = sub { $sel->fetchrow_arrayref };
my @tuple_status; $rc = $ins->execute_for_fetch($fetch_tuple_sub, \@tuple_status); my @errors = grep { ref $_ } @tuple_status;Similarly, if you already have an array containing the data rows to be processed you'd use a subroutine to shift off and return each array ref in turn:
$ins->execute_for_fetch( sub { shift @array_of_arrays }, \@tuple_status);The CWexecute_for_fetch method was added in DBI 1.38.
$ary_ref = $sth->fetchrow_arrayref; $ary_ref = $sth->fetch; # aliasFetches the next row of data and returns a reference to an array holding the field values. Null fields are returned as CWundef values in the array. This is the fastest way to fetch data, particularly if used with CW$sth->bind_columns. If there are no more rows or if an error occurs, then CWfetchrow_arrayref returns an CWundef. You should check CW$sth->err afterwards (or use the CWRaiseError attribute) to discover if the CWundef returned was due to an error. Note that the same array reference is returned for each fetch, so don't store the reference and then use it after a later fetch. Also, the elements of the array are also reused for each row, so take care if you want to take a reference to an element. See also bind_columns.
@ary = $sth->fetchrow_array;An alternative to CWfetchrow_arrayref. Fetches the next row of data and returns it as a list containing the field values. Null fields are returned as CWundef values in the list. If there are no more rows or if an error occurs, then CWfetchrow_array returns an empty list. You should check CW$sth->err afterwards (or use the CWRaiseError attribute) to discover if the empty list returned was due to an error. 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 CWundef is returned if there are no more rows or if an error occurred. That CWundef can't be distinguished from an CWundef returned because the first field value was NULL. For these reasons you should exercise some caution if you use CWfetchrow_array in a scalar context.
$hash_ref = $sth->fetchrow_hashref; $hash_ref = $sth->fetchrow_hashref($name);An alternative to CWfetchrow_arrayref. Fetches the next row of data and returns it as a reference to a hash containing field name and field value pairs. Null fields are returned as CWundef values in the hash. If there are no more rows or if an error occurs, then CWfetchrow_hashref returns an CWundef. You should check CW$sth->err afterwards (or use the CWRaiseError attribute) to discover if the CWundef returned was due to an error. The optional CW$name parameter specifies the name of the statement handle attribute. For historical reasons it defaults to "CWNAME, however using either CWNAME_lc or CWNAME_uc" is recomended for portability. The keys of the hash are the same names returned by CW$sth->{$name}. If more than one field has the same name, there will only be one entry in the returned hash for those fields. Because of the extra work CWfetchrow_hashref and Perl have to perform, it is not as efficient as CWfetchrow_arrayref or CWfetchrow_array. By default a reference to a new hash is returned for each row. It is likely that a future version of the DBI will support an attribute which will enable the same hash to be reused for each row. This will give a significant performance boost, but it won't be enabled by default because of the risk of breaking old code.
$tbl_ary_ref = $sth->fetchall_arrayref; $tbl_ary_ref = $sth->fetchall_arrayref( $slice ); $tbl_ary_ref = $sth->fetchall_arrayref( $slice, $max_rows );The CWfetchall_arrayref method can be used to fetch all the data to be returned from a prepared and executed statement handle. It returns a reference to an array that contains one reference per row. If there are no rows to return, CWfetchall_arrayref returns a reference to an empty array. If an error occurs, CWfetchall_arrayref returns the data fetched thus far, which may be none. You should check CW$sth->err afterwards (or use the CWRaiseError attribute) to discover if the data is complete or was truncated due to an error. If CW$slice is an array reference, CWfetchall_arrayref uses fetchrow_arrayref to fetch each row as an array ref. If the CW$slice array is not empty then it is used as a slice to select individual columns by perl array index number (starting at 0, unlike column and parameter numbers which start at 1). With no parameters, or if CW$slice is undefined, CWfetchall_arrayref acts as if passed an empty array ref. If CW$slice is a hash reference, CWfetchall_arrayref uses fetchrow_hashref to fetch each row as a hash reference. If the CW$slice hash is empty then fetchrow_hashref() is simply called in a tight loop and the keys in the hashes have whatever name lettercase is returned by default from fetchrow_hashref. (See FetchHashKeyName attribute.) If the CW$slice hash is not empty, then it is used as a slice to select individual columns by name. The values of the hash should be set to 1. The key names of the returned hashes match the letter case of the names in the parameter hash, regardless of the FetchHashKeyName attribute. For example, to fetch just the first column of every row:
$tbl_ary_ref = $sth->fetchall_arrayref([0]);To fetch the second to last and last column of every row:
$tbl_ary_ref = $sth->fetchall_arrayref([-2,-1]);To fetch all fields of every row as a hash ref:
$tbl_ary_ref = $sth->fetchall_arrayref({});To fetch only the fields called foo and bar of every row as a hash ref (with keys named foo and BAR):
$tbl_ary_ref = $sth->fetchall_arrayref({ foo=>1, BAR=>1 });The first two examples return a reference to an array of array refs. The third and forth return a reference to an array of hash refs. If CW$max_rows is defined and greater than or equal to zero then it is used to limit the number of rows fetched before returning. fetchall_arrayref() can then be called again to fetch more rows. This is especially useful when you need the better performance of fetchall_arrayref() but don't have enough memory to fetch and return all the rows in one go. Here's an example:
my $rows = []; # cache for batches of rows while( my $row = ( shift(@$rows) || # get row from cache, or reload cache: shift(@{$rows=$sth->fetchall_arrayref(undef,10_000)||[]}) ) ) { ... }That can be the fastest way to fetch and process lots of rows using the DBI, but it depends on the relative cost of method calls vs memory allocation. A standard CWwhile loop with column binding is often faster because the cost of allocating memory for the batch of rows is greater than the saving by reducing method calls. It's possible that the DBI may provide a way to reuse the memory of a previous batch in future, which would then shift the balance back towards fetchall_arrayref().
$hash_ref = $sth->fetchall_hashref($key_field);The CWfetchall_hashref method can be used to fetch all the data to be returned from a prepared and executed statement handle. It returns a reference to a hash containing a key for each distinct value of the CW$key_field column that was fetched For each key the corresponding value is a reference to a hash containing all the selected columns and their values, as returned by fetchrow_hashref(). If there are no rows to return, CWfetchall_hashref returns a reference to an empty hash. If an error occurs, CWfetchall_hashref returns the data fetched thus far, which may be none. You should check CW$sth->err afterwards (or use the CWRaiseError attribute) to discover if the data is complete or was truncated due to an error. The CW$key_field parameter provides the name of the field that holds the value to be used for the key for the returned hash. For example:
$dbh->{FetchHashKeyName} = 'NAME_lc'; $sth = $dbh->prepare("SELECT FOO, BAR, ID, NAME, BAZ FROM TABLE"); $sth->execute; $hash_ref = $sth->fetchall_hashref('id'); print "Name for id 42 is $hash_ref->{42}->{name}\n";The CW$key_field parameter can also be specified as an integer column number (counting from 1). If CW$key_field doesn't match any column in the statement, as a name first then as a number, then an error is returned. For queries returing more than one 'key' column, you can specify multiple column names by passing CW$key_field as a reference to an array containing one or more key column names (or index numbers). For example:
$sth = $dbh->prepare("SELECT foo, bar, baz FROM table"); $sth->execute; $hash_ref = $sth->fetchall_hashref( [ qw(foo bar) ] ); print "For foo 42 and bar 38, baz is $hash_ref->{42}->{38}->{baz}\n";The fetchall_hashref() method is normally used only where the key fields values for each row are unique. If multiple rows are returned with the same values for the key fields then later rows overwrite earlier ones.
$rc = $sth->finish;Indicate that no more data will be fetched from this statement handle before it is either executed again or destroyed. The CWfinish method is rarely needed, and frequently overused, but can sometimes be helpful in a few very specific situations to allow the server to free up resources (such as sort buffers). When all the data has been fetched from a CWSELECT statement, the driver should automatically call CWfinish for you. So you should not normally need to call it explicitly except when you know that you've not fetched all the data from a statement handle. The most common example is when you only want to fetch one row, but in that case the CWselectrow_* methods are usually better anyway. Adding calls to CWfinish after each fetch loop is a common mistake, don't do it, it can mask genuine problems like uncaught fetch errors. Consider a query like:
SELECT foo FROM table WHERE bar=? ORDER BY foowhere you want to select just the first (smallest) foo value from a very large table. When executed, the database server will have to use temporary buffer space to store the sorted rows. If, after executing the handle and selecting one row, the handle won't be re-executed for some time and won't be destroyed, the CWfinish method can be used to tell the server that the buffer space can be freed. Calling CWfinish resets the Active attribute for the statement. It may also make some statement handle attributes (such as CWNAME and CWTYPE) unavailable if they have not already been accessed (and thus cached). The CWfinish method does not affect the transaction status of the database connection. It has nothing to do with transactions. It's mostly an internal housekeeping method that is rarely needed. See also disconnect and the Active attribute. The CWfinish method should have been called CWdiscard_pending_rows.
$rv = $sth->rows;Returns the number of rows affected by the last row affecting command, or -1 if the number of rows is not known or not available. Generally, you can only rely on a row count after a non-CWSELECT CWexecute (for some specific operations like CWUPDATE and CWDELETE), or after fetching all the rows of a CWSELECT statement. For CWSELECT statements, it is generally not possible to know how many rows will be returned except by fetching them all. Some drivers will return the number of rows the application has fetched so far, but others may return -1 until all rows have been fetched. So use of the CWrows method or CW$DBI::rows with CWSELECT statements is not recommended. One alternative method to get a row count for a CWSELECT is to execute a SELECT COUNT(*) FROM ... SQL statement with the same ... as your query and then fetch the row count from that.
$rc = $sth->bind_col($column_number, \$var_to_bind); $rc = $sth->bind_col($column_number, \$var_to_bind, \%attr ); $rc = $sth->bind_col($column_number, \$var_to_bind, $bind_type );Binds a Perl variable and/or some attributes to an output column (field) of a CWSELECT statement. Column numbers count up from 1. You do not need to bind output columns in order to fetch data. For maximum portability between drivers, bind_col() should be called after execute() and not before. See also CWbind_columns for an example. The binding is performed at a low level using Perl aliasing. Whenever a row is fetched from the database CW$var_to_bind appears to be automatically updated simply because it now refers to the same memory location as the corresponding column value. This makes using bound variables very efficient. Binding a tied variable doesn't work, currently. The bind_param method performs a similar, but opposite, function for input variables. Data Types for Column Binding The CW\%attr parameter can be used to hint at the data type formatting the column should have. For example, you can use:
$sth->bind_col(1, undef, { TYPE => SQL_DATETIME });to specify that you'd like the column (which presumably is some kind of datetime type) to be returned in the standard format for SQL_DATETIME, which is 'YYYY-MM-DD HH:MM:SS', rather than the native formatting the database would normally use. There's no CW$var_to_bind in that example to emphasize the point that bind_col() works on the underlying column value and not just a particular bound variable. As a short-cut for the common case, the data type can be passed directly, in place of the CW\%attr hash reference. This example is equivalent to the one above:
$sth->bind_col(1, undef, SQL_DATETIME);The CWTYPE value indicates the standard (non-driver-specific) type for this parameter. To specify the driver-specific type, the driver may support a driver-specific attribute, such as CW{ ora_type => 97 }. The SQL_DATETIME and other related constants can be imported using
use DBI qw(:sql_types);See DBI Constants for more information. The data type for a bind variable cannot be changed after the first CWbind_col call. In fact the whole \%attr parameter is 'sticky' in the sense that a driver only needs to consider the \%attr parameter for the first call for a given CW$sth and column. The TYPE attribute for bind_col() was first specified in DBI 1.41.
$rc = $sth->bind_columns(@list_of_refs_to_vars_to_bind);Calls bind_col for each column of the CWSELECT statement. The CWbind_columns method will die if the number of references does not match the number of fields. For maximum portability between drivers, bind_columns() should be called after execute() and not before. For example:
$dbh->{RaiseError} = 1; # do this, or check every call for errors $sth = $dbh->prepare(q{ SELECT region, sales FROM sales_by_region }); $sth->execute; my ($region, $sales);
# Bind Perl variables to columns: $rv = $sth->bind_columns(\$region, \$sales);
# you can also use Perl's \(...) syntax (see perlref docs): # $sth->bind_columns(\($region, $sales));
# Column binding is the most efficient way to fetch data while ($sth->fetch) { print "$region: $sales\n"; }For compatibility with old scripts, the first parameter will be ignored if it is CWundef or a hash reference. Here's a more fancy example that binds columns to the values inside a hash (thanks to H.Merijn Brand):
$sth->execute; my %row; $sth->bind_columns( \( @row{ @{$sth->{NAME_lc} } } )); while ($sth->fetch) { print "$row{region}: $row{sales}\n"; }
$rows = $sth->dump_results($maxlen, $lsep, $fsep, $fh);Fetches all the rows from CW$sth, calls CWDBI::neat_list for each row, and prints the results to CW$fh (defaults to CWSTDOUT) separated by CW$lsep (default CW"\n"). CW$fsep defaults to CW", " and CW$maxlen defaults to 35. This method is designed as a handy utility for prototyping and testing queries. Since it uses neat_list to format and edit the string for reading by humans, it is not recomended for data transfer applications.
Statement Handle Attributes
This section describes attributes specific to statement handles. Most of these attributes are read-only.
Changes to these statement handle attributes do not affect any other existing or future statement handles.
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).
Example:
... = $h->{NUM_OF_FIELDS}; # get/read
Some drivers cannot provide valid values for some or all of these attributes until after CW$sth->execute has been successfully called. Typically the attribute will be CWundef in these situations.
Some attributes, like NAME, are not appropriate to some types of statement, like SELECT. Typically the attribute will be CWundef in these situations.
See also finish to learn more about the effect it may have on some attributes. Number of fields (columns) in the data the prepared statement may return. Statements that don't return rows of data, like CWDELETE and CWCREATE set CWNUM_OF_FIELDS to 0. The number of parameters (placeholders) in the prepared statement. See SUBSTITUTION VARIABLES below for more details. Returns a reference to an array of field names for each column. The names may contain spaces but should not be truncated or have any trailing space. Note that the names have the letter case (upper, lower or mixed) as returned by the driver being used. Portable applications should use NAME_lc or NAME_uc.
print "First column name: $sth->{NAME}->[0]\n";Like NAME but always returns lowercase names. Like NAME but always returns uppercase names. The CWNAME_hash, CWNAME_lc_hash, and CWNAME_uc_hash attributes return column name information as a reference to a hash. The keys of the hash are the names of the columns. The letter case of the keys corresponds to the letter case returned by the CWNAME, CWNAME_lc, and CWNAME_uc attributes respectively (as described above). The value of each hash entry is the perl index number of the corresponding column (counting from 0). For example:
$sth = $dbh->prepare("select Id, Name from table"); $sth->execute; @row = $sth->fetchrow_array; print "Name $row[ $sth->{NAME_lc_hash}{name} ]\n";Returns a reference to an array of integer values for each column. The value indicates the data type of the corresponding column. The values correspond to the international standards (ANSI X3.135 and ISO/IEC 9075) which, in general terms, means ODBC. Driver-specific types that don't exactly match standard types should generally return the same values as an ODBC driver supplied by the makers of the database. That might include private type numbers in ranges the vendor has officially registered with the ISO working group:
ftp://sqlstandards.org/SC32/SQL_Registry/Where there's no vendor-supplied ODBC driver to be compatible with, the DBI driver can use type numbers in the range that is now officially reserved for use by the DBI: -9999 to -9000. All possible values for CWTYPE should have at least one entry in the output of the CWtype_info_all method (see type_info_all). Returns a reference to an array of integer values for each column. For numeric columns, the value is the maximum number of digits (without considering a sign character or decimal point). Note that the display size for floating point types (REAL, FLOAT, DOUBLE) can be up to 7 characters greater than the precision (for the sign + decimal point + the letter E + a sign + 2 or 3 digits). For any character type column the value is the OCTET_LENGTH, in other words the number of bytes, not characters. (More recent standards refer to this as COLUMN_SIZE but we stick with PRECISION for backwards compatibility.) Returns a reference to an array of integer values for each column. NULL (CWundef) values indicate columns where scale is not applicable. Returns a reference to an array indicating the possibility of each column returning a null. Possible values are CW0 (or an empty string) = no, CW1 = yes, CW2 = unknown.
print "First column may return NULL\n" if $sth->{NULLABLE}->[0];Returns the name of the cursor associated with the statement handle, if available. If not available or if the database driver does not support the CW"where current of ..." SQL syntax, then it returns CWundef. Returns the parent CW$dbh of the statement handle. Returns a reference to a hash containing the values currently bound to placeholders. The keys of the hash are the 'names' of the placeholders, typically integers starting at 1. Returns undef if not supported by the driver. See ShowErrorStatement for an example of how this is used. If the driver supports CWParamValues but no values have been bound yet then the driver should return a hash with placeholders names in the keys but all the values undef, but some drivers may return a ref to an empty hash. It is possible that the values in the hash returned by CWParamValues are not exactly the same as those passed to bind_param() or execute(). The driver may have slightly modified values in some way based on the TYPE the value was bound with. For example a floating point value bound as an SQL_INTEGER type may be returned as an integer. The values returned by CWParamValues can be passed to another bind_param() method with the same TYPE and will be seen by the database as the same value. It is also possible that the keys in the hash returned by CWParamValues are not exactly the same as those implied by the prepared statement. For example, DBD::Oracle translates 'CW?' placeholders into 'CW:pN' where N is a sequence number starting at 1. The CWParamValues attribute was added in DBI 1.28. Returns a reference to a hash containing the type information currently bound to placeholders. The keys of the hash are the 'names' of the placeholders: either integers starting at 1, or, for drivers that support named placeholders, the actual parameter name string. The hash values are hashrefs of type information in the same form as that provided to the various bind_param() methods (See Data Types for Placeholders for the format and values), plus anything else that was passed as the third argument to bind_param(). Returns undef if not supported by the driver. If the driver supports CWParamTypes, but no values have been bound yet, then the driver should return a hash with the placeholder name keys, but all the values undef; however, some drivers may return a ref to an empty hash, or, alternately, may provide type information supplied by the database (only a few databases can do that). It is possible that the values in the hash returned by CWParamTypes are not exactly the same as those passed to bind_param() or execute(). The driver may have modified the type information in some way based on the bound values, other hints provided by the prepare()'d SQL statement, or alternate type mappings required by the driver or target database system. It is also possible that the keys in the hash returned by CWParamTypes are not exactly the same as those implied by the prepared statement. For example, DBD::Oracle translates 'CW?' placeholders into 'CW:pN' where N is a sequence number starting at 1. The CWParamTypes attribute was added in DBI 1.49. Implementation is the responsibility of individual drivers; the DBI layer default implementation simply returns undef. Returns the statement string passed to the prepare method. If the driver supports a local row cache for CWSELECT statements, then this attribute holds the number of un-fetched rows in the cache. If the driver doesn't, then it returns CWundef. Note that some drivers pre-fetch rows on execute, whereas others wait till the first fetch. See also the RowCacheSize database handle attribute.
OTHER METHODS
DBD::Foo::db->install_method($method_name, \%attr);
Installs the driver-private method named by CW$method_name into the DBI method dispatcher so it can be called directly, avoiding the need to use the func() method. It is called as a static method on the driver class to which the method belongs. The method name must begin with the corresponding registered driver-private prefix. For example, for DBD::Oracle CW$method_name must being with 'CWora_', and for DBD::AnyData it must begin with 'CWad_'. The attributes can be used to provide fine control over how the DBI dispatcher handles the dispatching of the method. However, at this point, it's undocumented and very liable to change. (Volunteers to polish up and document the interface are very welcome to get in touch via dbi-dev@perl.org) Methods installed using install_method default to the standard error handling behaviour for DBI methods: clearing err and errstr before calling the method, and checking for errors to trigger RaiseError etc. on return. This differs from the default behaviour of func(). Note for driver authors: The DBD::Foo::xx->install_method call won't work until the class-hierarchy has been setup. Normally the DBI looks after that just after the driver is loaded. This means install_method() can't be called at the time the driver is loaded unless the class-hierarchy is set up first. The way to do that is to call the setup_driver() method:
DBI->setup_driver('DBD::Foo');before using install_method().
FURTHER INFORMATION
Catalog Methods
An application can retrieve metadata information from the DBMS by issuing appropriate queries on the views of the Information Schema. Unfortunately, CWINFORMATION_SCHEMA views are seldom supported by the DBMS. Special methods (catalog methods) are available to return result sets for a small but important portion of that metadata:
column_info foreign_key_info primary_key_info table_info
All catalog methods accept arguments in order to restrict the result sets. Passing CWundef to an optional argument does not constrain the search for that argument. However, an empty string ('') is treated as a regular search criteria and will only match an empty value.
Note: SQL/CLI and ODBC differ in the handling of empty strings. An empty string will not restrict the result set in SQL/CLI.
Most arguments in the catalog methods accept only ordinary values, e.g. the arguments of CWprimary_key_info(). Such arguments are treated as a literal string, i.e. the case is significant and quote characters are taken literally.
Some arguments in the catalog methods accept search patterns (strings containing '_' and/or '%'), e.g. the CW$table argument of CWcolumn_info(). Passing '%' is equivalent to leaving the argument CWundef.
Caveat: The underscore ('_') is valid and often used in SQL identifiers. Passing such a value to a search pattern argument may return more rows than expected! To include pattern characters as literals, they must be preceded by an escape character which can be achieved with
$esc = $dbh->get_info( 14 ); # SQL_SEARCH_PATTERN_ESCAPE $search_pattern =~ s/([_%])/$esc$1/g;
The ODBC and SQL/CLI specifications define a way to change the default behaviour described above: All arguments (except list value arguments) are treated as identifier if the CWSQL_ATTR_METADATA_ID attribute is set to CWSQL_TRUE. Quoted identifiers are very similar to ordinary values, i.e. their body (the string within the quotes) is interpreted literally. Unquoted identifiers are compared in UPPERCASE.
The DBI (currently) does not support the CWSQL_ATTR_METADATA_ID attribute, i.e. it behaves like an ODBC driver where CWSQL_ATTR_METADATA_ID is set to CWSQL_FALSE.
Transactions
Transactions are a fundamental part of any robust database system. They protect against errors and database corruption by ensuring that sets of related changes to the database take place in atomic (indivisible, all-or-nothing) units.
This section applies to databases that support transactions and where CWAutoCommit is off. See AutoCommit for details of using CWAutoCommit with various types of databases.
The recommended way to implement robust transactions in Perl applications is to use CWRaiseError and CWeval { ... } (which is very fast, unlike CWeval "..."). For example:
$dbh->{AutoCommit} = 0; # enable transactions, if possible $dbh->{RaiseError} = 1; eval { foo(...) # do lots of work here bar(...) # including inserts baz(...) # and updates $dbh->commit; # commit the changes if we get this far }; if ($@) { warn "Transaction aborted because $@"; # now rollback to undo the incomplete changes # but do it in an eval{} as it may also fail eval { $dbh->rollback }; # add other application on-error-clean-up code here }
If the CWRaiseError attribute is not set, then DBI calls would need to be manually checked for errors, typically like this:
$h->method(@args) or die $h->errstr;
With CWRaiseError set, the DBI will automatically CWdie if any DBI method call on that handle (or a child handle) fails, so you don't have to test the return value of each method call. See RaiseError for more details.
A major advantage of the CWeval approach is that the transaction will be properly rolled back if any code (not just DBI calls) in the inner application dies for any reason. The major advantage of using the CW$h->{RaiseError} attribute is that all DBI calls will be checked automatically. Both techniques are strongly recommended.
After calling CWcommit or CWrollback many drivers will not let you fetch from a previously active CWSELECT statement handle that's a child of the same database handle. A typical way round this is to connect the the database twice and use one connection for CWSELECT statements.
See AutoCommit and disconnect for other important information about transactions.
Handling BLOB / LONG / Memo Fields
Many databases support blob (binary large objects), long, or similar datatypes for holding very long strings or large amounts of binary data in a single field. Some databases support variable length long values over 2,000,000,000 bytes in length.
Since values of that size can't usually be held in memory, and because databases can't usually know in advance the length of the longest long that will be returned from a CWSELECT statement (unlike other data types), some special handling is required.
In this situation, the value of the CW$h->{LongReadLen} attribute is used to determine how much buffer space to allocate when fetching such fields. The CW$h->{LongTruncOk} attribute is used to determine how to behave if a fetched value can't fit into the buffer.
See the description of LongReadLen for more information.
When trying to insert long or binary values, placeholders should be used since there are often limits on the maximum size of an CWINSERT statement and the quote method generally can't cope with binary data. See Placeholders and Bind Values.
Simple Examples
Here's a complete example program to select and fetch some data:
my $data_source = "dbi::DriverName:db_name"; my $dbh = DBI->connect($data_source, $user, $password) or die "Can't connect to $data_source: $DBI::errstr";
my $sth = $dbh->prepare( q{ SELECT name, phone FROM mytelbook }) or die "Can't prepare statement: $DBI::errstr";
my $rc = $sth->execute or die "Can't execute statement: $DBI::errstr";
print "Query will return $sth->{NUM_OF_FIELDS} fields.\n\n"; print "Field names: @{ $sth->{NAME} }\n";
while (($name, $phone) = $sth->fetchrow_array) { print "$name: $phone\n"; } # check for problems which may have terminated the fetch early die $sth->errstr if $sth->err;
$dbh->disconnect;
Here's a complete example program to insert some data from a file. (This example uses CWRaiseError to avoid needing to check each call).
my $dbh = DBI->connect("dbi:DriverName:db_name", $user, $password, { RaiseError => 1, AutoCommit => 0 });
my $sth = $dbh->prepare( q{ INSERT INTO table (name, phone) VALUES (?, ?) });
open FH, "<phone.csv" or die "Unable to open phone.csv: $!"; while (<FH>) { chomp; my ($name, $phone) = split /,/; $sth->execute($name, $phone); } close FH;
$dbh->commit; $dbh->disconnect;
Here's how to convert fetched NULLs (undefined values) into empty strings:
while($row = $sth->fetchrow_arrayref) { # this is a fast and simple way to deal with nulls: foreach (@$row) { $_ = '' unless defined } print "@$row\n"; }
The CWq{...} style quoting used in these examples avoids clashing with quotes that may be used in the SQL statement. Use the double-quote like CWqq{...} operator if you want to interpolate variables into the string. See Quote and Quote-like Operators in perlop for more details.
Threads and Thread Safety
Perl 5.7 and later support a new threading model called iThreads. (The old 5.005 style threads are not supported by the DBI.)
In the iThreads model each thread has it's own copy of the perl interpreter. When a new thread is created the original perl interpreter is 'cloned' to create a new copy for the new thread.
If the DBI and drivers are loaded and handles created before the thread is created then it will get a cloned copy of the DBI, the drivers and the handles.
However, the internal pointer data within the handles will refer to the DBI and drivers in the original interpreter. Using those handles in the new interpreter thread is not safe, so the DBI detects this and croaks on any method call using handles that don't belong to the current thread (except for DESTROY).
Because of this (possibly temporary) restriction, newly created threads must make their own connctions to the database. Handles can't be shared across threads.
But BEWARE, some underlying database APIs (the code the DBD driver uses to talk to the database, often supplied by the database vendor) are not thread safe. If it's not thread safe, then allowing more than one thread to enter the code at the same time may cause subtle/serious problems. In some cases allowing more than one thread to enter the code, even if not at the same time, can cause problems. You have been warned.
Using DBI with perl threads is not yet recommended for production environments. For more information see <http://www.perlmonks.org/index.pl?node_id=288022>
Note: There is a bug in perl 5.8.2 when configured with threads and debugging enabled (bug #24463) which causes a DBI test to fail.
Signal Handling and Canceling Operations
[The following only applies to systems with unix-like signal handling. I'd welcome additions for other systems, especially Windows.]
The first thing to say is that signal handling in Perl versions less than 5.8 is not safe. There is always a small risk of Perl crashing and/or core dumping when, or after, handling a signal because the signal could arrive and be handled while internal data structures are being changed. If the signal handling code used those same internal data structures it could cause all manner of subtle and not-so-subtle problems. The risk was reduced with 5.4.4 but was still present in all perls up through 5.8.0.
Beginning in perl 5.8.0 perl implements 'safe' signal handling if your system has the POSIX sigaction() routine. Now when a signal is delivered perl just makes a note of it but does not run the CW%SIG handler. The handling is 'defered' until a 'safe' moment.
Although this change made signal handling safe, it also lead to a problem with signals being defered for longer than you'd like. If a signal arrived while executing a system call, such as waiting for data on a network connection, the signal is noted and then the system call that was executing returns with an EINTR error code to indicate that it was interrupted. All fine so far.
The problem comes when the code that made the system call sees the EINTR code and decides it's going to call it again. Perl doesn't do that, but database code sometimes does. If that happens then the signal handler doesn't get called untill later. Maybe much later.
Fortunately there are ways around this which we'll discuss below. Unfortunately they make signals unsafe again.
The two most common uses of signals in relation to the DBI are for canceling operations when the user types Ctrl-C (interrupt), and for implementing a timeout using CWalarm() and CW$SIG{ALRM}.
- Cancel
-
The DBI provides a CWcancel method for statement handles. The
CWcancel method should abort the current operation and is designed
to be called from a signal handler. For example:
$SIG{INT} = sub { $sth->cancel };
However, few drivers implement this (the DBI provides a default method that just returns CWundef) and, even if implemented, there is still a possibility that the statement handle, and even the parent database handle, will not be usable afterwards. If CWcancel returns true, then it has successfully invoked the database engine's own cancel function. If it returns false, then CWcancel failed. If it returns CWundef, then the database driver does not have cancel implemented. - Timeout
-
The traditional way to implement a timeout is to set CW$SIG{ALRM}
to refer to some code that will be executed when an ALRM signal
arrives and then to call alarm($seconds) to schedule an ALRM signal
to be delivered CW$seconds in the future. For example:
eval { local $SIG{ALRM} = sub { die "TIMEOUT\n" }; alarm($seconds); ... code to execute with timeout here ... alarm(0); # cancel alarm (if code ran fast) }; alarm(0); # cancel alarm (if eval failed) if ( $@ eq "TIMEOUT" ) { ... }
Unfortunately, as described above, this won't always work as expected, depending on your perl version and the underlying database code. With Oracle for instance (DBD::Oracle), if the system which hosts the database is down the DBI->connect() call will hang for several minutes before returning an error.
The solution on these systems is to use the CWPOSIX::sigaction() routine to gain low level access to how the signal handler is installed.
The code would look something like this (for the DBD-Oracle connect()):
use POSIX ':signal_h';
my $mask = POSIX::SigSet->new( SIGALRM ); # signals to mask in the handler my $action = POSIX::SigAction->new( sub { die "connect timeout" }, # the handler code ref $mask, # not using (perl 5.8.2 and later) 'safe' switch or sa_flags ); my $oldaction = POSIX::SigAction->new(); sigaction( 'ALRM', $action, $oldaction ); my $dbh; eval { alarm(5); # seconds before time out $dbh = DBI->connect("dbi:Oracle:$dsn" ... ); alarm(0); # cancel alarm (if connect worked fast) }; alarm(0); # cancel alarm (if eval failed) sigaction( 'ALRM', $oldaction ); # restore original signal handler if ( $@ ) ....
Similar techniques can be used for canceling statement execution.
Unfortunately, this solution is somewhat messy, and it does not work with perl versions less than perl 5.8 where CWPOSIX::sigaction() appears to be broken.
For a cleaner implementation that works across perl versions, see Lincoln Baxter's Sys::SigAction module at <http://search.cpan.org/~lbaxter/Sys-SigAction/>. The documentation for Sys::SigAction includes an longer discussion of this problem, and a DBD::Oracle test script.
Be sure to read all the signal handling sections of the perlipc manual.
And finally, two more points to keep firmly in mind. Firstly, remember that what we've done here is essentially revert to old style unsafe handling of these signals. So do as little as possible in the handler. Ideally just die(). Secondly, the handles in use at the time the signal is handled may not be safe to use afterwards.
Subclassing the DBI
DBI can be subclassed and extended just like any other object oriented module. Before we talk about how to do that, it's important to be clear about how the DBI classes and how they work together.
By default CW$dbh = DBI->connect(...) returns a CW$dbh blessed into the CWDBI::db class. And the CW$dbh->prepare method returns an CW$sth blessed into the CWDBI::st class (actually it simply changes the last four characters of the calling handle class to be CW::st).
The leading 'CWDBI' is known as the 'root class' and the extra 'CW::db' or 'CW::st' are the 'handle type suffixes'. If you want to subclass the DBI you'll need to put your overriding methods into the appropriate classes. For example, if you want to use a root class of CWMySubDBI and override the do(), prepare() and execute() methods, then your do() and prepare() methods should be in the CWMySubDBI::db class and the execute() method should be in the CWMySubDBI::st class.
To setup the inheritance hierarchy the CW@ISA variable in CWMySubDBI::db should include CWDBI::db and the CW@ISA variable in CWMySubDBI::st should include CWDBI::st. The CWMySubDBI root class itself isn't currently used for anything visible and so, apart from setting CW@ISA to include CWDBI, it should be left empty.
So, having put your overriding methods into the right classes, and setup the inheritance hierarchy, how do you get the DBI to use them? You have two choices, either a static method call using the name of your subclass:
$dbh = MySubDBI->connect(...);
or specifying a CWRootClass attribute:
$dbh = DBI->connect(..., { RootClass => 'MySubDBI' });
The only difference between the two is that using an explicit RootClass attribute will make the DBI automatically attempt to load a module by that name if the class doesn't exist.
If both forms are used then the attribute takes precedence.
When subclassing is being used then, after a successful new connect, the DBI->connect method automatically calls:
$dbh->connected($dsn, $user, $pass, \%attr);
The default method does nothing. The call is made just to simplify any post-connection setup that your subclass may want to perform. The parameters are the same as passed to DBI->connect. If your subclass supplies a connected method, it should be part of the MySubDBI::db package.
One more thing to note: you must let the DBI do the handle creation. If you want to override the connect() method in your *::dr class then it must still call SUPER::connect to get a CW$dbh to work with. Similarly, an overridden prepare() method in *::db must still call SUPER::prepare to get a CW$sth. If you try to create your own handles using bless() then you'll find the DBI will reject them with an is not a DBI handle (has no magic) error.
Here's a brief example of a DBI subclass. A more thorough example can be found in t/subclass.t in the DBI distribution.
package MySubDBI;
use strict;
use DBI; use vars qw(@ISA); @ISA = qw(DBI);
package MySubDBI::db; use vars qw(@ISA); @ISA = qw(DBI::db);
sub prepare { my ($dbh, @args) = @_; my $sth = $dbh->SUPER::prepare(@args) or return; $sth->{private_mysubdbi_info} = { foo => 'bar' }; return $sth; }
package MySubDBI::st; use vars qw(@ISA); @ISA = qw(DBI::st);
sub fetch { my ($sth, @args) = @_; my $row = $sth->SUPER::fetch(@args) or return; do_something_magical_with_row_data($row) or return $sth->set_err(1234, "The magic failed", undef, "fetch"); return $row; }
When calling a SUPER::method that returns a handle, be careful to check the return value before trying to do other things with it in your overridden method. This is especially important if you want to set a hash attribute on the handle, as Perl's autovivification will bite you by (in)conveniently creating an unblessed hashref, which your method will then return with usually baffling results later on. It's best to check right after the call and return undef immediately on error, just like DBI would and just like the example above.
If your method needs to record an error it should call the set_err() method with the error code and error string, as shown in the example above. The error code and error string will be recorded in the handle and available via CW$h->err and CW$DBI::errstr etc. The set_err() method always returns an undef or empty list as approriate. Since your method should nearly always return an undef or empty list as soon as an error is detected it's handy to simply return what set_err() returns, as shown in the example above.
If the handle has CWRaiseError, CWPrintError, or CWHandleError etc. set then the set_err() method will honour them. This means that if CWRaiseError is set then set_err() won't return in the normal way but will 'throw an exception' that can be caught with an CWeval block.
You can stash private data into DBI handles via CW$h->{private_..._*}. See the entry under ATTRIBUTES COMMON TO ALL HANDLES for info and important caveats.
TRACING
The DBI has a powerful tracing mechanism built in. It enables you to see what's going on 'behind the scenes', both within the DBI and the drivers you're using.
Trace Settings
Which details are written to the trace output is controlled by a combination of a trace level, an integer from 0 to 15, and a set of trace flags that are either on or off. Together these are known as the trace settings and are stored together in a single integer. For normal use you only need to set the trace level, and generally only to a value between 1 and 4.
Each handle has it's own trace settings, and so does the DBI. When you call a method the DBI merges the handles settings into its own for the duration of the call: the trace flags of the handle are OR'd into the trace flags of the DBI, and if the handle has a higher trace level then the DBI trace level is raised to match it. The previous DBI trace setings are restored when the called method returns.
Trace Levels
Trace levels are as follows:
0 - Trace disabled. 1 - Trace DBI method calls returning with results or errors. 2 - Trace method entry with parameters and returning with results. 3 - As above, adding some high-level information from the driver and some internal information from the DBI. 4 - As above, adding more detailed information from the driver. 5 to 15 - As above but with more and more obscure information.
Trace level 1 is best for a simple overview of what's happening. Trace level 2 is a good choice for general purpose tracing. Levels 3 and above are best reserved for investigating a specific problem, when you need to see inside the driver and DBI.
The trace output is detailed and typically very useful. Much of the trace output is formatted using the neat function, so strings in the trace output may be edited and truncated by that function.
Trace Flags
Trace flags are used to enable tracing of specific activities within the DBI and drivers. The DBI defines some trace flags and drivers can define others. DBI trace flag names begin with a capital letter and driver specific names begin with a lowercase letter, as usual.
Curently the DBI only defines two trace flags:
ALL - turn on all DBI and driver flags (not recommended) SQL - trace SQL statements executed (not yet implemented)
The parse_trace_flags and parse_trace_flag methods are used to convert trace flag names into the coresponding integer bit flags.
Enabling Trace
The CW$h->trace method sets the trace settings for a handle and CWDBI->trace does the same for the DBI.
In addition to the trace method, you can enable the same trace information, and direct the output to a file, by setting the CWDBI_TRACE environment variable before starting Perl. See DBI_TRACE for more information.
Finally, you can set, or get, the trace settings for a handle using the CWTraceLevel attribute.
All of those methods use parse_trace_flags() and so allow you set both the trace level and multiple trace flags by using a string containing the trace level and/or flag names separated by vertical bar ("CW|) or comma (CW,") characters. For example:
local $h->{TraceLevel} = "3|SQL|foo";
Trace Output
Initially trace output is written to CWSTDERR. Both the CW$h->trace and CWDBI->trace methods take an optional CW$trace_filename parameter. If specified, and can be opened in append mode, then all trace output (currently including that from other handles) is redirected to that file. A warning is generated if the file can't be opened.
Further calls to trace() without a CW$trace_filename do not alter where the trace output is sent. If CW$trace_filename is undefined, then trace output is sent to CWSTDERR and the previous trace file is closed.
Currently CW$trace_filename can't be a filehandle. But meanwhile you can use the special strings CW"STDERR" and CW"STDOUT" to select those filehandles.
Tracing Tips
You can add tracing to your own application code using the trace_msg method.
It can sometimes be handy to compare trace files from two different runs of the same script. However using a tool like CWdiff doesn't work well because the trace file is full of object addresses that may differ each run. Here's a handy little command to strip those out:
perl -pe 's/\b0x[\da-f]{6,}/0xNNNN/gi; s/\b[\da-f]{6,}/<long number>/gi'
DBI ENVIRONMENT VARIABLES
The DBI module recognizes a number of environment variables, but most of them should not be used most of the time. It is better to be explicit about what you are doing to avoid the need for environment variables, especially in a web serving system where web servers are stingy about which environment variables are available.
DBI_DSN
The DBI_DSN environment variable is used by DBI->connect if you do not specify a data source when you issue the connect. It should have a format such as dbi:Driver:databasename.
DBI_DRIVER
The DBI_DRIVER environment variable is used to fill in the database driver name in DBI->connect if the data source string starts dbi:: (thereby omitting the driver). If DBI_DSN omits the driver name, DBI_DRIVER can fill the gap.
DBI_AUTOPROXY
The DBI_AUTOPROXY environment variable takes a string value that starts dbi:Proxy: and is typically followed by hostname=...;port=.... It is used to alter the behaviour of DBI->connect. For full details, see DBI::Proxy documentation.
DBI_USER
The DBI_USER environment variable takes a string value that is used as the user name if the DBI->connect call is given undef (as distinct from an empty string) as the username argument. Be wary of the security implications of using this.
DBI_PASS
The DBI_PASS environment variable takes a string value that is used as the password if the DBI->connect call is given undef (as distinct from an empty string) as the password argument. Be extra wary of the security implications of using this.
DBI_DBNAME (obsolete)
The DBI_DBNAME environment variable takes a string value that is used only when the obsolescent style of DBI->connect (with driver name as fourth parameter) is used, and when no value is provided for the first (database name) argument.
DBI_TRACE
The DBI_TRACE environment variable specifies the global default trace settings for the DBI at startup. Can also be used to direct trace output to a file. When the DBI is loaded it does:
DBI->trace(split /=/, $ENV{DBI_TRACE}, 2) if $ENV{DBI_TRACE};
So if CWDBI_TRACE contains an "CW=" character then what follows it is used as the name of the file to append the trace to.
output appended to that file. If the name begins with a number followed by an equal sign (CW=), then the number and the equal sign are stripped off from the name, and the number is used to set the trace level. For example:
DBI_TRACE=1=dbitrace.log perl your_test_script.pl
On Unix-like systems using a Bourne-like shell, you can do this easily on the command line:
DBI_TRACE=2 perl your_test_script.pl
See TRACING for more information.
PERL_DBI_DEBUG (obsolete)
An old variable that should no longer be used; equivalent to DBI_TRACE.
DBI_PROFILE
The DBI_PROFILE environment variable can be used to enable profiling of DBI method calls. See DBI::Profile for more information.
DBI_PUREPERL
The DBI_PUREPERL environment variable can be used to enable the use of DBI::PurePerl. See DBI::PurePerl for more information.
WARNING AND ERROR MESSAGES
Fatal Errors
The CW$dbh handle you're using to call CWprepare is probably undefined because the preceding CWconnect failed. You should always check the return status of DBI methods, or use the RaiseError attribute. The CW$sth handle you're using to call CWexecute is probably undefined because the preceeding CWprepare failed. You should always check the return status of DBI methods, or use the RaiseError attribute.
- DBI/DBD internal version mismatch
- The DBD driver module was built with a different version of DBI than the one currently being used. You should rebuild the DBD module under the current version of DBI. (Some rare platforms require static linking. On those platforms, there may be an old DBI or DBD driver version actually embedded in the Perl executable being used.)
- DBD driver has not implemented the AutoCommit attribute
- The DBD driver implementation is incomplete. Consult the author. You attempted to set or get an unknown attribute of a handle. Make sure you have spelled the attribute name correctly; case is significant (e.g., Autocommit is not the same as AutoCommit).
Pure-Perl DBI
A pure-perl emulation of the DBI is included in the distribution for people using pure-perl drivers who, for whatever reason, can't install the compiled DBI. See DBI::PurePerl.
SEE ALSO
Driver and Database Documentation
Refer to the documentation for the DBD driver that you are using.
Refer to the SQL Language Reference Manual for the database engine that you are using.
ODBC and SQL/CLI Standards Reference Information
More detailed information about the semantics of certain DBI methods that are based on ODBC and SQL/CLI standards is available on-line via microsoft.com, for ODBC, and www.jtc1sc32.org for the SQL/CLI standard:
DBI method ODBC function SQL/CLI Working Draft ---------- ------------- --------------------- column_info SQLColumns Page 124 foreign_key_info SQLForeignKeys Page 163 get_info SQLGetInfo Page 214 primary_key_info SQLPrimaryKeys Page 254 table_info SQLTables Page 294 type_info SQLGetTypeInfo Page 239
For example, for ODBC information on SQLColumns you'd visit:
http://msdn.microsoft.com/library/en-us/odbc/htm/odbcsqlcolumns.asp
If that URL ceases to work then use the MSDN search facility at:
http://search.microsoft.com/us/dev/
and search for CWSQLColumns returns using the exact phrase option. The link you want will probably just be called CWSQLColumns and will be part of the Data Access SDK.
And for SQL/CLI standard information on SQLColumns you'd read page 124 of the (very large) SQL/CLI Working Draft available from:
http://www.jtc1sc32.org/sc32/jtc1sc32.nsf/Attachments/7E3B41486BD99C3488256B410064C877/$FILE/32N0744T.PDF
Standards Reference Information
A hyperlinked, browsable version of the BNF syntax for SQL92 (plus Oracle 7 SQL and PL/SQL) is available here:
http://cui.unige.ch/db-research/Enseignement/analyseinfo/SQL92/BNFindex.html
A BNF syntax for SQL3 is available here:
http://www.sqlstandards.org/SC32/WG3/Progression_Documents/Informal_working_drafts/iso-9075-2-1999.bnf
The following links provide further useful information about SQL. Some of these are rather dated now but may still be useful.
http://www.jcc.com/SQLPages/jccs_sql.htm http://www.contrib.andrew.cmu.edu/~shadow/sql.html http://www.altavista.com/query?q=sql+tutorial
Books and Articles
Programming the Perl DBI, by Alligator Descartes and Tim Bunce. <http://books.perl.org/book/154>
Programming Perl 3rd Ed. by Larry Wall, Tom Christiansen & Jon Orwant. <http://books.perl.org/book/134>
Learning Perl by Randal Schwartz. <http://books.perl.org/book/101>
Details of many other books related to perl can be found at <http://books.perl.org>
Perl Modules
Index of DBI related modules available from CPAN:
http://search.cpan.org/search?mode=module&query=DBIx%3A%3A http://search.cpan.org/search?mode=doc&query=DBI
For a good comparison of RDBMS-OO mappers and some OO-RDBMS mappers (including Class::DBI, Alzabo, and DBIx::RecordSet in the former category and Tangram and SPOPS in the latter) see the Perl Object-Oriented Persistence project pages at:
http://poop.sourceforge.net
A similar page for Java toolkits can be found at:
http://c2.com/cgi-bin/wiki?ObjectRelationalToolComparison
Mailing List
The dbi-users mailing list is the primary means of communication among users of the DBI and its related modules. For details send email to:
dbi-users-help@perl.org
There are typically between 700 and 900 messages per month. You have to subscribe in order to be able to post. However you can opt for a 'post-only' subscription.
Mailing list archives (of variable quality) are held at:
http://groups.google.com/groups?group=perl.dbi.users http://www.xray.mpe.mpg.de/mailing-lists/dbi/ http://www.mail-archive.com/dbi-users%40perl.org/
Assorted Related WWW Links
The DBI Home Page:
http://dbi.perl.org/
Other DBI related links:
http://tegan.deltanet.com/~phlip/DBUIdoc.html http://dc.pm.org/perl_db.html http://wdvl.com/Authoring/DB/Intro/toc.html http://www.hotwired.com/webmonkey/backend/tutorials/tutorial1.html http://bumppo.net/lists/macperl/1999/06/msg00197.html http://gmax.oltrelinux.com/dbirecipes.html
Other database related links:
http://www.jcc.com/sql_stnd.html http://cuiwww.unige.ch/OSG/info/FreeDB/FreeDB.home.html
Security, especially the SQL Injection attack:
http://www.ngssoftware.com/research/papers.html http://www.ngssoftware.com/papers/advanced_sql_injection.pdf http://www.ngssoftware.com/papers/more_advanced_sql_injection.pdf http://www.esecurityplanet.com/trends/article.php/2243461 http://www.spidynamics.com/papers/SQLInjectionWhitePaper.pdf http://www.imperva.com/application_defense_center/white_papers/blind_sql_server_injection.html http://online.securityfocus.com/infocus/1644
Commercial and Data Warehouse Links
http://www.dwinfocenter.org http://www.datawarehouse.com http://www.datamining.org http://www.olapcouncil.org http://www.idwa.org http://www.knowledgecenters.org/dwcenter.asp
Recommended Perl Programming Links
http://language.perl.com/style/
FAQ
Please also read the DBI FAQ which is installed as a DBI::FAQ module. You can use perldoc to read it by executing the CWperldoc DBI::FAQ command.
AUTHORS
DBI by Tim Bunce. This pod text by Tim Bunce, J. Douglas Dunlop, Jonathan Leffler and others. Perl by Larry Wall and the CWperl5-porters.
COPYRIGHT
The DBI module is Copyright (c) 1994-2004 Tim Bunce. Ireland. All rights reserved.
You may distribute under the terms of either the GNU General Public License or the Artistic License, as specified in the Perl README file.
SUPPORT / WARRANTY
The DBI is free Open Source software. IT COMES WITHOUT WARRANTY OF ANY KIND.
Support
My consulting company, Data Plan Services, offers annual and multi-annual support contracts for the DBI. These provide sustained support for DBI development, and sustained value for you in return. Contact me for details.
Sponsor Enhancements
The DBI Roadmap is available at <http://search.cpan.org/~timb/DBI/Roadmap.pod>
If your company would benefit from a specific new DBI feature, please consider sponsoring its development. Work is performed rapidly, and usually on a fixed-price payment-on-delivery basis. Contact me for details.
Using such targeted financing allows you to contribute to DBI development, and rapidly get something specific and valuable in return.
ACKNOWLEDGEMENTS
I would like to acknowledge the valuable contributions of the many people I have worked with on the DBI project, especially in the early years (1992-1994). In no particular order: Kevin Stock, Buzz Moschetti, Kurt Andersen, Ted Lemon, William Hails, Garth Kennedy, Michael Peppler, Neil S. Briscoe, Jeff Urlwin, David J. Hughes, Jeff Stander, Forrest D Whitcher, Larry Wall, Jeff Fried, Roy Johnson, Paul Hudson, Georg Rehfeld, Steve Sizemore, Ron Pool, Jon Meek, Tom Christiansen, Steve Baumgarten, Randal Schwartz, and a whole lot more.
Then, of course, there are the poor souls who have struggled through untold and undocumented obstacles to actually implement DBI drivers. Among their ranks are Jochen Wiedmann, Alligator Descartes, Jonathan Leffler, Jeff Urlwin, Michael Peppler, Henrik Tougaard, Edwin Pratomo, Davide Migliavacca, Jan Pazdziora, Peter Haworth, Edmund Mergl, Steve Williams, Thomas Lowery, and Phlip Plumlee. Without them, the DBI would not be the practical reality it is today. I'm also especially grateful to Alligator Descartes for starting work on the first edition of the Programming the Perl DBI book and letting me jump on board.
The DBI and DBD::Oracle were originally developed while I was Technical Director (CTO) of the Paul Ingram Group (www.ig.co.uk). So I'd especially like to thank Paul for his generosity and vision in supporting this work for many years.
CONTRIBUTING
As you can see above, many people have contributed to the DBI and drivers in many ways over many years.
If you'd like to help then see <http://dbi.perl.org/contributing> and <http://search.cpan.org/~timb/DBI/Roadmap.pod>
If you'd like the DBI to do something new or different then a good way to make that happen is to do it yourself and send me a patch to the source code that shows the changes. (But read Speak before you patch below.)
Browsing the source code repository
Use http://svn.perl.org/modules/dbi/trunk (basic) or http://svn.perl.org/viewcvs/modules/ (more useful)
How to create a patch using Subversion
The DBI source code is maintained using Subversion (a replacement for CVS, see <http://subversion.tigris.org/>). To access the source you'll need to install a Subversion client. Then, to get the source code, do:
svn checkout http://svn.perl.org/modules/dbi/trunk
If it prompts for a username and password use your perl.org account if you have one, else just 'guest' and 'guest'. The source code will be in a new subdirectory called CWtrunk.
To keep informed about changes to the source you can send an empty email to dbi-changes@perl.org after which you'll get an email with the change log message and diff of each change checked-in to the source.
After making your changes you can generate a patch file, but before you do, make sure your source is still upto date using:
svn update
If you get any conflicts reported you'll need to fix them first. Then generate the patch file from within the CWtrunk directory using:
svn diff > foo.patch
Read the patch file, as a sanity check, and then email it to dbi-dev@perl.org.
How to create a patch without Subversion
Unpack a fresh copy of the distribution:
tar xfz DBI-1.40.tar.gz
Rename the newly created top level directory:
mv DBI-1.40 DBI-1.40.your_foo
Edit the contents of DBI-1.40.your_foo/* till it does what you want.
Test your changes and then remove all temporary files:
make test && make distclean
Go back to the directory you originally unpacked the distribution:
cd ..
Unpack another copy of the original distribution you started with:
tar xfz DBI-1.40.tar.gz
Then create a patch file by performing a recursive CWdiff on the two top level directories:
diff -r -u DBI-1.40 DBI-1.40.your_foo > DBI-1.40.your_foo.patch
Speak before you patch
For anything non-trivial or possibly controversial it's a good idea to discuss (on dbi-dev@perl.org) the changes you propose before actually spending time working on them. Otherwise you run the risk of them being rejected because they don't fit into some larger plans you may not be aware of.
TRANSLATIONS
A German translation of this manual (possibly slightly out of date) is available, thanks to O'Reilly, at:
http://www.oreilly.de/catalog/perldbiger/
Some other translations:
http://cronopio.net/perl/ - Spanish http://member.nifty.ne.jp/hippo2000/dbimemo.htm - Japanese
TRAINING
References to DBI related training resources. No recommendation implied.
http://www.treepax.co.uk/ http://www.keller.com/dbweb/
(If you offer professional DBI related training services, please send me your details so I can add them here.)
FREQUENTLY ASKED QUESTIONS
See the DBI FAQ for a more comprehensive list of FAQs. Use the CWperldoc DBI::FAQ command to read it.
Why doesn't my CGI script work right?
Read the information in the references below. Please do not post CGI related questions to the dbi-users mailing list (or to me).
http://www.perl.com/cgi-bin/pace/pub/doc/FAQs/cgi/perl-cgi-faq.html http://www3.pair.com/webthing/docs/cgi/faqs/cgifaq.shtml http://www-genome.wi.mit.edu/WWW/faqs/www-security-faq.html http://www.boutell.com/faq/ http://www.perl.com/perl/faq/
How can I maintain a WWW connection to a database?
For information on the Apache httpd server and the CWmod_perl module see
http://perl.apache.org/
OTHER RELATED WORK AND PERL MODULES
- Apache::DBI by E.Mergl@bawue.de
- To be used with the Apache daemon together with an embedded Perl interpreter like CWmod_perl. Establishes a database connection which remains open for the lifetime of the HTTP daemon. This way the CGI connect and disconnect for every database access becomes superfluous.
- JDBC Server by Stuart 'Zen' Bishop zen@bf.rmit.edu.au
- The server is written in Perl. The client classes that talk to it are of course in Java. Thus, a Java applet or application will be able to comunicate via the JDBC API with any database that has a DBI driver installed. The URL used is in the form CWjdbc:dbi://host.domain.etc:999/Driver/DBName. It seems to be very similar to some commercial products, such as jdbcKona.
- Remote Proxy DBD support
- As of DBI 1.02, a complete implementation of a DBD::Proxy driver and the DBI::ProxyServer are part of the DBI distribution.
- SQL Parser
- See also the SQL::Statement module, SQL parser and engine.