More refactoring, prefetch
[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 %seen;
16   $attrs->{cols} ||= [ map { "me.$_" } $db_class->_select_columns ];
17   $attrs->{from} ||= [ { 'me' => $db_class->_table_name } ];
18   if ($attrs->{join}) {
19     foreach my $j (ref $attrs->{join} eq 'ARRAY'
20               ? (@{$attrs->{join}}) : ($attrs->{join})) {
21       if (ref $j eq 'HASH') {
22         $seen{$_} = 1 foreach keys %$j;
23       } else {
24         $seen{$j} = 1;
25       }
26     }
27     push(@{$attrs->{from}}, $db_class->_resolve_join($attrs->{join}, 'me'));
28   }
29   foreach my $pre (@{$attrs->{prefetch} || []}) {
30     push(@{$attrs->{from}}, $db_class->_resolve_join($pre, 'me'))
31       unless $seen{$pre};
32     push(@{$attrs->{cols}},
33       map { "$pre.$_" }
34       $db_class->_relationships->{$pre}->{class}->columns);
35   }
36   my $new = {
37     class => $db_class,
38     cols => $attrs->{cols} || [ $db_class->_select_columns ],
39     cond => $attrs->{where},
40     from => $attrs->{from} || $db_class->_table_name,
41     count => undef,
42     pager => undef,
43     attrs => $attrs };
44   bless ($new, $it_class);
45   $new->pager if ($attrs->{page});
46   return $new;
47 }
48
49 sub cursor {
50   my ($self) = @_;
51   my ($db_class, $attrs) = @{$self}{qw/class attrs/};
52   if ($attrs->{page}) {
53     $attrs->{rows} = $self->pager->entries_per_page;
54     $attrs->{offset} = $self->pager->skipped;
55   }
56   return $self->{cursor}
57     ||= $db_class->storage->select($self->{from}, $self->{cols},
58           $attrs->{where},$attrs);
59 }
60
61 sub slice {
62   my ($self, $min, $max) = @_;
63   my $attrs = { %{ $self->{attrs} || {} } };
64   $self->{class}->throw("Can't slice without where") unless $attrs->{where};
65   $attrs->{offset} = $min;
66   $attrs->{rows} = ($max ? ($max - $min + 1) : 1);
67   my $slice = $self->new($self->{class}, $attrs);
68   return (wantarray ? $slice->all : $slice);
69 }
70
71 sub next {
72   my ($self) = @_;
73   my @row = $self->cursor->next;
74   return unless (@row);
75   return $self->_construct_object(@row);
76 }
77
78 sub _construct_object {
79   my ($self, @row) = @_;
80   my @cols = $self->{class}->_select_columns;
81   unless ($self->{attrs}{prefetch}) {
82     return $self->{class}->_row_to_object(\@cols, \@row);
83   } else {
84     my @main = splice(@row, 0, scalar @cols);
85     my $new = $self->{class}->_row_to_object(\@cols, \@main);
86     PRE: foreach my $pre (@{$self->{attrs}{prefetch}}) {
87       my $rel_obj = $self->{class}->_relationships->{$pre};
88       my @pre_cols = $rel_obj->{class}->columns;
89       my @vals = splice(@row, 0, scalar @pre_cols);
90       my $fetched = $rel_obj->{class}->_row_to_object(\@pre_cols, \@vals);
91       $self->{class}->throw("No accessor for prefetched $pre")
92         unless defined $rel_obj->{attrs}{accessor};
93       if ($rel_obj->{attrs}{accessor} eq 'single') {
94         foreach my $pri ($rel_obj->{class}->primary_columns) {
95           next PRE unless defined $fetched->get_column($pri);
96         }
97         $new->{_relationship_data}{$pre} = $fetched;
98       } elsif ($rel_obj->{attrs}{accessor} eq 'filter') {
99         $new->{_inflated_column}{$pre} = $fetched;
100       } else {
101         $self->{class}->throw("Don't know to to store prefetched $pre");
102       }
103     }
104     return $new;
105   }
106 }
107
108 sub count {
109   my ($self) = @_;
110   my $db_class = $self->{class};
111   my $attrs = { %{ $self->{attrs} } };
112   unless ($self->{count}) {
113     # offset and order by are not needed to count
114     delete $attrs->{$_} for qw/offset order_by/;
115         
116     my @cols = 'COUNT(*)';
117     $self->{count} = $db_class->storage->select_single($self->{from}, \@cols,
118                                               $self->{cond}, $attrs);
119   }
120   return 0 unless $self->{count};
121   return $self->{pager}->entries_on_this_page if ($self->{pager});
122   return ( $attrs->{rows} && $attrs->{rows} < $self->{count} ) 
123     ? $attrs->{rows} 
124     : $self->{count};
125 }
126
127 sub all {
128   my ($self) = @_;
129   return map { $self->_construct_object(@$_); }
130            $self->cursor->all;
131 }
132
133 sub reset {
134   my ($self) = @_;
135   $self->cursor->reset;
136   return $self;
137 }
138
139 sub first {
140   return $_[0]->reset->next;
141 }
142
143 sub delete {
144   my ($self) = @_;
145   $_->delete for $self->all;
146   return 1;
147 }
148
149 *delete_all = \&delete; # Yeah, yeah, yeah ...
150
151 sub pager {
152   my ($self) = @_;
153   my $attrs = $self->{attrs};
154   delete $attrs->{offset};
155   my $rows_per_page = delete $attrs->{rows} || 10;
156   $self->{pager} ||= Data::Page->new(
157     $self->count, $rows_per_page, $attrs->{page} || 1);
158   $attrs->{rows} = $rows_per_page;
159   return $self->{pager};
160 }
161
162 sub page {
163   my ($self, $page) = @_;
164   my $attrs = $self->{attrs};
165   $attrs->{page} = $page;
166   return $self->new($self->{class}, $attrs);
167 }
168
169 1;