DBIC 2 - Resultsets
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

DBIC 2 - Resultsets

on

  • 5,018 views

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.

Statistics

Views

Total Views
5,018
Views on SlideShare
4,995
Embed Views
23

Actions

Likes
2
Downloads
42
Comments
0

1 Embed 23

http://www.slideshare.net 23

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

DBIC 2 - Resultsets Presentation Transcript

  • 1. DBIx::Class Resultsets Help resultsets help you get results.
  • 2. 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.
  • 3. 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.
  • 4. Don’t be Scared
    • This is not a Microsoft style cursor.
    • This is much more than that.
  • 5. Iterating my $campaigns = $schema-> resultset ( ‘Main::Campaign’ ); while (my $campaign = $campaigns-> next ()) { print $campaign->campaign_name() . “ ”; }
  • 6. 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.
  • 7. Searching my $campaigns = $schema-> resultset ( ‘Main::Campaign’ ); $campaigns = $campaigns-> search ( { status => 1 }, ); while (my $campaign = $campaigns-> next ()) { print $campaign->campaign_name() . “ ”; }
  • 8. 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
  • 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. 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.
  • 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. Resources
    • DBIx::Class::ResultSet POD @ CPAN http://search.cpan.org/~jrobinson/DBIx-Class-0.08009/lib/DBIx/Class/ResultSet.pm
    • Iterator @ Wikipedia http://en.wikipedia.org/wiki/Iterator
    • SQL::Abstract @ CPAN http://search.cpan.org/~nwiger/SQL-Abstract-1.22/lib/SQL/Abstract.pm