NAME

LEOCHARRE::Database::Base - added methods to DBI:db

SYNOPSIS

use DBI;
use base 'LEOCHARRE::Database::Base';

my $dbh = connect_sqlite('/abs/path/to.db');

DESCRIPTION

When used, this module adds methods to dbh handles, (DBI::db objects).

If instead you import, it just imports these subs..

SUBS

sth()

argument is dbh handle and statment, returns statement handle, cached prepared in dbh object it will cache in the object, subsequent calls are not re-prepared

my $delete = sth( $dbh, 'DELETE FROM files WHERE id = ?');
$delete->execute(4);

# or..
for (@ids){
   sth( $dbh, 'DELETE FROM files WHERE id = ?')->execute($_);
} 

If the prepare fails, confess is called.

selectcol()

argument is statement will select and return array ref

my $users = $dbh->selectcol("SELECT user FROM users WHERE type = 'm'");

Now users has ['joe','rita','carl']

This is useful sometimes.

table_exists()

argument is tablename returns boolean

table_dump()

argument istablename returns string of table dump suitable for print to STDERR requires Data::Dumper

rows_count()

argument is statement or table name returns count number you MUST have a COUNT(*) if the first argument is a statement

takes 3 arguments or 1 argument, else throws an exception

my $matches = $dbh->rows_count( 'select count(*) from files' );

my $matches = $dbh->rows_count( 'files' ); #counts all entries in files table

my $matches = $dbh->rows_count( 'files', size => 34 ); # all rows in files table with col 'size' == 34

close_active_handles()

closes ChildHandles that are active, finishes and undefines them. returns true + number of active handles were finished and undefined here

lid()

argument is dbh and table name returns last insert id for that table returns undef if not there

this is often only available right after an insert, if you make an insert into a table, and then into another, you cant get last insert id on the first entry.

is_mysql()

returns boolean

is_sqlite()

returns boolean

driver()

returns name of DBI Driver, sqlite, mysql, etc. Currently mysql is used, sqlite is used for testing. For testing the package, you don't need to have mysqld running.

drop_table()

arument is dbh and table name returns boolean

CONSTRUCTORS

connect_sqlite()

argument is abs path to db returns db handle returns undef on failure

my $dbh = connect_sqlite('/home/myself/stuff.db');

connect_mysql()

args are dbname, dbuser, dbpass, hostname, if no hostname is provided, uses 'localhost' returns database handle returns undef on failure

my $dbh = connect_mysql('stuff_data','joe','joepass');

DEBUG

$LEOCHARRE::Database::Base::DEBUG = 1;

AUTHOR

Leo Charre leocharre at cpan dot org