NAME
DBIx::Simple - Easy-to-use OO interface to DBI, capable of emulating subqueries
SYNOPSIS
DBIx::Simple
$db = DBIx::Simple->connect(...) # or ->new
$db->omniholder = '(??)'
$db->emulate_subqueries = 0 # or ->esq
$db->keep_statements = 16
$db->lc_columns = 1
$db->begin_work $db->commit
$db->rollback $db->disconnect
$db->func(...) $db->last_insert_id
$result = $db->query(...)
DBIx::Simple::Result
$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
$result->finish
Examples
Please read DBIx::Simple::Examples for code examples.
DESCRIPTION
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 a simple $db->query(...) or die $db->error
.
DBIx::Simple methods
DBIx::Simple->connect($dbh)
DBIx::Simple->connect($dsn, $user, $pass, \%options)
DBIx::Simple->new($dbh)
DBIx::Simple->new($dsn, $user, $pass, \%options)
-
The
connect
ornew
class method takes either an existing DBI object ($dbh), or a list of arguments to pass toDBI->connect
. See DBI 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.
omniholder = $string
-
This method is deprecated and will be removed in a future version. In future versions, the omniholder will always be
(??)
and no longer be user definable. If you have a good reason to not want(??)
, please do try to convince me.The omniholder string is, when found in a query, replaced with
(?, ?, ?, ...)
with as many question marks as@values
passed toquery
. emulate_subqueries = $bool
esq = $bool
-
This method is deprecated and will be removed in a future version.
emulate_subqueries
was originally invented because MySQL had no subselects of its own, but it has now.When true at time of query execution, makes
query
emulate nested subqueries (SELECT only) by executing them and interpolating the results.emulate_subqueries
is false by default and should not be used if the database provides real subqueries.Only subqueries like
(SELECT ...)
(note the parentheses) are interpolated.Please note that emulation is done by doing multiple queries and is not atomic, as it would be if the database supported real subqueries. The queries are executed independently.
lc_columns = $bool
-
When true at time of query execution, makes
columns
,hash
,new_hash
,hashes
, andmap_hashes
use lower cased column names.lc_columns
is true by default. keep_statements = $integer
-
Sets the number of statement objects that DBIx::Simple can keep for reuse. This can dramatically speed up repeated queries (like when used in a loop).
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.
error
-
Returns the error string of the last DBI method. See the discussion of "
err
" and "errstr
" in DBI. query($query, @values)
-
The
query
method prepares and executes the query and returns a result object.If an omniholder (see above) is present in the query, it is replaced with a list of as many question marks as @values. If subquery emulation (see above) is enabled, subquery results are interpolated in the main query before the main query is executed.
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 user input in database queries.
On success, returns a DBIx::Simple::Result object.
On failure, returns a DBIx::Simple::Dummy object.
begin_work
,commit
,rollback
-
These transaction related methods call the DBI respective methods and Do What You Mean. See DBI for details.
func(...)
-
This calls the
func
method of DBI. See DBI for details. last_insert_id(...)
-
This calls the
last_insert_id
method of DBI. See DBI for details. Note that this feature requires DBI 1.38 or newer. dbh
-
Exposes the internal database handle. Use this only if you know what you are doing. Keeping a reference or doing queries can interfere with DBIx::Simple's garbage collection and error reporting.
disconnect
-
Destroys (finishes) active statements and disconnects. Whenever the database object is destroyed, this happens automatically. After disconnecting, you can no longer use the database object or any of its result objects.
DBIx::Simple::Dummy
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.
DBIx::Simple::Result methods
columns
-
Returns a list of column names. In scalar context, returns an array reference.
Column names are lower cased if
lc_underscores
was true when the query was executed. bind(LIST)
-
Binds the given LIST to the columns. The elements of LIST must be writable LVALUEs. In other words, use this method as:
$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
-
Fetches a single row and returns a reference to the array that holds the values. This is the same array every time.
Subsequent fetches (using any method) may change the values in the variables passed and the returned reference's array.
into(LIST)
-
Combines
bind
withfetch
. Returns whatfetch
returns. list
-
Fetches a single row and returns a list of values. In scalar context, returns only the last value.
array
-
Fetches a single row and returns an array reference.
hash
-
Fetches a single row and returns a hash reference.
Keys are lower cased if
lc_underscores
was true when the query was executed. flat
-
Fetches all remaining rows and returns a flattened list.
arrays
-
Fetches all remaining rows and returns a list of array references.
In scalar context, returns an array reference.
hashes
-
Fetches all remaining rows and returns a list of hash references.
In scalar context, returns an array reference.
Keys are lower cased if
lc_underscores
was true when the query was executed. map_arrays($column_number)
-
Constructs a hash of array references keyed by the values in the chosen column.
In scalar context, returns a hash reference.
map_hashes($column_name)
-
Constructs a hash of hash references keyed by the values in the chosen column.
In scalar context, returns a hash reference.
map
-
Constructs a simple hash, using the first two columns as key/value pairs. Should only be used with queries that return two columns.
In scalar context, returns a hash reference.
rows
-
Returns the number of rows affected by the last row affecting command, or -1 if the number of rows is not known or not available.
For SELECT statements, it is generally not possible to know how many rows are returned. MySQL does provide this information. See DBI for a detailed explanation.
attr(...)
-
Returns a copy of an sth attribute (property). See "Statement Handle Attributes" in DBI for details.
func(...)
-
This calls the
func
method of DBI. See DBI for details. finish
-
Finishes the statement. After finishing a statement, it can no longer be used. When the result object is destroyed, its statement handle is automatically finished and destroyed. There should be no reason to call this method explicitly; just let the result object go out of scope.
MISCELLANEOUS
Although this module has been tested thoroughly in production environments, it still has no automated test suite. If you want to write tests, please contact me.
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.
Note: this module does not provide any SQL abstraction and never will. If you don't want to write SQL queries, use DBIx::Abstract.
LICENSE
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.
AUTHOR
Juerd Waalboer <juerd@cpan.org> <http://juerd.nl/>