DBIx::Simple - Easy-to-use OO interface to DBI |
DBIx::Simple - Easy-to-use OO interface to DBI
$db = DBIx::Simple->connect(...) # or ->new
$db->keep_statements = 16 $db->lc_columns = 1 $db->result_class = 'DBIx::Simple::Result';
$db->begin_work $db->commit $db->rollback $db->disconnect $db->func(...) $db->last_insert_id
$result = $db->query(...)
$result = $db->iquery(...)
$db->abstract = SQL::Abstract->new(...)
$result = $db->select(...) $result = $db->insert(...) $result = $db->update(...) $result = $db->delete(...)
@columns = $result->columns
$result->into($foo, $bar, $baz) $row = $result->fetch
@row = $result->list @rows = $result->flat $row = $result->array @rows = $result->arrays $row = $result->hash @rows = $result->hashes
%map = $result->map_arrays(...) %map = $result->map_hashes(...) %map = $result->map
$rows = $result->rows
$dump = $result->text
$result->finish
$html = $result->html(...)
$table_object = $result->xto(...)
Please read the DBIx::Simple::Examples manpage for code examples.
DBIx::Simple provides a simplified interface to DBI, Perl's powerful database module.
This module is aimed at rapid development and easy maintenance. Query preparation and execution are combined in a single method, the result object (which is a wrapper around the statement handle) provides easy row-by-row and slurping methods.
The query
method returns either a result object, or a dummy object. The
dummy object returns undef (or an empty list) for all methods and when used in
boolean context, is false. The dummy object lets you postpone (or skip) error
checking, but it also makes immediate error checking simply <
$db-
query(...)
or die $db->error >>.
DBIx::Simple->connect($dbh)
DBIx::Simple->connect($dsn, $user, $pass, \%options)
DBIx::Simple->new($dbh)
DBIx::Simple->new($dsn, $user, $pass, \%options)
connect
or new
class method takes either an existing DBI object
($dbh), or a list of arguments to pass to DBI->connect
. See the DBI manpage for a
detailed description.
You cannot use this method to clone a DBIx::Simple object: the $dbh passed should be a DBI::db object, not a DBIx::Simple object.
This method is the constructor and returns a DBIx::Simple object on success. On failure, it returns undef.
lc_columns = $bool
columns
, hash
, hashes
, and
map_hashes
use lower cased column names. lc_columns
is true by default.
keep_statements = $integer
keep_statements
is 16 by default.
A query is only reused if it equals a previously used one literally. This means that to benefit from this caching mechanism, you must use placeholders and never interpolate variables yourself.
# Wrong: $db->query("INSERT INTO foo VALUES ('$foo', '$bar', '$baz')"); $db->query("SELECT FROM foo WHERE foo = '$foo' OR bar = '$bar'");
# Right: $db->query('INSERT INTO foo VALUES (??)', $foo, $bar, $baz); $db->query('SELECT FROM foo WHERE foo = ? OR bar = ?', $foo, $baz);
Of course, automatic value escaping is a much better reason for using placeholders.
result_class = $string
error
err
''
and ``errstr
'' in the DBI manpage.
query($query, @values)
query
method prepares and executes the query and returns a result object.
If the string (??)
is present in the query, it is replaced with a list of as
many question marks as @values.
The database drivers substitute placeholders (question marks that do not appear in quoted literals) in the query with the given @values, after them escaping them. You should always use placeholders, and never use raw user input in database queries.
On success, returns a DBIx::Simple::Result object.
On failure, returns a DBIx::Simple::Dummy object.
iquery
query
.
See SQL::Interp's documentation for usage information.
Requires that Mark Stosberg's SQL::Interp module be installed. It is available from CPAN. SQL::Interp is a fork from David Manura's SQL::Interpolate.
select
, insert
, update
, delete
abstract
, and uses the resulting generated
query and bind arguments with query
.
See SQL::Abstract's documentation for usage information. You can override the
object by assigning to the abstract
property.
Obviously, calling query
directly is faster for the computer and using these
abstracting methods is faster for the programmer.
abstract = SQL::Abstract->new(...)
select
, insert
, update
and delete
methods. On first access, will create one with SQL::Abstract's default options.
Requires that Nathan Wiger's SQL::Abstract module be installed. It is available from CPAN.
In theory, you can assign any object to this property, as long as that object
has these four methods, and they return a list suitable for use with the
query
method.
begin_work
, begin
, commit
, rollback
begin
is an alias for begin_work
.
func(...)
func
method of DBI. See the DBI manpage for details.
last_insert_id(...)
last_insert_id
method of DBI. See the DBI manpage for details. Note
that this feature requires DBI 1.38 or newer.
dbh
disconnect
The query
method of DBIx::Simple returns a dummy object on failure. Its
methods all return an empty list or undef, depending on context. When used in
boolean context, a dummy object evaluates to false.
columns
Column names are lower cased if lc_columns
was true when the query was
executed.
bind(LIST)
$result->bind(my ($foo, $bar)); $result->fetch;
Or, combined:
$result->into(my ($foo, $bar));
Unlike with DBI's bind_columns
, the \
operator is not needed.
Bound variables are very efficient. Binding a tied variable doesn't work.
fetch
Subsequent fetches (using any method) may change the values in the variables passed and the returned reference's array.
into(LIST)
bind
with fetch
. Returns what fetch
returns.
list
array
hash
Keys are lower cased if lc_columns
was true when the query was executed.
flat
In scalar context, returns an array reference.
arrays
In scalar context, returns an array reference.
hashes
In scalar context, returns an array reference.
Keys are lower cased if lc_columns
was true when the query was executed.
map_arrays($column_number)
In scalar context, returns a hash reference.
In list context, returns interleaved keys and values.
map_hashes($column_name)
In scalar context, returns a hash reference.
In list context, returns interleaved keys and values.
map
In scalar context, returns a hash reference.
In list context, returns interleaved keys and values.
rows
For SELECT statements, it is generally not possible to know how many rows are returned. MySQL does provide this information. See the DBI manpage for a detailed explanation.
xto(%attr)
%attr
.
Requires that Jeffrey Hayes Anderson's DBIx::XHTML_Table module be installed. It is available from CPAN.
In general, using the html
method (described below) is much easier. xto
is available in case you need more flexibility.
This method ignores the lc_columns
property.
html(%attr)
%attr
to both the constructor and the output
method.
Requires that Jeffrey Hayes Anderson's DBIx::XHTML_Table module be installed. It is available from CPAN.
This method is a shortcut method. That means that
$result->html
$result->html( tr => { bgcolor => [ 'silver', 'white' ] }, no_ucfirst => 1 )
do the same as:
$result->xto->output
$result->xto( tr => { bgcolor => [ 'silver', 'white' ] } )->output( no_ucfirst => 1 );
text($type)
$type
can be any of: neat
, table
, box
. It defaults to table
if
Text::Table is installed, to neat
if it is.
table
and box
require that Anno Siegel's Text::Table module be
installed. It is available from CPAN.
attr(...)
func(...)
func
method of DBI. See the DBI manpage for details.
finish
The mapping methods do not check whether the keys are unique. Rows that are fetched later overwrite earlier ones.
PrintError is disabled by default. If you enable it, beware that it will report line numbers in DBIx/Simple.pm.
There is no license. This software was released into the public domain. Do with it what you want, but on your own risk. The author disclaims any responsibility.
Juerd Waalboer <juerd@cpan.org> <http://juerd.nl/>
the perl manpage, the perlref manpage
the DBI manpage, the DBIx::Simple::Examples manpage, the SQL::Abstract manpage, the DBIx::XHTML_Table manpage
DBIx::Simple - Easy-to-use OO interface to DBI |