Click here to load reader

Introduction to DBIx::MoCo

  • View
    39

  • Download
    0

Embed Size (px)

DESCRIPTION

Introduction to DBIx::MoCo. Naoya Ito http://www.hatena.ne.jp/. What is DBIx::MoCo?. “ Light and Fast Model Component” O/R Mapper for MySQL and SQLite. Features. Easy SQL operations like CDBI / ActiveRecord (Rails) ‏ Ruby-like list operations - PowerPoint PPT Presentation

Text of Introduction to DBIx::MoCo

Introduction of DBIx::MoCoO/R Mapper for MySQL and SQLite
©2008
Ruby-like list operations
Test fixture
©2008
setup your own classes
package Bookmark::DataBase;
DBIx::MoCo
DBIx::MoCo::
DataBase
Bookmark::
DataBase
package Bookmark::MoCo;
DBIx::MoCo
DBIx::MoCo::
DataBase
Bookmark::
MoCo
Bookmark::
DataBase
uses
package Bookmark::MoCo::Entry;
DBIx::MoCo
DBIx::MoCo::
DataBase
Bookmark::
MoCo
Bookmark::
DataBase
Bookmark::
User
Bookmark::
Entry
uses
say $entry->entry_id;
say $entry->title;
say $entry->url;
## retrieve_by_foo(...) equals retrieve(foo => ...)‏
$entry = moco('Entry')->retrieve_by_url($url);
$entry = moco('Entry')->retrieve_by_entry_id($id);
©2008
url => 'http://d.hatena.ne.jp/%',
is_public => 1
search() : where ... in (...)‏
## SELECT * from entry where entry_id in (1, 2, 5, 6, 10)‏
my @entries = moco('Entry')->search(
],
## If it is not in session, updates are automatically saved.
$entry->save;
size
## create your own list class of Blog::Entry
package Blog::Entry::List;
## $entries is a Blog::Entry::List
my $entries = moco('Entry')->search(...);
package Bookmark::MoCo::Entry;
use Bookmark::MoCo;
my @bookmarks = $entry->bookmarks;
@bookmarks = $entry->bookmarks(10, 50);
©2008
package Bookmark::MoCo::Bookmark;
use Bookmark::MoCo;
my $entry = moco('Entry')->retrieve(...);
## oops, SQL is executed in 1,000 times.
for ($entry->bookmarks) {
say $_->owner->name;
my $entry = moco('Entry')->retrieve(...);
## bookmarks and owners will be retrieved at the same time.
## (SQL stetements are executed only 2 times.)‏
for ($entry->bookmarks(0, 0, {with => [qw/owner/]})) {
say $_->owner->name;
say $entry->timestamp_as_DateTime->hms;
say $entry->url_as_URI->host;
©2008
©2008
inflate_column() without builtin classes
my $entry = moco('Entry')->retrieve(1);
$entry = moco('Entry')->retrieve(1);
$entry->title('foobar');
$entry->save;
©2008
NOTE: "session" is needed when you use caching feature or prefetching.
Blog::MoCo->start_session;
## fixtures/entries.yml
my $f = fixtures(qw/entries/);
is $entry->title, "...";
is $entry->url, "...";
©2008
Pros and Cons
Transparent caching is "DB"
low test coverage
(primary author)‏
©2008