Your SlideShare is downloading. ×
0
DBIC 2 - Resultsets
DBIC 2 - Resultsets
DBIC 2 - Resultsets
DBIC 2 - Resultsets
DBIC 2 - Resultsets
DBIC 2 - Resultsets
DBIC 2 - Resultsets
DBIC 2 - Resultsets
DBIC 2 - Resultsets
DBIC 2 - Resultsets
DBIC 2 - Resultsets
DBIC 2 - Resultsets
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

DBIC 2 - Resultsets

3,498

Published on

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

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,498
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
43
Comments
0
Likes
3
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. DBIx::Class Resultsets Help resultsets help you get results.
  • 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. 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. 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. Iterating my $campaigns = $schema-&gt; resultset ( ‘Main::Campaign’ ); while (my $campaign = $campaigns-&gt; next ()) { print $campaign-&gt;campaign_name() . “ ”; }
  • 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. Searching my $campaigns = $schema-&gt; resultset ( ‘Main::Campaign’ ); $campaigns = $campaigns-&gt; search ( { status =&gt; 1 }, ); while (my $campaign = $campaigns-&gt; next ()) { print $campaign-&gt;campaign_name() . “ ”; }
  • 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. Building a Search $campaigns = $campaigns-&gt; search ( { status =&gt; 1 }, { page =&gt; 3, rows =&gt; 20 }, ); $campaigns = $campaigns-&gt;search( undef, { order_by =&gt; [‘aid’,’cid’] }, ); $campaigns = $campaigns-&gt;search( { tid =&gt; 5 }, ); print $campaigns-&gt;count(); # SELECT COUNT(*) FROM main.campaign # WHERE status = 1 AND tid = 5 # ORDER BY aid, cid LIMIT 41, 60
  • 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. Pagination $campaigns = $campaigns-&gt; search ( { status =&gt; 1 }, { page =&gt; 3, rows =&gt; 20 }, ); my $pager = $campaigns-&gt;pager(); printf( “Viewing %d - %d of %d. ”, $pager-&gt;first(), $pager-&gt;last(), $pager-&gt;total_entries(), ); while (my $campaign = $campaigns-&gt; next ()) { print $campaign-&gt;campaign_name() . “ ”; }
  • 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>

×