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