DBIx::QueryCursor - Database cursor handler


DBIx-Connection documentation Contained in the DBIx-Connection distribution.

Index


Code Index:

NAME

Top

DBIx::QueryCursor - Database cursor handler

SYNOPSIS

Top

    use DBIx::QueryCursor;
    my $cursor = new DBIx::QueryCursor(
        connection  => $connection,
        sql         => "SELECT * FROM emp WHERE ename = ?"
    );
    my $result_set = $cursor->execute(['Smith']);

    if($cursor->fetch()) {
        $ename = result_set->{ENAME};
        ... do some stuff
    }




    or

    use DBIx::Connection;

    my $cursor = $connection->query_cursor(
        sql         => "SELECT * FROM emp WHERE ename = ?"
    );




DESCRIPTION

Top

Class that represents database cursor.

attributes

result_set

Fetch resultset.

rows

Number of rows retrieved since last execution.

methods

columns

Function return list of column from current cursor

execute

Executes statements, takes bind parameters as ARRAY ref, optionaly resul set as reference(HASH, SCALAR, ARRAY) Returns result set.

iterator

Returns the cursor itarator, on each iteration database error is checked. For instance sub query returned more then error exception is capture here.

fetch

Move cursor to next result. Returns true if a row was fetched or false if no more results exist.

finish

Signals that the cursor will not be used again.

COPYRIGHT AND LICENSE

Top

SEE ALSO

Top

DBIx::Connection DBIx::SQLHandler.

AUTHOR

Top

Adrian Witas, adrian@webapp.strefa.pl


DBIx-Connection documentation Contained in the DBIx-Connection distribution.
package DBIx::QueryCursor;

use warnings;
use strict;

use Abstract::Meta::Class ':all';
use Carp 'confess';
use base 'DBIx::SQLHandler';
use vars qw($VERSION);

$VERSION = 0.03;

has '$.result_set';


has '$.rows';

sub columns {
    my ($self) = @_;
    \@{$self->sth->{NAME_lc}}
}


sub execute {
    my ($self, $bind_params, $result_set) = @_;
    $result_set ||= {};
    $self->set_result_set($result_set);
    $self->finish if $self->rows;
    $self->SUPER::execute(@$bind_params);
    $self->bind_columns($result_set);
    $self->set_rows(0);
    $result_set;
}


sub iterator {
    my ($self) = @_;
    my $sth = $self->sth;
    my $dbh = $self->connection->dbh; 
    sub {
        my $result = $sth->fetch();
        $self->set_rows($self->rows + 1);
        confess $self->error_handler 
          if $dbh->errstr;
        $result;
    };
}


sub fetch {
    my ($self) = @_;
    my $dbh = $self->connection->dbh; 
    my $has_result = $self->sth->fetch;
    $self->error_handler
      if $dbh->errstr;
    $self->set_rows($self->rows + 1);
    $has_result ? (wantarray ? @$has_result  : $has_result) : ();
}


sub finish {
    my ($self) = @_;
    $self->sth->finish if $self->sth;
}

1;

__END__