improved docs.
[dbsrgits/DBIx-Class-Historic.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 || {} } };
c7ce65e6 15 my %seen;
16 $attrs->{cols} ||= [ map { "me.$_" } $db_class->_select_columns ];
17 $attrs->{from} ||= [ { 'me' => $db_class->_table_name } ];
fef5d100 18 if ($attrs->{join}) {
c7ce65e6 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.$_" }
ddab752c 34 $db_class->_relationships->{$pre}->{class}->_select_columns);
fef5d100 35 }
89c0a5a2 36 my $new = {
37 class => $db_class,
fef5d100 38 cols => $attrs->{cols} || [ $db_class->_select_columns ],
89c0a5a2 39 cond => $attrs->{where},
fef5d100 40 from => $attrs->{from} || $db_class->_table_name,
3c5b25c5 41 count => undef,
42 pager => undef,
89c0a5a2 43 attrs => $attrs };
9229f20a 44 bless ($new, $it_class);
45 $new->pager if ($attrs->{page});
46 return $new;
89c0a5a2 47}
48
73f58123 49sub cursor {
50 my ($self) = @_;
51 my ($db_class, $attrs) = @{$self}{qw/class attrs/};
3c5b25c5 52 if ($attrs->{page}) {
53 $attrs->{rows} = $self->pager->entries_per_page;
54 $attrs->{offset} = $self->pager->skipped;
55 }
73f58123 56 return $self->{cursor}
fef5d100 57 ||= $db_class->storage->select($self->{from}, $self->{cols},
73f58123 58 $attrs->{where},$attrs);
59}
60
89c0a5a2 61sub 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);
7624b19f 67 my $slice = $self->new($self->{class}, $attrs);
89c0a5a2 68 return (wantarray ? $slice->all : $slice);
69}
70
71sub next {
72 my ($self) = @_;
73f58123 73 my @row = $self->cursor->next;
89c0a5a2 74 return unless (@row);
c7ce65e6 75 return $self->_construct_object(@row);
76}
77
78sub _construct_object {
79 my ($self, @row) = @_;
80 my @cols = $self->{class}->_select_columns;
33ce49d6 81 my $new;
c7ce65e6 82 unless ($self->{attrs}{prefetch}) {
33ce49d6 83 $new = $self->{class}->_row_to_object(\@cols, \@row);
c7ce65e6 84 } else {
85 my @main = splice(@row, 0, scalar @cols);
33ce49d6 86 $new = $self->{class}->_row_to_object(\@cols, \@main);
c7ce65e6 87 PRE: foreach my $pre (@{$self->{attrs}{prefetch}}) {
88 my $rel_obj = $self->{class}->_relationships->{$pre};
dd417d06 89 my $pre_class = $self->{class}->resolve_class($rel_obj->{class});
90 my @pre_cols = $pre_class->_select_columns;
c7ce65e6 91 my @vals = splice(@row, 0, scalar @pre_cols);
dd417d06 92 my $fetched = $pre_class->_row_to_object(\@pre_cols, \@vals);
c7ce65e6 93 $self->{class}->throw("No accessor for prefetched $pre")
94 unless defined $rel_obj->{attrs}{accessor};
95 if ($rel_obj->{attrs}{accessor} eq 'single') {
96 foreach my $pri ($rel_obj->{class}->primary_columns) {
97 next PRE unless defined $fetched->get_column($pri);
98 }
99 $new->{_relationship_data}{$pre} = $fetched;
100 } elsif ($rel_obj->{attrs}{accessor} eq 'filter') {
101 $new->{_inflated_column}{$pre} = $fetched;
102 } else {
3125eb1f 103 $self->{class}->throw("Don't know how to store prefetched $pre");
c7ce65e6 104 }
105 }
c7ce65e6 106 }
33ce49d6 107 $new = $self->{attrs}{record_filter}->($new)
108 if exists $self->{attrs}{record_filter};
109 return $new;
89c0a5a2 110}
111
112sub count {
113 my ($self) = @_;
7624b19f 114 my $db_class = $self->{class};
59f8e584 115 my $attrs = { %{ $self->{attrs} } };
3c5b25c5 116 unless ($self->{count}) {
117 # offset and order by are not needed to count
118 delete $attrs->{$_} for qw/offset order_by/;
119
120 my @cols = 'COUNT(*)';
fef5d100 121 $self->{count} = $db_class->storage->select_single($self->{from}, \@cols,
3c5b25c5 122 $self->{cond}, $attrs);
123 }
124 return 0 unless $self->{count};
9229f20a 125 return $self->{pager}->entries_on_this_page if ($self->{pager});
3c5b25c5 126 return ( $attrs->{rows} && $attrs->{rows} < $self->{count} )
59f8e584 127 ? $attrs->{rows}
3c5b25c5 128 : $self->{count};
89c0a5a2 129}
130
131sub all {
132 my ($self) = @_;
c7ce65e6 133 return map { $self->_construct_object(@$_); }
73f58123 134 $self->cursor->all;
89c0a5a2 135}
136
137sub reset {
138 my ($self) = @_;
73f58123 139 $self->cursor->reset;
89c0a5a2 140 return $self;
141}
142
143sub first {
144 return $_[0]->reset->next;
145}
146
28927b50 147sub delete {
89c0a5a2 148 my ($self) = @_;
149 $_->delete for $self->all;
150 return 1;
151}
152
28927b50 153*delete_all = \&delete; # Yeah, yeah, yeah ...
154
3c5b25c5 155sub pager {
156 my ($self) = @_;
157 my $attrs = $self->{attrs};
158 delete $attrs->{offset};
159 my $rows_per_page = delete $attrs->{rows} || 10;
160 $self->{pager} ||= Data::Page->new(
161 $self->count, $rows_per_page, $attrs->{page} || 1);
162 $attrs->{rows} = $rows_per_page;
163 return $self->{pager};
164}
165
166sub page {
167 my ($self, $page) = @_;
168 my $attrs = $self->{attrs};
169 $attrs->{page} = $page;
170 return $self->new($self->{class}, $attrs);
171}
172
076652e8 173=head1 NAME
174
175DBIX::Class::Recordset - Responsible for fetching and creating recordsets.
176
177=head1 SYNOPSIS;
178
179$rs=MyApp::DB::Class->search(registered=>1);
180
181
182=head1 Attributes
183
184The recordset is responsible for handling the various attributes that
185can be passed in with the search functions. Here's an overview of them:
186
187=over 4
188
189=item order_by
190
191Which column to order the results by.
192=item page
193
194Should the resultset be paged? This can also be enabled by using the
195'page' option.
196
197=item rows
198
199For paged resultsset, how many rows per page
200
201=item offset
202
203For paged resultsset, which page to start on.
204
205=back
206
89c0a5a2 2071;