From: Matt S Trout Date: Sat, 27 Aug 2005 18:04:58 +0000 (+0000) Subject: Lazy cursor instantiation for resultsets X-Git-Tag: v0.03001~24 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=73f581237f9fb7bf1e0233e50530321fb7f5d6e1;p=dbsrgits%2FDBIx-Class.git Lazy cursor instantiation for resultsets --- diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index 936bfdb..3f8403d 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -12,17 +12,22 @@ sub new { $it_class = ref $it_class if ref $it_class; $attrs = { %{ $attrs || {} } }; my $cols = [ $db_class->_select_columns ]; - my $cursor = $db_class->storage->select($db_class->_table_name,$cols, - $attrs->{where},$attrs); my $new = { class => $db_class, - cursor => $cursor, cols => $cols, cond => $attrs->{where}, attrs => $attrs }; return bless ($new, $it_class); } +sub cursor { + my ($self) = @_; + my ($db_class, $attrs) = @{$self}{qw/class attrs/}; + return $self->{cursor} + ||= $db_class->storage->select($db_class->_table_name, $self->{cols}, + $attrs->{where},$attrs); +} + sub slice { my ($self, $min, $max) = @_; my $attrs = { %{ $self->{attrs} || {} } }; @@ -35,7 +40,7 @@ sub slice { sub next { my ($self) = @_; - my @row = $self->{cursor}->next; + my @row = $self->cursor->next; return unless (@row); return $self->{class}->_row_to_object($self->{cols}, \@row); } @@ -60,12 +65,12 @@ sub count { sub all { my ($self) = @_; return map { $self->{class}->_row_to_object($self->{cols}, $_); } - $self->{cursor}->all; + $self->cursor->all; } sub reset { my ($self) = @_; - $self->{cursor}->reset; + $self->cursor->reset; return $self; }