PrePAN

Sign in to PrePAN

DBIx::Foo sql generator + DBI

Author
punytan@github
Date
URL
https://gist.github.com/1306523
Status
In Review
Good

Synopsis

use utf8;

use DBI;
use SQL::Maker;
local $DBIx::Foo::db::Generator = SQL::Maker->new(driver => 'SQLite'); # default is SQL::Abstract

# $default_attr = {
#    RaiseError => 1,
#    PrintError => 0,
#    ShowErrorStatement => 1,
#    AutoInactiveDestroy => 1,
#    sqlite_unicode => 1, # for DBD::SQLite
#    mysql_enable_utf8 => 1, # for DBD::mysql
# };

my $dbh = DBI->connect(
    'dbi:SQLite:dbname=:memory:', '', '', { RootClass => 'DBIx::Foo' }
);

# or
# use DBIx::Connector;
# my $conn = DBIx::Connector->new(
#    'dbi:SQLite:dbname=:memory:', '', '', { RootClass => 'DBIx::Foo' }
# );

$dbh->do(<<SQL);
CREATE TABLE users (
    id integer primary key,
    name text
)
SQL

for my $name (qw/ きよまさ ゆうき ゆうじ /) {
    $dbh->insert(users => { name => $name });
}

$dbh->last_insert_id;
$dbh->selectall('users', ['*']);
$dbh->select('users', ['*'], { id => 1 });
$dbh->update(users => {name => "ジョン"}, {id => 1});
$dbh->delete(users => { id => 1 });

Description

DBIx::Foo was inspired by DBIx::Simple, DBIx::Sunny and DBIx::Simple::Inject. (I don't name this yet, so Foo)

You can generate SQL and operate CRUD in a single statement.

Returned values are NOT blessed!

You can use SQL::Abstract and SQL::Maker to generate SQL.

local $DBIx::Foo::db::Generator is not cool, I think. Any proposal?

Comments

What's the motivation you implemented this despite existing similar modules?
This module avoid `bless`ing result values since I intend to extend DBI itself. I don't want to write ORM or something like that.
Providing proper default attributes and shortcuts (like selectall() method) are my main purpose. As you know, the interface of DBI is bit weird. I hesitate to say that `$dbh->selectall_arrayref($stmt, {Slice => {}}, @bind);` is clear and enough short to type.

And, other ORM doesn't cooperate with DBIx::Connector esp, ::Simple.

Please sign up to post a review.