DBI(3)
💖 DBI — Database independent interface for Perl 💖
okay so bestie, welcome to the DBI(3) man page. It's giving "database independent interface for Perl" and honestly? iconic. Let's get into it.
✨ NAME ✨
DBI — Database independent interface for Perl 💅
✨ SYNOPSIS ✨
okay so here's the whole vibe at a glance, no cap:
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. like, there's more where that came from, bestie.
🫶 GETTING HELP
General
okay before you slide into anyone's DMs with questions: reread this document first, consult the archives, and read the DBI FAQ. The archives are listed at the end of this document and on the DBI home page <http://dbi.perl.org/support/> 🫶
You might also like to read the Advanced DBI Tutorial at <http://www.slideshare.net/Tim.Bunce/dbi-advanced-tutorial-2007> — it's giving educational, periodt.
And to help you make the best use of the dbi-users mailing list, plus any other lists or forums you may use, the recommendation is that you read "Getting Answers" by Mike Ash: <http://mikeash.com/getting_answers.html>. fr fr, read it.
Mailing Lists
If you have questions about DBI, or DBD driver modules, you can get help from the dbi-users@perl.org mailing list. This is the best way to get help. You don't have to subscribe to the list in order to post, though subscribing is recommended. You can get help on subscribing and using the list by emailing dbi-users-help@perl.org.
Please note that Tim Bunce does not maintain the mailing lists or the web pages (generous volunteers do that). So please don't send mail directly to him; he just doesn't have the time to answer questions personally. The dbi-users mailing list has lots of experienced people who should be able to help you if you need it. If you do email Tim he is very likely to just forward it to the mailing list. 😭 not the forward, bestie.
IRC
DBI IRC Channel: #dbi on irc.perl.org (<irc://irc.perl.org/#dbi>) — come thru 💬
Online
StackOverflow has a DBI tag <https://stackoverflow.com/questions/tagged/dbi> with over 800 questions. 👑
The DBI home page at <https://dbi.perl.org/> might be worth a visit. It includes links to other resources, but is rather out-dated. we love a vintage moment 💀
Reporting a Bug
If you think you've found a bug then please read "How to Report Bugs Effectively" by Simon Tatham: <https://www.chiark.greenend.org.uk/~sgtatham/bugs.html>. Read it before you post, bestie, it's giving wisdom.
If you think you've found a memory leak then read "Memory Leaks".
Your problem is most likely related to the specific DBD driver module you're using. If that's the case then click on the 'Bugs' link on the <https://metacpan.org> page for your driver. Only submit a bug report against the DBI itself if you're sure that your issue isn't related to the driver you're using. 🚨 important — dont @ the wrong repo.
💜 NOTES 💜
This is the DBI specification that corresponds to DBI version 1.656 (see DBI::Changes for details).
The DBI is evolving at a steady pace, so it's good to check that you have the latest copy. ✨
The significant user-visible changes in each release are documented in the DBI::Changes module so you can read them by executing perldoc DBI::Changes.
Some DBI changes require changes in the drivers, but the drivers can take some time to catch up. Newer versions of the DBI have added features that may not yet be supported by the drivers you use. Talk to the authors of your drivers if you need a new feature that is not yet supported. lowkey the drivers are doing their best 💅
Features added after DBI 1.21 (February 2002) are marked in the text with the version number of the DBI release they first appeared in.
Extensions to the DBI API often use the DBIx::* namespace. See "Naming Conventions and Name Space". DBI extension modules can be found at <https://metacpan.org/search?q=DBIx>. And all modules related to the DBI can be found at <https://metacpan.org/search?q=DBI>. go explore 🦋
🌸 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. iconic behaviour honestly
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. like, DBI is the matchmaker, the drivers are the ones actually pulling up 💖
This document often uses terms like references, objects, methods. If you're not familiar with those terms then it would be a good idea to read at least the following perl manuals first: perlreftut, perldsc, perllol, and perlboot. do your homework bestie, it'll help 😌
🏗️ Architecture of a DBI Application
okay so here's the whole structure, and yes it's art:
|<- 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. she does it all, no cap 💅
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 — memorize these, bestie, they show up everywhere:
$dbh— Database handle object (your main connection bae)$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 filehandleundef— 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. 🧚 clean up after yourself, the garbage collector has your back.
🚀 Outline Usage
To use DBI, first you need to load the DBI module:
use DBI;
use strict;
(The use strict; isn't required but is strongly recommended.) do it anyway, bestie, trust.
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. don't be out here reconnecting every five seconds 💀
Explicitly defining the required "AutoCommit" behaviour is strongly recommended and may become mandatory in a later version. This determines whether changes are automatically committed to the database when executed, or need to be explicitly committed later. write it out explicitly, fr fr.
The DBI allows an application to "prepare" statements for later execution. A prepared statement is identified by a statement handle held in a Perl variable. We'll call the Perl variable $sth in our examples.
The typical method call sequence for a SELECT statement is:
prepare,
execute, fetch, fetch, ...
execute, fetch, fetch, ...
execute, fetch, fetch, ...
for example:
$sth = $dbh->prepare("SELECT foo, bar FROM table WHERE baz=?");
$sth->execute( $baz );
while ( @row = $sth->fetchrow_array ) {
print "@row\n";
}
For queries that are not executed many times at once, it is often cleaner to use the higher level select wrappers:
$row_hashref = $dbh->selectrow_hashref("SELECT foo, bar FROM table WHERE baz=?", undef, $baz);
$arrayref_of_row_hashrefs = $dbh->selectall_arrayref(
"SELECT foo, bar FROM table WHERE baz BETWEEN ? AND ?",
{ Slice => {} }, $baz_min, $baz_max);
The typical method call sequence for a non-SELECT statement is:
prepare,
execute,
execute,
execute.
for example:
$sth = $dbh->prepare("INSERT INTO table(foo,bar,baz) VALUES (?,?,?)");
while(<CSV>) {
chomp;
my ($foo,$bar,$baz) = split /,/;
$sth->execute( $foo, $bar, $baz );
}
The do() method is a wrapper of prepare and execute that can be simpler for non repeated non-SELECT statements (or with drivers that don't support placeholders):
$rows_affected = $dbh->do("UPDATE your_table SET foo = foo + 1");
$rows_affected = $dbh->do("DELETE FROM table WHERE foo = ?", undef, $foo);
To commit your changes to the database (when "AutoCommit" is off):
$dbh->commit; # or call $dbh->rollback; to undo changes
Finally, when you have finished working with the data source, you should "disconnect" from it:
$dbh->disconnect;
and that's where part 1 wraps up, bestie — there's so much more coming 🦋
💅 General Interface Rules & Caveats
Okay bestie, listen up, because these are the ✨vibes✨ of the whole API.
The DBI does NOT have a concept of a "current session". Nope, not a thing. Every single session gets its own handle object (that's your $dbh) which comes back to you from the "connect" method. That handle object is literally what you use to invoke every database-related method. No global session, no sneaky hidden state, just the handle. Periodt. 💖
Most data comes back to your Perl script as strings. (And if something is null? You get undef.) Why should you care? Because this is lowkey iconic — it means arbitrary precision numeric data can be handled without loss of accuracy. 🎀 But heads up: beware, because Perl may not preserve that same accuracy when the string is used as a number. So like, don't yassify your big numbers into floats and then act surprised. 😭
Dates and times come back as character strings, in the current default format of the corresponding database engine. Time zone effects are database/driver dependent — so yeah, it's giving "it depends", as usual. 🦋
Perl supports binary data in Perl strings, and the DBI will pass binary data to and from the driver without change. It's up to the driver implementors to decide how they wish to handle such binary data. Their call, their problem, not yours.
Perl supports two kinds of strings: Unicode (utf8 internally) and non-Unicode (which defaults to iso-8859-1 if it's forced to assume an encoding). Drivers should accept both kinds of strings and, if required, convert them to the character set of the database being used. Same energy in reverse: when fetching character data from the database that isn't iso-8859-1, the driver should convert it into utf8. Mutual conversion, mutual respect. 🫶
Multiple SQL statements may not be combined in a single statement handle ($sth) — although ngl, some databases and drivers do support it anyway (notably Sybase and SQL Server). So don't count on it unless your driver says so. 🙈
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. Gone. Memory-holed. It's giving "you had one chance and you took it". 💀
Positioned updates and deletes are not directly supported by the DBI. But wait — there's an alternative! See the description of the "CursorName" attribute. 👑
Individual driver implementors are free to provide any private functions and/or handle attributes that they feel are useful. Private driver functions can be invoked using the DBI func() method, and private driver attributes are accessed just like standard attributes. Very much "come as you are" energy. ✨
Many methods have an optional \%attr parameter, which you can use to pass information to the driver implementing the method. Except where specifically documented, the \%attr parameter can only be used to pass driver specific hints. So in general? You can just ignore \%attr parameters, or pass it as undef. It's chill. 🌸
🏷️ Naming Conventions and Name Space
The DBI package and all packages below it (DBI::*) are reserved for use by the DBI. So hands off, bestie, that's their house. 🏠
Extensions and related modules use the DBIx:: namespace (see <http://www.perl.com/CPAN/modules/by-module/DBIx/>). Package names beginning with DBD:: are reserved for use by DBI database drivers. And every single environment variable used by the DBI or by individual DBDs begins with "DBI_" or "DBD_". Consistent naming is literally the whole personality here.
Now, the letter case used for attribute names is significant — it 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. And private attribute names must be prefixed with the driver name or a suitable abbreviation (e.g., "ora_" for Oracle, "ing_" for Ingres, etc). No cap, lowercase plus prefix or it's not allowed. 💅
🗣️ SQL - A Query Language
Most DBI drivers require applications to use a dialect of SQL (Structured Query Language) to interact with the database engine. If you want links to useful info about SQL, the "Standards Reference Information" section has you covered. 📚
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. One string. That's the whole rule. ⭐
For an interesting diversion on the real history of RDBMS and SQL, from the people who made it happen, see:
http://www.mcjones.org/System_R/SQL_Reunion_95/sqlr95.html
Follow the "Full Contents" then "Intergalactic dataspeak" links for the SQL history. History lesson but make it iconic. 🌈
🎯 Placeholders and Bind Values
Some drivers support placeholders and bind values. Placeholders, also called parameter markers, are used to indicate values in a database statement that will be supplied later, before the prepared statement is executed. For example, an application might use the following to insert a row of data into the SALES table:
INSERT INTO sales (product_code, qty, price) VALUES (?, ?, ?)
or the following, to select the description for a product:
SELECT description FROM products WHERE product_code = ?
The "?" characters are the placeholders. 💖 The association of actual values with placeholders is known as binding, and the values are referred to as bind values. And important note: the "?" is not enclosed in quotation marks, even when the placeholder represents a string. Don't add quotes, that's not the vibe.
Some drivers also allow placeholders like ":"name and ":"N (e.g., ":1", ":2", and so on) in addition to "?", but their use is not portable. So like, cute but risky. 🦋
If the ":"N form of placeholder is supported by the driver you're using, then you should be able to use either "bind_param" or "execute" to bind values. Check your driver documentation, bestie. 👑
Some drivers allow you to prevent the recognition of a placeholder by placing a single backslash character ("\") immediately before it. The driver will remove the backslash character and ignore the placeholder, passing it unchanged to the backend. If the driver supports this, then "get_info"(9000) will return true. Sneaky but useful. ✨
With most drivers, placeholders can't be used for any element of a statement that would prevent the database server from validating the statement and creating a query execution plan for it. For example:
"SELECT name, age FROM ?" # wrong (will probably fail)
"SELECT name, ? FROM people" # wrong (but may not 'fail')
Also, placeholders can only represent single scalar values. For example, the following statement won't work as expected for more than one value:
"SELECT name, age FROM people WHERE name IN (?)" # wrong
"SELECT name, age FROM people WHERE name IN (?,?)" # two names
When using placeholders with the SQL "LIKE" qualifier, you must remember that the placeholder substitutes for the whole string. So you should use "\"... LIKE ? ...\"" and include any wildcard characters in the value that you bind to the placeholder. They go in the value, not in the SQL. 🎀
🫧 NULL Values
Undefined values, or undef, are used to indicate NULL values. You can insert and update columns with a NULL value just like you would a non-NULL value. These examples insert and update the column "age" with a NULL value:
$sth = $dbh->prepare(qq{
INSERT INTO people (fullname, age) VALUES (?, ?)
});
$sth->execute("Joe Bloggs", undef);
$sth = $dbh->prepare(qq{
UPDATE people SET age = ? WHERE fullname = ?
});
$sth->execute(undef, "Joe Bloggs");
However, care must be taken when trying to use NULL values in a "WHERE" clause. Consider:
SELECT fullname FROM people WHERE age = ?
Binding an "undef" (NULL) to the placeholder will not select rows which have a NULL "age"! At least for database engines that conform to the SQL standard. Refer to the SQL manual for your database engine or any SQL book for the reasons for this. To explicitly select NULLs you have to say "\"WHERE age IS NULL\"". Yeah, it's giving "the standard said no". 😭
A common issue is to have a code fragment handle a value that could be either "defined" or "undef" (non-NULL or NULL) at runtime. A simple technique is to prepare the appropriate statement as needed, and substitute the placeholder for non-NULL cases:
$sql_clause = defined $age? "age = ?" : "age IS NULL";
$sth = $dbh->prepare(qq{
SELECT fullname FROM people WHERE $sql_clause
});
$sth->execute(defined $age ? $age : ());
The following technique illustrates qualifying a "WHERE" clause with several columns, whose associated values ("defined" or "undef") are in a hash %h:
for my $col ("age", "phone", "email") {
if (defined $h{$col}) {
push @sql_qual, "$col = ?";
push @sql_bind, $h{$col};
}
else {
push @sql_qual, "$col IS NULL";
}
}
$sql_clause = join(" AND ", @sql_qual);
$sth = $dbh->prepare(qq{
SELECT fullname FROM people WHERE $sql_clause
});
$sth->execute(@sql_bind);
The techniques above call prepare for the SQL statement with each call to execute. Because calls to prepare() can be expensive, performance can suffer when an application iterates many times over statements like the above. Lowkey a performance red flag. 🚩
A better solution is a single "WHERE" clause that supports both NULL and non-NULL comparisons. Its SQL statement would need to be prepared only once for all cases, thus improving performance. Several examples of "WHERE" clauses that support this are presented below. But each example lacks portability, robustness, or simplicity. Whether an example is supported on your database engine depends on what SQL extensions it provides, and where it supports the "?" placeholder in a statement.
0) age = ?
1) NVL(age, xx) = NVL(?, xx)
2) ISNULL(age, xx) = ISNULL(?, xx)
3) DECODE(age, ?, 1, 0) = 1
4) age = ? OR (age IS NULL AND ? IS NULL)
5) age = ? OR (age IS NULL AND SP_ISNULL(?) = 1)
6) age = ? OR (age IS NULL AND ? = 1)
Statements formed with the above "WHERE" clauses require execute statements as follows. The arguments are required, whether their values are "defined" or "undef".
0,1,2,3) $sth->execute($age);
4,5) $sth->execute($age, $age);
6) $sth->execute($age, defined($age) ? 0 : 1);
Example 0 should not work (as mentioned earlier), but may work on a few database engines anyway (e.g. Sybase). Example 0 is part of examples 4, 5, and 6, so if example 0 works, these other examples may work, even if the engine does not properly support the right hand side of the "OR" expression. Chaotic but okay. 💀
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. That's a lot of trust to put in a single character. 🙈
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. Custom function, custom responsibilities. 👑
Example 6 is the ✨least simple✨ one, and honestly? that's exactly why it's probably the most portable — it should work with most, if not literally all, database engines. 💅
Here's the receipts table of which examples are known to work on which engines:
-----Examples------
0 1 2 3 4 5 6
- - - - - - -
Oracle 9 N Y N Y Y ? Y
Informix IDS 9 N N N Y N Y Y
MS SQL N N Y N Y ? Y
Sybase Y N N N N N Y
AnyData,DBM,CSV Y N N N Y Y* Y
SQLite 3.3 N N N N Y N N
MSAccess N N N N Y N Y
- Works only because Example 0 works. (iconic of it tbh)
DBI ships a lil sample perl script that runs all the examples above against your database engine and tells you which ones actually work. You'll find it in the ex/ subdirectory of the DBI source distribution, or right here:
https://github.com/perl5-dbi/dbi/blob/master/ex/perl_dbi_nulls_test.pl
Please run it bestie 🫶 — it helps us fill in and maintain this table.
🚀 Performance
okay so here's the glow-up. Without placeholders, that insert statement from earlier would've had to shove the literal values straight into the SQL text, and then be re-prepared and re-executed for every single row. 😭 With placeholders, the insert statement only needs to be prepared once. Then the bind values for each row just get handed to the execute method each time it's called. By dodging the whole re-prepare-per-row situation, the application typically runs many times faster. Obsessed.
my $sth = $dbh->prepare(q{
INSERT INTO sales (product_code, qty, price) VALUES (?, ?, ?)
}) or die $dbh->errstr;
while (<>) {
chomp;
my ($product_code, $qty, $price) = split /,/;
$sth->execute($product_code, $qty, $price) or die $dbh->errstr;
}
$dbh->commit or die $dbh->errstr;
See "execute" and "bind_param" for more details.
The q{...} style quoting used up there is a whole vibe because it avoids clashing with quotes that might be chillin' inside the SQL statement. If you wanna interpolate variables into the string, use the double-quote-like qq{...} operator instead. See "Quote and Quote-like Operators" in perlop for more details.
Also go look at the "bind_columns" method — it's the one you use to associate Perl variables with the output columns of a SELECT statement. 🌸
👑 THE DBI PACKAGE AND CLASS 👑
In this section we're covering the DBI class methods, the utility functions, and the dynamic attributes that hang out on generic DBI handles.
✨ DBI Constants ✨
The constants representing the values of the SQL standard types can be imported individually by name, or all together by importing the special :sql_types tag. 🎀
You can produce the names and values of all the defined SQL standard types like this:
foreach (@{ $DBI::EXPORT_TAGS{sql_types} }) {
printf "%s=%d\n", $_, &{"DBI::$_"};
}
These constants are defined by SQL/CLI, ODBC, or both. Heads up: SQL_BIGINT has conflicting codes in SQL/CLI and ODBC, and DBI uses the ODBC one. 🤝
See the "type_info", "type_info_all", and "bind_param" methods for possible uses.
⭐ Note that just because the DBI defines a named constant for a given data type doesn't mean that drivers will support that data type. (defining the constant is giving effort, but nothing more, ok?)
🌈 DBI Class Methods
The following methods are provided by the DBI class:
parse_dsn
($scheme, $driver, $attr_string, $attr_hash, $driver_dsn) = DBI->parse_dsn($dsn)
or die "Can't parse DBI DSN '$dsn'";
Breaks apart a DBI Data Source Name (DSN) and returns the individual parts. If $dsn doesn't contain a valid DSN then parse_dsn() returns an empty list. 💀
$scheme is the first part of the DSN and is currently always 'dbi'. $driver is the driver name, possibly defaulted to $ENV{DBI_DRIVER}, and may be undefined. $attr_string is the contents of the optional attribute string, which may be undefined. If $attr_string is not empty then $attr_hash is a reference to a hash containing the parsed attribute names and values. $driver_dsn is the last part of the DBI DSN string. For example:
($scheme, $driver, $attr_string, $attr_hash, $driver_dsn)
= DBI->parse_dsn("dbi:MyDriver(RaiseError=>1):db=test;port=42");
$scheme = 'dbi';
$driver = 'MyDriver';
$attr_string = 'RaiseError=>1';
$attr_hash = { 'RaiseError' => '1' };
$driver_dsn = 'db=test;port=42';
The parse_dsn() method was added in DBI 1.43. 🎂
connect
$dbh = DBI->connect($data_source, $username, $password)
or die $DBI::errstr;
$dbh = DBI->connect($data_source, $username, $password, \%attr)
or die $DBI::errstr;
Establishes a database connection, aka a session, to the requested $data_source. Returns a database handle object if the connection succeeds. Use $dbh->disconnect to terminate the connection. 🦋
If the connect fails (see below), it returns undef and sets both $DBI::err and $DBI::errstr. (It does not explicitly set $!.) You should generally test the return status of connect and print $DBI::errstr if it has failed.
Multiple simultaneous connections to multiple databases through multiple drivers can be made via the DBI. Simply make one connect call for each database and keep a copy of each returned database handle. 🫶
The $data_source value must begin with "dbi:"driver_name":". The driver_name specifies the driver that will be used to make the connection. (Letter case is significant.)
As a convenience, if the $data_source parameter is undefined or empty, the DBI will substitute the value of the environment variable DBI_DSN. If just the driver_name part is empty (i.e., the $data_source prefix is "dbi::"), the environment variable DBI_DRIVER is used. If neither variable is set, then connect dies. 💀
Examples of $data_source values are:
dbi:DriverName:database_name
dbi:DriverName:database_name@hostname:port
dbi:DriverName:database=database_name;host=hostname;port=port
There is no standard for the text following the driver name. Each driver is free to use whatever syntax it wants. The only requirement the DBI makes is that all the information is supplied in a single string. You must consult the documentation for the drivers you are using for a description of the syntax they require. 📖
It is recommended that drivers support the ODBC style, shown in the last example above. It is also recommended that they support the three common names "host", "port", and "database" (plus "db" as an alias for database). This simplifies automatic construction of basic DSNs: dbi:$driver:database=$db;host=$host;port=$port. Drivers should aim to 'do something reasonable' when given a DSN in this form, but if any part is meaningless for that driver (such as 'port' for Informix) it should generate an error if that part is not empty.
If the environment variable DBI_AUTOPROXY is defined (and the driver in $data_source is not "Proxy") then the connect request will automatically be changed to:
$ENV{DBI_AUTOPROXY};dsn=$data_source
DBI_AUTOPROXY is typically set as "dbi:Proxy:hostname=...;port=...". If $ENV{DBI_AUTOPROXY} doesn't begin with "dbi:" then dbi:Proxy: will be prepended to it first. See the DBD::Proxy documentation for more details. 🧚
If $username or $password are undefined (rather than just empty), then the DBI will substitute the values of the DBI_USER and DBI_PASS environment variables, respectively. The DBI will warn if the environment variables are not defined. However — and this matters — the everyday use of these environment variables is not recommended for security reasons. 🔥 The mechanism is primarily intended to simplify testing. See below for an alternative way to specify the username and password.
DBI->connect automatically installs the driver if it has not been installed yet. Driver installation either returns a valid driver handle, or it dies with an error message that includes the string "install_driver" and the underlying problem. So DBI->connect will die on a driver installation failure and will only return undef on a connect failure, in which case $DBI::errstr will hold the error message. Use eval if you need to catch the "install_driver" error. 🙈
The $data_source argument (with the "dbi:...:" prefix removed) and the $username and $password arguments are then passed to the driver for processing. The DBI does not define any interpretation for the contents of these fields. The driver is free to interpret the $data_source, $username, and $password fields in any way, and supply whatever defaults are appropriate for the engine being accessed. (Oracle, for example, uses the ORACLE_SID and TWO_TASK environment variables if no $data_source is specified.) 👑
The AutoCommit and PrintError attributes for each connection default to on. (See "AutoCommit" and "PrintError" for more information.) However, it is strongly recommended that you explicitly define AutoCommit rather than rely on the default. The PrintWarn attribute defaults to true. The RaiseWarn attribute defaults to false.
The \%attr parameter can be used to alter the default settings of PrintError, RaiseError, AutoCommit, and other attributes. For example:
$dbh = DBI->connect($data_source, $user, $pass, {
PrintError => 0,
AutoCommit => 0
});
The username and password can also be specified using the attributes Username and Password, in which case they take precedence over the $username and $password parameters.
You can also define connection attribute values within the $data_source parameter. For example:
dbi:DriverName(PrintWarn=>0,PrintError=>0,Taint=>1):...
Individual attribute values specified in this way take precedence over any conflicting values specified via the \%attr parameter to connect. ✨
The dbi_connect_method attribute can be used to specify which driver method should be called to establish the connection. The only useful values are 'connect', 'connect_cached', or some specialized case like 'Apache::DBI::connect' (which is automatically the default when running within Apache).
Where possible, each session ($dbh) is independent from the transactions in other sessions. This is useful when you need to hold cursors open across transactions — for example, if you use one session for your long lifespan cursors (typically read-only) and another for your short update transactions. 💜
For compatibility with old DBI scripts, the driver can be specified by passing its name as the fourth argument to connect (instead of \%attr):
$dbh = DBI->connect($data_source, $user, $pass, $driver);
In this old-style form of connect, your $data_source should not start with dbi:driver_name: — and fr fr bestie, if it does, that embedded driver name just gets straight up ignored. 🙈 Also, in this older form of connect, the $dbh->{AutoCommit} attribute is undefined, the $dbh->{PrintError} attribute is off, and the old DBI_DBNAME environment variable gets checked if DBI_DSN isn't defined.
⚠️ Heads up: thisold-styleconnectis gonna be withdrawn in a future version of DBI. So maybe don't build your whole personality around it. 💀
✨ connect_cached ✨
$dbh = DBI->connect_cached($data_source, $username, $password)
or die $DBI::errstr;
$dbh = DBI->connect_cached($data_source, $username, $password, \%attr)
or die $DBI::errstr;
connect_cached is literally connect but the database handle it hands back also gets stored in a hash tied to the parameters you passed in. So if you call connect_cached again with the same parameter values, you get that same cached $dbh back — as long as it's still valid. 🫶 And if the handle got disconnected, or the ping method fails? Boom, the cached handle gets swapped out for a fresh new connection.
Now, quick note: this method's behaviour differs in several respects from the behaviour of persistent connections implemented by Apache::DBI. But plot twist — if Apache::DBI is loaded, then connect_cached will use it. 👑
Caching connections can be useful in some applications, but it can also cause problems, like too many connections, so use it with care, bestie. Especially: 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. 😭 And when connect_cached() returns a handle, the attributes get reset to their initial values. This can cause problems, especially with the AutoCommit attribute. Be careful out there.
Also, to make sure the attributes you pass are always the same, avoid passing references inline. Like, the Callbacks attribute is specified as a hash reference. So declare it external to the call to connect_cached(), so that hash reference isn't re-created on every single call. A package-level lexical works great:
package MyDBH;
my $cb = {
'connect_cached.reused' => sub { delete $_[4]->{AutoCommit} },
};
sub dbh {
DBI->connect_cached( $dsn, $username, $auth, { Callbacks => $cb });
}
Now, when multiple separate parts of a program are using connect_cached() to connect to the same database with the same (initial) attributes, it's a good idea to add a private attribute to the connect_cached() call to effectively limit the scope of the caching. Like:
DBI->connect_cached(..., { private_foo_cachekey => "Bar", ... });
Handles returned from that connect_cached() call will only get handed back by other connect_cached() calls elsewhere in the code if those other calls also pass in the same attribute values — private one included. (I used private_foo_cachekey here as an example, but you can use any attribute name with a private_ prefix. 💅)
Taking it one step further: you can limit a particular connect_cached() call to return handles unique to that one place in the code by setting the private attribute to a unique value for that place:
DBI->connect_cached(..., { private_foo_cachekey => __FILE__.__LINE__, ... });
By using a private attribute you still get connection caching for the individual calls to connect_cached(), but, by making separate database connections for separate parts of the code, the database handles are isolated from any attribute changes made to other handles. Chef's kiss. 👩🍳💋
The cache can be accessed (and cleared) via the CachedKids attribute:
my $CachedKids_hashref = $dbh->{Driver}->{CachedKids};
%$CachedKids_hashref = () if $CachedKids_hashref;
🌸 available_drivers 🌸
@ary = DBI->available_drivers;
@ary = DBI->available_drivers($quiet);
Returns a list of all available drivers by searching for DBD::* modules through the directories in @INC. By default, a warning is given if some drivers are hidden by others of the same name in earlier directories. Passing a true value for $quiet will inhibit the warning. 🤫
🦋 installed_drivers 🦋
%drivers = DBI->installed_drivers();
Returns a list of driver name and driver handle pairs for all drivers 'installed' (loaded) into the current process. The driver name does not include the 'DBD::' prefix.
To get a list of all drivers available in your perl installation, you can use available_drivers.
Added in DBI 1.49. ✨
🎀 installed_versions 🎀
DBI->installed_versions;
@ary = DBI->installed_versions;
$hash = DBI->installed_versions;
Calls available_drivers() and attempts to load each of them in turn using install_driver(). For each load that succeeds, the driver name and version number get added to a hash. When running under DBI::PurePerl, drivers which appear not to be pure-perl are ignored. 🙈
When called in array context, the list of successfully loaded drivers is returned (without the 'DBD::' prefix).
When called in scalar context, an extra entry for DBI is added (and DBI::PurePerl if appropriate) and a reference to the hash is returned.
When called in a void context, the installed_versions() method will print out a formatted list of the hash contents, one per line, along with some other information about the DBI version and OS. 💻
⚠️ Because of the potentially high memory cost and unknown risks of loading in an unknown number of drivers that just happen to be installed on the system, this method is not recommended for general use. Use available_drivers() instead, bestie.
The installed_versions() method is primarily intended as a quick way to see from the command line what's installed. Like:
perl -MDBI -e 'DBI->installed_versions'
The installed_versions() method was added in DBI 1.38.
🌈 data_sources 🌈
@ary = DBI->data_sources($driver);
@ary = DBI->data_sources($driver, \%attr);
Returns a list of data sources (databases) available via the named driver. If $driver is empty or undef, then the value of the DBI_DRIVER environment variable is used. 👑
The driver will be loaded if it hasn't been already. Note that if the driver loading fails then data_sources() dies with an error message that includes the string install_driver and the underlying problem.
Data sources are returned in a form suitable for passing to the connect method — that is, they will include the dbi:$driver: prefix.
Note that many drivers have no way of knowing what data sources might be available for them. These drivers return an empty or incomplete list, or may require driver-specific attributes. It's giving ¯\_(ツ)_/¯.
There is also a data_sources() method defined for database handles. 🔁
💜 trace 💜
DBI->trace($trace_setting)
DBI->trace($trace_setting, $trace_filename)
DBI->trace($trace_setting, $trace_filehandle)
$trace_setting = DBI->trace;
The DBI->trace method sets the global default trace settings and returns the previous trace settings. It can also be used to change where the trace output is sent. 📝
There's a similar method, $h->trace, which sets the trace settings for the specific handle it's called on.
See the TRACING section for full details about the DBI's powerful tracing facilities. (Yes bestie, they're iconic. 🔥)
⭐ visit_handles ⭐
DBI->visit_handles( $coderef );
DBI->visit_handles( $coderef, $info );
Where $coderef is a reference to a subroutine and $info is an arbitrary value which, if undefined, defaults to a reference to an empty hash. Returns $info.
For each installed driver handle, if any, $coderef is invoked as:
$coderef->($driver_handle, $info);
If the execution of $coderef returns a true value, then visit_child_handles is called on that child handle and passed the returned value as $info.
For example:
my $info = $dbh->{Driver}->visit_child_handles(sub {
my ($h, $info) = @_;
++$info->{ $h->{Type} }; # count types of handles (dr/db/st)
return $info; # visit kids
});
See also visit_child_handles.
🧚 DBI Utility Functions 🧚
On top of the DBI methods listed in the previous section, the DBI package also provides a bunch of utility functions. Little side quests, yk?
You can import them into your code by listing them in the use statement. Like:
use DBI qw(neat data_diff);
Alternatively, all these utility functions (except hash) can be imported using the :utils import tag. Like:
use DBI qw(:utils);
🍓 data_string_desc 🍓
$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, undef
That initial UTF8 on/off refers to Perl's internal SvUTF8 flag. So if $string has the SvUTF8 flag set but the sequence of bytes it contains isn't a valid UTF-8 encoding, then data_string_desc() will report UTF8 on but INVALID encoding. 💀
The ASCII vs non-ASCII portion shows ASCII if all the characters in the string are ASCII (have code points <= 127).
The data_string_desc() function was added in DBI 1.46.
💅 data_string_diff 💅
$diff = data_string_diff($a, $b);
Returns an informal description of the first character difference between the strings. If both $a and $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 \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 \x{08XX}='C' where C 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.
🔥 data_diff 🔥
$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, data_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 $a and $b match in both the characters they contain and their physical encoding, then data_diff() hands you back an empty string. And if $logical is true, physical encoding differences get ignored (but still get reported if there's a difference in the characters themselves). ✨
data_diff() was added in DBI 1.46, so it's not ancient, bestie.
neat
$str = neat($value);
$str = neat($value, $maxlen);
Gives you a string holding a neat (and tidy, we love tidy) representation of whatever $value you hand it. 💅
Strings get quoted — though internal quotes will not be escaped, so don't come for it. Values known to be numeric stay unquoted. Undefined (NULL) values show up as "undef" (no quotes).
If the string is flagged internally as utf8 then it gets double quotes; otherwise single quotes are used and unprintable characters get replaced by a dot (.).
For result strings longer than $maxlen, the result gets truncated to "$maxlen-4" and "...'" gets appended. If $maxlen is 0 or "undef", it defaults to $DBI::neat_maxlen, which in turn defaults to 400.
This function is literally designed for formatting values for human consumption 🫶 — the DBI uses it internally for "trace" output. It should typically not be used for formatting values for database use. (See also "quote".)
neat_list
$str = neat_list(\@listref, $maxlen, $field_sep);
Calls "neat" on each element of the list and returns a string with the results joined up with $field_sep. $field_sep defaults to ", ".
looks_like_number
@bool = looks_like_number(@array);
Returns true for each element that looks like a number. Returns false for each element that doesn't look like a number. Returns "undef" for each element that's undefined or empty. Vibe check, but for numerics. 🔥
hash
$hash_value = DBI::hash($buffer, $type);
Returns a 32-bit integer 'hash' value matching the contents of $buffer. The $type parameter picks which kind of hash algorithm gets used.
For the technically curious (ngl, love that for you): type 0 — which is the default if $type isn't specified — is based on the Perl 5.1 hash, except the value is forced to be negative (for obscure historical reasons, we don't ask). Type 1 is the better "Fowler / Noll / Vo" (FNV) hash. See http://www.isthe.com/chongo/tech/comp/fnv/ for more info. Both types are implemented in C and are very fast. ⚡
This function doesn't have much to do with databases, except that it can sometimes be handy to store such values in a database. It also doesn't have much to do with perl hashes, like %foo. Different thing entirely, bestie.
sql_type_cast
$sts = DBI::sql_type_cast($sv, $sql_type, $flags);
sql_type_cast attempts to cast $sv to the SQL type (see DBI Constants) specified in $sql_type. At present only the SQL types "SQL_INTEGER", "SQL_DOUBLE" and "SQL_NUMERIC" are supported.
For "SQL_INTEGER" the effect is similar to using the value in an expression that requires an integer. It gives the perl scalar an 'integer aspect'. (Technically the value gains an IV, or possibly a UV or NV if the value is too large for an IV.)
For "SQL_DOUBLE" the effect is similar to using the value in an expression that requires a general numeric value. It gives the perl scalar a 'numeric aspect'. (Technically the value gains an NV.)
"SQL_NUMERIC" is similar to "SQL_INTEGER" or "SQL_DOUBLE" but more general and more cautious. It looks at the string first and if it looks like an integer (one that'll fit in an IV or UV) it acts like "SQL_INTEGER"; if it looks like a floating point value it acts like "SQL_DOUBLE"; if it looks like neither then it does nothing — and thereby avoids the warnings that "SQL_INTEGER" and "SQL_DOUBLE" would generate when given non-numeric data. 🌸
$flags may be:
"DBIstcf_DISCARD_STRING"— if this flag is specified then when the driver successfully casts the bound perl scalar to a non-string type, the string portion of the scalar gets discarded."DBIstcf_STRICT"— if$svcan't be cast to the requested$sql_typethen by default it's left untouched and no error is generated. If you specify"DBIstcf_STRICT"and the cast fails, that will generate an error. 🚨
The returned $sts value is:
-2 sql_type is not handled
-1 sv is undef so unchanged
0 sv could not be cast cleanly and DBIstcf_STRICT was used
1 sv could not be cast and DBIstcf_STRICT was not used
2 sv was cast successfully
This method is exported by the :utils tag and was introduced in DBI 1.611.
DBI Dynamic Attributes
Dynamic attributes are always associated with the last handle used (that handle is $h in the descriptions below). They're ride-or-die with whichever handle was most recent, whether you like it or not. 💀
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're 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.
$DBI::err— equivalent to$h->err.$DBI::errstr— equivalent to$h->errstr.$DBI::state— equivalent to$h->state.$DBI::rows— equivalent to$h->rows. Please refer to the documentation for the"rows"method.$DBI::lasth— returns the DBI object handle used for the most recent DBI method call. If the last DBI method call was aDESTROYthen$DBI::lasthwill 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. Every handle gets them, no gatekeeping. 💖
err
$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 $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. Messy, we know.
(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. Yes, all of them. 🫠
The DBI uses the value of $DBI::stderr as the "err" value for internal errors. Drivers should also do likewise. The default value for $DBI::stderr is 2000000000.
A driver may return 0 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.
errstr
$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. Short shelf life, bestie. 💔
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.
state
$str = $h->state;
Returns a state code in the standard SQLSTATE five character format. Note that the specific success code 00000 is translated to any empty string (false). If the driver does not support SQLSTATE (and most don't), then state() will return "S1000" (General Error) for all errors.
The driver is free to return any value via "state", 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 state() for methods that have not 'failed'.
set_err
$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 "err", "errstr", and "state" values for the handle. This method is typically only used by DBI drivers and DBI subclasses — it's an inside-the-house thing. 🏠
If the "HandleSetErr" attribute holds a reference to a subroutine it is called first. The subroutine can alter the $err, $errstr, $state, and $method values. See "HandleSetErr" for full details. If the subroutine returns a true value then the handle "err", "errstr", and "state" values are not altered and set_err() returns an empty list (it normally returns $rv which defaults to undef, see below).
Setting "err" to a true value indicates an error and will trigger the normal DBI error handling mechanisms, such as "RaiseError" and "HandleError", if they are enabled, when execution returns from the DBI back to the application.
Setting "err" to "" indicates an 'information' state, and setting it to "0" indicates a 'warning' state. Setting "err" to "undef" also sets "errstr" to undef, and "state" to "", irrespective of the values of the $errstr and $state parameters.
The $method parameter provides an alternate method name for the "RaiseError"/"PrintError"/"RaiseWarn"/"PrintWarn" error string instead of the fairly unhelpful '"set_err"'.
The "set_err" method normally returns undef. The $rv parameter provides an alternate return value.
Some special rules apply if the "err" or "errstr" values for the handle are already set... 👀
If "errstr" is true then: " [err was %s now %s]" is appended if $err is true and "err" is already true and the new err value differs from the original one. Similarly " [state was %s now %s]" is appended if $state is true and "state" is already true and the new state value differs from the original one. Finally "\n" and the new $errstr are appended if $errstr differs from the existing errstr value. Obviously the %s's above are replaced by the corresponding values.
The handle "err" value is set to $err if: $err is true; or handle "err" value is undef; or $err is defined and the length is greater than the handle "err" length. The effect is that an 'information' state only overrides undef; a 'warning' overrides undef or 'information', and an 'error' state overrides anything. Hierarchy of drama, honestly. 👑
okay and one more thing about state before we move on — the handle's "state" value gets set to $state if $state is true and the handle's "err" value was set (by the rules we just went through above). ✨
Support for warning and information states? That whole glow-up landed in DBI 1.41. 💫
trace
$h->trace($trace_settings);
$h->trace($trace_settings, $trace_filename);
$trace_settings = $h->trace;
The trace() method is your vibe-check dial for a handle — it changes the trace settings for that handle and any future children it spawns. 👶 You can also use it to redirect where the trace output actually goes. 📬
There's a sibling method, DBI->trace, which sets the global default trace settings instead of the per-handle ones. 🌍
Full details on the DBI's genuinely iconic tracing setup live in the "TRACING" section. Go read it, bestie. 👑
trace_msg
$h->trace_msg($message_text);
$h->trace_msg($message_text, $min_level);
Writes $message_text to the trace file if the trace level is greater than or equal to $min_level (which defaults to 1). 🖊️ You can also call it as DBI->trace_msg($msg). Same energy, different handle.
More deets in "TRACING". 💖
func
$h->func(@func_arguments, $func_name) or die ...;
The func method is your backstage pass 🎟️ — it calls private, non-standard, non-portable methods that a driver has implemented. Heads up though: the function name goes last, as the final argument. No cap. 🫶
Also important, also iconic: func() does not clear a previous error ($DBI::err etc.), and it does not trigger automatic error detection (RaiseError etc.). So you have to check the return status and/or $h->err yourself to catch errors. 👀 Nobody's saving you here.
(Quick clarification: this method is not directly related to calling stored procedures. Calling stored procedures is currently not defined by the DBI. Some drivers, like DBD::Oracle, support it in non-portable ways. Check the driver documentation for the details. 📚)
Also see install_method() in DBI::DBD for how you can skip needing func() entirely and get direct access to driver-private methods instead. Slay. 💅
can
$is_implemented = $h->can($method_name);
Returns true if $method_name is implemented by the driver, or if a default method is provided by the DBI's driver base class. 🟢 It returns false where a driver hasn't implemented a method and the default method provided by the DBI's driver base class is just an empty stub. 🫥 Basically: real implementation = yes, hollow little shell = no.
parse_trace_flags
$trace_settings_integer = $h->parse_trace_flags($trace_settings);
Parses a string containing trace settings and hands you back the corresponding integer value that the DBI and drivers use internally. 🔢✨
The $trace_settings argument is a string with a trace level between 0 and 15 and/or trace flag names, separated by vertical bar ("|") or comma (",") characters. For example: "SQL|3|foo". 🎀
It uses the parse_trace_flag() method — described right below, we're getting there — to process the individual trace flag names. 🧚
The parse_trace_flags() method was added in DBI 1.42.
parse_trace_flag
$bit_flag = $h->parse_trace_flag($trace_flag_name);
Returns the bit flag corresponding to the trace flag name in $trace_flag_name. 🚩 Drivers are expected to override this method and check whether $trace_flag_name is a driver-specific trace flag, and if it isn't, then call the DBI's default parse_trace_flag(). Chain of command, babe. 👑
The parse_trace_flag() method was added in DBI 1.42.
private_attribute_info
$hash_ref = $h->private_attribute_info();
Returns a reference to a hash whose keys are the names of driver-private handle attributes available for the kind of handle (driver, database, statement) that the method was called on. 🗝️
For example, the return value when called with a DBD::Sybase $dbh could look like this:
{
syb_dynamic_supported => undef,
syb_oc_version => undef,
syb_server_version => undef,
syb_server_version_string => undef,
}
and when called with a DBD::Sybase $sth they could look like this:
{
syb_types => undef,
syb_proc_status => undef,
syb_result_type => undef,
}
The values should be undef. Meanings may be assigned to particular values in future. 🍓 (So don't get attached to them.)
swap_inner_handle
$rc = $h1->swap_inner_handle( $h2 );
$rc = $h1->swap_inner_handle( $h2, $allow_reparent );
brain transplants for handles. 🧠🔁 You genuinely do not need to know about this unless you want to become a handle surgeon. 🥼
Here's the deal: 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 $h1 and $h2 handles still point to the same tied hashes, but what those hashes are tied to has been swapped. In effect $h1 becomes $h2 and vice-versa. This is powerful stuff — expect problems. Use with care. 😭
As a small safety measure, the two handles, $h1 and $h2, have to share the same parent unless $allow_reparent is true. 🫶
The swap_inner_handle() method was added in DBI 1.44.
Here's a quick kind of 'diagram' as a worked example to help you think about what's happening:
Original state:
dbh1o -> dbh1i
sthAo -> sthAi(dbh1i)
dbh2o -> dbh2i
swap_inner_handle dbh1o with dbh2o:
dbh2o -> dbh1i
sthAo -> sthAi(dbh1i)
dbh1o -> dbh2i
create new sth from dbh1o:
dbh2o -> dbh1i
sthAo -> sthAi(dbh1i)
dbh1o -> dbh2i
sthBo -> sthBi(dbh2i)
swap_inner_handle sthAo with sthBo:
dbh2o -> dbh1i
sthBo -> sthAi(dbh1i)
dbh1o -> dbh2i
sthAo -> sthBi(dbh2i)
(it's giving identity crisis, honestly 💀)
visit_child_handles
$h->visit_child_handles( $coderef );
$h->visit_child_handles( $coderef, $info );
Where $coderef is a reference to a subroutine and $info is an arbitrary value which, if undefined, defaults to a reference to an empty hash. Returns $info. 🎁
For each child handle of $h, if any, $coderef is invoked as:
$coderef->($child_handle, $info);
If the execution of $coderef returns a true value then "visit_child_handles" is called on that child handle and passed the returned value as $info. 🔁 Recursion goes brrr.
For example:
# count database connections with names (DSN) matching a pattern
my $connections = 0;
$dbh->{Driver}->visit_child_handles(sub {
my ($h, $info) = @_;
++$connections if $h->{Name} =~ /foo/;
return 0; # don't visit kids
})
See also "visit_handles". 💜
✨ 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. 🦋 (Boundaries. We love to see it.)
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/read
Warn
Type: boolean, inherited
The "Warn" 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 warn function, they can be intercepted using the Perl $SIG{__WARN__} hook. 🪝
The "Warn" attribute is not related to the "PrintWarn" attribute. Different things, bestie, don't mix them up. 💅
Active
Type: boolean, read-only
The "Active" 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 ("$dbh->disconnect" sets "Active" off). For a statement handle it typically means that the handle is a "SELECT" that may have more data to fetch. (Fetching all the data or calling "$sth->finish" sets "Active" off.) ⭐
Executed
Type: boolean
The "Executed" attribute is true if the handle object has been "executed".<br> Currently only the $dbh do() method and the $sth execute(), execute_array(), and execute_for_fetch() methods set the "Executed" 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 $sth also sets the "Executed" attribute on the parent $dbh. 👑
The "Executed" attribute for a database handle is cleared by the commit() and rollback() methods (even if they fail). The "Executed" 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. 📜 (Receipts forever.)
The "Executed" attribute was added in DBI 1.41.
Kids
Type: integer, read-only
For a driver handle, "Kids" is the number of currently existing database handles that were created from that driver handle. For a database handle, "Kids" is the number of currently existing statement handles that were created from that database handle. For a statement handle, the value is zero. 🍼 (sths don't have kids, they're just babies themselves)
ActiveKids
Type: integer, read-only
Like "Kids", but only counting those that are "Active" (as above). 🏃✨
CachedKids
Type: hash ref
For a database handle, "CachedKids" 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. 🗃️ Cached and thriving.
Type
Type: scalar, read-only
The "Type" attribute identifies the type of a DBI handle. Returns "dr" for driver handles, "db" for database handles and "st" for statement handles. 🏷️
ChildHandles
Type: array ref
The ChildHandles attribute contains a reference to an array of all the handles created by this handle which are still accessible. The contents of the array are weak-refs and will become undef when the handle goes out of scope. (They're cleared out occasionally.) 🍃
"ChildHandles" 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) = @_;
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($_, 0) for (values %drivers);
CompatMode
Type: boolean, inherited
CompatMode — okay so this one is not for you, bestie. It's for emulation layers (like Oraperl) to turn on vibes-compatible behaviour in the driver underneath (say, DBD::Oracle) for this particular handle. Application code doesn't normally touch it. 🙅♀️
It also has the side effect of turning off the 'quick FETCH' of attribute values from the handle's attribute cache. So every attribute value goes through the driver's own FETCH method instead. Slightly slower, yeah, but it's genuinely useful for special-purpose drivers like DBD::Multiplex. 💅
InactiveDestroy
Type: boolean
Default is false, and false means a handle gets fully destroyed like normal when the last reference to it goes away — exactly what you'd expect. The standard behaviour. Nothing weird. ✨
Set it true and DESTROY will treat the handle as if it's no longer Active, so all the database-engine-related effects of DESTROYing a handle get skipped. Think of the name as literally meaning 'treat the handle as not-Active in the DESTROY method'. That's the whole personality.
For a database handle, this attribute does not disable an explicit call to the disconnect method. It only skips the implicit call from DESTROY that happens if the handle is still marked as "Active". Important distinction, don't get it twisted. 💀
This attribute is specifically designed for Unix applications that "fork" child processes. For some drivers, when the child process exits, the destruction of inherited handles makes the corresponding handles in the parent process stop working. Rude honestly.
Either the parent or the child process, but not both, should set "InactiveDestroy" true on all their shared handles. Alternatively — and preferably — "AutoInactiveDestroy" can be set in the parent on connect. 👑
To help tracing applications that use fork, the process id shows up in the trace log whenever a DBI or handle trace() method is called. The process id is also shown for every method call if the DBI trace level (not the handle trace level) is set high enough to show the trace from the DBI's method dispatcher, e.g. >= 9. 🦋
AutoInactiveDestroy
Type: boolean, inherited
So "InactiveDestroy", the one we just talked about, needs to be explicitly set in the child process after a fork(), on every single active database and statement handle. Which is a problem if the code doing the fork() isn't under your control — maybe it's lurking in some third-party module. 🙈 Use "AutoInactiveDestroy" to get around that whole situation.
If set true, the DESTROY method checks the process id of the handle and, if it's different from the current process id, it sets the InactiveDestroy attribute. It is strongly recommended that "AutoInactiveDestroy" is enabled on all new code (it's only not enabled by default to dodge backwards compatibility problems). fr fr.
Here's the exact example it's designed to deal with:
my $dbh = DBI->connect(...);
some_code_that_forks(); # Perhaps without your knowledge
# Child process dies, destroying the inherited dbh
$dbh->do(...); # Breaks because parent $dbh is now broken
The "AutoInactiveDestroy" attribute was added in DBI 1.614. 🎀
PrintWarn
Type: boolean, inherited
"PrintWarn" controls the printing of warnings recorded by the driver. When it's a true value (and true is the default, bestie) the DBI checks method calls to see whether a warning condition got set. If it did, the DBI will effectively do a warn("$class $method warning: $DBI::errstr") where $class is the driver class and $method is the name of the method which failed. Like so:
DBD::Oracle::db execute warning: ... warning text here ...
If you want, those warnings can be caught and processed with a $SIG{__WARN__} handler, or with modules like CGI::Carp and CGI::ErrorWrap. 🫶
See also "set_err" for how warnings get recorded and "HandleSetErr" for how to influence that.
Fetching the full details of warnings can require an extra round-trip to the database server for some drivers. When that's the case the driver may choose to only fetch full warning details if the "PrintWarn" attribute is true. If "PrintWarn" is false, those drivers should still signal that warnings existed by setting the warning string to something like "3 warnings". 🍓
PrintError
Type: boolean, inherited
"PrintError" can force errors to generate warnings (using "warn") in addition to returning error codes the normal way. When set "on", any method that results in an error occurring will cause the DBI to effectively do a warn("$class $method failed: $DBI::errstr"), where $class is the driver class and $method is the name of the method which failed. E.g.,
DBD::Oracle::db prepare failed: ... error text here ...
By default, DBI->connect sets "PrintError" "on". 🔥
If desired, the warnings can be caught and processed using a $SIG{__WARN__} handler or modules like CGI::Carp and CGI::ErrorWrap.
RaiseWarn
Type: boolean, inherited
"RaiseWarn" can force warnings to raise exceptions instead of just printing them. It's "off" by default. When set "on", any method which sets a warning condition will cause the DBI to effectively do a die("$class $method warning: $DBI::errstr"), where $class is the driver class and $method is the name of the method that sets the warning condition. Like:
DBD::Oracle::db execute warning: ... warning text here ...
If you turn "RaiseWarn" on, then you'd normally turn "PrintWarn" off. If "PrintWarn" is also on, then the "PrintWarn" happens first (naturally). 💅
This attribute was added in DBI 1.643.
RaiseError
Type: boolean, inherited
"RaiseError" can force errors to raise exceptions rather than just returning error codes the normal way. It's "off" by default. When set "on", any method resulting in an error will cause the DBI to effectively do a die("$class $method failed: $DBI::errstr"), where $class is the driver class and $method is the name of the method that failed. E.g.,
DBD::Oracle::db prepare failed: ... error text here ...
If you turn "RaiseError" on, then you'd normally turn "PrintError" off. If "PrintError" is also on, then the "PrintError" happens first (naturally).
Typically "RaiseError" is used together with "eval", or a module like Try::Tiny or TryCatch, to catch the exception that got thrown and handle it. For example:
use Try::Tiny;
try {
...
$sth->execute();
...
} catch {
# $sth->err and $DBI::err will be true if error was from DBI
warn $_; # print the error (which Try::Tiny puts into $_)
... # do whatever you need to deal with the error
};
Inside that catch block, the $DBI::lasth variable is super useful for diagnosis and reporting when you can't be sure which handle triggered the error. For example, $DBI::lasth->{Type} and $DBI::lasth->{Statement}. 🕵️♀️
See also "Transactions".
If you want to temporarily turn "RaiseError" off (say, inside a library function that's likely to fail), the recommended way is this:
{
local $h->{RaiseError}; # localize and turn off for this block
...
}
The original value will automatically and reliably be restored by Perl, no matter how the block is exited. Same logic applies to other attributes, including "PrintError". Iconic behaviour honestly. ✨
HandleError
Type: code ref, inherited
"HandleError" lets you provide your own alternative behaviour when errors happen. If you set it to a reference to a subroutine, that subroutine gets called whenever an error is detected — at the exact same point where "RaiseError" and "PrintError" are handled. It's also called when "RaiseWarn" is enabled and a warning is detected. 👑
The subroutine is called with three parameters: the error message string that "RaiseError", "RaiseWarn" or "PrintError" would use, the DBI handle being used (dbh, sth or drh as appropriate), and the first value being returned by the method that failed (typically undef).
If the subroutine returns a false value, then the "RaiseError", "RaiseWarn" and/or "PrintError" attributes are checked and acted upon as normal.
For example, to "die" 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]) };
You can even '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 "my" inside a subroutine to store the previous "HandleError" value is important. See perlsub and perlref for more info about closures. 🧚
"HandleError" can also alter the error message that "RaiseError", "RaiseWarn" and "PrintError" will use, if it returns false. It does that by altering the value of $_[0]. This example appends a stack trace to all errors and, unlike the previous example using Carp::confess, this one works for "PrintError" as well as "RaiseError":
$h->{HandleError} = sub { $_[0]=Carp::longmess($_[0]); 0; };
It's also possible for "HandleError" to hide an error, to a limited degree, by using "set_err" to reset $DBI::err and $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 it's hard to make reliable (avoiding infinite loops, for example), so it isn't recommended for general use! 💀 If you find a good use for it then please let me know.
HandleSetErr
Type: code ref, inherited
"HandleSetErr" lets you intercept the setting of handle "err", "errstr", and "state" values. If you set it to a reference to a subroutine, that subroutine gets 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 "err", "errstr", and "state" values being set, and the method name. You can alter any of these by changing the values in the @_ array. The return value affects set_err() behaviour, so see "set_err" for the details.
You can 'stack' multiple HandleSetErr handlers by using closures. See "HandleError" for an example. 🎀
The HandleSetErr and HandleError subroutines are lowkey siblings but they are NOT the same, and the difference is kinda significant. ✨
- HandleError only fires at the exact moment the DBI is about to hand control back to your app with
errset to true. It does not get invoked because some method that another DBI method called internally fell over. Nope. - HandleSetErr, on the other hand, gets called every single time
set_err()is called with a definederrvalue — even if that value is false. So yeah, despite the name, it's not just for errors, bestie: it also covers warn and info states. 💅
The set_err() method — and therefore HandleSetErr too — can be called multiple times inside one method, and it's usually invoked from, like, deep deep down in driver code. 🕳️
So in theory a driver can look at the return value from HandleSetErr (via set_err()) and decide whether to keep going or just give up. If set_err() returns an empty list — which signals that the HandleSetErr code has already "handled" the "error" — then the driver could continue instead of failing, assuming that's actually a reasonable thing to do in that situation. This isn't expected to be a common thing, and any case like this should be clearly marked in the driver docs and talked about on the dbi-dev mailing list. 📬
The HandleSetErr attribute was added in DBI 1.41.
💜 ErrCount
Type: unsigned integer
The ErrCount attribute gets incremented every time the set_err() method records an error. Warnings and information states do not bump it. And the DBI never resets it — not ever, no takebacks. 😤
The ErrCount attribute was added in DBI 1.41. Heads up though: older drivers may never have been updated to use set_err() to record errors, so with those guys this attribute might just... not increment. 🙈
💜 ShowErrorStatement
Type: boolean, inherited
The ShowErrorStatement attribute makes the relevant Statement text get appended onto the error messages that the RaiseError, PrintError, RaiseWarn and PrintWarn attributes generate. Slay. 🎀
It only applies to errors on statement handles, plus the prepare(), do(), and the various select*() database handle methods. (And FYI the exact format of that appended text is subject to change, so don't get attached.)
If $h->{ParamValues} hands back a hash reference of parameter (placeholder) values, then those get formatted and appended to the end of the Statement text in the error message. 🫶
💜 TraceLevel
Type: integer, inherited
The TraceLevel attribute is your alternative to the trace method — it lets you set the DBI trace level and trace flags for one specific handle. More deets live in "TRACING".
It is extra useful when you pair it with local, because then you can change trace settings for just one little block of code and dip. 🦋
{
local $h->{TraceLevel} = 3;
# ... only this block is loud
}
💜 FetchHashKeyName
Type: string, inherited
FetchHashKeyName decides whether fetchrow_hashref() does case conversion on the field names it uses for the hash keys. For ✨historical reasons✨ it defaults to "NAME", but honestly it's recommended you set it to "NAME_lc" (lower case, bestie) or "NAME_uc" (upper case, if that's your vibe) — whichever you prefer.
It can only be set on driver handles and database handles, okay? On statement handles the value is frozen the moment prepare() is called. 🧊
💜 ChopBlanks
Type: boolean, inherited
ChopBlanks controls the trimming of trailing space characters from fixed-width character (CHAR) fields. No other field types get touched — not even if their values legit have trailing spaces. Only CHAR. Periodt.
The default is false (although, fair warning, it's possible that default could change someday). Apps that need specific behaviour should just set the attribute like they mean it.
Drivers are not required to support this attribute. But any driver that doesn't support it has to arrange to return undef as the attribute value. No fake support, bestie. 😭
💜 LongReadLen
Type: unsigned integer, inherited
LongReadLen controls the maximum length of 'long' type fields (LONG, BLOB, CLOB, MEMO, etc.) that the driver will read from the database automatically whenever it fetches each row of data.
Important distinction: LongReadLen is only about fetching and reading long values. It has literally nothing to do with inserting or updating them. 🙅
A value of 0 means don't automatically fetch any long data at all. When LongReadLen is 0, drivers may return undef or an empty string for long fields.
The default is typically 0 (zero) or 80 bytes, but it can vary between drivers. If your app is fetching long fields, set this value to something slightly larger than the longest long field value you're gonna fetch. 📏
Some databases return certain long types encoded as pairs of hex digits. For those types, LongReadLen relates to the underlying data length — not the doubled-up length of the encoded string. Don't get tricked by the doubling, iykyk.
Changing the value of LongReadLen on a statement handle after it's been prepare'd will typically have no effect, so the move is to set LongReadLen on the $dbh before calling prepare. 💅
For most drivers, the value you set here has a direct effect on how much memory the statement handle uses while it's active — so don't be too generous, okay? Chill. If you can't be sure what value to use, you could run an extra select statement to figure out the longest value. Like this:
$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 might need to take extra care if the table can be modified between that first select and the second one being executed. And you may also need a different function if OCTET_LENGTH() doesn't work for long types in your database. For example, for Sybase use DATALENGTH() and for Oracle use LENGTHB(). 🌈
Also go read LongTruncOk for info about truncation of long types.
💜 LongTruncOk
Type: boolean, inherited
LongTruncOk controls what happens when you fetch a long field value that's been truncated — typically because it's longer than whatever LongReadLen says. ✂️
By default LongTruncOk is false, so fetching a long value that needs truncating will make the fetch fail. (And apps should ALWAYS, and I mean always, check for errors after a fetch loop, because an error like a divide by zero or a long field truncation could've made the fetch terminate early. 😭)
If a fetch does fail because of a long field truncation while LongTruncOk is false, many drivers will let you keep on fetching further rows anyway. Nice of them.
See also LongReadLen. 💖
💜 TaintIn
Type: boolean, inherited
If TaintIn is set to a true value, and Perl is running in taint mode (like, started with the -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. And if Perl isn't running in taint mode at all, this attribute does absolutely nothing. 🫥
When you're fetching data you actually trust, you can turn off TaintIn for that statement handle, just for the duration of the fetch loop. 🎀
The TaintIn attribute was added in DBI 1.31.
💜 TaintOut
Type: boolean, inherited
If TaintOut is set to a true value and Perl is running in taint mode (e.g. started with the -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. And again, if Perl isn't in taint mode, this attribute has no effect. Nothing. Nada. 🦋
When you're fetching data you trust, you can turn off TaintOut for that statement handle for the duration of the fetch loop.
Right now, only fetched data gets tainted. But it's possible that the results of other DBI method calls — and the values of fetched attributes — may also get tainted in future versions. ⚠️ And bestie, that change could absolutely break your applications unless you take great care now. So if you use DBI Taint mode, please report your experience and any suggestions for changes. 📝
The TaintOut attribute was added in DBI 1.31.
💜 Taint
Type: boolean, inherited
Taint is the shortcut for TaintIn + TaintOut (it's also hanging around for backwards compatibility, bless). 💖
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. Both. No halfsies.
💜 Profile
Type: inherited
The Profile attribute turns on the collection and reporting of method call timing statistics. Go read the DBI::Profile module documentation for much more detail. 📊
The Profile attribute was added in DBI 1.24.
💜 ReadOnly
Type: boolean, inherited
An application can set the ReadOnly attribute of a handle to a true value to say "hey, I will not be attempting to make any changes using this handle or any of its children." 🚫
Heads up: the exact definition of 'read only' is kinda fuzzy. For more details, see the docs for whichever driver you're using.
If the driver can make the handle truly read-only, then it should — unless doing so would have some unpleasant side effect, like changing the consistency level from per-statement to per-session. Otherwise, the attribute is just advisory. It's giving "suggestion, not a command."
A driver can also set ReadOnly itself, to signal that the data it's connected to can't be changed for some reason.
If the driver can't ensure ReadOnly is actually adhered to, it will record a warning. In that case, reading ReadOnly back after setting it true will return true anyway — even if the underlying driver can't guarantee it — so any application knows that the application declared itself ReadOnly. 📢
Library modules and proxy drivers can use this attribute to influence their behaviour. For example, the DBD::Gofer driver considers the ReadOnly attribute when deciding whether to retry an operation that failed. 🔁
The attribute should be set to 1 or 0 (or undef). Other values are reserved — don't go trying random numbers, okay? 🙈
💜 Callbacks
Type: hash ref
Okay so the DBI callback mechanism lets you intercept — and optionally replace — any method call on a DBI handle. At the extreme end, it lets you become a full-on puppet master, deceiving the application in literally any way you want. 👑 🎭
The Callbacks attribute is a hash reference where the keys are DBI method names and the values are code references. For every key that names a method, the DBI will execute the associated code reference before executing the method. ✨
The arguments passed to that code reference will be the same as the arguments to the method — including the invocant (a database handle or statement handle). For example, say you wanna callback to some code on a call to prepare():
$dbh->{Callbacks} = {
prepare => sub {
my ($dbh, $query, $attrs) = @_;
print "Preparing q{$query}\n"
},
};
Iconic. And there's more where that came from... 💅
So like, that callback would then be executed when you called the prepare() method, bestie:
$dbh->prepare('SELECT 1');
And the output of course would be, no surprises:
Preparing q{SELECT 1}
Because callbacks run before the methods they're linked to, you can edit the args before they get passed along to the method call. Like, if you want every call to prepare() to be immediately prepared by DBD::Pg, you add a callback that keeps the "pg_prepare_now" attribute always set:
my $dbh = DBI->connect($dsn, $username, $auth, {
Callbacks => {
prepare => sub {
$_[2] ||= {};
$_[2]->{pg_prepare_now} = 1;
return; # must return nothing
},
}
});
Note that we're editing the contents of @_ directly. In this case we've created the attributes hash if it's not passed to the "prepare" call.
You can also stop the associated method from ever executing. While a callback executes, $_ holds the method name. (This allows multiple callbacks to share the same code reference and still know what method was called.) To prevent the method from executing, simply undef $_. For example, if you wanted to disable calls to ping(), you could do this:
$dbh->{Callbacks} = {
ping => sub {
# tell dispatch to not call the method:
undef $_;
# return this value instead:
return "42 bells";
}
};
As with other attributes, Callbacks can be specified on a handle or via the attributes to connect(). Callbacks can also be applied to statement methods on a statement handle. For example:
$sth->{Callbacks} = {
execute => sub {
print "Executing ", shift->{Statement}, "\n";
}
};
The "Callbacks" attribute of a database handle isn't copied to any statement handles it creates. So setting callbacks for a statement handle requires you to set the "Callbacks" attribute on the statement handle yourself, as in the example above, or use the special "ChildCallbacks" key described below.
✨ Special Keys in Callbacks Attribute ✨
Besides DBI handle method names, the "Callbacks" hash reference supports four extra keys.
The first is the "ChildCallbacks" key. When a statement handle is created from a database handle the "ChildCallbacks" key of the database handle's "Callbacks" attribute, if any, becomes the new "Callbacks" attribute of the statement handle. This allows you to define callbacks for all statement handles created from a database handle. For example, if you wanted to count how many times "execute" was called in your application, you could write:
my $exec_count = 0;
my $dbh = DBI->connect( $dsn, $username, $auth, {
Callbacks => {
ChildCallbacks => {
execute => sub { $exec_count++; return; }
}
}
});
END {
print "The execute method was called $exec_count times\n";
}
The other three special keys are "connect_cached.new", "connect_cached.connected", and "connect_cached.reused". These keys define callbacks that are called when connect_cached() is called, but allow different behaviors depending on whether a new handle is created or a handle is returned. The callback is invoked with these arguments: "$dbh, $dsn, $user, $auth, $attr".
For example, some applications use connect_cached() to connect with "AutoCommit" enabled and then disable "AutoCommit" temporarily for transactions. If connect_cached() is called during a transaction, perhaps in a utility method, then it might select the same cached handle and then force "AutoCommit" on, forcing a commit of the transaction. See the "connect_cached" documentation for one way to deal with that. Here we'll describe an alternative approach using a callback.
Because the "connect_cached.new" and "connect_cached.reused" callbacks are invoked before connect_cached() has applied the connect attributes, you can use them to edit the attributes that will be applied. To prevent a cached handle from having its transactions committed before it's returned, you can eliminate the "AutoCommit" attribute in a "connect_cached.reused" callback, like so:
my $cb = {
'connect_cached.reused' => sub { delete $_[4]->{AutoCommit} },
};
sub dbh {
my $self = shift;
DBI->connect_cached( $dsn, $username, $auth, {
PrintError => 0,
RaiseError => 1,
AutoCommit => 1,
Callbacks => $cb,
});
}
The upshot is that new database handles are created with "AutoCommit" enabled, while cached database handles are left in whatever transaction state they happened to be in when retrieved from the cache.
Note that we've also used a lexical for the callbacks hash reference. This is because connect_cached() returns a new database handle if any of the attributes passed to it have changed. If we used an inline hash reference, connect_cached() would return a new database handle every time. Which would rather defeat the purpose.
A more common application for callbacks is setting connection state only when a new connection is made (by connect() or connect_cached()). Adding a callback to the connected method (when using "connect") or via "connect_cached.connected" (when using connect_cached()) makes this easy. The connected() method is a no-op by default (unless you subclass the DBI and change it). The DBI calls it to indicate that a new connection has been made and the connection attributes have all been set. You can give it a bit of added functionality by applying a callback to it. For example, to make sure that MySQL understands your application's ANSI-compliant SQL, set it up like so:
my $dbh = DBI->connect($dsn, $username, $auth, {
Callbacks => {
connected => sub {
shift->do(q{
SET SESSION sql_mode='ansi,strict_trans_tables,no_auto_value_on_zero';
});
return;
},
}
});
If you're using connect_cached(), use the "connect_cached.connected" callback, instead. This is because connected() is called for both new and reused database handles, but you want to execute a callback only when a new database handle is returned. For example, to set the time zone on connection to a PostgreSQL database, try this:
my $cb = {
'connect_cached.connected' => sub {
shift->do('SET timezone = UTC');
}
};
sub dbh {
my $self = shift;
DBI->connect_cached( $dsn, $username, $auth, { Callbacks => $cb });
}
One significant limitation with callbacks is that there can only be one per method per handle. This means it's easy for one use of callbacks to interfere with, or typically simply overwrite, another use of callbacks. For this reason modules using callbacks should document the fact clearly so application authors can tell if use of callbacks by the module will clash with use of callbacks by the application.
You might be able to work around this issue by taking a copy of the original callback and calling it within your own. For example:
my $prev_cb = $h->{Callbacks}{method_name};
$h->{Callbacks}{method_name} = sub {
if ($prev_cb) {
my @result = $prev_cb->(@_);
return @result if not $_; # $prev_cb vetoed call
}
... your callback logic here ...
};
private_your_module_name_*
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 "private_".
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., "private_YourFullModuleName_thingy").
Because of the way the Perl tie mechanism works you cannot reliably use the "||=" operator directly to initialise the attribute, like this:
my $foo = $dbh->{private_yourmodname_foo} ||= { ... }; # WRONG
you 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, or for applications to piggy-back extra information onto DBI handles.
💖 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:
✨ clone ✨
$new_dbh = $dbh->clone(\%attr);
The "clone" method duplicates the $dbh connection by connecting with the same parameters ($dsn, $user, $password) as originally used.
The attributes for the cloned connect are the same as those used for the original connect, with any other attributes in "\%attr" merged over them. Effectively the same as doing:
%attributes_used = ( %original_attributes, %attr );
If \%attr is not given then it defaults to a hash containing all the attributes in the attribute cache of $dbh excluding any non-code references, plus the main boolean attributes (RaiseError, PrintError, AutoCommit, etc.). This behaviour is unreliable and so use of clone without an argument is deprecated and may cause a warning in a future release.
The clone method can be used even if the database handle is disconnected.
The "clone" method was added in DBI 1.33.
🌈 data_sources 🌈
@ary = $dbh->data_sources();
@ary = $dbh->data_sources(\%attr);
Returns a list of data sources (databases) available via the $dbh driver's data_sources() method, plus any extra data sources that the driver can discover via the connected $dbh. Typically the extra data sources are other databases managed by the same server process that the $dbh is connected to.
Data sources are returned in a form suitable for passing to the "connect" method (that is, they will include the "dbi:$driver:" prefix).
The data_sources() method, for a $dbh, was added in DBI 1.38.
🔥 do 🔥
$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 "undef" on error. A return value of -1 means the number of rows is not known, not applicable, or not available.
okay so bestie this method is typically most useful for non-SELECT statements — the ones that either can't even be prepared in advance (blame a driver limitation, not you 💅) or just don't need to be run over and over. And do NOT use it for SELECT statements, because it doesn't hand back a statement handle, which means... nothing to fetch from. RIP to your data, it's just vibes now 😭
The default do method is logically similar to this:
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
}
And like, for example 🍓:
my $rows_deleted = $dbh->do(q{
DELETE FROM table
WHERE status = ?
}, undef, 'DONE') or die $dbh->errstr;
Using placeholders and @bind_values with the do method is genuinely useful, because it saves you from having to correctly quote every variable inside $statement (quoting is the villain of every SQL-injection story, fr fr 💀). But — heads up — if you're gonna run that statement a bunch of times, it's more efficient to prepare it once and call execute many times instead. Be lazy smart, not lazy sloppy ✨
That q{...} style quoting used in the example? It avoids clashing with quotes that might be chillin' inside your SQL statement. If you want to interpolate variables into the string, use the double-quote-like qq{...} operator instead. More deets in "Quote and Quote-like Operators" in perlop.
And ngl, drivers are free to avoid the overhead of making a DBI statement handle for do(), especially when there are no parameters. In that case, error handlers, if they get invoked during do(), will be passed the database handle. iykyk 🦋
🌸 last_insert_id 🌸
$rv = $dbh->last_insert_id();
$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 you just inserted, if possible. Typically that's a value the database server assigned to a column with an auto_increment or serial type. Returns undef if the driver doesn't support the method or can't figure out the value. she's trying her best, okay 🫶
The $catalog, $schema, $table, and $field parameters may be required for some drivers (see below, bestie). If you don't know the parameter values and your driver doesn't need them, just use undef for each. undef is a valid love language 💜
There are several caveats to be aware of with this method if you want to use it for portable applications. Read them. All of them. No skipping. 👑
- For some drivers the value may only be available immediately after the insert statement has executed (e.g., mysql, Informix).
- For some drivers the
$catalog,$schema,$table, and$fieldparameters are required, for others they are ignored (e.g., mysql). - Drivers may return an indeterminate value if no insert has been performed yet. mystery value, spooky season 🎀
- 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
\%attrparameter. - If the underlying database offers nothing better, then some drivers may attempt to implement this method by executing
"select max($field) from $table". Drivers using any approach like this should issue a warning ifAutoCommitis true, because it is generally unsafe — another process may have modified the table between your insert and the select. not safe, not cute 😭 For situations where you know it is safe, such as when you have locked the table, you can silence the warning by passing"Warn" => 0in\%attr. - If no insert has been performed yet, or the last insert failed, then the value is implementation defined. So like... ask your driver, not me 💅
Given all the caveats above, it's clear that this method must be used with care. handle with the energy of carrying a full iced coffee in one hand 🍓
The last_insert_id method was added in DBI 1.38.
✨ selectrow_array ✨
@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 — one function doing three jobs, main character energy fr 👑. If you call it in a list context, it returns the first row of data from the statement. And the $statement parameter can be a previously prepared statement handle, in which case the prepare is skipped entirely. efficiency, we love her ✨
If any method fails, and RaiseError is not set, selectrow_array will return an empty list.
If you call it in a scalar context for a statement handle that has more than one column, it is undefined whether the driver returns the value of the first column or the last. So don't do that. Also, in a scalar context, an undef is returned if there are no more rows or if an error occurred. And that undef can't be distinguished from an undef returned because the first field value was NULL. two very different undefs wearing the same outfit 💀 For these reasons you should exercise some caution if you use selectrow_array in a scalar context... or just don't do that. bestie, just don't. I'm begging 🫶
🦋 selectrow_arrayref 🦋
$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 $statement parameter can be a previously prepared statement handle, in which case the prepare is skipped. skip the prep, keep the vibes 🌸
If any method fails, and RaiseError is not set, selectrow_arrayref will return undef.
💅 selectrow_hashref 💅
$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 $statement parameter can be a previously prepared statement handle, in which case the prepare is skipped. same deal, different fetch ✨
If any method fails, and RaiseError is not set, selectrow_hashref will return undef.
⭐ selectall_arrayref ⭐
$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 (or hash, see below 👀) for each row of data fetched. arrays within arrays, inception but make it data 🎀
The $statement parameter can be a previously prepared statement handle, in which case the prepare is skipped. This is recommended if the statement is going to be executed many times.
If RaiseError is not set and any method except fetchall_arrayref fails, then selectall_arrayref will return undef; if fetchall_arrayref fails then it will return with whatever data has been fetched thus far. You should check $dbh->err afterwards (or use the RaiseError attribute) to discover if the data is complete or was truncated due to an error. truncated data is the silent jump scare of database calls 😭
The fetchall_arrayref method called by selectall_arrayref supports a $max_rows parameter. You can specify a value for $max_rows by including a "MaxRows" attribute in \%attr. In which case finish() is called for you after fetchall_arrayref() returns. she handles cleanup, queen behavior 👑
The fetchall_arrayref method called by selectall_arrayref also supports a $slice parameter. You can specify a value for $slice by including a "Slice" or "Columns" attribute in \%attr. The only difference between the two is that if Slice is not defined and Columns 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 /fetchall_arrayref. yes the counting starts at 1, perl is not the boss of SQL 🦋
You may often want to fetch an array of rows where each row is stored as a hash. That can be done simply using:
my $emps = $dbh->selectall_arrayref(
"SELECT ename FROM emp ORDER BY ename",
{ Slice => {} }
);
foreach my $emp ( @$emps ) {
print "Employee: $emp->{ename}\n";
}
Or, to fetch into an array instead of an array ref:
@result = @{ $dbh->selectall_arrayref($sql, { Slice => {} }) };
See "fetchall_arrayref" method for more details.
🌈 selectall_array 🌈
@ary = $dbh->selectall_array($statement);
@ary = $dbh->selectall_array($statement, \%attr);
@ary = $dbh->selectall_array($statement, \%attr, @bind_values);
This is a convenience wrapper around selectall_arrayref that returns the rows directly as a list, rather than a reference to an array of rows. no dereferencing required, we love a shortcut 💅
Note that if RaiseError is not set then you can't tell the difference between returning no rows and an error. Using RaiseError is best practice. do it for your future self, bestie 🫶
The selectall_array method was added in DBI 1.635.
💜 selectall_hashref 💜
$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(). at most one entry per row, she said, firmly ✨
The $statement parameter can be a previously prepared statement handle, in which case the prepare is skipped. This is recommended if the statement is going to be executed many times.
The $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. Using multiple names yields a tree of nested hashes. nested hashes!!! the drama, the structure, the beauty 🌈 🌸
If a row has the same key as an earlier row then it replaces the earlier row. last one standing wins, no mercy 💀
If any method except fetchall_hashref fails, and RaiseError is not set, selectall_hashref will return undef. If fetchall_hashref fails and RaiseError is not set, then it will return with whatever data it has fetched thus far. $DBI::err should be checked to catch that. check your errors, she's telling you twice for a reason 🎀
See fetchall_hashref() for more details.
🎀 selectcol_arrayref 🎀
$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. one column, every row, no notes ✨
That $statement can totally be a statement handle you already prepped earlier — in that case the whole "prepare" step just gets skipped. 💅 And fr fr, do that if you're gonna run the same statement a million times, it's the move.
Okay so if any method fails except fetch, and RaiseError isn't set? selectcol_arrayref hands you back undef. 🙈 But if fetch is the one that failed and RaiseError isn't set, it just returns whatever it managed to fetch up to that point. So bestie, check $DBI::err to catch that, okay?
By default selectcol_arrayref pushes one single column value (the first one, obviously, first is the main character) from each row into the result array. But lowkey it can also push another column, or even multiple columns per row. You control that with a "Columns" attribute — and it has to be a ref to an array holding the column number(s) you want. Like:
# get array of id and name pairs:
my $ary_ref = $dbh->selectcol_arrayref("select id, name from table", { Columns=>[1,2] });
my %hash = @$ary_ref; # build hash from key-value pairs so $hash{$id} => name
And you can cap how many rows get fetched by sliding a "MaxRows" attribute into \%attr. 👑
prepare
$sth = $dbh->prepare($statement) or die $dbh->errstr;
$sth = $dbh->prepare($statement, \%attr) or die $dbh->errstr;
This preps a statement so the database engine can run it later, and hands you back a reference to a statement handle object. ✨
That returned statement handle? You can use it to peek at the statement's attributes and to call the execute method on it. Go read "Statement Handle Methods" for the tea.
Drivers for engines that don't even have the concept of preparing a statement will usually just stash the statement in the returned handle and process it whenever $sth->execute gets called. Those drivers are not gonna give you much useful info about the statement — stuff like $sth->{NUM_OF_FIELDS} — until after $sth->execute has run. So if your app is meant to be portable, keep that in mind. 🧚
Generally speaking, DBI drivers do not parse what's inside your statement (they literally just count up any Placeholders). The statement gets passed straight to the database engine, which is sometimes called pass-thru mode. Advantages and disadvantages, we contain multitudes:
- On the plus side, you get to access literally all the functionality of the engine you're using. Ate.
- On the downside, you're limited if you're on a simple engine, and you have to be extra careful if you're writing apps meant to be portable across engines.
Portable applications should not assume a new statement can be prepared and/or executed while you're still fetching results from a previous statement. Just don't do it.
Some command-line SQL tools use statement terminators, like a semicolon, to mark the end of a statement. Normally you should not use those terminators with the DBI. 💀
prepare_cached
$sth = $dbh->prepare_cached($statement)
$sth = $dbh->prepare_cached($statement, \%attr)
$sth = $dbh->prepare_cached($statement, \%attr, $if_active)
Same as prepare, except the statement handle it returns gets stored in a hash tied to that $dbh. If somebody calls prepare_cached again with the same $statement and same %attr values, you get that cached $sth back without ever bothering the database server. Iconic. But please, please read the cautions and caveats below, I'm begging. 😭
The $if_active parameter lets you choose what happens when an already-cached statement handle is still Active. Options, bestie:
0: A warning gets generated, andfinish()is called on the statement handle before it's returned. This is the default behaviour if$if_activeisn't passed.1:finish()gets called on the statement handle, but the warning is suppressed.2: Disables any checking. (chaotic)3: The existing active statement handle gets removed from the cache, and a brand new statement handle is prepared and cached in its place. This is the safest option because it doesn't affect the state of the old handle — it just yeets it from the cache. [Added in DBI 1.40]
Some prepare_cached examples for you:
sub insert_hash {
my ($table, $field_values) = @_;
# sort to keep field order, and thus sql, stable for prepare_cached
my @fields = sort keys %$field_values;
my @values = @{$field_values}{@fields};
my $sql = sprintf "insert into %s (%s) values (%s)",
$table, join(",", @fields), join(",", ("?")x@fields);
my $sth = $dbh->prepare_cached($sql);
return $sth->execute(@values);
}
sub search_hash {
my ($table, $field_values) = @_;
# sort to keep field order, and thus sql, stable for prepare_cached
my @fields = sort keys %$field_values;
my @values = @{$field_values}{@fields};
my $qualifier = "";
$qualifier = "where ".join(" and ", map { "$_=?" } @fields) if @fields;
$sth = $dbh->prepare_cached("SELECT * FROM $table $qualifier");
return $dbh->selectall_arrayref($sth, {}, @values);
}
Caveat emptor: this caching can be super useful in some applications, but it can also cause problems and you should use it with care. Here's a contrived case where caching causes a genuinely 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(...);
}
}
So in this example, since both handles are preparing the exact same statement, $sth2 won't be its own statement handle — it's a duplicate of $sth handed back from the cache. The results will absolutely not be what you expect. Typically the inner fetch loop works totally normally, fetching all the records and terminating when there are no more, but now that $sth is the same as $sth2, the outer fetch loop terminates too. 💀
You'll know you hit this problem because prepare_cached() generates a warning by default (when $if_active is false). So there's your heads up.
The cache that prepare_cached() uses is keyed by both the statement and any attributes, so you can also dodge this issue by doing something like:
$sth = $dbh->prepare_cached("...", { dbi_dummy => __FILE__.__LINE__ });
which guarantees that prepare_cached only ever returns statements cached by that line of code in that source file. Slay.
Also, to make sure the attributes you pass are always the same, avoid passing references inline. For example, the Slice attribute is specified as a reference. Declare it external to the call to prepare_cached(), so a fresh hash reference isn't created on every single call. See "connect_cached" for more details and examples.
And if you want your cache managed intelligently, you can tie the hashref returned by "CachedKids" to a suitable caching module, like Tie::Cache::LRU:
my $cache;
tie %$cache, 'Tie::Cache::LRU', 500;
$dbh->{CachedKids} = $cache;
commit
$rc = $dbh->commit or die $dbh->errstr;
Commits (aka makes permanent) the most recent series of database changes, if the database supports transactions and AutoCommit is off. ✨
If "AutoCommit" is on, then calling commit triggers a "commit ineffective with AutoCommit" warning. 😬
Also see "Transactions" in the "FURTHER INFORMATION" section below.
rollback
$rc = $dbh->rollback or die $dbh->errstr;
Rolls back (aka undoes) the most recent series of uncommitted database changes, if the database supports transactions and AutoCommit is off.
If "AutoCommit" is on, then calling rollback triggers a "rollback ineffective with AutoCommit" warning. 🙈
Also see "Transactions" in the "FURTHER INFORMATION" section below.
begin_work
$rc = $dbh->begin_work or die $dbh->errstr;
Turns transactions on (by flipping "AutoCommit" off) until the next call to commit or rollback. And after that next commit or rollback, "AutoCommit" automatically flips back on again. So convenient honestly.
If "AutoCommit" is already off when begin_work gets called, it does nothing except return an error. And if the driver doesn't support transactions, then when begin_work tries to set "AutoCommit" off, the driver triggers a fatal error. ⚠️
Also see "Transactions" in the "FURTHER INFORMATION" section below.
disconnect
$rc = $dbh->disconnect or warn $dbh->errstr;
Disconnects the database from the database handle. disconnect is typically only used right before you exit the program. After disconnecting, the handle is of little use, ngl.
The transaction behaviour of the disconnect method is, sadly, undefined. 💀 Some database systems (like Oracle and Ingres) will automatically commit any outstanding changes, but others (like Informix) will rollback any outstanding changes. So applications not using "AutoCommit" should explicitly call commit or rollback before calling disconnect. Do not leave this to chance bestie.
The database gets automatically disconnected by the "DESTROY" method if it's still connected and there are no longer any references to the handle. The "DESTROY" method for each driver should implicitly call rollback to undo any uncommitted changes. That behaviour is vital to make sure incomplete transactions don't get committed just because Perl calls "DESTROY" on every object before exiting. Also, do not rely on the order of object destruction during "global destruction", because it is undefined.
Generally, if you want your changes committed or rolled back when you disconnect, then explicitly call commit or rollback before disconnecting. Periodt.
If you disconnect from a database while you still have active statement handles (e.g., SELECT statement handles that might have more data to fetch), you'll get a warning. The warning might be telling you a fetch loop terminated early, maybe because of an uncaught error. To avoid the warning, call the finish method on the active handles. 🧚
ping
$rc = $dbh->ping;
Tries to figure out, in a reasonably efficient way, whether the database server is still running and whether your connection to it still works. Individual drivers should implement this function in whatever manner is most suitable for their database engine.
The current default implementation always returns true without actually doing anything. Actually, it returns "0 but true" — which is true but zero. That way you can tell whether 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.
get_info
$value = $dbh->get_info( $info_type );
It hands you back info about the implementation — driver and data source capabilities, restrictions, all that jazz. If the info type is unknown, or just not implemented, you get undef back. No drama, no error, just undef. For example:
$database_version = $dbh->get_info( 18 ); # SQL_DBMS_VER
$max_select_tables = $dbh->get_info( 106 ); # SQL_MAXIMUM_TABLES_IN_SELECT
Want the deep lore on what each info type means and what values it might spit out? Go read "Standards Reference Information". ✨
The DBI::Const::GetInfoType module exports a %GetInfoType hash, which maps info type names to numbers so you don't have to memorise magic numbers like it's a cursed phone number. Like:
$database_version = $dbh->get_info( $GetInfoType{SQL_DBMS_VER} );
The names are a mashup of the ANSI and ODBC standards (which, iconic of standards, disagree in some cases 💀). More deets live in DBI::Const::GetInfoType.
Because a bunch of DBI methods internally call get_info(), drivers are strongly encouraged — like, please bestie, we are begging — to support at least this very minimal set of information types so 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
Values from 9000 to 9999 for get_info are officially reserved for use by Perl DBI. And the ones in that range that actually have an assigned meaning are defined right here:
9000: true if a backslash character (\) placed before placeholder-like text (e.g. ?, :foo) will stop it from being treated as a placeholder by the driver. That backslash gets removed before the text is passed along to the backend.
✨ table_info ✨
$sth = $dbh->table_info( $catalog, $schema, $table, $type );
$sth = $dbh->table_info( $catalog, $schema, $table, $type, \%attr );
# then $sth->fetchall_arrayref or $sth->fetchall_hashref etc
Gives you back an active statement handle you can use to fetch info about the tables and views chillin' in your database. 💅
The arguments $catalog, $schema and $table may accept search patterns, depending on the database/driver, like: $table = '%FOO%';
⚠️ And remember bestie: the underscore character ("_") is itself a search pattern meaning "match any character". So 'FOO_%' is the same as 'FOO%', and 'FOO_BAR%' will match names like 'FOO1BAR'.
The value of $type is a comma-separated list of one or more types of tables to return in the result set. Each value may optionally be quoted, e.g.:
$type = "TABLE";
$type = "'TABLE','VIEW'";
On top of that, some drivers may also support these following special cases:
- If the value of
$catalogis'%'and the$schemaand$tablename are empty strings, the result set contains a list of catalog names. For example:
$sth = $dbh->table_info('%', '', '');
- If the value of
$schemais'%'and$catalogand$tableare empty strings, the result set contains a list of schema names. - If the value of
$typeis'%'and$catalog,$schema, and$tableare all empty strings, the result set contains a list of table types.
If your driver doesn't support one or more of the selection filter parameters, then you might get back more than you asked for — and you can just do the filtering yourself. Not ideal, but you'll survive. 🙈
Heads up: this method can be expensive, and it can return a whole lot of data. (For example, a small Oracle installation returns over 2000 rows.) So it's a genuinely good idea to use the filters to limit the data as much as possible.
The statement handle you get back has at least the following fields, in the order shown below. Other fields may also be present after these ones.
TABLE_CAT: Table catalog identifier. This field is NULL ("undef") if it's not applicable to the data source, which is usually the case. This field is empty if it's not applicable to the table.TABLE_SCHEM: The name of the schema containing theTABLE_NAMEvalue. This field is NULL ("undef") if not applicable to the 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's specific to the data source.REMARKS: A description of the table. May be NULL ("undef").
Note that "table_info" might not return records for all tables. Applications can use any valid table regardless of whether it's returned by "table_info". It's giving "the list isn't the law". 🦋
See also "tables", "Catalog Methods" and "Standards Reference Information".
✨ column_info ✨
$sth = $dbh->column_info( $catalog, $schema, $table, $column );
# then $sth->fetchall_arrayref or $sth->fetchall_hashref etc
Returns an active statement handle you can use to fetch info about the columns in specified tables. 🎀
The arguments $schema, $table and $column may accept search patterns according to the database/driver, for example: $table = '%FOO%';
Note: 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 you can do the filtering yourself.
Note: if your driver does not supportcolumn_info, an undef is returned. This is a whole different situation from asking for something that doesn't exist in a driver which does supportcolumn_info— in that case you get a valid statement handle to an empty result-set instead. Know the difference bestie, it matters.
And if the arguments don't match any tables at all, you'll still get a statement handle. It'll just return no rows. Empty queen. 💅
The statement handle returned has at least the following fields, in the order shown below. Other fields may also be present after these.
TABLE_CAT: The catalog identifier. This field is NULL ("undef") 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 ("undef") 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 the detailed info.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 ("undef") if not applicable.NULLABLE: Indicates if a column can accept NULLs. These are the defined values:
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, in a format that can be used directly in an SQL statement.
Note that this may be an expression and not simply the text used for the default value in the original CREATE TABLE statement. For example, given:
col1 char(30) default current_user -- a 'function'
col2 char(30) default 'string' -- a string literal
where"current_user"is the name of a function, the corresponding"COLUMN_DEF"values would be:
Database col1 col2
-------- ---- ----
Oracle: current_user 'string'
Postgres: "current_user"() 'string'::text
MS SQL: (user_name()) ('string')
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 these 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. No faking it. ✨
Drivers wishing to provide extra database/driver specific info should do so in extra columns beyond all the ones listed above, and should use lowercase field names with the driver-specific prefix (i.e., 'ora_...'). Applications accessing such fields should do it by name and not by column number. Fr fr, names only.
The result set is ordered by TABLE_CAT, TABLE_SCHEM, TABLE_NAME and ORDINAL_POSITION.
Note: there's some overlap with statement handle attributes (in perl) andSQLDescribeCol(in ODBC). However,SQLColumnsprovides more metadata. More is more bestie. 💖
See also "Catalog Methods" and "Standards Reference Information".
✨ primary_key_info ✨
$sth = $dbh->primary_key_info( $catalog, $schema, $table );
# then $sth->fetchall_arrayref or $sth->fetchall_hashref etc
Returns an active statement handle you can use to fetch info about the columns that make up the primary key for a table. 🗝️ And no, the arguments do not accept search patterns here (unlike table_info()). Sorry bestie.
The statement handle will return one row per column, ordered by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, and KEY_SEQ. If there's no primary key, then the statement handle will fetch no rows.
Note: support for the selection criteria, such as $catalog, is driver specific. If the driver doesn't support catalogs and/or schemas, it may just ignore these criteria entirely. 🙈
The statement handle returned has at least the following fields, in the order shown below. Other fields may also be present after these.
TABLE_CAT: The catalog identifier. This field is NULL ("undef") 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 ("undef") 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 namedORDINAL_POSITIONin SQL/CLI. Same energy, different fit. 👑PK_NAME— the primary key constraint identifier 💅 and yeah, this field isNULL("undef") if it's just not applicable to the data source. Not every table's got one, bestie, it's fine.
Also go peek at "Catalog Methods" and "Standards Reference Information", they're the receipts for all this 🧾
🌸 primary_key 🌸
@key_column_names = $dbh->primary_key( $catalog, $schema, $table );
It's the simple girlie version of the primary_key_info() method — like, why do the whole thing when the shortcut ate 💅
Returns a list of the column names that make up the primary key of the table you named. The list comes back in primary key column sequence order. And if there's no primary key at all? Empty list, bestie. Nothing. Zero. Nada. She's got nothing for you 🙈
🔥 foreign_key_info 🔥
$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 );
# then $sth->fetchall_arrayref or $sth->fetchall_hashref etc
Returns an active statement handle you can use to fetch info about foreign keys in and/or referencing the specified table(s) 🔑 The arguments do not accept search patterns (unlike table_info()), so no wildcards, no games, we're being specific here.
$pk_catalog,$pk_schema,$pk_tableidentify the primary (unique) key table (PKT).$fk_catalog,$fk_schema,$fk_tableidentify the foreign key table (FKT).
So like, here's the vibe:
- If you give both PKT and FKT, 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 you give only PKT, then the result set contains the primary key of that table and all foreign keys that refer to it.
- If you give only FKT, 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');
# then $sth->fetchall_arrayref or $sth->fetchall_hashref etc
Note: the support for the selection criteria, like $catalog, is driver specific. If the driver doesn't support catalogs and/or schemas, it may just... ignore these criteria. Rude but true 🙈
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 does. SQL/CLI column names are shown in parentheses 👑
PKTABLE_CAT(UK_TABLE_CAT) — the primary (unique) key table catalog identifier. This field isNULL("undef") 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.NULL("undef") 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.NULL("undef") if not applicable to the data source, which is often the case. Empty if not applicable to the table.FKTABLE_SCHEM(FK_TABLE_SCHEM) — the foreign key table schema identifier.NULL("undef") 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. These 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. Codes are the same as forUPDATE_RULE, so yeah, same energy 💅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. These 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 isUNIQUEif the foreign key references an alternate key andPRIMARYif the foreign key references a primary key, or it may be undefined if the driver doesn't have access to the information. No info, no answer, we respect the mystery 🦋
Also check out "Catalog Methods" and "Standards Reference Information".
⭐ statistics_info ⭐
$sth = $dbh->statistics_info( $catalog, $schema, $table, $unique_only, $quick );
# then $sth->fetchall_arrayref or $sth->fetchall_hashref etc
Returns an active statement handle you can use to fetch statistical information about a table and its indexes 📊 numbers girlies rise up.
The arguments don't accept search patterns (unlike "table_info").
- If the boolean argument
$unique_onlyis true, only UNIQUE indexes will be returned in the result set. Otherwise, all indexes come through. - If the boolean argument
$quickis set, the actual statistical information columns (CARDINALITYandPAGES) will only be returned if they're readily available from the server, and might not be current. Some databases may return stale statistics or no statistics at all with this flag set 👀 so like, be aware, bestie.
The statement handle will return at most one row per column name per index, plus at most one row for the entire table itself, ordered by NON_UNIQUE, TYPE, INDEX_QUALIFIER, INDEX_NAME, and ORDINAL_POSITION. Iconic ordering behavior, we love a deterministic sort 💅
Note: the support for the selection criteria, like $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 — surprises are allowed ✨
TABLE_CAT— the catalog identifier.NULL("undef") if not applicable to the data source, which is often the case. Empty if not applicable to the table.TABLE_SCHEM— the schema identifier.NULL("undef") if not applicable to the data source, and empty if not applicable to the table.TABLE_NAME— the table identifier.NON_UNIQUE— unique index indicator. Returns0for unique indexes,1for non-unique indexes.INDEX_QUALIFIER— index qualifier identifier. This is the identifier used to qualify the index name when doing a"DROP INDEX";NULL("undef") is returned if an index qualifier is not supported by the data source. If a non-NULL(defined) value is returned in this column, it must be used to qualify the index name on a"DROP INDEX"statement; otherwise, theTABLE_SCHEMshould be used to qualify the index name. Read that part twice, it matters 🫶INDEX_NAME— the index identifier.TYPE— the type of information being returned. Can be any of the following values:'table','btree','clustered','content','hashed', or'other'.
In the case that this field is 'table', all fields other than TABLE_CAT, TABLE_SCHEM, TABLE_NAME, TYPE, CARDINALITY, and PAGES will be NULL ("undef"). Everything else just ghosts you 💀
ORDINAL_POSITION— column sequence number (starting with 1).COLUMN_NAME— the column identifier.ASC_OR_DESC— column sort sequence."A"for Ascending,"D"for Descending, orNULL("undef") if not supported for this index.CARDINALITY— cardinality of the table or index. For indexes, this is the number of unique values in the index. For tables, this is the number of rows in the table. If not supported, the value will beNULL("undef").PAGES— number of storage pages used by this table or index. If not supported, the value will beNULL("undef").FILTER_CONDITION— the index filter condition as a string. If the index is not a filtered index, or it cannot be determined whether the index is a filtered index, this value isNULL("undef"). If the index is a filtered index, but the filter condition cannot be determined, this value is the empty string''. Otherwise it'll be the literal filter condition as a string, such as"SALARY <= 4500"🌈
Also: "Catalog Methods" and "Standards Reference Information". Go, read, slay.
🎀 tables 🎀
@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 "$dbh->get_info(29)" returns true (29 is SQL_IDENTIFIER_QUOTE_CHAR) then the table names are constructed and quoted by "quote_identifier" to make sure they're usable even if they contain whitespace or reserved words etc. Which means the table names returned will include quote characters 🎀 that's not a bug, that's the feature, iykyk.
Yeah and heads up: @names = $dbh->tables; with no args? Deprecated. She's old, she's tired, let her rest 🫶
💜 type_info_all 💜
$type_info_all = $dbh->type_info_all;
Returns a reference to an array holding information about each data type variant supported by the database and driver. That array and everything inside it should be treated as read-only — look, don't touch 👀
The first item is a reference to an 'index' hash of "Name => "Index" pairs. The items following that are references to arrays, one per supported data type variant. That leading index hash defines the names and the 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
],
];
Look at that structure. Gorgeous. Structured. Organised. The index hash is literally the blueprint and everything else just follows her lead 👑
More than one row can totes have the same value in the DATA_TYPE field btw — that happens when there are multiple ways to spell the type name and/or there are variants of the type with different attributes (like, with and without AUTO_UNIQUE_VALUE set, with and without UNSIGNED_ATTRIBUTE, etc). So don't panic if you see dupes, it's giving on purpose.
The rows are ordered by DATA_TYPE first, and then by how closely each type maps to the corresponding ODBC SQL data type — closest first, bestie. 💅
The meaning of each field is described in the documentation for the type_info method (scroll down, it's right there 🫶).
An 'index' hash is provided so you don't need to rely on the index values defined above. Iconic convenience behaviour. BUT — using DBD::ODBC with some old ODBC drivers may return older names, which are shown as comments in the example above. Another issue with the index hash: the lettercase of the keys is not defined. It's usually uppercase, as shown 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. Room to grow, we love to see it ✨.
Thetype_info_all()method is not normally used directly. Thetype_infomethod provides a more usable and useful interface to the data. So like, use that one instead bestie, fr fr.
🌈 type_info 🌈
@type_info = $dbh->type_info($data_type);
Returns a list of hash references holding information about one or more variants of $data_type. The list is ordered by DATA_TYPE first and then by how closely each type maps to the corresponding ODBC SQL data type, closest first. If you call it in a scalar context then only the first (best) element is returned — just the bestie, no entourage 💅.
If $data_type is undefined or SQL_ALL_TYPES, then the list will contain hashes for all data type variants supported by the database and driver. Everything. The whole wardrobe 👑
If $data_type is an array reference then type_info returns the information for the first type in the array that has any matches. First match wins, periodt.
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 they may be undef:
TYPE_NAME(string) — Data type name for use inCREATE TABLEstatements etc.DATA_TYPE(integer) — SQL data type number.COLUMN_SIZE(integer) — For numeric types, this is either the total number of digits (if theNUM_PREC_RADIXvalue is10) or the total number of bits allowed in the column (ifNUM_PREC_RADIXis2). 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"'"for characters, or possibly"0x"for binary values passed as hexadecimal. NULL (undef) is returned for data types for which this is not applicable.LITERAL_SUFFIX(string) — Characters used to suffix a literal. Typically"'"for characters. NULL (undef) is returned for data types where this is not applicable.CREATE_PARAMS(string) — Parameter names for data type definition. For example,CREATE_PARAMSfor aDECIMALwould be"precision,scale"if the DECIMAL type should be declared asDECIMAL(precision,scale)where precision and scale are integer values. For aVARCHARit would be"max length". NULL (undef) is returned for data types for which this is not applicable.NULLABLE(integer) — Indicates whether the data type accepts a NULL value:0or an empty string = no,1= yes,2= unknown. (Unknown is so real of it tbh.)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 aWHEREclause, 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 (undef) 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 (undef) 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 (undef) is returned for data types for which this is not applicable.LOCAL_TYPE_NAME(string) — Localized version of theTYPE_NAMEfor use in dialog with users. NULL (undef) is returned if a localized name is not available (in which caseTYPE_NAMEshould be used).MINIMUM_SCALE(integer) — The minimum scale of the data type. If a data type has a fixed scale, thenMAXIMUM_SCALEholds the same value. NULL (undef) 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, thenMINIMUM_SCALEholds the same value. NULL (undef) is returned for data types for which this is not applicable.SQL_DATA_TYPE(integer) — This column is the same as theDATA_TYPEcolumn, except for interval and datetime data types. For interval and datetime data types, theSQL_DATA_TYPEfield will returnSQL_INTERVALorSQL_DATETIME, and theSQL_DATETIME_SUBfield 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 theSQL_DATA_TYPEfield above isSQL_INTERVALorSQL_DATETIME, this field will hold the subcode for the specific interval or datetime data type. Otherwise it will be NULL (undef). 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,NUM_PREC_RADIXcontains the value2andCOLUMN_SIZEholds the number of bits. For exact numeric types,NUM_PREC_RADIXcontains the value10andCOLUMN_SIZEholds the number of decimal digits. NULL (undef) 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 SQL_SMALLINT, SQL_INTEGER, SQL_DECIMAL, etc. Shotgun approach but make it strategic 🎯.
See also "Standards Reference Information".
✨ quote ✨
$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. Bestie, this is your injection-protection moment, do not skip it 😤.
$sql = sprintf "SELECT foo FROM bar WHERE baz = %s",
$dbh->quote("Don't");
For most database types — at least those that conform to SQL standards — quote would return 'Don''t' (including the outer quotation marks). For others it may return something like 'Don\'t'.
An undefined $value value will be returned as the string NULL (without single quotation marks) to match how NULLs are represented in SQL. Undefined in, NULL out, that's the deal 🤝.
If $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 $value without calling type_info. (Number go brrr, no lookup needed ⚡.)
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. Do not mix those two up, I am begging 🙈.
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(12), 'two', CHAR(0), 'three')
The quote() method should not be used with "Placeholders and Bind Values". Use placeholders for those, queen 👑.
💖 quote_identifier 💖
$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. It's the names version of quote, ifykyk 🎀.
Undefined names are ignored and the remainder are quoted and then joined together, typically with a dot (.) character. For example:
$id = $dbh->quote_identifier( undef, 'Her schema', 'My table' );
would, for most database types, return "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 "schema"."table"@"link".
🧟 take_imp_data 🧟
$imp_data = $dbh->take_imp_data;
Leaves the $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 DESTROY will generate a warning and return undef. Yeah. She's a zombie now. 🧟♀️
Why would you want to do this? You don't, forget I even mentioned it. Unless, that is, you're implementing something advanced like a multi-threaded connection pool like DBI::Pool. (If you're doing that, you're the main character and I respect it 💜.)
okay so that $imp_data you just pulled out? You can pass it as a dbi_imp_data attribute to a later connect() call — even in a separate thread in the same process — and over there the driver can use it to 'adopt' the existing connection that the implementation data was taken from. slay 🫶
Some things to keep in mind, bestie (this part matters)…
- the
$imp_dataholds the only reference to the underlying database API connection data. That connection is still 'live' and won't be cleaned up properly unless the$imp_datais used to create a new$dbhwhich is then allowed todisconnect()normally. 💀 - using the same
$imp_datato create more than one other new$dbhat 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. no cap 😭
ngl, the take_imp_data method was added in DBI 1.36 but wasn't useful till 1.49. iykyk ✨
🎀 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. they mind their own business, periodt.
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, cute).
Example:
$h->{AutoCommit} = ...; # set/write
... = $h->{AutoCommit}; # get/read
AutoCommit
Type: boolean
If true, then database changes cannot be rolled-back (undone). If false, then database changes automatically occur within a "transaction", which must either be committed or rolled back using the commit or rollback methods.
Drivers should always default to AutoCommit mode (an unfortunate choice largely forced on the DBI by ODBC and JDBC conventions. 😭)
Attempting to set AutoCommit to an unsupported value is a fatal error. This is an important feature of the DBI. Applications that need full transaction behaviour can set $dbh->{AutoCommit} = 0 (or set AutoCommit to 0 via connect) without having to check that the value was assigned successfully. iconic, honestly ✨
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
AutoCommitoff is a fatal error.commitandrollbackboth issue warnings about being ineffective whileAutoCommitis in effect. - Databases in which a transaction is always active — These are typically mainstream commercial relational databases with "ANSI standard" transaction behaviour. If
AutoCommitis off, then changes to the database won't have any lasting effect unlesscommitis called (but see alsodisconnect). Ifrollbackis called then any changes since the last commit are undone.
If AutoCommit is on, then the effect is the same as if the DBI called commit automatically after every successful database operation. So calling commit or rollback explicitly while AutoCommit is on would be ineffective because the changes would have already been committed.
Changing AutoCommit 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 COMMIT after it completes successfully (and roll it back using an explicit ROLLBACK 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 AutoCommit 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.
Driver
Type: handle
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}
Name
Type: string
Holds the name of the database. Usually (and recommended to be) the same as the "dbi:DriverName:..." string used to connect to the database, but with the leading "dbi:DriverName:" removed.
Statement
Type: string, read-only
Returns the statement string passed to the most recent prepare or do method called in this database handle, even if that method failed. This is especially useful where RaiseError is enabled and the exception handler checks $@ and sees that a 'prepare' method call failed. 🕵️♀️
RowCacheSize
Type: integer
A hint to the driver indicating the size of the local row cache that the application would like the driver to use for future SELECT statements. If a row cache is not implemented, then setting RowCacheSize is ignored and getting the value returns undef.
Some RowCacheSize 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.
Username
Type: string
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:
bind_param
$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 bind_param method takes a copy of $bind_value and associates it (binds it 💅) with a placeholder, identified by $p_num, embedded in the prepared statement. Placeholders are indicated with question mark character (?). 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 \%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 \%attr hash reference. This example is equivalent to the one above:
$sth->bind_param(1, $value, SQL_INTEGER);
The TYPE 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 { 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 is 'sticky' in that bind values passed to execute() are bound with the data type specified by earlier bind_param() calls, if any. Portable applications should not rely on being able to change the data type after the first bind_param call.
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 $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 bind_param call, you can let the driver pass the value as the default type (VARCHAR). 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 CONVERT 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.
bind_param_inout
$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 $bind_value must be passed as a reference to the actual value to be used.
Note that unlike bind_param, the $bind_value variable is not copied when bind_param_inout is called. Instead, the value in the variable is read at the time execute is called. 💅
The additional $max_len parameter specifies the minimum amount of memory to allocate to $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 undef are used to indicate null values. See also Placeholders and Bind Values for more information.
bind_param_array
$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 bind_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 %attr ($bind_type) argument is literally the same one we defined back in "bind_param" 💫 — go peek at "bind_param" for the general tea on how placeholders work.
okay real talk bestie, big disclaimer:bind_param_array()can NOT be used to expand a placeholder into a list of values for something like"SELECT foo WHERE bar IN (?)". A placeholder can only ever represent one value per execution. That's just how it is. Periodt. 💅
Scalar values — including undef — can also be bound by "bind_param_array". When you do that, the same value gets used for every "execute" call. Driver-specific implementations may behave differently tho, e.g. when binding to a stored procedure call, some databases might let you mix scalars and arrays as arguments. 👀
The default implementation that DBI provides (for drivers that haven't implemented array binding, rip) is to iteratively call "execute" for each parameter tuple provided in the bound arrays. Drivers can provide more optimized implementations using whatever bulk-operation support their database API gives them ✨. The default driver behaviour should match the default DBI behaviour, but always go read your driver documentation because there may be driver-specific issues lurking 🫣.
Note that the default implementation currently only supports non-data-returning statements (so INSERT, UPDATE, but not SELECT). Also, "bind_param_array" and "bind_param" cannot be mixed in the same statement execution 😭, and "bind_param_array" must be used with "execute_array"; using "bind_param_array" will have no effect for "execute".
The "bind_param_array" method was added in DBI 1.22. 🎀
✨ execute ✨
$rv = $sth->execute or die $sth->errstr;
$rv = $sth->execute(@bind_values) or die $sth->errstr;
Do whatever processing is necessary to actually run the prepared statement 🏃♀️. An undef comes back if something goes wrong. A successful "execute" always returns true, no matter how many rows were affected — even if it's zero (more on that below 👇). It is always important to check the return status of "execute" (and most other DBI methods, ngl) for errors if you're not using "RaiseError".
For a non-SELECT statement, "execute" returns the number of rows affected, if known. If no rows were affected, then "execute" returns "0E0", which Perl will treat as 0 but will regard as true. So heads up: it is not an error for no rows to be affected by a statement 🫶. If the number of rows affected is not known, then "execute" returns -1.
For SELECT statements, execute just simply "starts" the query inside the database engine 🌸. Use one of the fetch methods to grab the data after calling "execute". The "execute" method does not return the number of rows that will be returned by the query (because most databases can't tell in advance, they're not psychic bestie 🔮), it simply returns a true value.
You can tell if the statement was a SELECT statement by checking if $sth->{NUM_OF_FIELDS} is greater than zero after calling "execute". Slay 🔥.
If any arguments are given, then "execute" will effectively call "bind_param" for each value before executing the statement. Values bound this way are usually treated as "SQL_VARCHAR" types unless the driver can determine the correct type (which is rare 🙈), or unless "bind_param" (or "bind_param_inout") has already been used to specify the type.
⚠️ watch out: passing "execute" an empty array is the same as passing no arguments at all, which will execute the statement with previously bound values. That's probably not what you want, bestie. 💀
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. Clean up after yourself 💅.
✨ execute_array ✨
$tuples = $sth->execute_array(\%attr) or die $sth->errstr;
$tuples = $sth->execute_array(\%attr, @bind_values) or die $sth->errstr;
($tuples, $rows) = $sth->execute_array(\%attr) or die $sth->errstr;
($tuples, $rows) = $sth->execute_array(\%attr, @bind_values) or die $sth->errstr;
Execute the prepared statement once for each parameter tuple (aka each group of values) provided either in the @bind_values, or by prior calls to "bind_param_array", or via a reference passed in \%attr. So many options, we love a flexible queen 👑.
When called in scalar context, the execute_array() method returns the number of tuples executed, or undef if an error occurred. Like execute(), a successful execute_array() always returns true regardless of the number of tuples executed, even if it's zero 💖. If there were any errors, the ArrayTupleStatus array can be used to discover which tuples failed and with what errors. Detective mode 🕵️♀️.
When called in list context, the execute_array() method returns two scalars; $tuples is the same as calling execute_array() in scalar context, and $rows is the number of rows affected for each tuple, if available — or -1 if the driver cannot determine this. NOTE: some drivers cannot determine the number of rows affected per tuple but can provide the number of rows affected for the batch 🤷♀️. If you're doing an update operation, the returned rows affected may not be what you expect if, for instance, one or more of the tuples affected the same row multiple times (the way that math just stops mathing 😭). Some drivers may not yet support list context, in which case $rows will be undef, or may not be able to provide the number of rows affected when performing this batch operation, in which case $rows will be -1.
Bind values for the tuples to be executed may be supplied row-wise by an "ArrayTupleFetch" attribute, or else column-wise in the @bind_values argument, or else column-wise by prior calls to "bind_param_array".
Where column-wise binding is used (via the @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's treated as a variable length array with all elements having the same value 🦋. It 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 "ArrayTupleFetch" attribute can be used to specify a reference to a subroutine that will be called to provide the bind values for each tuple execution. That subroutine should return a reference to an array which contains the appropriate number of bind values, or return an undef if there's no more data to execute 👋.
As a lil convenience 🎀, the "ArrayTupleFetch" 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 @bind_values parameter may be either scalars or arrayrefs. If any @bind_values are given, then "execute_array" will effectively call "bind_param_array" for each value before executing the statement. Values bound in this way are usually treated as "SQL_VARCHAR" types unless the driver can determine the correct type (which is rare), or unless "bind_param", "bind_param_inout", "bind_param_array", or "bind_param_inout_array" has already been used to specify the type. See "bind_param_array" for details ✨.
The "ArrayTupleStatus" attribute can be used to specify a reference to an array which will receive the execute status of each executed parameter tuple. Note the "ArrayTupleStatus" attribute was mandatory until DBI 1.38.
For tuples which are successfully executed, the element at the same ordinal position in the status array is the resulting rowcount (or -1 if unknown). 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 "err", "errstr" and "state" set by the failed execution 📋.
If any tuple execution returns an error, "execute_array" will return undef. 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, "execute_array" returns the number of tuples executed 🎉. If no tuples were executed, then execute_array() returns "0E0", 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"];
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 "AutoCommit" is on, the default DBI implementation will cause each parameter tuple to be individually committed (or rolled back in the event of an error) 🙈. If "AutoCommit" 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 — no guessing, bestie, read the docs 💅.
Note that, in general, performance will usually be better with "AutoCommit" turned off, and using explicit "commit" after each "execute_array" call 🚀.
The execute_array method dropped in DBI 1.22, and ArrayTupleFetch showed up in 1.36. ✨
🌈 execute_for_fetch — the bulk-operation bestie
$tuples = $sth->execute_for_fetch($fetch_tuple_sub);
$tuples = $sth->execute_for_fetch($fetch_tuple_sub, \@tuple_status);
($tuples, $rows) = $sth->execute_for_fetch($fetch_tuple_sub);
($tuples, $rows) = $sth->execute_for_fetch($fetch_tuple_sub, \@tuple_status);
okay so execute_for_fetch() is your girl for doing bulk operations, and even though she usually gets called through execute_array(), you can absolutely just call her directly, no middleman, she's fine with it 💅
the big difference between execute_array and execute_for_fetch? execute_array does column- or row-wise binding, while execute_for_fetch keeps it strictly row-wise. periodt.
the fetch subroutine that $fetch_tuple_sub points at is supposed to hand back a reference to an array (we call that a 'tuple', adorable) or undef.
execute_for_fetch() keeps calling $fetch_tuple_sub — with no parameters at all, bestie, zero args — until it returns a false value. every tuple it gets back is used to supply bind values for an $sth->execute(@$tuple) call. 🎀
In scalar context, execute_for_fetch() returns "undef" if there were any errors, and otherwise the number of tuples executed. just like execute() and execute_array(), a zero comes back as "0E0", so execute_for_fetch() is only false on error — that's the whole vibe check. and if there were errors, the @tuple_status array is how you go find out which tuples failed and what their deal was. 🔍
When called in list context, execute_for_fetch() hands you two scalars; $tuples is the same thing you'd get in scalar context, and $rows is the sum of the number of rows affected for each tuple, if that's available — or -1 if the driver can't figure it out. If you're doing an update operation ngl the returned rows affected may not be what you expect, like if one or more of the tuples affected the same row multiple times, that's just how it is 😭. Some drivers may not yet support list context, in which case $rows will be undef, or they may not be able to provide the number of rows affected when doing this batch operation, in which case $rows will be -1.
If @tuple_status gets passed in, then execute_for_fetch uses it to return status info. The tuple_status array holds one element per tuple. If the corresponding execute() didn't fail then that 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, $sth->errstr, $sth->state). 🫶
If the driver detects an error that it knows means no further tuples can be executed, then it may return — with an error status — even though $fetch_tuple_sub may still have more tuples waiting to be executed. yeah. it just bails. iconic honestly.
Even though each tuple returned by $fetch_tuple_sub is effectively used to call $sth->execute(@$tuple_array_ref), the exact timing may vary. Drivers are free to accumulate sets of tuples and pass them to the database server in bulk group operations for more efficient execution. However, $fetch_tuple_sub is specifically allowed to return the same array reference each time (which is literally what fetchrow_arrayref() usually does, she's a repeat-offender 💀).
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 "execute_for_fetch" method was added in DBI 1.38. ⭐
🌸 last_insert_id — who even was she (the last row, I mean)
$rv = $sth->last_insert_id();
$rv = $sth->last_insert_id($catalog, $schema, $table, $field);
$rv = $sth->last_insert_id($catalog, $schema, $table, $field, \%attr);
Returns a value that 'identifies' the row inserted by the last execution of the statement $sth, if possible. note the air quotes, bestie, because "identifying" is doing a lot of work there 🙈
For some drivers the value may be 'identifying' the row inserted by the last executed statement, not by $sth. so like. read that again. 📖
See the database handle method last_insert_id for all the details (she's the main character, this one's just the statement-handle version).
The "last_insert_id" statement method was added in DBI 1.642. ✨
🦋 fetchrow_arrayref — speedy girl
$ary_ref = $sth->fetchrow_arrayref;
$ary_ref = $sth->fetch; # alias
Fetches the next row of data and returns a reference to an array holding the field values. Null fields come back as "undef" values in the array. This is the fastest way to fetch data, especially if you're using it with "$sth->bind_columns". like, no cap, fastest. 🔥
If there are no more rows, or if an error occurs, then "fetchrow_arrayref" returns an "undef". You should check "$sth->err" afterwards (or just use the "RaiseError" attribute) to find out whether that "undef" was because of an error or just because you're done.
⚠️ ok so heads up: the same array reference is returned for each fetch, so do not store the reference and then go use it after a later fetch. also the elements of the array are reused for each row too, so be careful if you want to take a reference to an element. see also "bind_columns". 💀
🧚 fetchrow_array — the listified version
@ary = $sth->fetchrow_array;
An alternative to "fetchrow_arrayref". Fetches the next row of data and returns it as a list containing the field values. Null fields are returned as "undef" values in the list. 🍓
If there are no more rows, or if an error occurs, then "fetchrow_array" returns an empty list. You should check "$sth->err" afterwards (or use the "RaiseError" attribute) to find out whether that empty list was because of an error.
If you call it in a scalar context on 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 just. don't do that. 🙅 Also, in scalar context an "undef" is returned if there are no more rows or if an error occurred. And that "undef" can't be distinguished from an "undef" that was returned because the first field value was NULL. for these reasons you should exercise some caution if you use "fetchrow_array" in a scalar context. consider this your gentle warning bestie 💜
🎀 fetchrow_hashref — the hash one
$hash_ref = $sth->fetchrow_hashref;
$hash_ref = $sth->fetchrow_hashref($name);
Another alternative to "fetchrow_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 "undef" values in the hash. 🌈
If there are no more rows or if an error occurs, then "fetchrow_hashref" returns an "undef". You should check "$sth->err" afterwards (or use the "RaiseError" attribute) to discover if the "undef" returned was due to an error.
The optional $name parameter specifies the name of the statement handle attribute. For historical reasons it defaults to ""NAME"", however using either ""NAME_lc"" or ""NAME_uc"" is recommended for portability. 🫶
The keys of the hash are the same names returned by "$sth->{$name}". If more than one field has the same name, there will only be one entry in the returned hash for those fields, so statements like ""select foo, foo from bar"" will return only a single key from "fetchrow_hashref". In these cases, use column aliases or "fetchrow_arrayref". Note that it is the database server (and not the DBD implementation) which provides the name for fields containing functions like "count(*)" or "max(c_foo)", and they may clash with existing column names (most databases don't care about duplicate column names in a result-set). If you want these to return as unique names that are the same across databases, use aliases, as in ""select count(*) as cnt"" or ""select max(c_foo) mx_foo, ..."" depending on the syntax your database supports. 😭
Because of the extra work "fetchrow_hashref" and Perl have to perform, it is not as efficient as "fetchrow_arrayref" or "fetchrow_array". she's cute but she's high-maintenance. 💅
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. ⭐
👑 fetchall_arrayref — get the whole table, queen
$tbl_ary_ref = $sth->fetchall_arrayref;
$tbl_ary_ref = $sth->fetchall_arrayref( $slice );
$tbl_ary_ref = $sth->fetchall_arrayref( $slice, $max_rows );
The "fetchall_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 called on an inactive statement handle, "fetchall_arrayref" returns undef.
If there are no rows left to return from an active statement handle, "fetchall_arrayref" returns a reference to an empty array. If an error occurs, "fetchall_arrayref" returns the data fetched thus far, which may be none. You should check "$sth->err" afterwards (or use the "RaiseError" attribute) to discover if the data is complete or was truncated due to an error. 🔍
If $slice is an array reference, "fetchall_arrayref" uses "fetchrow_arrayref" to fetch each row as an array ref. If the $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). yes that's the classic off-by-one moment, stay woke 😤
With no parameters, or if $slice is undefined, "fetchall_arrayref" acts as if passed an empty array ref.
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]);
Those two examples both return a reference to an array of array refs. 🎀
If $slice is a hash reference, "fetchall_arrayref" fetches each row as a hash reference. If the $slice hash is empty then the keys in the hashes have whatever name lettercase is returned by default. (See "FetchHashKeyName" attribute.) If the $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 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", regardless of the original capitalization):
$tbl_ary_ref = $sth->fetchall_arrayref({ foo=>1, BAR=>1 });
Those two examples both return a reference to an array of hash refs. ✨
If $slice is a reference to a hash reference, that hash is used to select and rename columns. The keys are 0-based column index numbers and the values are the corresponding keys for the returned row hashes. 🧚
For example, say you want to grab only the first and second column of every single row as a hash ref — with keys named "k" and "v" no matter what their original names were. Like this:
$tbl_ary_ref = $sth->fetchall_arrayref( \{ 0 => 'k', 1 => 'v' } );
If $max_rows is defined and greater than or equal to zero, then bestie, it gets used to cap how many rows get fetched before the call hands control back to you. And then? You can just call fetchall_arrayref() again to pull more rows. 💅 This is a whole moment when you need that fetchall_arrayref() performance glow-up but your memory is not serving enough space to fetch and return every row in one go.
Here's the vibe (and it assumes RaiseError is enabled):
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 might lowkey be the fastest way to fetch and process lots of rows using the DBI… but it really depends on the relative cost of method calls vs memory allocation. It's giving ✨tradeoffs✨.
A standard "while" loop with column binding is often faster, because the cost of allocating memory for the whole batch of rows is greater than the saving you get by reducing method calls. And it's possible that the DBI may someday provide a way to reuse the memory of a previous batch, which would then shift the balance back towards fetchall_arrayref(). iykyk.
🌸 fetchall_hashref 🌸
$hash_ref = $sth->fetchall_hashref($key_field);
The fetchall_hashref method is your girl for fetching all the data that's gonna come back from a prepared and executed statement handle. It returns a reference to a hash with a key for each distinct value of the $key_field column that was fetched. And for each one of those keys, the value is a reference to a hash holding all the selected columns and their values, exactly as fetchrow_hashref() would have returned them. 🦋
If there are no rows to return, fetchall_hashref hands you back a reference to an empty hash. If an error happens, fetchall_hashref returns the data fetched thus far, which might be none at all. So you should check $sth->err afterwards (or just use the RaiseError attribute) to find out whether your data is complete or got truncated because of an error. 👀
The $key_field parameter gives the name of the field holding the value that becomes the key for the returned hash. Example time:
$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 $key_field parameter can also be specified as an integer column number, counting from 1. And if $key_field doesn't match any column in the statement — first checked as a name, then as a number — then an error gets returned. No cap. 🔥
For queries that return more than one 'key' column, you can specify multiple column names by passing $key_field as a reference to an array containing one or more key column names (or index numbers). Like so:
$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 field values for each row are unique. Because if multiple rows come back with the same values for the key fields, then later rows overwrite earlier ones. Which is… a choice, but it's the documented one. 🙈
💀 more_results 💀
... not yet documented ...
🎀 finish 🎀
$rc = $sth->finish;
This one signals that no more data will be fetched from this statement handle before it either gets executed again or destroyed. And ngl, you almost certainly do not need to call this method. Periodt.
Adding calls to finish after a loop that fetches all rows is a common mistake — don't do it, because it can mask genuine problems like uncaught fetch errors. 🚩
When all the data has been fetched from a SELECT statement, the driver will automatically call finish for you. So you shouldn't call it explicitly except when you know you haven't fetched all the data from a statement handle and that handle won't be destroyed soon.
The most common example of that is when you only want to fetch just one row — but honestly, in that case the selectrow_* methods are usually better anyway. ✨
Consider a query like:
SELECT foo FROM table WHERE bar=? ORDER BY baz
…on a very large table. When it's executed, the database server has to use temporary buffer space to store the sorted rows. So if, after executing the handle and selecting just a few rows, the handle won't be re-executed for some time and won't be destroyed, the finish method can be used to tell the server that the buffer space can be freed. 🫶
Calling finish resets the Active attribute for the statement. It may also make some statement handle attributes (such as NAME and TYPE) unavailable — that happens if they haven't already been accessed (and thus cached). 💅
The finish 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 finish method should have been called "discard_pending_rows". 😭 iconic naming fail fr fr.
🌈 rows 🌈
$rv = $sth->rows;
Returns the number of rows affected by the last row-affecting command, or -1 if the number of rows isn't known or isn't available.
Generally, you can only rely on a row count after a non-"SELECT" "execute" (for some specific operations like "UPDATE" and "DELETE"), or after fetching all the rows of a "SELECT" statement.
For "SELECT" statements, it's 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 using the "rows" method or $DBI::rows with "SELECT" statements is not recommended. 🚫
One alternative method to get a row count for a "SELECT" is to execute a "SELECT COUNT(*) FROM ..." SQL statement with the same "..." as your query, and then fetch the row count from that. Slay, simple, effective. ✨
💖 bind_col 💖
$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 "SELECT" statement. Column numbers count up from 1. And heads up bestie: 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 "bind_columns" for an example. 🦋
The binding gets performed at a low level using Perl aliasing. Whenever a row is fetched from the database, $var_to_bind appears to be automatically updated — simply because it now refers to the same memory location as the corresponding column value. Which 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 \%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 $var_to_bind in that example to emphasize the point that bind_col() works on the underlying column and not just on a particular bound variable. 👑
As a short-cut for the common case, the data type can be passed directly, in place of the \%attr hash reference. This example is equivalent to the one above:
$sth->bind_col(1, undef, SQL_DATETIME);
The "TYPE" 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 { 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.
Few drivers support specifying a data type via a "bind_col" call (most will simply ignore the data type). Fewer still allow the data type to be altered once set. If you do set a column type, the type should remain sticky through further calls to bind_col for the same column — that is, if the type isn't overridden. (This is important, for instance, when you're using a slice in fetchall_arrayref.) 💅
The TYPE attribute for bind_col() was first specified in DBI 1.41.
From DBI 1.611, drivers can use the "TYPE" attribute to attempt to cast the bound scalar to a perl type which more closely matches "TYPE". At present DBI supports "SQL_INTEGER", "SQL_DOUBLE" and "SQL_NUMERIC". See "sql_type_cast" for details of how types are cast. ✨
🍓 Other attributes for Column Binding 🍓
The \%attr parameter may also contain the following attributes:
💜 StrictlyTyped 💜
If a "TYPE" attribute is passed to bind_col, then the driver will attempt to change the bound perl scalar to match the type more closely. If the bound value cannot be cast to the requested "TYPE", then by default it's left untouched and no error is generated. If you specify "StrictlyTyped" as 1 and the cast fails, this will generate an error. ⚠️
This attribute was first added in DBI 1.611. When 1.611 was released, few drivers actually supported this attribute, but DBD::Oracle and DBD::ODBC should from versions 1.24.
🌸 DiscardString 🌸
When the "TYPE" attribute is passed to "bind_col" and the driver successfully casts the bound perl scalar to a non-string type, then if "DiscardString" is set to 1, the string portion of the scalar will be discarded. By default, "DiscardString" is not set.
This attribute was first added in DBI 1.611. When 1.611 was released, few drivers actually supported this attribute, but DBD::Oracle and DBD::ODBC should from versions 1.24.
🌈 bind_columns 🌈
$rc = $sth->bind_columns(@list_of_refs_to_vars_to_bind);
Calls "bind_col" for each column of the "SELECT" statement. 🫶
The list of references should have the same number of elements as the number of columns in the "SELECT" statement. If it doesn't, then "bind_columns" will bind the elements given, up to the number of columns, and then return an error. 💀
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";
}
okay so for the girlies running ancient scripts: that very first parameter gets straight-up ignored if it's undef or a hash reference. backwards compat, she's iconic, we love her 💅
Here's a fancier example that binds columns to the values inside a hash (shoutout to H.Merijn Brand, king behavior 👑):
$sth->execute;
my %row;
$sth->bind_columns (\( @row{ @{$sth->{NAME_lc} }} ));
while ($sth->fetch) {
print "$row{region}: $row{sales}\n";
}
but it has a tiny little drawback bestie: if the data has already been fetched, the call to bind_columns will flush current values 😭. So if you wanna bind_columns after you've already fetched, you can do this:
use feature "refaliasing";
no warnings "experimental::refaliasing";
while (my $row = $sth->fetchrow_arrayref) {
\(@$data{ $sth->{NAME_lc}->@* }) = \(@$row);
}
or, for the perl girlies on older versions:
use Data::Alias;
alias @$data{ $sth->{NAME_lc}->@* } = @$row;
This one's genuinely useful when you have a million left joins but only want to join your %$data hash to a subset of the fetched values. no cap ✨
🗑️ dump_results 🗑️
$rows = $sth->dump_results($maxlen, $lsep, $fsep, $fh);
Fetches all the rows from $sth, calls DBI::neat_list on each row, and prints the results to $fh (which defaults to STDOUT) separated by $lsep (default "\n"). $fsep defaults to ", " and $maxlen defaults to 35.
This method is literally designed as a handy little utility for prototyping and testing queries 🧪. And because it uses neat_list to format and edit the string for human eyeballs, it is NOT recommended for data transfer applications, ok? it's giving debug-only 🫶
✨ Statement Handle Attributes ✨
This section is all about the attributes that are specific to statement handles. Most of these are read-only</> — take the hint 💅
Changes to these statement handle attributes do not affect any other existing or future statement handles. ur fave is unique, periodt.
Tryna set or get the value of an unknown attribute? That generates a warning ⚠️ — unless it's a private driver-specific attribute (those all have names starting with a lowercase letter, iykyk).
Example:
... = $h->{NUM_OF_FIELDS}; # get/read
Some drivers can't provide valid values for some or all of these attributes until after $sth->execute has been successfully called. Typically the attribute will be undef in these situations. so like, patience bestie 🦋
Some attributes, like NAME, are not appropriate to some types of statement, like SELECT. Typically the attribute will be undef in these situations.
For drivers which support stored procedures and multiple result sets (see "more_results") these attributes relate to the current result set.
See also "finish" to learn more about the effect it may have on some attributes 👀
🎀 NUM_OF_FIELDS 🎀
Type: integer, read-only
Number of fields (columns) in the data the prepared statement may return. Statements that don't return rows of data, like DELETE and CREATE, set NUM_OF_FIELDS to 0 (though it may be undef in some drivers 🙈).
🎀 NUM_OF_PARAMS 🎀
Type: integer, read-only
The number of parameters (placeholders) in the prepared statement. See SUBSTITUTION VARIABLES below for more details.
🎀 NAME 🎀
Type: array-ref, read-only
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. So portable applications should use NAME_lc or NAME_uc.
print "First column name: $sth->{NAME}->[0]\n";
Also note that the name returned for (aggregate) functions like count(*) or max(c_foo) is determined by the database server and not by DBI or the DBD backend. not me blaming the driver for something the server did 😭
🎀 NAME_lc 🎀
Type: array-ref, read-only
Like /NAME but always returns lowercase names.
🎀 NAME_uc 🎀
Type: array-ref, read-only
Like /NAME but always returns uppercase names.
🎀 NAME_hash 🎀
Type: hash-ref, read-only
🎀 NAME_lc_hash 🎀
Type: hash-ref, read-only
🎀 NAME_uc_hash 🎀
Type: hash-ref, read-only
The NAME_hash, NAME_lc_hash, and NAME_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 NAME, NAME_lc, and NAME_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";
🎀 TYPE 🎀
Type: array-ref, read-only
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 TYPE should have at least one entry in the output of the type_info_all method (see "type_info_all"). slay 💅
🎀 PRECISION 🎀
Type: array-ref, read-only
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). math is mathing 🔥
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. tradition is tradition 💖)
🎀 SCALE 🎀
Type: array-ref, read-only
Returns a reference to an array of integer values for each column. NULL (undef) values indicate columns where scale is not applicable.
🎀 NULLABLE 🎀
Type: array-ref, read-only
Returns a reference to an array indicating the possibility of each column returning a null. Possible values are 0 (or an empty string) = no, 1 = yes, 2 = unknown 🤷
print "First column may return NULL\n" if $sth->{NULLABLE}->[0];
🎀 CursorName 🎀
Type: string, read-only
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 "where current of ..." SQL syntax, then it returns undef. ghosted by the cursor fr 👻
🎀 Database 🎀
Type: dbh, read-only
Returns the parent $dbh of the statement handle. it's giving family tree 🌳
🎀 Statement 🎀
Type: string, read-only
Returns the statement string passed to the prepare method.
🎀 ParamValues 🎀
Type: hash ref, read-only
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.
Keys:
If the driver supports ParamValues 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 because they can't pre-determine the names. drivers being mysterious, we stan 🫶
It is possible that the keys in the hash returned by ParamValues are not exactly the same as those implied by the prepared statement. For example, DBD::Oracle translates "?" placeholders into ":pN" where N is a sequence number starting at 1 with a hard limit of 99999. noted 📝
Values:
It is possible that the values in the hash returned by ParamValues 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 ParamValues can be passed to another bind_param() method with the same TYPE and will be seen by the database as the same value. See also "ParamTypes" below.
The ParamValues attribute was added in DBI 1.28 ✨
🎀 ParamTypes 🎀
Type: hash ref, read-only
Returns a reference to a hash containing the type information currently bound to placeholders. Returns undef if not supported by the driver.
Keys:
See "ParamValues" above.
Values:
The hash values are hashrefs of type information in the same form as that passed to the various bind_param() methods (See "bind_param" for the format and values).
It is possible that the values in the hash returned by ParamTypes are not exactly the same as those passed to bind_param() or execute(). Param attributes specified using the abbreviated form, like this:
$sth->bind_param(1, SQL_INTEGER);
are returned in the expanded form, as if called like this:
$sth->bind_param(1, { TYPE => SQL_INTEGER });
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. The driver may also add private keys (with names beginning with the drivers reserved prefix, e.g., odbc_xxx). sneaky but valid 💅
Example:
The keys and values in the returned hash can be passed to the various bind_param() methods to effectively reproduce a previous param binding. For example:
# assuming $sth1 is a previously prepared statement handle
my $sth2 = $dbh->prepare( $sth1->{Statement} );
my $ParamValues = $sth1->{ParamValues} || {};
my $ParamTypes = $sth1->{ParamTypes} || {};
$sth2->bind_param($_, $ParamValues->{$_}, $ParamTypes->{$_})
for keys %{ {%$ParamValues, %$ParamTypes} };
$sth2->execute();
look at that, copying ur whole vibe from one statement handle to another 💖
The <code>ParamTypes</code> attribute was added in DBI 1.49. And bestie, here's the tea: making it Actually Do Something is on the individual drivers, not on DBI itself. The DBI layer's default implementation is literally just "idk, here's undef". 🫠 So if you're expecting magic from the core layer alone, you are not getting it, periodt.
✨ ParamArrays ✨
Type: hash ref, read-only 🔒
It hands you back a reference to a hash stuffed with the values currently bound to your placeholders via execute_array or bind_param_array. The keys of that hash are the 'names' of the placeholders — usually integers kicking off at 1. If your driver doesn't do this, or you've got no arrays of parameters bound at all, you just get undef. 💀
Each key's value is an array ref holding the list of bound parameters for that column. Slay.
Like, for example:
$sth = $dbh->prepare("INSERT INTO staff (id, name) values (?,?)");
$sth->execute_array({},[1,2], ['fred','dave']);
if ($sth->{ParamArrays}) {
foreach $param (keys %{$sth->{ParamArrays}}) {
printf "Parameters for %s : %s\n", $param,
join(",", @{$sth->{ParamArrays}->{$param}});
}
}
Real talk though: the values in that hash might not be exactly the same as what you passed to bind_param_array or execute_array. Your driver may have gently tweaked values based on the TYPE they were bound with — like, a floating point value bound as an SQL_INTEGER type could come back as an integer. That's just how it is. 🤷
And also — the keys in that hash might not be exactly the same as what the prepared statement implies either. For instance, DBD::Oracle translates "?" placeholders into ":pN" where N is a sequence number starting at 1, and it's got a hard limit of 99999. So. Yeah. 👀
✨ RowsInCache ✨
Type: integer, read-only 🔒
If your driver keeps a local row cache for SELECT statements, this attribute holds the number of un-fetched rows chilling in that cache. If the driver doesn't do the cache thing? You get undef. Simple as.
⚠️ Note that some drivers pre-fetch rows on execute, whereas others wait till the first fetch. So timing matters, bestie.
See also the "RowCacheSize" database handle attribute. 🫶
🌈 FURTHER INFORMATION 🌈
🎀 Catalog Methods 🎀
So an application can go grab metadata info from the DBMS by firing off the appropriate queries at the views of the Information Schema. Cool, right? Except… INFORMATION_SCHEMA views are seldom supported by the DBMS. 💀 Tragic. So instead, special methods — the catalog methods — exist to hand you result sets for a small but very important slice of that metadata:
column_info
foreign_key_info
primary_key_info
table_info
statistics_info
All of these accept arguments to restrict the result sets. Here's the vibe:
- Pass
undefto an optional argument and it doesn't constrain the search for that argument. - But an empty string (
'') is treated as a regular search criteria and will only match an empty value. Not the same thing, bestie!
📌 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 only take ordinary values — like the arguments of primary_key_info(). Those get treated as a literal string, meaning the case is significant and quote characters are taken literally. No games.
Some arguments do take search patterns (strings containing '_' and/or '%'), like the $table argument of column_info(). And passing '%' is equivalent to just leaving the argument undef. Same energy. ✨
🚨 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! Not a vibe. To include pattern characters as literals, they must be preceded by an escape character, which you can achieve with:
$esc = $dbh->get_info( 14 ); # SQL_SEARCH_PATTERN_ESCAPE
$search_pattern =~ s/([_%])/$esc$1/g;
The ODBC and SQL/CLI specs define a way to change that default behaviour: all arguments (except list value arguments) are treated as identifier if the "SQL_ATTR_METADATA_ID" attribute is set to "SQL_TRUE". Quoted identifiers are very similar to ordinary values — their body (the string within the quotes) gets interpreted literally. Unquoted identifiers, though? Compared in UPPERCASE. 😤
And the DBI (currently) does not support the "SQL_ATTR_METADATA_ID" attribute — it behaves like an ODBC driver where "SQL_ATTR_METADATA_ID" is set to "SQL_FALSE". That's the whole deal. 👑
💖 Transactions 💖
Transactions are a fundamental part of any robust database system, no cap. 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. It's giving main character energy for data integrity. ✨
This section applies to databases that support transactions and where "AutoCommit" is off. See "AutoCommit" for the deets on using it with various types of databases.
The recommended way to implement robust transactions in Perl applications is to enable "RaiseError" and catch the error that's 'thrown' as an exception. Like, for example, using Try::Tiny:
use Try::Tiny;
$dbh->{AutoCommit} = 0; # enable transactions, if possible
$dbh->{RaiseError} = 1;
try {
foo(...) # do lots of work here
bar(...) # including inserts
baz(...) # and updates
$dbh->commit; # commit the changes if we get this far
} catch {
warn "Transaction aborted because $_"; # Try::Tiny copies $@ into $_
# 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 "RaiseError" attribute is not set, then your DBI calls need to be manually checked for errors, typically like this:
$h->method(@args) or die $h->errstr;
With "RaiseError" set, the DBI will automatically die if any DBI method call on that handle (or a child handle) fails, so you don't have to go test the return value of every single method call. Obsessed. 😌 See "RaiseError" for more details.
A major advantage of the eval 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 $h->{RaiseError} attribute is that all DBI calls get checked automatically. Both techniques are strongly recommended. Do both. Slay. 🫶
⚠️ After callingcommitorrollback, many drivers will not let you fetch from a previously activeSELECTstatement handle that's a child of the same database handle. A typical way round this is to connect to the database twice and use one connection forSELECTstatements. yeah it's extra, but it works. 💅
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. And bestie, some databases support variable length long values over 2,000,000,000 bytes in length. That's... a lot of bytes. 😭
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 SELECT statement (unlike other data types), some special handling is required.
In this situation, the value of the $h->{LongReadLen} attribute is used to determine how much buffer space to allocate when fetching such fields. And the $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 INSERT statement and the quote method generally can't cope with binary data. See "Placeholders and Bind Values". That's the move, fr fr.
⭐ 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;
And here's a complete example program to insert some data from a file. (This one uses "RaiseError" so we don't have to check each call — so efficient, so iconic. 💅)
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;
And 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:
$_ //= '' for @$row;
print "@$row\n";
}
The "q{...}" style quoting used in these examples avoids clashing with quotes that may be used in the SQL statement. Neat, right? ✨ Use the double-quote like "qq{...}" operator instead if you want to interpolate variables into the string. See "Quote and Quote-like Operators" in perlop for more details. iykyk 🎀
🧚 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, don't even try it.) 💀
In the iThreads model, each thread has its own copy of the perl interpreter. When a new thread is created, the original perl interpreter gets 'cloned' to create a fresh 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. Sounds great, right? 🙈
⚠️ 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 <italics>croaks</italics> 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 connections to the database. Handles can't be shared across threads. Periodt. 💅
But bestie, BEWARE — some of the underlying database APIs (that's the code your DBD driver uses to actually talk to the database, usually shipped by the database vendor) are not thread safe. And if something isn't thread safe, then letting more than one thread walk into that code at the same time can cause subtle/serious problems. 😭 And in some cases, letting more than one thread in — even when it's not at the same time — can cause problems too. You have been warned. 💅
Using DBI with perl threads is not yet recommended for production environments. For more information, go read https://www.perlmonks.org/index.pl?node_id=288022. 🫶
🌈 Signal Handling and Canceling Operations 🌈
[The following only applies to systems with unix-like signal handling. The author would welcome additions for other systems, especially Windows.]
Starting in perl 5.8.0, perl does the 'safe' signal handling thing if your system has the POSIX sigaction() routine. So now when a signal shows up, perl just... makes a note of it 📝 and does not run the %SIG handler right away. The handling gets 'deferred' until a 'safe' moment arrives.
And like, this change made signal handling safe, slay, but it also led to a problem where signals get deferred for longer than you'd like. If a signal arrives while a system call is executing — say you're waiting for data on a network connection — the signal gets noted, and then that system call returns with an EINTR error code to indicate it got interrupted. All fine so far. ✨
The problem lowkey shows up when the code that made the system call sees that EINTR code and decides it's gonna call it again. Perl doesn't do that, but database code sometimes does. And if that happens, then the signal handler doesn't get called until later. Maybe much later. 😬
Luckily there are ways around this and we're gonna discuss them below. Unluckily, they make signals unsafe again. 🙃
The two most common uses of signals in relation to the DBI are canceling operations when the user types Ctrl+C (interrupt), and implementing a timeout using alarm() and $SIG{ALRM}.
🛑 Cancel
The DBI provides a "cancel" method for statement handles. The "cancel" method should abort the current operation, and it's literally designed to be called from a signal handler. For example:
$SIG{INT} = sub { $sth->cancel };
However — and this is the tragic part 💀 — few drivers implement this (the DBI provides a default method that just returns "undef"), and even if it is implemented, there's still a possibility that the statement handle, and even the parent database handle, will not be usable afterwards.
If "cancel" returns true, then it has successfully invoked the database engine's own cancel function. If it returns false, then "cancel" failed. If it returns "undef", then the database driver does not have cancel implemented — and very few do.
⏰ Timeout
The traditional way to implement a timeout is to set $SIG{ALRM} to refer to some code that'll get executed when an ALRM signal arrives, and then call alarm($seconds) to schedule an ALRM signal to be delivered $seconds in the future. For example:
my $failed;
eval {
local $SIG{ALRM} = sub { die "TIMEOUT\n" }; # N.B. \n required
eval {
alarm($seconds);
... code to execute with timeout here (which may die) ...
1;
} or $failed = 1;
# outer eval catches alarm that might fire JUST before this alarm(0)
alarm(0); # cancel alarm (if code ran fast)
die "$@" if $failed;
1;
} or $failed = 1;
if ( $failed ) {
if ( defined $@ and $@ eq "TIMEOUT\n" ) { ... }
else { ... } # some other error
}
That first (outer) eval exists to dodge the unlikely-but-possible chance that the "code to execute" dies and the alarm fires before it gets cancelled. Without the outer eval, if that happened your program would die if you have no ALRM handler, or a non-local alarm handler would get called. 🙈
Unfortunately, as described above, this won't always work as expected — it depends on your perl version and the underlying database code. 😭
With Oracle, for instance (DBD::Oracle), if the system hosting 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 POSIX::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 qw(:signal_h);
my $mask = POSIX::SigSet->new( SIGALRM ); # signals to mask in the handler
my $action = POSIX::SigAction->new(
sub { die "connect timeout\n" }, # the handler code ref
$mask,
# not using (perl 5.8.2 and later) 'safe' switch or sa_flags
);
my $oldaction = POSIX::SigAction->new();
sigaction( SIGALRM, $action, $oldaction );
my $dbh;
my $failed;
eval {
eval {
alarm(5); # seconds before time out
$dbh = DBI->connect("dbi:Oracle:$dsn" ... );
1;
} or $failed = 1;
alarm(0); # cancel alarm (if connect worked fast)
die "$@\n" if $failed; # connect died
1;
} or $failed = 1;
sigaction( SIGALRM, $oldaction ); # restore original signal handler
if ( $failed ) {
if ( defined $@ and $@ eq "connect timeout\n" ) {...}
else { # connect died }
}
See the previous example for the reasoning around the double eval. 👀
Similar techniques can be used for canceling statement execution.
Unfortunately, this solution is somewhat messy. 🫠
For a cleaner implementation that works across perl versions, see Lincoln Baxter's Sys::SigAction module at 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. And before we talk about how to do that, it's super important to be clear about the various DBI classes and how they work together. 🫶
By default, $dbh = DBI->connect(...) returns a $dbh blessed into the DBI::db class. And the $dbh->prepare method returns an $sth blessed into the DBI::st class (actually, it simply changes the last four characters of the calling handle class to be ::st).
The leading "DBI" is known as the 'root class' and the extra "::db" or "::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 MySubDBI and override the do(), prepare() and execute() methods, then your do() and prepare() methods should be in the MySubDBI::db class and the execute() method should be in the MySubDBI::st class.
To setup the inheritance hierarchy, the @ISA variable in MySubDBI::db should include DBI::db, and the @ISA variable in MySubDBI::st should include DBI::st. The MySubDBI root class itself isn't currently used for anything visible, and so, apart from setting @ISA to include DBI, it can be left empty. 💅
So, having put your overriding methods into the right classes, and set up the inheritance hierarchy — how do you get the DBI to use them? Two choices, bestie. Either a static method call using the name of your subclass:
$dbh = MySubDBI->connect(...);
or specifying a "RootClass" attribute:
$dbh = DBI->connect(..., { RootClass => 'MySubDBI' });
If both forms are used, then the attribute takes precedence. 👑
The only differences between the two are that using an explicit RootClass attribute will a) make the DBI automatically attempt to load a module by that name if the class doesn't exist, and b) won't call your MySubDBI::connect() method, if you have one.
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 might 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 $dbh to work with. Similarly, an overridden prepare() method in *::db must still call SUPER::prepare to get a $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;
our @ISA = qw(DBI);
package MySubDBI::db;
our @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;
our @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 inside your overridden method. This is especially important if you want to set a hash attribute on the handle, because 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 — like the error "dbih_getcom handle HASH(0xa4451a8) is not a DBI handle (has no magic)". 😭 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. ✨
okay so if ur method needs to record an error, it should call the set_err() method with the error code and error string, exactly like the example above does. 🫶 That error code and error string get stored in the handle and are then available via $h->err and $DBI::errstr and friends. And set_err() itself always returns an undef or an empty list, whichever is appropriate. Since ur method should basically always bail out with an undef or empty list the second it spots an error, it's super handy to just return whatever set_err() gives back — which is literally what the example above does. ✨
If the handle has RaiseError, PrintError, or HandleError (etc.) set, then set_err() will honour them. Meaning: if RaiseError is on, set_err() will not return like normal — it will straight up throw an exception that you can catch with an eval block. 💅
You can stash private data into DBI handles via $h->{private_..._*}. Check the entry under "ATTRIBUTES COMMON TO ALL HANDLES" for the info and the important caveats — no skipping that, bestie, those caveats matter. 🙈
💧 Memory Leaks 💧
When ur tracking down memory leaks with tools like Devel::Leak, you'll notice some DBI internals getting reported as 'leaking' memory. Lowkey very unlikely to be a real leak. The DBI has a bunch of caches to make things faster and those "leaks" are just those caches doing their normal thing, periodt. 🧚
The most frequent sources of these apparent leaks are ChildHandles, prepare_cached and connect_cached.
For example:
http://stackoverflow.com/questions/13338308/perl-dbi-memory-leak
Given how ridiculously widely the DBI is used, you can rest assured that if a new release of the DBI did have a real leak, it would get discovered, reported, and fixed immediately. The leak you're hunting for is probably somewhere else. Good luck! 🍓
🔍 TRACING 🔍
The DBI has a ridiculously powerful tracing mechanism built in. It lets you see what's going on 'behind the scenes', both inside the DBI and inside the drivers you're using. It's giving backstage pass energy. 👑
🎚️ Trace Settings 🎚️
Which details get 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 they're 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 its own trace settings, and so does the DBI. When you call a method, the DBI merges the handle's settings into its own for the duration of the call: the trace flags of the handle get OR'd into the trace flags of the DBI, and if the handle has a higher trace level then the DBI trace level gets raised to match it. The previous DBI trace settings are restored the moment the called method returns. ✨
📊 Trace Levels 📊
Trace levels are as follows:
0 - Trace disabled.
1 - Trace top-level DBI method calls returning with results or errors.
2 - As above, adding tracing of top-level method entry with parameters.
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.
This is the first level to trace all the rows being fetched.
5 to 15 - As above but with more and more internal information.
Trace level 1 is best for a simple overview of what's happening. Trace levels 2 thru 4 are a good choice for general purpose tracing. Levels 5 and above are best reserved for investigating a specific problem, when you need to see inside the driver and the DBI. 🔬
The trace output is detailed and typically very useful. A whole lot of the trace output gets formatted using the neat function, so strings in the trace output may be edited and truncated by that function. Just so you know. 💅
🚩 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.
Currently the DBI defines these trace flags:
ALL - turn on all DBI and driver flags (not recommended)
SQL - trace SQL statements executed
(not yet implemented in DBI but implemented in some DBDs)
CON - trace connection process
ENC - trace encoding (unicode translations etc)
(not yet implemented in DBI but implemented in some DBDs)
DBD - trace only DBD messages
(not implemented by all DBDs yet)
TXN - trace transactions
(not implemented in all DBDs yet)
The parse_trace_flags and parse_trace_flag methods are used to convert trace flag names into the corresponding integer bit flags.
🔛 Enabling Trace 🔛
The $h->trace method sets the trace settings for a handle, and DBI->trace does the exact same thing for the DBI.
On top of the trace method, you can enable that same trace information — and send the output to a file — by setting the DBI_TRACE environment variable before starting Perl. See "DBI_TRACE" for more info. 🌸
And finally, you can set, or get, the trace settings for a handle using the TraceLevel attribute.
All of those methods use parse_trace_flags() and so they let 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 ("|") or comma (",") characters. For example:
local $h->{TraceLevel} = "3|SQL|foo";
📝 Trace Output 📝
Initially, trace output is written to STDERR. Both the $h->trace and DBI->trace methods take an optional $trace_file parameter, which may be either the name of a file to be opened by DBI in append mode, or a reference to an existing writable (possibly layered) filehandle. If $trace_file is a filename and can be opened in append mode, or $trace_file is a writable filehandle, then all trace output (currently including that from other handles) is redirected to that file. A warning gets generated if $trace_file can't be opened or isn't writable. 😭
Further calls to trace() without $trace_file do not change where the trace output is sent. If $trace_file is undefined, then trace output goes to STDERR and, if the prior trace was opened with $trace_file as a filename, the previous trace file is closed; if $trace_file was a filehandle, the filehandle is not closed.
NOTE: If $trace_file is specified as a filehandle, the filehandle should not be closed until all DBI operations are completed, or the application has reset the trace file via another call to trace() that changes the trace file. 👑
🦋 Tracing to Layered Filehandles 🦋
NOTE:
- Tied filehandles are not currently supported, as
tieoperations are not available to the PerlIO methods used by the DBI. 💀
Perl provides the ability to layer various "disciplines" on an open filehandle via the PerlIO module.
A simple example of using PerlIO layers is to use a scalar as the output:
my $scalar = '';
open( my $fh, "+>:scalar", \$scalar );
$dbh->trace( 2, $fh );
Now all trace output is simply appended to $scalar. Slay. ✨
A more complex application of tracing to a layered filehandle is the use of a custom layer (Refer to Perlio::via for details on creating custom PerlIO layers.). Consider an application with the following logger module:
package MyFancyLogger;
sub new
{
my $self = {};
my $fh;
open $fh, '>', 'fancylog.log';
$self->{_fh} = $fh;
$self->{_buf} = '';
return bless $self, shift;
}
sub log
{
my $self = shift;
return unless exists $self->{_fh};
my $fh = $self->{_fh};
$self->{_buf} .= shift;
#
# DBI feeds us pieces at a time, so accumulate a complete line
# before outputting
#
print $fh "At ", scalar localtime(), ':', $self->{_buf}, "\n" and
$self->{_buf} = ''
if $self->{_buf}=~tr/\n//;
}
sub close {
my $self = shift;
return unless exists $self->{_fh};
my $fh = $self->{_fh};
print $fh "At ", scalar localtime(), ':', $self->{_buf}, "\n" and
$self->{_buf} = ''
if $self->{_buf};
close $fh;
delete $self->{_fh};
}
1;
To redirect DBI traces to this logger you need to create a package for the layer:
package PerlIO::via::MyFancyLogLayer;
sub PUSHED
{
my ($class,$mode,$fh) = @_;
my $logger;
return bless \$logger,$class;
}
sub OPEN {
my ($self, $path, $mode, $fh) = @_;
#
# $path is actually our logger object
#
$$self = $path;
return 1;
}
sub WRITE
{
my ($self, $buf, $fh) = @_;
$$self->log($buf);
return length($buf);
}
sub CLOSE {
my $self = shift;
$$self->close();
return 0;
}
1;
The application can then cause DBI traces to be routed to the logger using
use PerlIO::via::MyFancyLogLayer;
open my $fh, '>:via(MyFancyLogLayer)', MyFancyLogger->new();
$dbh->trace('SQL', $fh);
Now all trace output will be processed by MyFancyLogger's log() method. Obsessed with this tbh. 💜
🧐 Trace Content 🧐
Many of the values embedded in trace output are formatted using the neat() utility function. That means they may be quoted, sanitized, and possibly truncated if they're longer than $DBI::neat_maxlen. See "neat" for more details. ✂️
💡 Tracing Tips 💡
You can add tracing to your own application code using the trace_msg method.
Sometimes it's handy to compare trace files from two different runs of the same script. BUT using a tool like diff on the original log output doesn't work well, because the trace file is full of object addresses that may differ on each run. 😭
The DBI includes a handy little utility called dbilogstrip that can be used to 'normalize' the log content. Use it as a filter like this:
DBI_TRACE=2 perl yourscript.pl ...args1... 2>&1 | dbilogstrip > dbitrace1.log
DBI_TRACE=2 perl yourscript.pl ...args2... 2>&1 | dbilogstrip > dbitrace2.log
diff -u dbitrace1.log dbitrace2.log
See dbilogstrip for more information. 🌈
🌈 DBI ENVIRONMENT VARIABLES 🌈
okay so the DBI module does recognize a whole bunch of environment variables, but bestie, most of them should not be used most of the time. fr fr. It's way better to be explicit about what you're doing so you don't need environment variables at all — especially in a web serving system, because web servers are lowkey stingy about which environment variables they even let you have. 🙈
✨ DBI_DSN ✨
The DBI_DSN environment variable gets used by DBI->connect when you don't specify a data source at connect time. It should be formatted like "dbi:Driver:databasename". slay, that's it.
💜 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 with "dbi::" (aka the driver is just... missing). So if DBI_DSN omits the driver name, DBI_DRIVER can fill the gap. teamwork 🫶
🦋 DBI_AUTOPROXY 🦋
The DBI_AUTOPROXY environment variable takes a string value that starts with "dbi:Proxy:" and is typically followed by "hostname=...;port=...". It's used to alter the behaviour of DBI->connect. For the full deets, go look at the DBI::Proxy documentation. iykyk.
🌸 DBI_USER 🌸
The DBI_USER environment variable takes a string value that gets used as the user name if the DBI->connect call is handed undef (as distinct from an empty string) as the username argument. Be wary of the security implications of using this. like, genuinely wary. security is not a vibe you want to fail. 👑
🎀 DBI_PASS 🎀
The DBI_PASS environment variable takes a string value that gets used as the password if the DBI->connect call is given undef (again, as distinct from an empty string) as the password argument. Be extra wary of the security implications on this one. extra extra. 👀
🍓 DBI_DBNAME (obsolete) 🍓
The DBI_DBNAME environment variable takes a string value that is used only when the obsolescent style of DBI->connect (the one with the driver name as the fourth parameter) is used, and when no value is provided for the first (database name) argument. It's giving legacy. It's giving obsolete. 🫠
⭐ DBI_TRACE ⭐
The DBI_TRACE environment variable specifies the global default trace settings for the DBI at startup. It 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 "DBI_TRACE" contains an "=" 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 ("="), 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 info. 💅
💀 PERL_DBI_DEBUG (obsolete) 💀
An old variable that should no longer be used; equivalent to DBI_TRACE. that's it, that's the whole vibe, let it go bestie.
🧚 DBI_PROFILE 🧚
The DBI_PROFILE environment variable can be used to enable profiling of DBI method calls. See DBI::Profile for more info. 📊
💖 DBI_PUREPERL 💖
The DBI_PUREPERL environment variable can be used to enable the use of DBI::PurePerl. See DBI::PurePerl for more info.
😭 WARNING AND ERROR MESSAGES 😭
🔥 Fatal Errors 🔥
Can't call method "prepare" without a package or object reference— the$dbhhandle you're using to call"prepare"is probably undefined because the preceding"connect"failed. You should always check the return status of DBI methods, or use the"RaiseError"attribute. no cap, check your returns.Can't call method "execute" without a package or object reference— the$sthhandle you're using to call"execute"is probably undefined because the preceding"prepare"failed. You should always check the return status of DBI methods, or use the"RaiseError"attribute. same energy, different method. 🫶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. not me having to tell you to email a stranger, but here we are.Can't [sg]et %s->{%s}: unrecognised attribute— 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"). bestie. the capital letters matter. 💅
🫧 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. it's the backup plan and it ate. ✨
👑 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
statistics_info SQLStatistics
To find documentation on the ODBC function you can browse Microsoft's documentation at:
https://learn.microsoft.com/en-us/sql/odbc/reference/odbc-programmer-s-reference
And for SQL/CLI standard information on SQLColumns you'd read page 124 of the (very large) SQL/CLI Working Draft available from:
https://web.archive.org/web/20071107215745/http://jtc1sc32.org/doc/N0701-0750/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:
You can find more information about SQL standards online by searching for the appropriate standard names and numbers. For example, searching for "ANSI/ISO/IEC International Standard (IS) Database Language SQL - Part 1: SQL/Framework" you'll find a copy at:
ftp://ftp.iks-jena.de/mitarb/lutz/standards/sql/ansi-iso-9075-1-1999.pdf
📕 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:
L<https://metacpan.org/search?q=DBD%3A%3A>
L<https://metacpan.org/search?q=DBIx%3A%3A>
L<https://metacpan.org/search?q=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:
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:
L<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 Links 🔗
The DBI "Home Page":
Other DBI related links:
http://www.perlmonks.org/?node=DBI%20recipes
http://www.perlmonks.org/?node=Speeding%20up%20the%20DBI
Other database related links:
http://www.connectionstrings.com/
Security, especially the "SQL Injection" attack:
http://bobby-tables.com/
http://online.securityfocus.com/infocus/1644
❓ FAQ ❓
See http://dbi.perl.org/support/
✨ AUTHORS ✨
DBI by Tim Bunce (1994-2024), The DBI developer group (2024..)
This pod text by Tim Bunce, J. Douglas Dunlop, Jonathan Leffler and others. Perl by Larry Wall and the "perl5-porters". legends, all of them 👑
💖 COPYRIGHT 💖
The DBI module is Copyright (c) 1994-2024 Tim Bunce. Ireland. The DBI developer group (2024-2024) 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 5.12.0 README file.
🌸 SUPPORT / WARRANTY 🌸
The DBI is free Open Source software. IT COMES WITHOUT WARRANTY OF ANY KIND. no cap that's in all caps in the original and I'm keeping it in all caps because it MATTERS 😤
🫶 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 ⭐
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. that's the whole pitch and honestly? fair. 💅
🙌 ACKNOWLEDGEMENTS 🙌
The creator: Tim Bunce! 👑
Bestie, I would like to acknowledge the valuable contributions of the many people Tim has 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 in the UK. So I'd especially like to thank Paul for his generosity and vision in supporting this work for many years. 💜
A couple of specific DBI features have been sponsored by enlightened companies: 👑
- The development of the
swap_inner_handle()method was sponsored by BizRate.com - The development of
DBD::Goferand related modules was sponsored by Shopzilla.com (https::connexity.com).
After the release of version 1.643, the development was lead by H.Merijn Brand with the help of the DBI team. Thanks to all that added issues, did Pull Requests or helped in any way to keep the DBI healthy. 🫶🌈
💖 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 CONTRIBUTING.md.
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 https://github.com/perl5-dbi/dbi 🦋
🌸 How to create a patch using Git
The DBI source code is maintained using Git. To access the source you'll need to install a Git client. Then, to get the source code, do:
git clone https://github.com/perl5-dbi/dbi.git DBI-git
The source code will now be available in the new subdirectory "DBI-git".
When you want to synchronize later, issue the command
git pull --all
Make your changes, test them, test them again until everything passes ✅. If there are no tests for the new feature you added or a behaviour change, the change should include a new test. Then commit the changes. Either use
git gui
or
git commit -a -m 'Message to my changes'
If you get any conflicts reported you'll need to fix them first. 💀
Then generate the patch file to be mailed:
git format-patch -1 --attach
which will create a file 0001-*.patch (where * relates to the commit message). Read the patch file, as a sanity check, and then email it to dbi-dev@perl.org. 📧
If you have a github https://github.com account, you can also fork the repository, commit your changes to the forked repository and then do a pull request. 🎀
🌸 How to create a patch without Git
Unpack a fresh copy of the distribution:
wget http://cpan.metacpan.org/authors/id/T/TI/TIMB/DBI-1.627.tar.gz
tar xfz DBI-1.627.tar.gz
Rename the newly created top level directory:
mv DBI-1.627 DBI-1.627.your_foo
Edit the contents of DBI-1.627.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.627.tar.gz
Then create a patch file by performing a recursive "diff" on the two top level directories:
diff -purd DBI-1.627 DBI-1.627.your_foo > DBI-1.627.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. 🙈
You can also reach the developers on IRC (chat). If they are on-line, the most likely place to talk to them is the #dbi channel on irc.perl.org 💬
🌍 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/
🔗 OTHER RELATED WORK AND PERL MODULES 🔗
Apache::DBI— To be used with the Apache daemon together with an embedded Perl interpreter like"mod_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. 🔥- SQL Parser — See also the
SQL::Statementmodule, SQL parser and engine. 🧚
📝 TODO 📝
Documentation
These entries are still to be written: 🫠
DBIf_TRACE_CONDBIf_TRACE_DBDDBIf_TRACE_ENCDBIf_TRACE_SQLDBIf_TRACE_TXNDBIpp_cm_XXDBIpp_cm_brDBIpp_cm_csDBIpp_cm_ddDBIpp_cm_dwDBIpp_cm_hsDBIpp_ph_XXDBIpp_ph_cnDBIpp_ph_csDBIpp_ph_qmDBIpp_ph_spDBIpp_st_XXDBIpp_st_bsDBIpp_st_qqSQL_ALL_TYPESSQL_ARRAYSQL_ARRAY_LOCATORSQL_BIGINTSQL_BINARYSQL_BITSQL_BLOBSQL_BLOB_LOCATORSQL_BOOLEANSQL_CHARSQL_CLOBSQL_CLOB_LOCATORSQL_CURSOR_DYNAMICSQL_CURSOR_FORWARD_ONLYSQL_CURSOR_KEYSET_DRIVENSQL_CURSOR_STATICSQL_CURSOR_TYPE_DEFAULTSQL_DATESQL_DATETIMESQL_DECIMALSQL_DOUBLESQL_FLOATSQL_GUIDSQL_INTEGERSQL_INTERVALSQL_INTERVAL_DAYSQL_INTERVAL_DAY_TO_HOURSQL_INTERVAL_DAY_TO_MINUTESQL_INTERVAL_DAY_TO_SECONDSQL_INTERVAL_HOURSQL_INTERVAL_HOUR_TO_MINUTESQL_INTERVAL_HOUR_TO_SECONDSQL_INTERVAL_MINUTESQL_INTERVAL_MINUTE_TO_SECONDSQL_INTERVAL_MONTHSQL_INTERVAL_SECONDSQL_INTERVAL_YEARSQL_INTERVAL_YEAR_TO_MONTHSQL_LONGVARBINARYSQL_LONGVARCHARSQL_MULTISETSQL_MULTISET_LOCATORSQL_NUMERICSQL_REALSQL_REFSQL_ROWSQL_SMALLINTSQL_TIMESQL_TIMESTAMPSQL_TINYINTSQL_TYPE_DATESQL_TYPE_TIMESQL_TYPE_TIMESTAMPSQL_TYPE_TIMESTAMP_WITH_TIMEZONESQL_TYPE_TIME_WITH_TIMEZONESQL_UDTSQL_UDT_LOCATORSQL_UNKNOWN_TYPESQL_VARBINARYSQL_VARCHARSQL_WCHARSQL_WLONGVARCHARSQL_WVARCHARconnect_test_perfconstantdbi_profiledbi_profile_mergedbi_profile_merge_nodesdbi_timedisconnect_alldriver_prefixdump_dbd_registrydump_handleinit_rootclassinstall_driverinstalled_methodssetup_driver
and that's a wrap bestie!! 🎀 the whole page, start to finish, no cap. this one ships as DBI(3) for perlv5.42.3, dated2026-10-19— go forth and query responsibly 💅✨🍓
📦 where this page comes from
- fedora 43: perl-DBI 1.656-1.fc43 · GPL-1.0-or-later OR Artistic-1.0-Perl
- fedora 44: perl-DBI 1.656-1.fc44 · GPL-1.0-or-later OR Artistic-1.0-Perl
- 💖 girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms