NAME

DB::Object - SQL API

SYNOPSIS

use DB::Object;

my $dbh = DB::Object->connect({
driver => 'Pg',
conf_file => 'db-settings.json',
database => 'webstore',
host => 'localhost',
login => 'store-admin',
schema => 'auth',
debug => 3,
}) || bailout( "Unable to connect to sql server on host localhost: ", DB::Object->error );

# Legacy regular query
my $sth = $dbh->prepare( "SELECT login,name FROM login WHERE login='jack'" ) ||
die( $dbh->errstr() );
$sth->execute() || die( $sth->errstr() );
my $ref = $sth->fetchrow_hashref();
$sth->finish();

# Get a list of databases;
my @databases = $dbh->databases;
# Doesn't exist? Create it:
my $dbh2 = $dbh->create_db( 'webstore' );
# Load some sql into it
my $rv = $dbh2->do( $sql ) || die( $dbh->error );

# Check a table exists
$dbh->table_exists( 'customers' ) || die( "Cannot find the customers table!\n" );

# Get list of tables, as array reference:
my $tables = $dbh->tables;

my $cust = $dbh->customers || die( "Cannot get customers object." );
$cust->where( email => 'john@example.org' );
my $str = $cust->delete->as_string;
# Becomes: DELETE FROM customers WHERE email='john\@example.org'

# Do some insert with transaction
$dbh->begin_work;
# Making some other inserts and updates here...
my $cust_sth_ins = $cust->insert(
    first_name => 'Paul',
    last_name => 'Goldman',
    email => 'paul@example.org',
    active => 0,
) || do
{
    # Rollback everything since the begin_work
    $dbh->rollback;
    die( "Error while create query to add data to table customers: " . $cust->error );
};
$result = $cust_sth_ins->as_string;
# INSERT INTO customers (first_name, last_name, email, active) VALUES('Paul', 'Goldman', 'paul\@example.org', '0')
$dbh->commit;
## Get the last used insert id
my $id = $dbh->last_insert_id();

$cust->where( email => 'john@example.org' );
$cust->order( 'last_name' );
$cust->having( email => qr/\@example/ );
$cust->limit( 10 );
my $cust_sth_sel = $cust->select || die( "An error occurred while creating a query to select data frm table customers: " . $cust->error );
# Becomes:
# SELECT id, first_name, last_name, email, created, modified, active, created::ABSTIME::INTEGER AS created_unixtime, modified::ABSTIME::INTEGER AS modified_unixtime, CONCAT(first_name, ' ', last_name) AS name FROM customers WHERE email='john\@example.org' HAVING email ~ '\@example' ORDER BY last_name LIMIT 10

$cust->reset;
$cust->where( email => 'john@example.org' );
my $cust_sth_upd = $cust->update( active => 0 )
# Would become:
# UPDATE ONLY customers SET active='0' WHERE email='john\@example.org'

## Lets' dump the result of our query
## First to STDERR
$login->where( "login='jack'" );
$login->select->dump();
## Now dump the result to a file
$login->select->dump( "my_file.txt" );

VERSION

v0.9.7

DESCRIPTION

DB::Object is a SQL API much alike DBI. So why use a private module instead of using that great DBI package?

At first, I started to inherit from DBI to conform to perlmod perl manual page and to general perl coding guidlines. It became very quickly a real hassle. Barely impossible to inherit, difficulty to handle error, too much dependent from an API that change its behaviour with new versions. In short, I wanted a better, more accurate control over the SQL connection.

So, DB::Object acts as a convenient, modifiable wrapper that provide the programmer with an intuitive, user-friendly and hassle free interface.

CONSTRUCTOR

METHODS

OPERATORS

AND( VALUES )

Given a value, this returns a DB::Object::AND object. You can retrieve the value with value

This is used by where

my $op = $dbh->AND( login => 'joe', status => 'active' );
## will produce:
WHERE login = 'joe' AND status = 'active'

NOT( VALUES )

Given a value, this returns a DB::Object::NOT object. You can retrieve the value with value

This is used by where

my $op = $dbh->AND( login => 'joe', status => $dbh->NOT( 'active' ) );
## will produce:
WHERE login = 'joe' AND status != 'active'

OR( VALUES )

Given a value, this returns a DB::Object::OR object. You can retrieve the value with value

This is used by where

my $op = $dbh->OR( login => 'joe', login => 'john' );
## will produce:
WHERE login = 'joe' OR login = 'john'

COPYRIGHT

Copyright (c) 2019-2020 DEGUEST Pte. Ltd.

CREDITS

Jacques Deguest <jack@deguest.jp>

SEE ALSO

DBI, Apache::DBI