Commit | Line | Data |
89c0a5a2 |
1 | package DBIx::Class::ResultSet; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | use overload |
6 | '0+' => 'count', |
7 | fallback => 1; |
3c5b25c5 |
8 | use Data::Page; |
89c0a5a2 |
9 | |
10 | sub 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 |
49 | sub 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 |
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); |
7624b19f |
67 | my $slice = $self->new($self->{class}, $attrs); |
89c0a5a2 |
68 | return (wantarray ? $slice->all : $slice); |
69 | } |
70 | |
71 | sub 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 | |
78 | sub _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}; |
89 | my @pre_cols = $rel_obj->{class}->columns; |
90 | my @vals = splice(@row, 0, scalar @pre_cols); |
91 | my $fetched = $rel_obj->{class}->_row_to_object(\@pre_cols, \@vals); |
92 | $self->{class}->throw("No accessor for prefetched $pre") |
93 | unless defined $rel_obj->{attrs}{accessor}; |
94 | if ($rel_obj->{attrs}{accessor} eq 'single') { |
95 | foreach my $pri ($rel_obj->{class}->primary_columns) { |
96 | next PRE unless defined $fetched->get_column($pri); |
97 | } |
98 | $new->{_relationship_data}{$pre} = $fetched; |
99 | } elsif ($rel_obj->{attrs}{accessor} eq 'filter') { |
100 | $new->{_inflated_column}{$pre} = $fetched; |
101 | } else { |
3125eb1f |
102 | $self->{class}->throw("Don't know how to store prefetched $pre"); |
c7ce65e6 |
103 | } |
104 | } |
c7ce65e6 |
105 | } |
33ce49d6 |
106 | $new = $self->{attrs}{record_filter}->($new) |
107 | if exists $self->{attrs}{record_filter}; |
108 | return $new; |
89c0a5a2 |
109 | } |
110 | |
111 | sub count { |
112 | my ($self) = @_; |
7624b19f |
113 | my $db_class = $self->{class}; |
59f8e584 |
114 | my $attrs = { %{ $self->{attrs} } }; |
3c5b25c5 |
115 | unless ($self->{count}) { |
116 | # offset and order by are not needed to count |
117 | delete $attrs->{$_} for qw/offset order_by/; |
118 | |
119 | my @cols = 'COUNT(*)'; |
fef5d100 |
120 | $self->{count} = $db_class->storage->select_single($self->{from}, \@cols, |
3c5b25c5 |
121 | $self->{cond}, $attrs); |
122 | } |
123 | return 0 unless $self->{count}; |
9229f20a |
124 | return $self->{pager}->entries_on_this_page if ($self->{pager}); |
3c5b25c5 |
125 | return ( $attrs->{rows} && $attrs->{rows} < $self->{count} ) |
59f8e584 |
126 | ? $attrs->{rows} |
3c5b25c5 |
127 | : $self->{count}; |
89c0a5a2 |
128 | } |
129 | |
130 | sub all { |
131 | my ($self) = @_; |
c7ce65e6 |
132 | return map { $self->_construct_object(@$_); } |
73f58123 |
133 | $self->cursor->all; |
89c0a5a2 |
134 | } |
135 | |
136 | sub reset { |
137 | my ($self) = @_; |
73f58123 |
138 | $self->cursor->reset; |
89c0a5a2 |
139 | return $self; |
140 | } |
141 | |
142 | sub first { |
143 | return $_[0]->reset->next; |
144 | } |
145 | |
28927b50 |
146 | sub delete { |
89c0a5a2 |
147 | my ($self) = @_; |
148 | $_->delete for $self->all; |
149 | return 1; |
150 | } |
151 | |
28927b50 |
152 | *delete_all = \&delete; # Yeah, yeah, yeah ... |
153 | |
3c5b25c5 |
154 | sub pager { |
155 | my ($self) = @_; |
156 | my $attrs = $self->{attrs}; |
157 | delete $attrs->{offset}; |
158 | my $rows_per_page = delete $attrs->{rows} || 10; |
159 | $self->{pager} ||= Data::Page->new( |
160 | $self->count, $rows_per_page, $attrs->{page} || 1); |
161 | $attrs->{rows} = $rows_per_page; |
162 | return $self->{pager}; |
163 | } |
164 | |
165 | sub page { |
166 | my ($self, $page) = @_; |
167 | my $attrs = $self->{attrs}; |
168 | $attrs->{page} = $page; |
169 | return $self->new($self->{class}, $attrs); |
170 | } |
171 | |
89c0a5a2 |
172 | 1; |