1 package DBIx::Class::ResultSet;
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 || {} } };
16 $attrs->{cols} ||= [ map { "me.$_" } $db_class->_select_columns ];
17 $attrs->{from} ||= [ { 'me' => $db_class->_table_name } ];
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;
27 push(@{$attrs->{from}}, $db_class->_resolve_join($attrs->{join}, 'me'));
29 foreach my $pre (@{$attrs->{prefetch} || []}) {
30 push(@{$attrs->{from}}, $db_class->_resolve_join($pre, 'me'))
32 push(@{$attrs->{cols}},
34 $db_class->_relationships->{$pre}->{class}->columns);
38 cols => $attrs->{cols} || [ $db_class->_select_columns ],
39 cond => $attrs->{where},
40 from => $attrs->{from} || $db_class->_table_name,
44 bless ($new, $it_class);
45 $new->pager if ($attrs->{page});
51 my ($db_class, $attrs) = @{$self}{qw/class attrs/};
53 $attrs->{rows} = $self->pager->entries_per_page;
54 $attrs->{offset} = $self->pager->skipped;
56 return $self->{cursor}
57 ||= $db_class->storage->select($self->{from}, $self->{cols},
58 $attrs->{where},$attrs);
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);
73 my @row = $self->cursor->next;
75 return $self->_construct_object(@row);
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);
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);
97 $new->{_relationship_data}{$pre} = $fetched;
98 } elsif ($rel_obj->{attrs}{accessor} eq 'filter') {
99 $new->{_inflated_column}{$pre} = $fetched;
101 $self->{class}->throw("Don't know to to store prefetched $pre");
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/;
116 my @cols = 'COUNT(*)';
117 $self->{count} = $db_class->storage->select_single($self->{from}, \@cols,
118 $self->{cond}, $attrs);
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} )
129 return map { $self->_construct_object(@$_); }
135 $self->cursor->reset;
140 return $_[0]->reset->next;
145 $_->delete for $self->all;
149 *delete_all = \&delete; # Yeah, yeah, yeah ...
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};
163 my ($self, $page) = @_;
164 my $attrs = $self->{attrs};
165 $attrs->{page} = $page;
166 return $self->new($self->{class}, $attrs);