I wrote a module called DBIv that does a 'use DBI ; '.
These are the functions that I defined. This module was written with the
intention of providing 2 things:
- a very simple interface
- database independence
--------------------------------------------------
Connection Functions:
$dbh = &DBIv::OpenDBConnection($server, $database, $uid, $password)
;
&DBIv::CloseDBConnection($dbh) ;
Query Functions:
$rh_results = &DBIv::SelectQuery($dbh, $query) ;
$rows_inserted = &DBIv::InsertQuery($dbh, $query) ;
$rows_affected = &DBIv::UpdateQuery($dbh, $query) ;
$rows_affected = &DBIv::DeleteQuery($dbh, $query) ;
Stored Procedures:
$return_value = &DBIv::ExecuteStoredProcedure($dbh, $command) ;
$return_value = &DBIv::ExecuteSQLBatch($dbh, \@commands) ;
Display Result Functions:
&DBIv::DisplayAllResults($rh_results, $format) ;
Extract Portions of a Result Set:
$row_count = &DBIv::ReadResultRowCount($rh_results) ;
$col_count = &DBIv::ReadResultColCount($rh_results) ;
@values = &DBIv::ReadResultColumn($rh_results, $column) ;
@values = &DBIv::ReadResultColumn($rh_results, $column) ;
$value = &DBIv::ReadResultRowColumn($rh_results, $row,
$column) ;
Error Message:
$DBIv::errstr - Contains err message text if one has occurred
-------------------------------------------------
By simplified interface, I mean that the number of statements required to
submit a query and get results are reduced. In most cases this reduces the
series of steps such as prepare, execute, ... etc. single step.
More importantly it provides a level of database independence by
dynamically loading in another module via AUTOLOAD depending on the
database type that is being connected to. In other words based on an
Environment variable I would load another module into DBIv such as
Sybase_DB.pm or Oracle_DB.pm, and Default.pm. Each of these modules would
have the same functions, but written in different ways. This is very
similar to the way that DBIx works. However due to my inexperience with
programming classes I couldn't get DBIx to work the way I thought that it
should so I developed this library without it.
> What do you mean by "valid"?
>
> Are you trying to verify $dbh is an object that has a disconnect() method
in
> its class?
>
> if (ref($dbh) && UNIVERSAL::can($dbh, "disconnect")) {
> # $dbh is an object with a disconnect() method
> }
I was just trying to make sure that if I executed the code:
$dbh->disconnect()
I would always catch any possible error. I was worried not only about
disconnection errors caused by the database, but also programmer errors
where $dbh might have been inadvertantly defined as a string 'xyz' or '' or
undef. All these cases are captured quite nicely by using an earlier
suggestion in the list:
--------------------------
sub CloseDBConnection {
my ($dbh) = @_ ;
my ($status) ;
# avoid the eval for the simple error cases if possible
if (!defined($dbh) || $dbh eq '')
{
$DBIv::errstr = "Error: Unable to close connection.\nInvalid database
handle." ;
return 0 ;
}
# in order to trap programmer error
# use an eval to catch invalid $dbh values
eval { $dbh->disconnect(); } ;
if ($@ ne '')
{
$DBIv::errstr = "Error: Unable to close connection.\n($@)" ;
return 0 ;
}
return 1 ;
}
Thanks for the reminder of the use of $dbh->ping ... I had forgotten about
that. I may use that elsewhere.
Thanks to everyone who helped on this.
Jeff
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]