|
NAMEClass::DBI::Sweet - Making sweet things sweeter SYNOPSIS package MyApp::DBI;
use base 'Class::DBI::Sweet';
MyApp::DBI->connection('dbi:driver:dbname', 'username', 'password');
package MyApp::Article;
use base 'MyApp::DBI';
use DateTime;
__PACKAGE__->table('article');
__PACKAGE__->columns( Primary => qw[ id ] );
__PACKAGE__->columns( Essential => qw[ title created_on created_by ] );
__PACKAGE__->has_a(
created_on => 'DateTime',
inflate => sub { DateTime->from_epoch( epoch => shift ) },
deflate => sub { shift->epoch }
);
# Simple search
MyApp::Article->search( created_by => 'sri', { order_by => 'title' } );
MyApp::Article->count( created_by => 'sri' );
MyApp::Article->page( created_by => 'sri', { page => 5 } );
MyApp::Article->retrieve_all( order_by => 'created_on' );
# More powerful search with deflating
$criteria = {
created_on => {
-between => [
DateTime->new( year => 2004 ),
DateTime->new( year => 2005 ),
]
},
created_by => [ qw(chansen draven gabb jester sri) ],
title => {
-like => [ qw( perl% catalyst% ) ]
}
};
MyApp::Article->search( $criteria, { rows => 30 } );
MyApp::Article->count($criteria);
MyApp::Article->page( $criteria, { rows => 10, page => 2 } );
MyApp::Article->retrieve_next( $criteria,
{ order_by => 'created_on' } );
MyApp::Article->retrieve_previous( $criteria,
{ order_by => 'created_on' } );
MyApp::Article->default_search_attributes(
{ order_by => 'created_on' } );
# Automatic joins for search and count
MyApp::CD->has_many(tracks => 'MyApp::Track');
MyApp::CD->has_many(tags => 'MyApp::Tag');
MyApp::CD->has_a(artist => 'MyApp::Artist');
MyApp::CD->might_have(liner_notes
=> 'MyApp::LinerNotes' => qw/notes/);
MyApp::Artist->search({ 'cds.year' => $cd }, # $cd->year subtituted
{ order_by => 'artistid DESC' });
my ($tag) = $cd->tags; # Grab first tag off CD
my ($next) = $cd->retrieve_next( { 'tags.tag' => $tag },
{ order_by => 'title' } );
MyApp::CD->search( { 'liner_notes.notes' => { "!=" => undef } } );
MyApp::CD->count(
{ 'year' => { '>', 1998 }, 'tags.tag' => 'Cheesy',
'liner_notes.notes' => { 'like' => 'Buy%' } } );
# Multi-step joins
MyApp::Artist->search({ 'cds.tags.tag' => 'Shiny' });
# Retrieval with pre-loading
my ($cd) = MyApp::CD->search( { ... },
{ prefetch => [ qw/artist liner_notes/ ] } );
$cd->artist # Pre-loaded
# Caching of resultsets (*experimental*)
__PACKAGE__->default_search_attributes( { use_resultset_cache => 1 } );
DESCRIPTIONClass::DBI::Sweet provides convenient count, search, page, and cache functions in a sweet package. It integrates these functions with "Class::DBI" in a convenient and efficient way. RETRIEVING OBJECTSAll retrieving methods can take the same criteria and attributes. Criteria is the only required parameter. criteriaCan be a hash, hashref, or an arrayref. Takes the same options as the SQL::Abstract "where" method. If values contain any objects, they will be deflated before querying the database. attributes
countReturns a count of the number of rows matching the criteria. "count" will discard "offset", "order_by", and "rows". $count = MyApp::Article->count(%criteria); searchReturns an iterator in scalar context, or an array of objects in list context. @objects = MyApp::Article->search(%criteria);
$iterator = MyApp::Article->search(%criteria);
search_likeAs search but adds the attribute { cmp => 'like' }. pageRetuns a page object and an iterator. The page object is an instance of Data::Page. ( $page, $iterator )
= MyApp::Article->page( $criteria, { rows => 10, page => 2 );
printf( "Results %d - %d of %d Found\n",
$page->first, $page->last, $page->total_entries );
pagerAn alias to page. retrieve_allSame as "Class::DBI" with addition that it takes "attributes" as arguments, "attributes" can be a hash or a hashref. $iterator = MyApp::Article->retrieve_all( order_by => 'created_on' ); retrieve_nextReturns the next record after the current one according to the order_by attribute (or primary key if no order_by specified) matching the criteria. Must be called as an object method. retrieve_previousAs retrieve_next but retrieves the previous record. CACHING OBJECTSObjects will be stored deflated in cache. Only "Primary" and "Essential" columns will be cached. cacheClass method: if this is set caching is enabled. Any cache object that has a "get", "set", and "remove" method is supported. __PACKAGE__->cache(
Cache::FastMmap->new(
share_file => '/tmp/cdbi',
expire_time => 3600
)
);
cache_keyReturns a cache key for an object consisting of class and primary keys. Overloaded methods
UNIVERSALLY UNIQUE IDENTIFIERSIf enabled a UUID string will be generated for primary column. A CHAR(36) column is suitable for storage. __PACKAGE__->sequence('uuid');
MAINTAINERSFred Moyer <fred@redhotpenguin.com> AUTHORSChristian Hansen <ch@ngmedia.com> Matt S Trout <mstrout@cpan.org> Andy Grundman <andy@hybridized.org> THANKS TODanijel Milicevic, Jesse Sheidlower, Marcus Ramberg, Sebastian Riedel, Viljo Marrandi, Bill Moseley SUPPORT#catalyst on <irc://irc.perl.org> <http://lists.rawmode.org/mailman/listinfo/catalyst> <http://lists.rawmode.org/mailman/listinfo/catalyst-dev> LICENSEThis library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. SEE ALSOClass::DBI Data::Page Data::UUID SQL::Abstract Catalyst <http://cpan.robm.fastmail.fm/cache_perf.html> A comparison of different caching modules for perl.
|