1 |
package Frey::Collection; |
package Frey::ORM::Collection; |
2 |
use Moose::Role; |
use Moose::Role; |
3 |
|
|
4 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
5 |
|
use Carp; |
6 |
|
|
7 |
sub collection { |
=head1 NAME |
8 |
my ( $class, $limit, $offset ) = @_; |
|
9 |
my $schema = $class->SchemaClass()->Schema(); |
Frey::ORM::Collection - role for Fey::ORM to get all results |
10 |
my $select = $class->SchemaClass()->SQLFactoryClass()->new_select(); |
|
11 |
|
=head1 METHODS |
12 |
|
|
13 |
my $table = $class->collection_table; |
=cut |
14 |
|
|
15 |
warn "## table $table"; |
=head2 total_entries |
16 |
|
|
17 |
$limit ||= 20; |
my $rows = $o->total_entries; |
|
$offset ||= 0; |
|
18 |
|
|
19 |
my $users_t = $schema->table( $table ); |
=cut |
20 |
|
|
21 |
$select->select( $users_t ) |
sub total_entries { |
22 |
->from( $users_t ) |
my $self = shift; |
23 |
|
my $table = $self->_table; |
24 |
|
my $count = Fey::Literal::Function->new( 'COUNT', $table->column('id') ); # FIXME id? |
25 |
|
my $select = $self->_sql->select( $count )->from( $self->_table ); |
26 |
|
my $dbh = $self->_dbh($select); |
27 |
|
my $sth = $dbh->prepare( $select->sql($dbh) ); |
28 |
|
$sth->execute; |
29 |
|
return $sth->fetchrow_arrayref->[0]; |
30 |
|
} |
31 |
|
|
32 |
|
sub _table { |
33 |
|
my $self = shift; |
34 |
|
return $self->SchemaClass()->Schema()->table( $self->collection_table ); |
35 |
|
} |
36 |
|
|
37 |
|
sub _sql { |
38 |
|
my $self = shift; |
39 |
|
return $self->SchemaClass()->SQLFactoryClass()->new_select(); |
40 |
|
} |
41 |
|
|
42 |
|
=head2 collection |
43 |
|
|
44 |
|
my $pager = Frey::Pager->new( per_page => 20, page => 1 ); |
45 |
|
my $Fey_Object_Iterator = $o->collection($pager); |
46 |
|
|
47 |
|
=cut |
48 |
|
|
49 |
|
sub collection { |
50 |
|
my ( $self, $pager ) = @_; |
51 |
|
|
52 |
|
my $table = $self->_table; |
53 |
|
|
54 |
|
my $select = $self->_sql->select( $table ) |
55 |
|
->from( $table ) |
56 |
# ->where( $message_t->column('message_date'), '>=', |
# ->where( $message_t->column('message_date'), '>=', |
57 |
# DateTime->today()->subtract( days => 7 )->strftime( '%Y-%m-%d' ) ) |
# DateTime->today()->subtract( days => 7 )->strftime( '%Y-%m-%d' ) ) |
58 |
->limit( $limit, $offset ) |
; |
|
; |
|
59 |
|
|
60 |
my $dbh = $class->_dbh($select); |
if ( blessed $pager ) { |
61 |
|
if ( ref($pager) eq 'Frey::Pager' ) { |
62 |
|
warn "FIXING wrong pager object to collection!"; |
63 |
|
$pager = $pager->pager; |
64 |
|
} |
65 |
|
warn "## using pager"; |
66 |
|
$pager->total_entries( $self->total_entries ); |
67 |
|
$select->limit( $pager->entries_per_page, $pager->skipped ); |
68 |
|
} |
69 |
|
|
70 |
|
my $dbh = $self->_dbh($select); |
71 |
my $sth = $dbh->prepare( $select->sql($dbh) ); |
my $sth = $dbh->prepare( $select->sql($dbh) ); |
72 |
|
|
73 |
return |
my $i = |
74 |
Fey::Object::Iterator->new( |
Fey::Object::Iterator->new( |
75 |
classes => [ $class->meta()->ClassForTable( $users_t ) ], |
classes => [ $self->meta()->ClassForTable( $table ) ], |
76 |
handle => $sth, |
handle => $sth, |
77 |
bind_params => [ $select->bind_params() ], |
bind_params => [ $select->bind_params() ], |
78 |
); |
); |
79 |
|
|
80 |
|
confess "LEGACY: this calling convention is no longer supported" if wantarray; |
81 |
|
return $i; |
82 |
} |
} |
83 |
|
|
84 |
1; |
1; |