SQL::DB::Cursor - SQL::DB database cursor


SQL-DB documentation Contained in the SQL-DB distribution.

Index


Code Index:

NAME

Top

SQL::DB::Cursor - SQL::DB database cursor

SYNOPSIS

Top

  use SQL::DB::Cursor;
  my $cursor = SQL::DB::Cursor->new($sth, $class);

  while (my $next = $cursor->next) {
    print $next->column()
  }

DESCRIPTION

Top

SQL::DB::Cursor is a cursor interface to DBI. It is typically only used by the SQL::DB fetch() method, and in user code.

METHODS

Top

new($sth,$class)

Create a new cursor object. $sth is a DBI statement handle (ie the result of a DBI->execute call). $class must be the result of a SQL::DB::Row make_class_from() method call.

next

Returns the next row from the database as an object of type $class. Returns undef when no data is left. Croaks on failure.

PRIATE METHODS

Top

_finish

Calls finish() on the DBI statement handle.

AUTHOR

Top

Mark Lawrence <nomad@null.net>

COPYRIGHT AND LICENSE

Top


SQL-DB documentation Contained in the SQL-DB distribution.

package SQL::DB::Cursor;
use strict;
use warnings;
use Carp qw(croak);


sub new {
    my $proto = shift;
    my $class = ref($proto) || $proto;
    my $sth   = shift;
    my $cl    = shift;

    ($sth && $cl) || croak 'usage: new($sth,$class)';

    my $self = {
        sth   => $sth,
        class => $cl,
    };

    bless($self, $class);
    return $self;
}


sub next {
    my $self = shift;
    if ($self->{finish}) {
        croak 'attempt to fetch row from handle that is finished';
    }

    # we don't use fetchrow_arrarref because that always returns the
    # same reference and might overwrite user data.
    my @list;
    eval {@list = $self->{sth}->fetchrow_array;};

    if ($@) {
        croak "DBI:fetchrow_array $DBI::errstr. $@";
    }

    if (!@list) {
        return;
    }

    my $class = $self->{class};
    return $class->new_from_arrayref(\@list)->_inflate;
}


sub _finish {
    my $self = shift;
    $self->{sth}->finish();
    $self->{finish}++;
}


1;
__END__


# vim: set tabstop=4 expandtab: