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;

=head1 NAME

DBIx::QueryCursor - Database cursor handler


    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


    use DBIx::Connection;

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


Class that represents database cursor.

=head2 attributes


=item result_set

Fetch resultset.


has '$.result_set';

=item rows

Number of rows retrieved since last execution.


has '$.rows';


=head2 methods


=item columns

Function return list of column from current cursor


sub columns {
    my ($self) = @_;

=item execute

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


sub execute {
    my ($self, $bind_params, $result_set) = @_;
    $result_set ||= {};
    $self->finish if $self->rows;

=item iterator

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


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;

=item fetch

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


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

=item finish

Signals that the cursor will not be used again.


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





The DBIx::QueryCursor module is free software. You may distribute under the terms of
either the GNU General Public License or the Artistic License, as specified in
the Perl README file.

=head1 SEE ALSO


=head1 AUTHOR

Adrian Witas,