Programming the Perl DBI
时间:2007-06-20 来源:剑胆琴心
A.3. The DBI Class
In this section, we cover the DBI class methods, utility functions, and the dynamic attributes associated with generic DBI handles.
A.3.1. DBI Class Methods
The following methods are provided by the DBI class:
connect |
|
$dbh = DBI->connect($data_source, $username, $password) |
|
connect establishes a database connection, or session, to the requested $data_source . Returns a database handle object if the connection succeeds. Use $dbh->disconnect to terminate the connection.
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
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:
dbi:Proxy:$ENV{DBI_AUTOPROXY};dsn=$data_source
and passed to the DBD::Proxy module. DBI_AUTOPROXY is typically set as "hostname=...;port=...". See the DBD::Proxy documentation for more details.
If $username or $password are undefined (rather than just empty), then the DBI will substitute the values of the DBI_USER and DBI_PASS environment variables, respectively. The DBI will warn if the environment variables are not defined. However, the everyday use of these environment variables is not recommended for security reasons. The mechanism is primarily intended to simplify testing.
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. Future versions of the DBI may issue a warning if AutoCommit is not explicitly defined.
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
});
You can also define connection attribute values within the $data_source parameter. For example:
dbi:DriverName(PrintError=>0,Taint=>1):...
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.
$dbh = DBI->connect($data_source, $user, $pass, $driver);
In this "old-style" form of connect, the $data_source should not start with dbi:driver_name:. (If it does, the embedded driver_name will be ignored). Also note that in this older form of connect, the $dbh->{AutoCommit} attribute is undefined, the $dbh->{PrintError} attribute is off, and the old DBI_DBNAME environment variable is checked if DBI_DSN is not defined. Beware that this "old-style" connect will be withdrawn in a future version of DBI.
connect_cached (NEW ) |
|
$dbh = DBI->connect_cached($data_source, $username, $password) |
|
connect_cached is like connect, except that the database handle returned is also stored in a hash associated with the given parameters. If another call is made to connect_cached with the same parameter values, then the corresponding cached $dbh will be returned if it is still valid. The cached database handle is replaced with a new connection if it has been disconnected or if the ping method fails.
The cache can be accessed (and cleared) via the CachedKids attribute.
available_drivers |
|
@ary = DBI->available_drivers; |
|
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.
data_sources |
|
@ary = DBI->data_sources($driver); |
|
Returns a list of all data sources (databases) available via the named driver. The driver will be loaded if it hasn't been already. If $driver is empty or undef, then the value of the DBI_DRIVER environment variable is used.
trace |
|
DBI->trace($trace_level) |
|
DBI trace information can be enabled for all handles using the trace DBI class method. To enable trace information for a specific handle, use the similar $h->trace method described elsewhere.
0 1Trace DBI method calls returning with results or errors.
2Trace method entry with parameters and returning with results.
3 4 5 and aboveA.3.2. DBI Utility Functions
neat |
|
$str = DBI::neat($value, $maxlen); |
|
Returns a string containing a neat (and tidy) representation of the supplied value.
neat_list |
|
$str = DBI::neat_list(\@listref, $maxlen, $field_sep); |
|
Calls DBI::neat on each element of the list and returns a string containing the results joined with $field_sep. $field_sep defaults to ", ".
looks_like_number |
|
@bool = DBI::looks_like_number(@array); |
|
Returns true for each element that looks like a number. Returns false for each element that does not look like a number. Returns undef for each element that is undefined or empty.
A.3.3. DBI Dynamic Attributes
$DBI::err $DBI::errstr $DBI::state $DBI::rowsEquivalent to $h->rows. Please refer to the documentation for the rows method.