DBIx::Kohada - yet another O/R Mapper

2010-12-20

DBIx::Kohada is lightweight OO-ish O/R Mapper. It mostly likes Class::DBI.

The feature of DBIx::Kohada is:

OO

The object have a $dbh , unlike Class::DBI.

Class::DBI holds $dbh in the class variable. It is bad idea. It makes hard to manage connections by users.

Simple code

DBIx::Kohada is readable. It does not inherit Ima::DBI :P

No unintended automatic DB call

DBIx::Kohada does not call RDBMS without user's request.

Efficient pagination

DBIx::Kohada has 2 pagination plugins.

DBIx::Kohada::Plugin::Pager fetches "entries_per_page + 1" rows. And detect "this page has a next page or not".

DBIx::Kohada::Plugin::Pager::MySQLFoundRows uses MySQL's SQL_CALC_FOUND_ROWS. It is faster than calling COUNT(*).

DBIx::Class's pagination makes very slow query at some time. I dislike it.

No transparent cache

I don't like transparent cache on O/R mapper.

No caching in iterator

DBIx::Skinny's iterator caches all data in iterator object. It makes the situation: user uses too much memory. Why user want to use iterator?? User want to save a memory!

Then, DBIx::Kohada's iterator doesn't use a cache.

Programmable meta data API

DBIx::Skinny provides only DSL style API for creating schema. It makes hard to meta programming for schema.

DBIx::Kohada provides complete API for the operating schema.

use SQL::Maker

DBIx::Kohada uses SQL::Maker for creating SQL. SQL::Maker is simple and flexible SQL generator.

    package MyApp::DB::Row::User;
    use parent qw/DBIx::Kohada::Row/;
    __PACKAGE__->set_table('user');
    __PACKAGE__->set_primary_key('user_id');
    __PACKAGE__->add_column($_) for qw/user_id name email/;

    package main;
    use DBIx::Kohada::Schema;
    use DBIx::Kohada;
    use DBI;

    my $schema = DBIx::Kohada::Schema->new();
    $schema->register_row_class('MyApp::DB::Row::User');

    my $dbh = DBI->connect('dbi:SQLite:');
    my $db = DBIx::Kohada->new(
        dbh    => $dbh,
        schema => $schema,
    );
    $db->dbh; # => #dbh
    my $user = $db->insert('user' => {name => 'john', email => 'john@exapmle.com'});
    say $user->name; # => john
    $user->name('mark');
    $user->update;
    $user->delete();

    my @users = $db->search_by_sql('user' => q{SELECT * FROM user WHERE name LIKE 'dai%'});

    {
        my $user = $db->single('user' => {user_id => 3});
        my $iter = $db->search('user' => {user_id => 3});
        my @users = $db->search('user' => {user_id => 3});
    }