7. Initialization
• Passing $dbh
my $dbix = DBIx::Lite->new(dbh => $dbh);
• Using DBIx::Connector
my $dbix = DBIx::Lite->connect(
'dbi:mysql:dbname=...',
$username, $password, {
mysql_enable_utf8 => 1,
RootClass => 'DBIx::Sunny',
},
);
8. CRUD
my $entries = $dbix->table('entries');
• Call table() to start building query
• Returns ResultSet object
9. SELECT
• Build ResultSet by method chain
my $entries_rs = $dbix->table('entries')
->search({ author_id => 1 })
->order_by('created_at');
• Finally retrieve row objects
my @entries = $entries_rs->all;
# SELECT me.* FROM entries AS me
WHERE ( author_id = '1' ) ORDER BY created_at
my $entry = $entries_rs->limit(1)->single;
# SELECT me.* FROM entries AS me
WHERE ( author_id = '1' ) ORDER BY created_at
LIMIT 1 OFFSET 0
10. INSERT
my $entry = $dbix->table('entries')->insert({
author_id => 47,
body => '…',
created_at => time(),
});
# INSERT INTO entries ( author_id, body, created_at)
VALUES ( '47', '…', '1345196410' )
• If the table has an auto-
incremented pkey, LAST_INSERT_ID
is filled in
11. UPDATE/DELETE
$dbix->table('entries')
->search({ id => [ 2, 3, 5 ] })
->update({ body => '…' });
# UPDATE entries AS me SET body = '…' WHERE ( ( id = '2' OR
id = '3' OR id = '5' ) )
$dbix->schema->table('entries')->pk('id');
$entry->update({ body => '…' });
# UPDATE entries AS me SET body = '…' WHERE ( id = '1' )
14. DBIx::Lite
• Represents a database (connection)
• $dbix->{connector}
• isa DBIx::Connector
• $dbix->{schema}
• isa DBIx::Lite::Schema
• Generates ResultSet (next)
15. DBIL::ResultSet
• Represents a set of database rows
• Has most major methods
• Does not hold actual data in itself
• To retrieve row objects:
my @all = $rs->all;
my $row = $rs->single;
16. DBIL::ResultSet
• Chain methods to build specific
result set
$rs = $dbix->table(…);
$rs = $rs->search(%where); # WHERE
$rs = $rs->order_by($col); # ORDER BY
$rs = $rs->limit($n); # LIMIT
$rs = $rs->select(@cols); # SELECT
• search() uses SQL::Abstract
• Multiple search()’es joined by “AND”
20. DBIL::Schema
& DBIL::Schema::Table
• Setting primary key lets row objects’
update methods to work
• Use autopk() for auto-inc pk
$dbix->schema->table('entries')->pk('id');
• Give row objects custom methods
from the package
$dbix->schema->table('entries')
->class('My::Row::Entry');
21. DBIL::Schema
& DBIL::Schema::Table
• Register has-many relations
$dbix->schema->one_to_many(
'authors.id' => 'entries.author_id'
);
• ResultSet gains relation method
my @entries = $dbix->table('authors')
->search({ name => 'motemen' })
->entries->all;
# SELECT entries.* FROM authors AS me
INNER JOIN entries
ON ( me.id = entries.author_id )
WHERE ( name = 'motemen' )
23. Paging
• Paging by offset/limit
my $entries_rs = $dbix->table('entries')
->rows_per_page(10)->page(3);
$entries_rs->all;
# SELECT COUNT(*) FROM entries AS me LIMIT 10
OFFSET 0
# SELECT me.* FROM entries AS me LIMIT 10 OFFSET 20
my $pager = $entries_rs->pager; # => Data::Page
24. JOIN
$dbix->table('entries')
->left_join('authors', { author_id => 'id' })
->select_also([ 'authors.name' => 'author_name' ])
->all;
# SELECT me.*, authors.name AS `author_name`
FROM entries AS me
LEFT OUTER JOIN authors
ON ( me.author_id = authors.id )
• Use $rs->select_also() to fetch
joined tables
25. Raw DBI access
my $now = $dbix->dbh_do(sub {
$_->selectcol_arrayref('SELECT NOW()')->[0]
});
• Internally uses DBIx::Connector#run
26. CAVEATS
• “SELECT me.* FROM …” queries
• Sometimes need to prefix “me.” to
field names
• Row classes are not automatically
require’d