DBIx::Simple(3pm) | User Contributed Perl Documentation | DBIx::Simple(3pm) |
DBIx::Simple - Very complete 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 @row = $result->kv_list @rows = $result->kv_flat $row = $result->kv_array @rows = $result->kv_arrays $obj = $result->object @objs = $result->objects %map = $result->map %grouped = $result->group %map = $result->map_hashes(...) %grouped = $result->group_hashes(...) %map = $result->map_arrays(...) %grouped = $result->group_arrays(...) $rows = $result->rows $dump = $result->text $result->finish
$html = $result->html(...) $table_object = $result->xto(...)
Please read DBIx::Simple::Examples 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".
Class methods
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.
For new connections, PrintError is disabled by default. If you enable it, beware that it will report line numbers in DBIx/Simple.pm.
For new connections, RaiseError is enabled by default unless the environment variable "PERL_DBIX_SIMPLE_NO_RAISEERROR" is set to a non-empty non-0 value.
This method is the constructor and returns a DBIx::Simple object on success. On failure, it returns undef.
Object methods
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.
Requires Mark Stosberg's SQL::Interp, which is available from CPAN. SQL::Interp is a fork from David Manura's SQL::Interpolate.
Requires Nathan Wiger's SQL::Abstract, which is available from CPAN.
"begin" is an alias for "begin_work".
Object properties
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.
Requires Nathan Wiger's SQL::Abstract, which 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.
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.
Methods documented to return "a list" return a reference to an array of the same in scalar context, unless something else is explicitly mentioned.
Bound variables are very efficient. Binding a tied variable doesn't work.
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.
Fetching a single row at a time
Subsequent fetches (using any method) may change the values in the variables passed and the returned reference's array.
Fetching all remaining rows
With unique keys, use "map". With non-unique keys, use "group", which gives an array of values per key.
With unique keys, use "map_hashes", which gives a single hash per key. With non-unique keys, use "group_hashes", which gives an array of hashes per key.
With unique keys, use "map_arrays", which gives a single array per key. With non-unique keys, use "group_arrays", which gives an array of arrays per key.
Requires Jeffrey Hayes Anderson's DBIx::XHTML_Table, which is available from CPAN.
In general, using the "html" method (described below) is much easier. "xto" is available in case you need more flexibility. Not affected by "lc_columns".
Requires Jeffrey Hayes Anderson's DBIx::XHTML_Table, which 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 );
"table" and "box" require Anno Siegel's Text::Table, which is available from CPAN.
DBIx::Simple has basic support for returning results as objects. The actual construction method has to be provided by the chosen class, making this functionality rather advanced and perhaps unsuited for beginning programmers.
When the "object" or "objects" method is called on the result object returned by one of the query methods, two approaches are tried. In either case, pass the name of a class as the first argument. A prefix of a single colon can be used as an alias for "DBIx::Simple::Result::", e.g. ":Example" is short for "DBIx::Simple::Result::Example". Modules are loaded on demand.
The default class when no class is given, is ":RowObject". It requires Jos Boumans' Object::Accessor, which is available from CPAN.
Simple object construction
When "object" is given a class that provides a "new" method, but not a "new_from_dbix_simple" method, "new" is called with a list of interleaved column names and values, like a flattened hash, but ordered. "objects" causes "new" to be called multiple times, once for each remaining row.
Example:
{ package DBIx::Simple::Result::ObjectExample; sub new { my ($class, %args) = @_; return bless $class, \%args; } sub foo { ... } sub bar { ... } } $db->query('SELECT foo, bar FROM baz')->object(':ObjectExample')->foo();
Advanced object construction
When "object" or "objects" is given a class that provides a "new_from_dbix_simple" method, any "new" is ignored, and "new_from_dbix_simple" is called with a list of the DBIx::Simple::Result object and any arguments passed to "object" or "objects".
"new_from_dbix_simple" is called in scalar context for "object", and in list context for "objects". In scalar context, it should fetch exactly one row, and in list context, it should fetch all remaining rows.
Example:
{ package DBIx::Simple::Result::ObjectExample; sub new_from_dbix_simple { my ($class, $result, @args) = @_; return map { bless $class, $_ } $result->hashes if wantarray; return bless $class, $result->hash; } sub foo { ... } sub bar { ... } } $db->query('SELECT foo, bar FROM baz')->object(':ObjectExample')->foo();
The mapping methods do not check whether the keys are unique. Rows that are fetched later overwrite earlier ones.
Pick your favourite OSI approved license :)
http://www.opensource.org/licenses/alphabetical
Juerd Waalboer <#####@juerd.nl> <http://juerd.nl/>
perl, perlref
DBI, DBIx::Simple::Examples, SQL::Abstract, DBIx::XHTML_Table
2023-09-01 | perl v5.36.0 |