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->{from} = [ { 'me' => $db_class->_table_name },
17 $db_class->_resolve_join($attrs->{join}, 'me') ];
18 $attrs->{cols} = [ map { "me.$_" } $db_class->_select_columns ];
22 cols => $attrs->{cols} || [ $db_class->_select_columns ],
23 cond => $attrs->{where},
24 from => $attrs->{from} || $db_class->_table_name,
28 bless ($new, $it_class);
29 $new->pager if ($attrs->{page});
35 my ($db_class, $attrs) = @{$self}{qw/class attrs/};
37 $attrs->{rows} = $self->pager->entries_per_page;
38 $attrs->{offset} = $self->pager->skipped;
40 return $self->{cursor}
41 ||= $db_class->storage->select($self->{from}, $self->{cols},
42 $attrs->{where},$attrs);
46 my ($self, $min, $max) = @_;
47 my $attrs = { %{ $self->{attrs} || {} } };
48 $self->{class}->throw("Can't slice without where") unless $attrs->{where};
49 $attrs->{offset} = $min;
50 $attrs->{rows} = ($max ? ($max - $min + 1) : 1);
51 my $slice = $self->new($self->{class}, $attrs);
52 return (wantarray ? $slice->all : $slice);
57 my @row = $self->cursor->next;
59 return $self->{class}->_row_to_object($self->{cols}, \@row);
64 my $db_class = $self->{class};
65 my $attrs = { %{ $self->{attrs} } };
66 unless ($self->{count}) {
67 # offset and order by are not needed to count
68 delete $attrs->{$_} for qw/offset order_by/;
70 my @cols = 'COUNT(*)';
71 $self->{count} = $db_class->storage->select_single($self->{from}, \@cols,
72 $self->{cond}, $attrs);
74 return 0 unless $self->{count};
75 return $self->{pager}->entries_on_this_page if ($self->{pager});
76 return ( $attrs->{rows} && $attrs->{rows} < $self->{count} )
83 return map { $self->{class}->_row_to_object($self->{cols}, $_); }
94 return $_[0]->reset->next;
99 $_->delete for $self->all;
103 *delete_all = \&delete; # Yeah, yeah, yeah ...
107 my $attrs = $self->{attrs};
108 delete $attrs->{offset};
109 my $rows_per_page = delete $attrs->{rows} || 10;
110 $self->{pager} ||= Data::Page->new(
111 $self->count, $rows_per_page, $attrs->{page} || 1);
112 $attrs->{rows} = $rows_per_page;
113 return $self->{pager};
117 my ($self, $page) = @_;
118 my $attrs = $self->{attrs};
119 $attrs->{page} = $page;
120 return $self->new($self->{class}, $attrs);