Enable offset based paging
nperez [Mon, 8 Feb 2010 22:47:16 +0000 (16:47 -0600)]
Changes
lib/Catalyst/Controller/DBIC/API.pm
lib/Catalyst/Controller/DBIC/API/RequestArguments.pm
lib/Catalyst/Controller/DBIC/API/StaticArguments.pm

diff --git a/Changes b/Changes
index e4eef49..1e2a764 100644 (file)
--- a/Changes
+++ b/Changes
@@ -5,6 +5,7 @@ Revision history for Catalyst-Controller-DBIC-API: {{ $dist->version }}
 - Fixed missing requirement for MooseX::Types::Structured
 - Fixed MooseX::Aliases usage (we don't any more)
 - Object inflation during end was broken out into its own method: each_object_inflate
+- Enable offset based paging
 
 2.001001
 **************************************************************
index 561508a..89aca62 100644 (file)
@@ -221,7 +221,7 @@ sub deserialize :ActionClass('Deserialize')
     {
         $req_params = CGI::Expand->expand_hash($c->req->params);
 
-        foreach my $param (@{[$self->search_arg, $self->count_arg, $self->page_arg, $self->ordered_by_arg, $self->grouped_by_arg, $self->prefetch_arg]})
+        foreach my $param (@{[$self->search_arg, $self->count_arg, $self->page_arg, $self->offset_arg, $self->ordered_by_arg, $self->grouped_by_arg, $self->prefetch_arg]})
         {
             # these params can also be composed of JSON
             # but skip if the parameter is not provided
@@ -369,7 +369,12 @@ sub list_perform_search
         $req->_set_current_result_set($rs);
 
         $req->_set_search_total_entries($req->current_result_set->pager->total_entries)
-            if $req->has_search_attributes && $req->search_attributes->{page};
+            if $req->has_search_attributes && 
+            (
+                (exists($req->search_attributes->{page}) && defined($req->search_attributes->{page}) && length($req->search_attributes->{page}))
+                ||(exists($req->search_attributes->{offset}) && defined($req->search_attributes->{offset}) && length($req->search_attributes->{offset})) 
+                ||(exists($req->search_attributes->{rows}) && defined($req->search_attributes->{rows}) && length($req->search_attributes->{rows}))
+            );
     }
     catch
     {
index f85a4e2..5dedb28 100644 (file)
@@ -90,6 +90,20 @@ page is what page to return while paging
         predicate => 'has_page',
     );
 
+=attribute_public offset is ro, isa: Int
+
+offset specifies where to start the paged result (think SQL LIMIT)
+
+=cut
+
+    has 'offset' =>
+    (
+        is => 'ro',
+        writer => '_set_offset',
+        isa => Int,
+        predicate => 'has_offset',
+    );
+
 =attribute_public ordered_by is: ro, isa: L<Catalyst::Controller::DBIC::API::Types/OrderedBy>
 
 ordered_by is passed to ->search to determine sorting
@@ -460,6 +474,7 @@ request_data holds the raw (but deserialized) data for ths request
             $self->_set_search($new->{$controller->search_arg}) if exists $new->{$controller->search_arg};
             $self->_set_count($new->{$controller->count_arg}) if exists $new->{$controller->count_arg};
             $self->_set_page($new->{$controller->page_arg}) if exists $new->{$controller->page_arg};
+            $self->_set_offset($new->{$controller->offset_arg}) if exists $new->{$controller->offset_arg};
         }
     );
 
@@ -562,10 +577,20 @@ This builder method generates the search attributes
             as => $self->as || ((scalar(@{$static->as})) ? $static->as : undef),
             prefetch => $self->prefetch || $static->prefetch || undef,
             rows => $self->count || $static->count,
-            page => $self->page,
+            offset => $self->offset,
             join => $self->build_joins,
         };
 
+        if($self->has_page)
+        {
+            $search_attributes->{page} = $self->page;
+        }
+        elsif(!$self->has_page && defined($search_attributes->{offset}) && defined($search_attributes->{rows}))
+        {
+            $search_attributes->{page} = $search_attributes->{offset} / $search_attributes->{rows} + 1;
+        }
+        
+
         $search_attributes = 
         {   
             map { @$_ }
index 862dc21..3ee8ec2 100644 (file)
@@ -66,6 +66,14 @@ page_arg controls how to reference 'page' in the the request_data
 
 has 'page_arg' => ( is => 'ro', isa => Str, default => 'list_page' );
 
+=attribute_public offset_arg is: ro, isa: Str, default: 'offset'
+
+offset_arg controls how to reference 'offset' in the the request_data
+
+=cut
+
+has 'offset_arg' => ( is => 'ro', isa => Str, default => 'list_offset' );
+
 =attribute_public select_arg is: ro, isa: Str, default: 'list_returns'
 
 select_arg controls how to reference 'select' in the the request_data