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=vdeck;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));
METHODS
- new( connect => $connect_string )
-
Constructs a new DB object. Connect string is in the format:
database:host:port:user:password
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" );
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=vdeck;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 in March 2007.
COPYRIGHT
Copyright (c) 1995-2010 The BearHeart Group, LLC
HISTORY
2010-02-02 bw 1.0.4 -- first CPAN version - some cleanup and documenting
2007-10-15 bw 1.0 -- fixed a small bug in sql_select -- should now return
empty data sets properly
2007-07-16 bw 0.4 -- added sql_select_column and sql_select_value methods
-- added 'socket' property for use in connecting database
2007-05-18 bw -- added insert method and fixed a few bugs
2007-05-17 bw -- fixed small typo in POD
2007-03-21 bw -- updated to allow connecting to the database
2007-03-14 bw -- initial release.