|
NAMEDBIx::Sunny::Schema - SQL Class Builder SYNOPSIS package MyProj::Data::DB;
use parent qw/DBIx::Sunny::Schema/;
use Mouse::Util::TypeConstraints;
subtype 'Uint'
=> as 'Int'
=> where { $_ >= 0 };
subtype 'Natural'
=> as 'Int'
=> where { $_ > 0 };
enum 'Flag' => qw/1 0/;
no Mouse::Util::TypeConstraints;
__PACKAGE__->select_one(
'max_id',
'SELECT max(id) FROM member'
);
__PACKAGE__->select_row(
'member',
id => { isa => 'Natural' }
'SELECT * FROM member WHERE id=?',
);
__PACAKGE__->select_all(
'recent_article',
public => { isa => 'Flag', default => 1 },
offset => { isa => 'Uint', default => 0 },
limit => { isa => 'Uint', default => 10 },
'SELECT * FROM articles WHERE public=? ORDER BY created_on LIMIT ?,?',
);
__PACAKGE__->select_all(
'recent_article',
id => { isa => 'ArrayRef[Uint]' },
'SELECT * FROM articles WHERE id IN(?)',
);
# This method rewrites query like 'id IN (?,?..)' with Array's value number
__PACKAGE__->query(
'add_article',
member_id => 'Natural',
flag => { isa => 'Flag', default => '1' },
subject => 'Str',
body => 'Str',
created_on => { isa => .. },
<<SQL);
INSERT INTO articles (member_id, public, subject, body, created_on)
VALUES ( ?, ?, ?, ?, ?)',
SQL
__PACKAGE__->select_one(
'article_count_by_member',
member_id => 'Natural',
'SELECT COUNT(*) FROM articles WHERE member_id = ?',
);
__PACKAGE__->query(
'update_member_article_count',
article_count => 'Uint',
id => 'Natural'
'UPDATE member SET article_count = ? WHERE id = ?',
);
...
package main;
use MyProj::Data::DB;
use DBIx::Sunny;
my $dbh = DBIx::Sunny->connect(...);
my $db = MyProj::Data::DB->new(dbh=>$dbh,readonly=>0);
my $max = $db->max_id;
my $member_hashref = $db->member(id=>100);
# my $member = $db->member(id=>'abc'); #validator error
my $article_arrayref = $db->recent_article( offset => 10 );
{
my $txn = $db->dbh->txn_scope;
$db->add_article(
member_id => $id,
subject => $subject,
body => $body,
created_on =>
);
my $last_insert_id = $db->dbh->last_insert_id;
my $count = $db->article_count_by_member( id => $id );
$db->update_member_article_count(
article_count => $count,
id => $id
);
$txn->commit;
}
DESCRIPTIONBUILDER CLASS METHODS
FILTERING and DEFLATING
METHODS
AUTHORMasahiro Nagano <kazeburo KZBRKZBR@ gmail.com> SEE ALSO"DBI", "DBIx::TransactionManager", "Data::Validator" LICENSEThis library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
|