Joins work for search, some refactoring
[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   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   }
20   my $new = {
21     class => $db_class,
22     cols => $attrs->{cols} || [ $db_class->_select_columns ],
23     cond => $attrs->{where},
24     from => $attrs->{from} || $db_class->_table_name,
25     count => undef,
26     pager => undef,
27     attrs => $attrs };
28   bless ($new, $it_class);
29   $new->pager if ($attrs->{page});
30   return $new;
31 }
32
33 sub cursor {
34   my ($self) = @_;
35   my ($db_class, $attrs) = @{$self}{qw/class attrs/};
36   if ($attrs->{page}) {
37     $attrs->{rows} = $self->pager->entries_per_page;
38     $attrs->{offset} = $self->pager->skipped;
39   }
40   return $self->{cursor}
41     ||= $db_class->storage->select($self->{from}, $self->{cols},
42           $attrs->{where},$attrs);
43 }
44
45 sub 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);
51   my $slice = $self->new($self->{class}, $attrs);
52   return (wantarray ? $slice->all : $slice);
53 }
54
55 sub next {
56   my ($self) = @_;
57   my @row = $self->cursor->next;
58   return unless (@row);
59   return $self->{class}->_row_to_object($self->{cols}, \@row);
60 }
61
62 sub count {
63   my ($self) = @_;
64   my $db_class = $self->{class};
65   my $attrs = { %{ $self->{attrs} } };
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(*)';
71     $self->{count} = $db_class->storage->select_single($self->{from}, \@cols,
72                                               $self->{cond}, $attrs);
73   }
74   return 0 unless $self->{count};
75   return $self->{pager}->entries_on_this_page if ($self->{pager});
76   return ( $attrs->{rows} && $attrs->{rows} < $self->{count} ) 
77     ? $attrs->{rows} 
78     : $self->{count};
79 }
80
81 sub all {
82   my ($self) = @_;
83   return map { $self->{class}->_row_to_object($self->{cols}, $_); }
84            $self->cursor->all;
85 }
86
87 sub reset {
88   my ($self) = @_;
89   $self->cursor->reset;
90   return $self;
91 }
92
93 sub first {
94   return $_[0]->reset->next;
95 }
96
97 sub delete {
98   my ($self) = @_;
99   $_->delete for $self->all;
100   return 1;
101 }
102
103 *delete_all = \&delete; # Yeah, yeah, yeah ...
104
105 sub 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
116 sub page {
117   my ($self, $page) = @_;
118   my $attrs = $self->{attrs};
119   $attrs->{page} = $page;
120   return $self->new($self->{class}, $attrs);
121 }
122
123 1;