/[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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 111 - (show annotations)
Sun Jul 13 16:17:34 2008 UTC (15 years, 9 months ago) by dpavlin
File size: 1593 byte(s)
implemented pager for collection [0.10]
1 package Frey::Collection;
2 use Moose::Role;
3
4 use Data::Dump qw/dump/;
5 use Carp;
6 use Data::Page;
7
8 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 sub collection {
30 my ( $self, $args ) = @_;
31
32 croak "expect HASH not ",dump( $args ) unless ref($args) eq 'HASH';
33
34 warn "## collection args = ",dump( $args );
35
36 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
41 my $users_t = $self->_table;
42
43 my $select = $self->_sql->select( $users_t )
44 ->from( $users_t )
45 # ->where( $message_t->column('message_date'), '>=',
46 # DateTime->today()->subtract( days => 7 )->strftime( '%Y-%m-%d' ) )
47 ->limit( $pager->entries_per_page, $pager->skipped )
48 ;
49
50 my $dbh = $self->_dbh($select);
51 my $sth = $dbh->prepare( $select->sql($dbh) );
52
53 my $i =
54 Fey::Object::Iterator->new(
55 classes => [ $self->meta()->ClassForTable( $users_t ) ],
56 handle => $sth,
57 bind_params => [ $select->bind_params() ],
58 );
59
60 return ( $i, $pager ) if wantarray;
61 return $i;
62 }
63
64 1;

  ViewVC Help
Powered by ViewVC 1.1.26