Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Perl
Tree: 86ca77c46b

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
docs
eg
lib/DBIx
t
xt
.gitignore
.shipit
Changes
MANIFEST
MANIFEST.SKIP
Makefile.PL
README
README.pod

README.pod

NAME

DBIx::Kohada - ActiveRecord

SYNOPSIS

    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});
    }

DESCRIPTION

DBIx::Kohada is yet another O/R mapper based on Active Record strategy.

WHY ANOTHER ONE?

I had using Class::DBI, DBIx::Class, and DBIx::Skinny. But the three O/R mappers are not enough for me.

METHODS

my $db = DBIx::Kohada->new(%args);

Create new instance of DBIx::Kohada. The arguments are following:

dbh

Database handle. Required.

schema

Instance of DBIx::Kohada::Schema. Required.

quote_char

quote_char for quoting identifiers. Optional.

name_sep

name_sep for join the identifiers. Optional.

query_builder

Instance of SQL::Maker. Optional.

my $iter = $db->new_iterator();

Create a new instance of DBIx::Kohada::Iterator. It's normally used internally.

__PACKAGE__->load_plugin($name, \%opt);

You can load a plugin.

The arguments are:

$name

The package name of the plugin. You can specify the "Pager" to load "DBIx::Kohada::Plugin::Pager", and "+MyApp::Plugin::Pager" for loading "MyApp::Plugin::Pager"(It's like Catalyst).

\%opt

You can specify the following options:

$opt->{alias} : HashRef

You can specify the method alias map to resolve method name conflict.

Return: nothing.

$db->single($table_name, \%where, \%opt);

Get the single row from database. The arguments are passing to $db->search().

my $iter = $db->search($table_name, \%where, \%opt);
my @rows = $db->search($table_name, \%where, \%opt);

SELECT rows from database.

The arguments are passing to $db->query_builder.

Return: Instance of iterator in scalar context, array of rows in list context.

my $query = $db->new_query_object();

Create a new instance of SQL::Maker::Select object from the settings.

my $iter = $db->search_by_query_object($table_name, $query);
my @rows = $db->search_by_query_object($table_name, $query);

SELECT rows by $query. $query is a instance of SQL::Maker::Select.

$table_name is using to determine row class. If it's undef, each rows are bless to DBIx::Kohada::AnonRow.

Return: Instance of iterator in scalar context, array of rows in list context.

my $iter = $db->search_by_sql($table_name, $sql, @binds);
my @rows = $db->search_by_sql($table_name, $sql, @binds);

SELECT rows by $sql. $sql is a string contains SQL.

$table_name is using to determine row class. If it's undef, each rows are bless to DBIx::Kohada::AnonRow.

Return: Instance of iterator in scalar context, array of rows in list context.

my $row = $db->insert($table, \%values, \%opts);

Send the INSERT statement for the database. The SQL is construct by $db->query_builder.

If you want to use "INSERT IGNORE" query, you can use $opt->{prefix} = "INSERT IGNORE " . If you want to use "REPLACE INTO ..." query, you can use $opt->{prefix} = "REPLACE " .

Return: In scalar context, this method sends SELECT query and get fresh data. In void context, this method don't works anymore. But if you set a AFTER_INSERT trigger, Kohada sends SELECT query.

my $row = $db->retrieve($table, \@pk);
my $row = $db->retrieve($table, $pk);

Fetch a row by primary keys.

$db->bulk_insert($table, \@rows);

This method inserts much rows at once for performance. This method *DON'T* CALL TRIGGERS for performance. Be careful.

On MySQL, Kohada make multiline insert statement by SQL::Maker::Plugin::InsertMulti. On other RDBMS, Kohada makes normal insert statement.

$db->delete($table, \%where);

DELETE rows from database. The query is constructed by $db->query_builder.

Return: none

$db->update($table, \%set, \%where);

UPDATE rows from database. The query is constructed by $db->query_builder.

Return: none

my $transaction_manager = $db->transaction_manager();

Returns instance of DBIx::TransactionManager for $db->dbh.

my $txn = $db->txn_scope();

Make a scoped session. You can nest the transaction.

For more details, see DBIx::TransactionManager.

FAQ

How do you use tracer like DBIx::Skinny::Profiler::Trace?

You can use tracer by DBI. And you can use the advanced tracer like this article: http://d.hatena.ne.jp/tokuhirom/20101201/1291196584.

How do you use relationships?

It is not supported in core. You can write a plugin to do it.

How do you use on_connect_do like DBIC?

use $dbh->{Callbacks}->{connected}.

How do you use display the profiling result like DBIx::Skinny::Profiler?

use Devel::KYTProf.

How do you display pretty error message?

use DBI's callback functions. for more details, see eg/dbi-callback.pl.

How do you load child classes automatically?

use Module::Find.

    use Module::Find;
    my $schema = DBIx::Kohada::Schema->new();
    $schema->register_row_class($_) for useall "MyApp::DB::Row";
How do you handle UTF-8 columns?

You should use mysql_enable_utf8, sqlite_unicode, etc.

Why don't you implement 'find_or_create' method?

It is not atomic operation. It makes issue at sometime.

How do you inflate by rule like DBIx::Skinny?

You can use following snippet code.

    for my $table ($schema->tables()) {
        my $row_classs = $schema->table_name2row_class($table->name);
        for my $column ($table->columns()) {
            if ($column eq 'ctime') {
                $row_class->set_inflation_rule(
                    $column => sub { Time::Piece->new($_[0]) }
                );
                $row_class->set_deflation_rule(
                    $column => sub { $_[0]->epoch }
                );
            }
        }
    }

AUTHOR

Tokuhiro Matsuno <tokuhirom AAJKLFJEF GMAIL COM>

SEE ALSO

LICENSE

Copyright (C) Tokuhiro Matsuno

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

Something went wrong with that request. Please try again.