DBIC 2 - Resultsets

3,760 views
3,676 views

Published on

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

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,760
On SlideShare
0
From Embeds
0
Number of Embeds
45
Actions
Shares
0
Downloads
43
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

DBIC 2 - Resultsets

  1. 1. DBIx::Class Resultsets Help resultsets help you get results.
  2. 2. Resultset <ul><li>A resultset is an object that represents a SQL query. </li></ul><ul><li>Resultsets are much more flexible than SQL queries. </li></ul><ul><li>There are many different ways to create resultsets. </li></ul><ul><li>A resultset is an iterator. </li></ul>
  3. 3. Iterator <ul><li>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. </li></ul><ul><li>An iterator is sometimes called a cursor, especially within the context of a database. </li></ul>
  4. 4. Don’t be Scared <ul><li>This is not a Microsoft style cursor. </li></ul><ul><li>This is much more than that. </li></ul>
  5. 5. Iterating my $campaigns = $schema-> resultset ( ‘Main::Campaign’ ); while (my $campaign = $campaigns-> next ()) { print $campaign->campaign_name() . “ ”; }
  6. 6. Search <ul><li>The search method further refines the resultset. </li></ul><ul><li>This includes modifying the WHERE clause and any other part of the query, such as LIMIT, ORDER BY, JOINs, etc. </li></ul><ul><li>Returns a new resultset (the original remains untouched). </li></ul><ul><li>Sets up the query, but does NOT execute the query. </li></ul>
  7. 7. Searching my $campaigns = $schema-> resultset ( ‘Main::Campaign’ ); $campaigns = $campaigns-> search ( { status => 1 }, ); while (my $campaign = $campaigns-> next ()) { print $campaign->campaign_name() . “ ”; }
  8. 8. More on Search <ul><li>search( $conditions, $attributes ); </li></ul><ul><li>Both arguments are hash refs. </li></ul><ul><li>$attributes is optional. </li></ul><ul><li>$conditions is not optional, but may be specified as undef . </li></ul><ul><li>Combines the conditions and attributes of the originating resultset with the new one. </li></ul><ul><li>$conditions contains SQL::Abstract data structures. </li></ul><ul><li>$attributes may specify: order_by, columns, include_columns, select, +select, +as, as, join, prefetch, page, rows, offest, group_by, having, distinct, where, cache, from </li></ul>
  9. 9. 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
  10. 10. Paging Results <ul><li>An example of the simplicity that DBIC allows. </li></ul><ul><li>Uses Data::Page. </li></ul><ul><li>Configurable through the ‘page’ and ‘rows’ attributes. </li></ul><ul><li>Accessible through the pager() and the lesser used page() methods. </li></ul>
  11. 11. Pagination $campaigns = $campaigns-> search ( { status => 1 }, { page => 3, rows => 20 }, ); my $pager = $campaigns->pager(); printf( “Viewing %d - %d of %d. ”, $pager->first(), $pager->last(), $pager->total_entries(), ); while (my $campaign = $campaigns-> next ()) { print $campaign->campaign_name() . “ ”; }
  12. 12. Resources <ul><li>DBIx::Class::ResultSet POD @ CPAN http://search.cpan.org/~jrobinson/DBIx-Class-0.08009/lib/DBIx/Class/ResultSet.pm </li></ul><ul><li>Iterator @ Wikipedia http://en.wikipedia.org/wiki/Iterator </li></ul><ul><li>SQL::Abstract @ CPAN http://search.cpan.org/~nwiger/SQL-Abstract-1.22/lib/SQL/Abstract.pm </li></ul>

×