/[Frey]/trunk/lib/Frey/Collection.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /trunk/lib/Frey/Collection.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 111 - (hide annotations)
Sun Jul 13 16:17:34 2008 UTC (14 years, 6 months ago) by dpavlin
File size: 1593 byte(s)
implemented pager for collection [0.10]
1 dpavlin 106 package Frey::Collection;
2     use Moose::Role;
3    
4     use Data::Dump qw/dump/;
5 dpavlin 109 use Carp;
6 dpavlin 111 use Data::Page;
7 dpavlin 106
8 dpavlin 111 sub total_rows {
9     my $self = shift;
10     my $table = $self->_table;
11     my $count = Fey::Literal::Function->new( 'COUNT', $table->column('id') ); # FIXME id?
12     my $select = $self->_sql->select( $count )->from( $self->_table );
13     my $dbh = $self->_dbh($select);
14     my $sth = $dbh->prepare( $select->sql($dbh) );
15     $sth->execute;
16     return $sth->fetchrow_arrayref->[0];
17     }
18    
19     sub _table {
20     my $self = shift;
21     return $self->SchemaClass()->Schema()->table( $self->collection_table );
22     }
23    
24     sub _sql {
25     my $self = shift;
26     return $self->SchemaClass()->SQLFactoryClass()->new_select();
27     }
28    
29 dpavlin 106 sub collection {
30 dpavlin 111 my ( $self, $args ) = @_;
31 dpavlin 109
32     croak "expect HASH not ",dump( $args ) unless ref($args) eq 'HASH';
33    
34     warn "## collection args = ",dump( $args );
35    
36 dpavlin 111 my $pager = Data::Page->new;
37     $pager->total_entries( $self->total_rows );
38     $pager->entries_per_page( $args->{per_page} || 20 );
39     $pager->current_page( $args->{page} || 1 );
40 dpavlin 106
41 dpavlin 111 my $users_t = $self->_table;
42 dpavlin 106
43 dpavlin 111 my $select = $self->_sql->select( $users_t )
44 dpavlin 106 ->from( $users_t )
45     # ->where( $message_t->column('message_date'), '>=',
46     # DateTime->today()->subtract( days => 7 )->strftime( '%Y-%m-%d' ) )
47 dpavlin 111 ->limit( $pager->entries_per_page, $pager->skipped )
48 dpavlin 106 ;
49    
50 dpavlin 111 my $dbh = $self->_dbh($select);
51 dpavlin 106 my $sth = $dbh->prepare( $select->sql($dbh) );
52    
53 dpavlin 111 my $i =
54 dpavlin 106 Fey::Object::Iterator->new(
55 dpavlin 111 classes => [ $self->meta()->ClassForTable( $users_t ) ],
56 dpavlin 106 handle => $sth,
57     bind_params => [ $select->bind_params() ],
58     );
59 dpavlin 111
60     return ( $i, $pager ) if wantarray;
61     return $i;
62 dpavlin 106 }
63    
64     1;

  ViewVC Help
Powered by ViewVC 1.1.26