LEOCHARRE::Database - common database methods for oo
Mymod.pm:
package Mymod; use base 'LEOCHARRE::Database'; 1;
script.pl:
use Mymod; my $m = new Mymod({ DBHOST => 'localhost' , DBUSER => 'username', DBPASSWORD => 'passwerd', DBNAME => 'superdb', }); $m->dbh; my $a = $m->dbh_sth('select * from avocado where provider = ?'); $a->execute(3); $m->dbh_sth('INSERT INTO fruit (name,provider) values(?,?)')->execute('grape','joe'); # gets prepared and cached $m->dbh_sth('INSERT INTO fruit (name,provider) values(?,?)')->execute('orange','joe'); # uses the previous cached prepared handle
This is meant to be used as base by another oo module.
This can be passed an open database handle via DBH or it will attempt to open a new connection. By default it attempts to open a mysql connection. It can also open a sqlite connection.
Basically, if you provide the argument DBABSPATH, will attempt to open a mysql connection instead.
PLEASE NOTE autocommit is off. DESTROY calls commit and finishes and closes handles.
These and any other modules under my name as namespace base (LEOCHARRE) are parts of code that I use for doing repetitive tasks. They are not bringing anything really new to the table, and I wouldn't assume that 'my way' is the way to attack these problems by any stretch of the imagination. This is why I place them under my namespace, as a gesture of deference.
new ({ DBHOST => $host, DBNAME => $dbname, DBUSER => $dbuser, DBPASSWORD => $pw, });
new ({ DBABSPATH => '/home/myself/great.db', });
new ({ DBH => $dbh, });
These are meant to be inherited by your module with
use base 'LEOCHARRE::Database';
returns database handle
argument is statement will select and return array ref
my $users = $self->dbh_selectcol("SELECT user FROM users WHERE type = 'm'");
Now users has ['joe','rita','carl']
This is useful sometimes.
argis hash ref with mysql and sqlite keys
will select one or the other with dbh_is_mysql etc
$self->dbh_do({ sqlite => 'CREATE TABLE bla( name varchar(25) )', mysql => 'CREATE TABLE bla( name varchar(25) )', });
argument is statement returns count number you MUST have a COUNT(*) in the select statement
my $matches = $self->dbh_count('select count(*) from files');
argument is a statment, returns handle it will cache in the object, subsequent calls are not re-prepared
my $delete = $self->dbh_sth('DELETE FROM files WHERE id = ?'); $delete->execute(4); # or.. for (@ids){ $self->dbh_sth('DELETE FROM files WHERE id = ?')->execute($_); }
If the prepare fails, confess is called.
returns boolean
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.
argument is table name, returns boolean
argument is table name returns string of dump of table suitable for print to STDERR
arg is table name, drops the table. returns boolean will drop IF EXISTS
arg is table name, returns last insert id. returns undef if not there
closes ChildHandles that are active, finishes and undefines them. returns true + number of active handles were finished and undefined here
If the database handle was created in this object and not passed via constructor, then we close all handles, commit, and disconnect.
finishes active database handles etc, makes a commit to the database. Note, this method is called automatically.
Autocommit is set to 0 by default. That means you should commit after indexing_lock(), indexing_lock_release(), delete_record()
DESTROY will finish and commit if there are open handles created by the object
To turn on debug,
$LEOCHARRE::Database::DEBUG = 1;
LEOCHARRE::CLI LEOCHARRE::Dev DBI
Leo Charre leocharre at cpan dot org http://leocharre.com
Copyright (c) 2008 Leo Charre. All rights reserved.
This package is free software; you can redistribute it and/or modify it under the same terms as Perl itself, i.e., under the terms of the "Artistic License" or the "GNU General Public License".
This package is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the "GNU General Public License" for more details.
To install LEOCHARRE::Database, copy and paste the appropriate command in to your terminal.
cpanm
cpanm LEOCHARRE::Database
CPAN shell
perl -MCPAN -e shell install LEOCHARRE::Database
For more information on module installation, please visit the detailed CPAN module installation guide.