3 |
|
|
4 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
5 |
use Carp; |
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 { |
sub collection { |
30 |
my ( $class, $args ) = @_; |
my ( $self, $args ) = @_; |
31 |
|
|
32 |
croak "expect HASH not ",dump( $args ) unless ref($args) eq 'HASH'; |
croak "expect HASH not ",dump( $args ) unless ref($args) eq 'HASH'; |
33 |
|
|
34 |
warn "## collection args = ",dump( $args ); |
warn "## collection args = ",dump( $args ); |
35 |
|
|
36 |
my $schema = $class->SchemaClass()->Schema(); |
my $pager = Data::Page->new; |
37 |
my $select = $class->SchemaClass()->SQLFactoryClass()->new_select(); |
$pager->total_entries( $self->total_rows ); |
38 |
|
$pager->entries_per_page( $args->{per_page} || 20 ); |
39 |
my $table = $class->collection_table; |
$pager->current_page( $args->{page} || 1 ); |
|
|
|
|
warn "## table $table"; |
|
40 |
|
|
41 |
my $users_t = $schema->table( $table ); |
my $users_t = $self->_table; |
42 |
|
|
43 |
$select->select( $users_t ) |
my $select = $self->_sql->select( $users_t ) |
44 |
->from( $users_t ) |
->from( $users_t ) |
45 |
# ->where( $message_t->column('message_date'), '>=', |
# ->where( $message_t->column('message_date'), '>=', |
46 |
# DateTime->today()->subtract( days => 7 )->strftime( '%Y-%m-%d' ) ) |
# DateTime->today()->subtract( days => 7 )->strftime( '%Y-%m-%d' ) ) |
47 |
->limit( $args->{limit} || 20, $args->{offset} || 0 ) |
->limit( $pager->entries_per_page, $pager->skipped ) |
48 |
; |
; |
49 |
|
|
50 |
my $dbh = $class->_dbh($select); |
my $dbh = $self->_dbh($select); |
|
|
|
51 |
my $sth = $dbh->prepare( $select->sql($dbh) ); |
my $sth = $dbh->prepare( $select->sql($dbh) ); |
52 |
|
|
53 |
return |
my $i = |
54 |
Fey::Object::Iterator->new( |
Fey::Object::Iterator->new( |
55 |
classes => [ $class->meta()->ClassForTable( $users_t ) ], |
classes => [ $self->meta()->ClassForTable( $users_t ) ], |
56 |
handle => $sth, |
handle => $sth, |
57 |
bind_params => [ $select->bind_params() ], |
bind_params => [ $select->bind_params() ], |
58 |
); |
); |
59 |
|
|
60 |
|
return ( $i, $pager ) if wantarray; |
61 |
|
return $i; |
62 |
} |
} |
63 |
|
|
64 |
1; |
1; |