DBIC 2 - Resultsets
-
Upload
aran-deltac -
Category
Technology
-
view
5.094 -
download
0
description
Transcript of DBIC 2 - Resultsets
DBIx::Class Resultsets
Help resultsets help you get results.
Resultset
• A resultset is an object that represents a SQL query.
• Resultsets are much more flexible than SQL queries.
• There are many different ways to create resultsets.
• A resultset is an iterator.
Iterator
• In computer science, an iterator is an object which allows a programmer to traverse through all the elements of a collection, regardless of its specific implementation.
• An iterator is sometimes called a cursor, especially within the context of a database.
Don’t be Scared
• This is not a Microsoft style cursor.• This is much more than that.
Iterating
my $campaigns = $schema->resultset( ‘Main::Campaign’ );
while (my $campaign = $campaigns->next()) {
print $campaign->campaign_name() . “\n”;
}
Search
• The search method further refines the resultset.
• This includes modifying the WHERE clause and any other part of the query, such as LIMIT, ORDER BY, JOINs, etc.
• Returns a new resultset (the original remains untouched).
• Sets up the query, but does NOT execute the query.
Searching
my $campaigns = $schema->resultset( ‘Main::Campaign’ );
$campaigns = $campaigns->search( { status => 1 },);
while (my $campaign = $campaigns->next()) {
print $campaign->campaign_name() . “\n”;
}
More on Search
• search( $conditions, $attributes );• Both arguments are hash refs.• $attributes is optional.• $conditions is not optional, but may be specified as undef.
• Combines the conditions and attributes of the originating resultset with the new one.
• $conditions contains SQL::Abstract data structures.• $attributes may specify:
order_by, columns, include_columns, select, +select, +as, as, join, prefetch, page, rows, offest, group_by, having, distinct, where, cache, from
Building a Search
$campaigns = $campaigns->search( { status => 1 }, { page => 3, rows => 20 },);
$campaigns = $campaigns->search( undef, { order_by => [‘aid’,’cid’] },);
$campaigns = $campaigns->search( { tid => 5 },);
print $campaigns->count();
# SELECT COUNT(*) FROM main.campaign# WHERE status = 1 AND tid = 5# ORDER BY aid, cid LIMIT 41, 60
Paging Results
• An example of the simplicity that DBIC allows.
• Uses Data::Page.
• Configurable through the ‘page’ and ‘rows’ attributes.
• Accessible through the pager() and the lesser used page() methods.
Pagination
$campaigns = $campaigns->search( { status => 1 }, { page => 3, rows => 20 },);
my $pager = $campaigns->pager();
printf( “Viewing %d - %d of %d.\n”, $pager->first(), $pager->last(), $pager->total_entries(),);
while (my $campaign = $campaigns->next()) {
print $campaign->campaign_name() . “\n”;
}
Resources
• DBIx::Class::ResultSet POD @ CPANhttp://search.cpan.org/~jrobinson/DBIx-Class-0.08009/lib/DBIx/Class/ResultSet.pm
• Iterator @ Wikipediahttp://en.wikipedia.org/wiki/Iterator
• SQL::Abstract @ CPANhttp://search.cpan.org/~nwiger/SQL-Abstract-1.22/lib/SQL/Abstract.pm