DBIC 2 - Resultsets

12
DBIx::Class Resultsets Help resultsets help you get results.

description

Introduction to DBIx::Class resultsets. Convers some mid-level topics. Assumes some basic DBIC knowledge as well as database and SQL understanding.

Transcript of DBIC 2 - Resultsets

Page 1: DBIC 2 - Resultsets

DBIx::Class Resultsets

Help resultsets help you get results.

Page 2: DBIC 2 - Resultsets

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.

Page 3: DBIC 2 - Resultsets

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.

Page 4: DBIC 2 - Resultsets

Don’t be Scared

• This is not a Microsoft style cursor.• This is much more than that.

Page 5: DBIC 2 - Resultsets

Iterating

my $campaigns = $schema->resultset( ‘Main::Campaign’ );

while (my $campaign = $campaigns->next()) {

print $campaign->campaign_name() . “\n”;

}

Page 6: DBIC 2 - Resultsets

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.

Page 7: DBIC 2 - Resultsets

Searching

my $campaigns = $schema->resultset( ‘Main::Campaign’ );

$campaigns = $campaigns->search( { status => 1 },);

while (my $campaign = $campaigns->next()) {

print $campaign->campaign_name() . “\n”;

}

Page 8: DBIC 2 - Resultsets

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

Page 9: DBIC 2 - Resultsets

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

Page 10: DBIC 2 - Resultsets

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.

Page 11: DBIC 2 - Resultsets

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”;

}

Page 12: DBIC 2 - Resultsets

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