NAME
BW::DB - Normalized database routines
SYNOPSIS
use BW::DB;
my $errstr;
my $db = BW::DB->new( connect => "database:host:3306:user:password" );
error($errstr) if (($errstr = $db->error));
my $db = BW::DB->new(
dsn => 'DBI:mysql:database=database;mysql_socket=/tmp/mysql2.sock",
user => "me", password => "foo!bar" );
error($errstr) if(($errstr = $db->error));
my $db = BW::DB->new(
database => "database", host => "host", port => "3306",
user => "user", password => "pass");
error($errstr) if (($errstr = $db->error));
# experimental SQLite support ...
my $db = BW::DB->new(dbengine => "SQLite", dbname => "dbfile");
error($errstr) if (($errstr = $db->error));
METHODS
- new( connect => $connect_string )
-
Constructs a new DB object. Connect string is in the format:
database:host:port:user:password
... or for experimental SQLite support:
SQLite:dbfile
Returns a blessed DB object reference. Returns VOID if an object cannot be created. If the object is constructed but there is an error connecting to the database, the object reference is returned and $db->error is set.
Alternately you can call new() with separate parameters for the database connection thusly:
my $db = BW::DB->new( database => "database", host => "host", port => "3306", user => "user", password => "pass" );
... or for experimental SQLite support:
my $db = BW::DB->new( dbengine => "SQLite", dbname => "dbfile" );
If your database is listening on a named pipe you can connect using a DBI DSN like this:
my $db = BW::DB->new( dsn => "DBI:mysql:database=database;mysql_socket=/tmp/mysql2.sock", user => "me", password => "foo!bar" ); error($errstr) if(($errstr = $db->error));
- sql_do( $query, @bind_values )
-
Calls DBD::do for queries that don't return data, like INSERT or DELETE. Returns SUCCESS or FAILURE.
- sql_select( $query, @bind_values )
-
Performs the query with the bind values and returns an arrayref where each element in the array is a hashref with a row of data returned from the query. Keys are set to column names.
Returns FAILURE and sets $db->error if a DBI error was encountered.
- sql_select_column( $query, @bind_values )
-
Performs the query with the bind values and returns an arrayref where each element in the array is a scalar value from the first column returned from the query.
Returns FAILURE and sets $db->error if a DBI error was encountered.
- sql_select_value( $query, @bind_values )
-
Performs the query with the bind values and returns a scalar with a single value from the query. The query should return a single value from a single column.
Returns FAILURE and sets $db->error if a DBI error was encountered.
- insert( $table, $hashref )
-
Performs an insert into table with the names/values in $hashref. Use the insert_id method to get the value of any auto_increment field.
Returns SUCCESS or FAILURE (and sets $db->error).
- insert_id
-
Returns the value of any auto_increment field from the last insert operation.
- table_exists( $table )
-
Performs a "describe" and returns TRUE or FALSE.
- error
-
Returns and clears the object error message.
AUTHOR
Written by Bill Weinman <http://bw.org/>.
COPYRIGHT
Copyright (c) 1995-2010 The BearHeart Group, LLC
HISTORY
See HISTORY file.