SYNOPSIS

DBIx::Simple

    $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(...)

DBIx::SImple + SQL::Interp

$result = $db->iquery(...)

DBIx::Simple + SQL::Abstract

$db->abstract = SQL::Abstract->new(...)

$result = $db->select(...) $result = $db->insert(...) $result = $db->update(...) $result = $db->delete(...)

DBIx::Simple::Result

@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

DBIx::Simple::Result + DBIx::XHTML_Table

$html = $result->html(...)

$table_object = $result->xto(...)

Examples

Please read DBIx::Simple::Examples for code examples.

DESCRIPTION

DBIx::Simple provides a simplified interface to \s-1DBI\s0, 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 \*(C`query\*(C' 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 \*(C`$db->query(...) or die $db->error\*(C'.

DBIx::Simple methods

The \*(C`connect\*(C' or \*(C`new\*(C' class method takes either an existing \s-1DBI\s0 object ($dbh), or a list of arguments to pass to \*(C`DBI->connect\*(C'. See \s-1DBI\s0 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. When true at time of query execution, makes \*(C`columns\*(C', \*(C`hash\*(C', \*(C`hashes\*(C', and \*(C`map_hashes\*(C' use lower cased column names. \*(C`lc_columns\*(C' is true by default. 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). \*(C`keep_statements\*(C' 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. Class to use for result objects. Defaults to DBIx::Simple::Result. A constructor is not used. Returns the error string of the last \s-1DBI\s0 method. See the discussion of "\*(C`err\*(C'\*(L" and \*(R"\*(C`errstr\*(C'" in \s-1DBI\s0. The \*(C`query\*(C' method prepares and executes the query and returns a result object. If the string \*(C`(??)\*(C' 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. Uses SQL::Interp to interpolate values into a query, and uses the resulting generated query and bind arguments with \*(C`query\*(C'. See SQL::Interp's documentation for usage information. Requires that Mark Stosberg's SQL::Interp module be installed. It is available from \s-1CPAN\s0. SQL::Interp is a fork from David Manura's SQL::Interpolate. Calls the respective method on \*(C`abstract\*(C', and uses the resulting generated query and bind arguments with \*(C`query\*(C'. See SQL::Abstract's documentation for usage information. You can override the object by assigning to the \*(C`abstract\*(C' property. Obviously, calling \*(C`query\*(C' directly is faster for the computer and using these abstracting methods is faster for the programmer. Sets the object to use with the \*(C`select\*(C', \*(C`insert\*(C', \*(C`update\*(C' and \*(C`delete\*(C' 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 \s-1CPAN\s0. 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 \*(C`query\*(C' method. These transaction related methods call the \s-1DBI\s0 respective methods and Do What You Mean. See \s-1DBI\s0 for details. \*(C`begin\*(C' is an alias for \*(C`begin_work\*(C'. This calls the \*(C`func\*(C' method of \s-1DBI\s0. See \s-1DBI\s0 for details. This calls the \*(C`last_insert_id\*(C' method of \s-1DBI\s0. See \s-1DBI\s0 for details. Note that this feature requires \s-1DBI\s0 1.38 or newer. 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. Destroys (finishes) active statements and disconnects. Whenever the database object is destroyed, this happens automatically if DBIx::Simple handled the connection (i.e. you didn't use an existing \s-1DBI\s0 handle). After disconnecting, you can no longer use the database object or any of its result objects.

DBIx::Simple::Dummy

The \*(C`query\*(C' 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

Returns a list of column names. In scalar context, returns an array reference. Column names are lower cased if \*(C`lc_columns\*(C' was true when the query was executed. Binds the given \s-1LIST\s0 to the columns. The elements of \s-1LIST\s0 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 \s-1DBI\s0's \*(C`bind_columns\*(C', the \*(C`\\*(C' operator is not needed. Bound variables are very efficient. Binding a tied variable doesn't work. 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. Combines \*(C`bind\*(C' with \*(C`fetch\*(C'. Returns what \*(C`fetch\*(C' returns. Fetches a single row and returns a list of values. In scalar context, returns only the last value. Fetches a single row and returns an array reference. Fetches a single row and returns a hash reference. Keys are lower cased if \*(C`lc_columns\*(C' was true when the query was executed. Fetches all remaining rows and returns a flattened list. In scalar context, returns an array reference. Fetches all remaining rows and returns a list of array references. In scalar context, returns an array reference. Fetches all remaining rows and returns a list of hash references. In scalar context, returns an array reference. Keys are lower cased if \*(C`lc_columns\*(C' was true when the query was executed. Constructs a hash of array references keyed by the values in the chosen column. In scalar context, returns a hash reference. In list context, returns interleaved keys and values. Constructs a hash of hash references keyed by the values in the chosen column. In scalar context, returns a hash reference. In list context, returns interleaved keys and values. Constructs a simple hash, using the two columns as key/value pairs. Should only be used with queries that return two columns. In scalar context, returns a hash reference. In list context, returns interleaved keys and values. 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 \s-1SELECT\s0 statements, it is generally not possible to know how many rows are returned. MySQL does provide this information. See \s-1DBI\s0 for a detailed explanation. Returns a DBIx::XHTML_Table object, passing the constructor a reference to %attr. Requires that Jeffrey Hayes Anderson's DBIx::XHTML_Table module be installed. It is available from \s-1CPAN\s0. In general, using the \*(C`html\*(C' method (described below) is much easier. \*(C`xto\*(C' is available in case you need more flexibility. This method ignores the \*(C`lc_columns\*(C' property. Returns an (X)HTML formatted table, using the DBIx::XHTML_Table module. Passes a reference to %attr to both the constructor and the \*(C`output\*(C' method. Requires that Jeffrey Hayes Anderson's DBIx::XHTML_Table module be installed. It is available from \s-1CPAN\s0. 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 ); Returns a string with a simple text representation of the data. $type can be any of: \*(C`neat\*(C', \*(C`table\*(C', \*(C`box\*(C'. It defaults to \*(C`table\*(C' if Text::Table is installed, to \*(C`neat\*(C' if it is. \*(C`table\*(C' and \*(C`box\*(C' require that Anno Siegel's Text::Table module be installed. It is available from \s-1CPAN\s0. Returns a copy of an sth attribute (property). See \*(L"Statement Handle Attributes\*(R" in \s-1DBI\s0 for details. This calls the \*(C`func\*(C' method of \s-1DBI\s0. See \s-1DBI\s0 for details. 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

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.

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 <[email protected]> <http://juerd.nl/>

RELATED TO DBIx::Simple…

perl, perlref

\s-1DBI\s0, DBIx::Simple::Examples, SQL::Abstract, DBIx::XHTML_Table