DB-Handy

 view release on metacpan or  search on metacpan

lib/DB/Handy.pm  view on Meta::CPAN


use vars qw($errstr);
$errstr = '';

sub new {
    my($class, $dbh, $sql) = @_;
    my $self = {
        _dbh          => $dbh,
        _sql          => $sql,
        _rows         => undef,
        _cursor       => 0,
        _executed     => 0,
        _bind_params  => [],
        rows          => 0,
        errstr        => '',
        err           => 0,
        NAME          => [],
        NUM_OF_FIELDS => 0,
    };
    bless $self, $class;
    return $self;

lib/DB/Handy.pm  view on Meta::CPAN

    $self->{_executed} = 1;

    if ($res->{type} eq 'error') {
        $self->_set_err($res->{message});
        return undef;
    }

    if ($res->{type} eq 'rows') {
        my $data         = $res->{data};
        $self->{_rows}   = $data;
        $self->{_cursor} = 0;
        my $n            = scalar @$data;
        $self->{rows}    = $n;
        # Determine column order: prefer SELECT list order; for SELECT *
        # use schema declaration order; fall back to alphabetical.
        my @name_order = $self->_col_order_from_sql($sql, $data, $engine);
        $self->{NAME}          = [ @name_order ];
        $self->{NUM_OF_FIELDS} = scalar @name_order;
        return $n || '0E0';
    }

lib/DB/Handy.pm  view on Meta::CPAN

        $self->{_rows} = undef;
        if ($sql =~ /^\s*INSERT\b/i) {
            $self->{_dbh}{_last_insert_id} = $affected;
        }
        return $affected || '0E0';
    }

    # SHOW / DESCRIBE and other statement types
    if (ref($res->{data}) eq 'ARRAY') {
        $self->{_rows}   = $res->{data};
        $self->{_cursor} = 0;
        $self->{rows}    = scalar @{$res->{data}};
    }
    return '0E0';
}

# _col_order_from_sql($sql, $data, $engine)
#
# Return column names in the order they should be presented to the caller.
#
# For named SELECT lists (SELECT a, b, c) the order follows the SELECT list,

lib/DB/Handy.pm  view on Meta::CPAN

            return @fallback unless $keys{$nm};
        }
    }
    return @names;
}

# fetchrow_hashref -- return next row as hashref (undef at EOF)
sub fetchrow_hashref {
    my($self) = @_;
    return undef unless defined $self->{_rows};
    return undef if $self->{_cursor} >= scalar @{$self->{_rows}};
    my $row = $self->{_rows}[ $self->{_cursor}++ ];
    return { %$row };
}

# fetchrow_arrayref -- return next row as arrayref (columns in NAME order)
sub fetchrow_arrayref {
    my($self) = @_;
    my $href = $self->fetchrow_hashref or return undef;
    my @cols = @{$self->{NAME}} ? @{$self->{NAME}} : sort keys %$href;
    return [ map { $href->{$_} } @cols ];
}

lib/DB/Handy.pm  view on Meta::CPAN

    return { %h };
}

# bind_param($pos, $val [, $attr]) -- pre-bind a placeholder by position
sub bind_param {
    my($self, $pos, $val, $attr) = @_;
    $self->{_bind_params}[$pos - 1] = $val;
    return 1;
}

# finish -- reset cursor and release resources
sub finish {
    my($self) = @_;
    $self->{_rows}        = undef;
    $self->{_cursor}      = 0;
    $self->{_bind_params} = [];
    return 1;
}

# rows -- number of rows affected or fetched by the last execute
sub rows { return $_[0]->{rows} }

# errstr / err accessors
sub errstr { return $_[0]->{errstr} }
sub err    { return $_[0]->{err}    }

lib/DB/Handy.pm  view on Meta::CPAN

=item * B<fetchall_hashref> - Works as in DBI.

=item * B<selectall_arrayref / selectall_hashref / selectrow_hashref / selectrow_arrayref> -
All four convenience methods have the same signature and return values as
their DBI counterparts.

=item * B<quote> -
Single-quotes a scalar and doubles embedded single-quotes; returns C<NULL>
for C<undef>.  Behaviour matches DBI's default C<quote>.

=item * B<finish> - Resets the cursor; returns 1.

=item * B<rows> -
C<< $sth->rows >> returns the row count for the last execute, as in DBI.

=item * B<errstr / err> -
Both the handle-level accessors (C<< $dbh->errstr >>, C<< $sth->errstr >>)
and the package-level variable (C<$DB::Handy::errstr>) work the same way
as C<$DBI::errstr> / C<$DBI::err>.

=item * B<NAME / NUM_OF_FIELDS> -

lib/DB/Handy.pm  view on Meta::CPAN

  my $count = $sth->rows;

Return the number of rows affected by the last DML statement or returned
by the last SELECT.  This value is also the return value of C<execute>.
Compatible with DBI.

=head2 finish()

  $sth->finish;

Reset the cursor to the beginning of the result set and release any
associated resources.  Does not close the statement handle; the same
C<$sth> can be re-executed.  Always returns 1.
Compatible with DBI.

=head2 errstr() / err()

The error message and error code from the most recent failed operation on
this statement handle.  See L</"errstr()"> and L</"err()"> under the
connection handle section.
Compatible with DBI.



( run in 0.593 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )