NAME
DBIx::DBO - An OO interface to SQL queries and results. Easily constructs SQL queries, and simplifies processing of the returned data.
SYNOPSIS
use DBIx::DBO;
# Create the DBO
my $dbo = DBIx::DBO->connect('DBI:mysql:my_db', 'me', 'mypasswd') or die $DBI::errstr;
# Create a "readonly" connection - useful for a slave database
$dbo->connect_readonly('DBI:mysql:my_db', 'me', 'mypasswd') or die $DBI::errstr;
# Start with a Query object
my $query = $dbo->query('my_table');
# Find records with an 'o' in the name
$query->where('name', 'LIKE', '%o%');
# And with an id that is less than 500
$query->where('id', '<', 500);
# Exluding those with an age range from 20 to 29
$query->where('age', 'NOT BETWEEN', [20, 29]);
# Return only the first 10 rows
$query->limit(10);
# Fetch the rows
while (my $row = $query->fetch) {
# Use the row as an array reference
printf "id=%d name=%s status=%s\n", $row->[0], $row->[1], $row->[4];
# Or as a hash reference
print 'id=', $row->{id}, "\n", 'name=', $row->{name};
# Update/delete rows
$row->update(status => 'Fired!') if $row->{name} eq 'Harry';
$row->delete if $record->{id} == 27;
}
DESCRIPTION
This module provides a convenient and efficient way to access a database. It can construct queries for you and returns the results in easy to use methods.
Once you've created a DBIx::DBO
object using one or both of connect
or connect_readonly
, you can begin creating DBIx::DBO::Query
objects. These are the "workhorse" objects, they encapsulate an entire query with JOINs, WHERE clauses, etc. You need not have to know about what created the Query
to be able to use or modify it. This makes it valuable in environments like mod_perl or large projects that prefer an object oreinted approach to data.
The query is only automatically executed when the data is requested. This is to make it possible to minimise lookups that may not be needed or to delay them as late as possible.
The DBIx::DBO::Row
object returned can be treated as both an arrayref or a hashref. The data is aliased for efficient use of memory. Row
objects can be updated or deleted, even when created by JOINs (If the DB supports it).
METHODS
connect
$dbo = DBIx::DBO->connect($data_source, $username, $password, \%attr)
or die $DBI::errstr;
Takes the same arguments as DBI->connect for a read-write connection to a database. It returns the DBIx::DBO object if the connection succeeds or undefined on failure.
connect_readonly
Takes the same arguments as connect
for a read-only connection to a database. It returns the DBIx::DBO
object if the connection succeeds or undefined on failure.
Both connect
& connect_readonly
can be called on a DBIx::DBO
object to add that respective connection to create a DBIx::DBO
with both read-write and read-only connections.
my $dbo = DBIx::DBO->connect($master_dsn, $username, $password, \%attr)
or die $DBI::errstr;
$dbo->connect_readonly($slave_dsn, $username, $password, \%attr)
or die $DBI::errstr;
dbh
The read-write DBI
handle.
rdbh
The read-only DBI
handle, or if there is no read-only connection, the read-write DBI
handle.
do
$dbo->do($statement) or die $dbo->dbh->errstr;
$dbo->do($statement, \%attr) or die $dbo->dbh->errstr;
$dbo->do($statement, \%attr, @bind_values) or die ...
This provides access to the DBI->do method. It defaults to using the read-write DBI
handle.
selectrow_array
$dbo->selectrow_array($statement, \%attr, @bind_values);
This provides access to the DBI->selectrow_array method.
selectrow_arrayref
$dbo->selectrow_arrayref($statement, \%attr, @bind_values);
This provides access to the DBI->selectrow_arrayref method.
selectall_arrayref
$dbo->selectall_arrayref($statement, \%attr, @bind_values);
This provides access to the DBI->selectall_arrayref method.
table_info
$dbo->table_info($table);
$dbo->table_info([$schema, $table]);
$dbo->table_info($table_object);
Returns a hashref containing PrimaryKeys
, Columns
and Column_Idx
for the table. Mainly for internal use.
table
$dbo->table($table);
$dbo->table([$schema, $table]);
$dbo->table($table_object);
Create and return a new DBIx::DBO::Table object. Tables can be specified by their name or an arrayref of schema and table name or a DBIx::DBO::Table object.
query
$dbo->query($table, ...);
$dbo->query([$schema, $table], ...);
$dbo->query($table_object, ...);
Create a new DBIx::DBO::Query object from the tables specified. In scalar context, just the Query
object will be returned. In list context, the Query
object and DBIx::DBO::Table objects will be returned for each table specified.
my ($query, $table1, $table2) = $dbo->query(['my_schema', 'my_table'], 'my_other_table');
row
$dbo->row($table_object);
$dbo->row($query_object);
Create and return a new DBIx::DBO::Row object.
disconnect
Disconnect both the read-write & read-only connections to the database.
config
$global_setting = DBIx::DBO->config($option);
DBIx::DBO->config($option => $global_setting);
$dbo_setting = $dbo->config($option);
$dbo->config($option => $dbo_setting);
Get or set the global or DBIx::DBO
config settings. When setting an option, the previous value is returned.
Options include:
QuoteIdentifier
Boolean setting to control quoting of SQL identifiers (schema, table and column names). Defaults to 1._Debug_SQL
Set to a number 0 - 2 to warn with varying levels of debugging for each SQL command executed. Defaults to 0.
Global options can also be set when use
'ing the module:
use DBIx::DBO QuoteIdentifier => 0, _Debug_SQL => 1;
AUTHOR
Vernon Lyon, <vlyon AT cpan.org>
SUPPORT
You can find more information for this module at:
RT: CPAN's request tracker http://rt.cpan.org/NoAuth/Bugs.html?Dist=DBIx-DBO
AnnoCPAN: Annotated CPAN documentation http://annocpan.org/dist/DBIx-DBO
CPAN Ratings http://cpanratings.perl.org/d/DBIx-DBO
Search CPAN http://search.cpan.org/dist/DBIx-DBO
BUGS
Please report any bugs or feature requests to bug-dbix-dbo AT rt.cpan.org
, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=DBIx-DBO. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
COPYRIGHT & LICENSE
Copyright 2009 Vernon Lyon, all rights reserved.
This package is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
SEE ALSO
DBI, DBIx::SearchBuilder.