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; |
8 | |
9 | sub new { |
10 | my ($it_class, $db_class, $cursor, $args, $cols, $attrs) = @_; |
11 | #use Data::Dumper; warn Dumper(@_); |
12 | $it_class = ref $it_class if ref $it_class; |
13 | $attrs = { %{ $attrs || {} } }; |
14 | unless ($cursor) { |
15 | $attrs->{bind} = $args; |
16 | $cursor = $db_class->storage->select($db_class->_table_name,$cols, |
17 | $attrs->{where},$attrs); |
18 | } |
19 | my $new = { |
20 | class => $db_class, |
21 | cursor => $cursor, |
22 | cols => $cols, |
23 | args => $args, |
24 | cond => $attrs->{where}, |
25 | attrs => $attrs }; |
26 | return bless ($new, $it_class); |
27 | } |
28 | |
29 | sub slice { |
30 | my ($self, $min, $max) = @_; |
31 | my $attrs = { %{ $self->{attrs} || {} } }; |
32 | $self->{class}->throw("Can't slice without where") unless $attrs->{where}; |
33 | $attrs->{offset} = $min; |
34 | $attrs->{rows} = ($max ? ($max - $min + 1) : 1); |
35 | my $slice = $self->new($self->{class}, undef, $self->{args}, |
36 | $self->{cols}, $attrs); |
37 | return (wantarray ? $slice->all : $slice); |
38 | } |
39 | |
40 | sub next { |
41 | my ($self) = @_; |
42 | my @row = $self->{cursor}->next; |
43 | return unless (@row); |
44 | return $self->{class}->_row_to_object($self->{cols}, \@row); |
45 | } |
46 | |
47 | sub count { |
48 | my ($self) = @_; |
49 | return $self->{attrs}{rows} if $self->{attrs}{rows}; |
50 | return $self->{class}->count($self->{cond}, { bind => $self->{args} }); |
51 | } |
52 | |
53 | sub all { |
54 | my ($self) = @_; |
55 | $self->reset; |
56 | my @all; |
57 | while (my $obj = $self->next) { |
58 | push(@all, $obj); |
59 | } |
60 | $self->reset; |
61 | return @all; |
62 | } |
63 | |
64 | sub reset { |
65 | my ($self) = @_; |
66 | $self->{cursor}->reset; |
67 | return $self; |
68 | } |
69 | |
70 | sub first { |
71 | return $_[0]->reset->next; |
72 | } |
73 | |
74 | sub delete_all { |
75 | my ($self) = @_; |
76 | $_->delete for $self->all; |
77 | return 1; |
78 | } |
79 | |
80 | 1; |