NAME
Labyrinth::DBUtils - Standard Database Access Methods
SYNOPSIS
use Labyrinth::DBUtils;
my $dbi = Labyrinth::DBUtils->new({
driver => 'CSV',
file => '/var/www/mysite/db');
sub errors { print STDERR "Error: $_[0], sql=$_[1]\n" }
my @arr = $dbi->GetQuery('array','getTables');
my @arr = $dbi->GetQuery('array','getTable',$bid);
my @arr = $dbi->GetQuery('hash','getOneRow',
{table=>'uds_build',field=>'bid'},$bid});
my $id = $dbi->IDQuery('insertRow',$id,$name);
$dbi->DoQuery('deleteRow',$id);
$dbi->DoQuery('updateRow',{id=>$id,name=>$name});
my $next = Iterator('array','getTables');
my $row = $next->(); # returns an array ref
my $next = Iterator('hash','getTables');
my $row = $next->(); # returns a hash ref
$value = $dbi->Quote($value);
DESCRIPTION
The DBUtils package is a further database interface layer, providing a collection of control methods to initiate the database connection, handle errors and a smooth handover from the program to the database drivers.
Reads and handles the SQL from the phrasebook, passing the statement and any additional parameters through to the DBI onject.
CONSTRUCTOR
- new DBUtils({})
-
The Constructor method. Can be called with an anonymous hash, listing the values to be used to connect to and handle the database.
Values in the hash can be
logfile phrasebook (*) dictionary driver (*) database (+) dbfile (+) dbhost dbport dbuser dbpass autocommit
(*) These entries MUST exist in the hash. (+) At least ONE of these must exist in the hash, and depend upon the driver.
Note that 'file' is for use with a flat file database, such as DBD::CSV.
PUBLIC INTERFACE METHODS
- GetQuery(type,key,<list>)
-
type - 'array' or 'hash' key - hash key to sql in phrasebook <list> - optional additional values to be inserted into SQL placeholders
The function performs a SELECT statement, which returns either a list of lists, or a list of hashes. The difference being that for each record, the field values are listed in the order they are returned, or via the table column name in a hash.
The first entry in <list> can be an anonymous hash, containing the placeholder values to be interpolated by Class::Phrasebook.
Note that if the key is not found in the phrasebook, the function returns with undef.
- Iterator(type,key,<list>)
-
type - 'array' or 'hash' key - hash key to sql in phrasebook <list> - optional additional values to be inserted into SQL placeholders
The function performs a SELECT statement, which returns a subroutine reference which can then be used to obtain either a list of lists, or a list of hashes. The difference being that for each record, the field values are listed in the order they are returned, or via the table column name in a hash.
The first entry in <list> can be an anonymous hash, containing the placeholder values to be interpolated by Class::Phrasebook.
Note that if the key is not found in the phrasebook, the function returns with undef.
- DoQuery(key,<list>)
-
key - hash key to sql in phrasebook <list> - optional additional values to be inserted into SQL placeholders
The function performs an SQL statement. If performing an INSERT statement that returns an record id, this is returned to the calling function.
The first entry in <list> can be an anonymous hash, containing the placeholder values to be interpolated by Class::Phrasebook.
Note that if the key is not found in the phrasebook, the function returns with undef.
- IDQuery(key,<list>)
-
key - hash key to sql in phrasebook <list> - optional additional values to be inserted into SQL placeholders
The function performs an SQL statement. If performing an INSERT statement that returns an record id, this is returned to the calling function.
The first entry in <list> can be an anonymous hash, containing the placeholder values to be interpolated by Class::Phrasebook.
Note that if the key is not found in the phrasebook, the function returns with undef.
- DoSQL(sql,<list>)
-
sql - SQL statement <list> - optional additional values to be inserted into SQL placeholders
- Quote(string)
-
string - string to be quoted
The function performs a DBI quote operation, which will quote a string according to the SQL rules.
- Get & Set Methods
-
The following accessor methods are available:
driver database file host port user password
All functions can be called to return the current value of the associated object variable, or be called with a parameter to set a new value for the object variable.
(*) Setting these methods will take action immediately. All other access methods require a new object to be created, before they can be used.
Examples:
my $database = db_database(); db_database('another');
SEE ALSO
DBI,
Labyrinth
AUTHOR
Barbie, <barbie@missbarbell.co.uk> for Miss Barbell Productions, http://www.missbarbell.co.uk/
COPYRIGHT & LICENSE
Copyright (C) 2002-2011 Barbie for Miss Barbell Productions
All Rights Reserved.
This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.