Added tests for software-based limiting/paging, and a few related bug fixes
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / ResultSet.pm
1 package DBIx::Class::ResultSet;
2
3 use strict;
4 use warnings;
5 use overload
6         '0+'     => 'count',
7         fallback => 1;
8 use Data::Page;
9
10 sub new {
11   my ($it_class, $db_class, $attrs) = @_;
12   #use Data::Dumper; warn Dumper(@_);
13   $it_class = ref $it_class if ref $it_class;
14   $attrs = { %{ $attrs || {} } };
15   my $cols = [ $db_class->_select_columns ];
16   my $new = {
17     class => $db_class,
18     cols => $cols,
19     cond => $attrs->{where},
20     count => undef,
21     pager => undef,
22     attrs => $attrs };
23   bless ($new, $it_class);
24   $new->pager if ($attrs->{page});
25   return $new;
26 }
27
28 sub cursor {
29   my ($self) = @_;
30   my ($db_class, $attrs) = @{$self}{qw/class attrs/};
31   if ($attrs->{page}) {
32     $attrs->{rows} = $self->pager->entries_per_page;
33     $attrs->{offset} = $self->pager->skipped;
34   }
35   return $self->{cursor}
36     ||= $db_class->storage->select($db_class->_table_name, $self->{cols},
37           $attrs->{where},$attrs);
38 }
39
40 sub slice {
41   my ($self, $min, $max) = @_;
42   my $attrs = { %{ $self->{attrs} || {} } };
43   $self->{class}->throw("Can't slice without where") unless $attrs->{where};
44   $attrs->{offset} = $min;
45   $attrs->{rows} = ($max ? ($max - $min + 1) : 1);
46   my $slice = $self->new($self->{class}, $attrs);
47   return (wantarray ? $slice->all : $slice);
48 }
49
50 sub next {
51   my ($self) = @_;
52   my @row = $self->cursor->next;
53   return unless (@row);
54   return $self->{class}->_row_to_object($self->{cols}, \@row);
55 }
56
57 sub count {
58   my ($self) = @_;
59   my $db_class = $self->{class};
60   my $attrs = { %{ $self->{attrs} } };
61   unless ($self->{count}) {
62     # offset and order by are not needed to count
63     delete $attrs->{$_} for qw/offset order_by/;
64         
65     my @cols = 'COUNT(*)';
66     $self->{count} = $db_class->storage->select_single($db_class->_table_name, \@cols,
67                                               $self->{cond}, $attrs);
68   }
69   return 0 unless $self->{count};
70   return $self->{pager}->entries_on_this_page if ($self->{pager});
71   return ( $attrs->{rows} && $attrs->{rows} < $self->{count} ) 
72     ? $attrs->{rows} 
73     : $self->{count};
74 }
75
76 sub all {
77   my ($self) = @_;
78   return map { $self->{class}->_row_to_object($self->{cols}, $_); }
79            $self->cursor->all;
80 }
81
82 sub reset {
83   my ($self) = @_;
84   $self->cursor->reset;
85   return $self;
86 }
87
88 sub first {
89   return $_[0]->reset->next;
90 }
91
92 sub delete {
93   my ($self) = @_;
94   $_->delete for $self->all;
95   return 1;
96 }
97
98 *delete_all = \&delete; # Yeah, yeah, yeah ...
99
100 sub pager {
101   my ($self) = @_;
102   my $attrs = $self->{attrs};
103   delete $attrs->{offset};
104   my $rows_per_page = delete $attrs->{rows} || 10;
105   $self->{pager} ||= Data::Page->new(
106     $self->count, $rows_per_page, $attrs->{page} || 1);
107   $attrs->{rows} = $rows_per_page;
108   return $self->{pager};
109 }
110
111 sub page {
112   my ($self, $page) = @_;
113   my $attrs = $self->{attrs};
114   $attrs->{page} = $page;
115   return $self->new($self->{class}, $attrs);
116 }
117
118 1;