view release on metacpan or search on metacpan
$rows = $dbh->do($statement_string, \%attr, @bind_values) or die $dbh->errstr;
$rows = $dbh->do($statement_handle) or die $dbh->errstr;
$rows = $dbh->do($statement_handle, @bind_values) or die $dbh->errstr;
Prepares (if necessary) and executes a single statement. Returns the
number of rows affected or undef on error. A return value of -1 means
the number of rows is not known, not applicable, or not available. When
no rows have been affected this method continues the "DBI" tradition of
returning 0E0 on successful execution and "undef" on failure.
The "do" method accepts optional callbacks for further processing of the
result.
The "do" implementation provided by this module allows for some minor
deviations in usage over the standard "DBI" implementation. In spite of
this, the new method may be used just like the original.
Refer to <http://search.cpan.org/dist/DBI/DBI.pm#do> for a more detailed
description of this method.
Examples
@results = $dbh->getrows_arrayref($statement_string, @bind_values);
$results = $dbh->getrows_arrayref($statement_string, \%attr, @bind_values);
@results = $dbh->getrows_arrayref($statement_string, \%attr, @bind_values);
$results = $dbh->getrows_arrayref($statement_handle, @bind_values);
@results = $dbh->getrows_arrayref($statement_handle, @bind_values);
Prepares (if necessary) and executes a single statement with the
specified data bindings and fetches the result set as an array of array
references.
The "getrows_arrayref" method accepts optional callbacks for further
processing of the results by the caller.
Examples
1. Prepare, execute it then get the results as a reference:
$sql = << '//';
SELECT solarSystemName AS name
, security
FROM mapsolarsystems
WHERE regional = 1
# Bourynes 1.0
# Ryddinjorn 1.0
# Luminaire 1.0
# Duripant 1.0
# Yulai 1.0
3. Re-use a prepared statement, execute it then return modified results
as a reference:
We'll use the query from Example 1 but have the results returned as
a list for further processing by a caller who will be using
callbacks to modify those results.
$sth = $dbh->prepare($sql);
$systems = $dbh->getrows_arrayref($sql, minimum_security => 1.0, callback {
my ($row) = @_;
return sprintf("%-11s %.1f\n", @$row);
});
# Returns a structure something like this:
#
@results = $dbh->getrows_hashref($statement_string, @bind_values);
$results = $dbh->getrows_hashref($statement_string, \%attr, @bind_values);
@results = $dbh->getrows_hashref($statement_string, \%attr, @bind_values);
$results = $dbh->getrows_hashref($statement_handle, @bind_values);
@results = $dbh->getrows_hashref($statement_handle, @bind_values);
Prepares (if necessary) and executes a single statement with the
specified data bindings and fetches the result set as an array of hash
references.
The "getrows_hashref" method accepts optional callbacks for further
processing of the results by the caller.
Examples
1. Prepare, execute it then get the results as a reference:
$sql = << '//';
SELECT solarSystemName AS name
, security
FROM mapsolarsystems
WHERE regional = 1
# Bourynes 1.0
# Ryddinjorn 1.0
# Luminaire 1.0
# Duripant 1.0
# Yulai 1.0
3. Re-use a prepared statement, execute it then return modified results
as a reference:
We'll use the query from Example 1 but have the results returned as
a list for further processing by a caller who will be using
callbacks to modify those results.
$sth = $dbh->prepare($sql);
$systems = $dbh->getrows_hashref($sql, minimum_security => 1.0, callback {
sprintf("%-11s %.1f\n", @{$_}{'name', 'security'}); # Hash slice
});
# Returns a structure something like this:
#
# [ 'Kisogo 1.0',
$results = $dbh->getrows($statement_string, \%attr, @bind_values);
@results = $dbh->getrows($statement_string, \%attr, @bind_values);
$results = $dbh->getrows($statement_handle, @bind_values);
@results = $dbh->getrows$statement_handle, @bind_values);
Alias for "getrows_hashref".
If array references are preferred, have the symbol table glob point
alias the "getrows_arrayref" method.
The "getrows" method accepts optional callbacks for further processing
of the results by the caller.
getrow_arrayref *(database handles)*
$result = $dbh->getrow_arrayref($statement_string, @bind_values);
$result = $dbh->getrow_arrayref($statement_string, \%attr, @bind_values);
$result = $dbh->getrow_arrayref($statement_handle, @bind_values);
Prepares (if necessary) and executes a single statement with the
specified data bindings and fetches the first row as an array reference.
The "getrow_arrayref" method accepts optional callbacks for further
processing of the result by the caller.
getrow_hashref *(database handles)*
$result = $dbh->getrow_hashref($statement_string, @bind_values);
$result = $dbh->getrow_hashref($statement_string, \%attr, @bind_values);
$result = $dbh->getrow_hashref($statement_handle, @bind_values);
Prepares (if necessary) and executes a single statement with the
specified data bindings and fetches the first row as a hash reference.
The "getrow_hashref" method accepts optional callbacks for further
processing of the result by the caller.
getrow *(database handles)*
$result = $dbh->getrow($statement_string, @bind_values);
$result = $dbh->getrow($statement_string, \%attr, @bind_values);
$result = $dbh->getrow($statement_handle, @bind_values);
Alias for "getrow_hashref".
If array references are preferred, have the symbol table glob point
alias the "getrows_arrayref" method.
The "getrow" method accepts optional callbacks for further processing of
the result by the caller.
STATEMENT HANDLE METHODS
bind_param
$sth->bind_param($param_num, $bind_value)
$sth->bind_param($param_num, $bind_value, \%attr)
$sth->bind_param($param_num, $bind_value, $bind_type)
$sth->bind_param($param_name, $bind_value)
$sth->bind_param($param_name, $bind_value, \%attr)
iterate
$iterator = $sth->iterate() or die $DBI::errstr;
$iterator = $sth->iterate(@bind_values) or die $DBI::errstr;
Perform whatever processing is necessary to execute the prepared
statement. An "undef" is returned if an error occurs. A successful call
returns an iterator which can be used to traverse the result set.
Examples
1. Using an iterator and callbacks to process the result set:
use strict;
use warnings;
use DBIx::FlexibleBinding -subs => [ 'TestDB' ];
use Data::Dumper;
use Test::More;
$Data::Dumper::Terse = 1;
$Data::Dumper::Indent = 1;
is_deeply( \@rows, $expected_result, 'iterate' )
and diag( Dumper(\@rows) );
}
done_testing();
In this example, we're traversing the result set using an iterator.
As we iterate through the result set, a callback is applied to each
row and we're left with an array of transformed rows.
2. Using an iterator's "for_each" method and callbacks to process the
result set:
use strict;
use warnings;
use DBIx::FlexibleBinding -subs => [ 'TestDB' ];
use Data::Dumper;
use Test::More;
$Data::Dumper::Terse = 1;
$Data::Dumper::Indent = 1;
were using to process each row of the result set has now been passed
into the "for_each" method also eliminating a "while" loop and an
empty declaration for @rows.
getrows_arrayref *(database handles)*
$results = $sth->getrows_arrayref();
@results = $sth->getrows_arrayref();
Fetches the entire result set as an array of array references.
The "getrows_arrayref" method accepts optional callbacks for further
processing of the results by the caller.
getrows_hashref *(database handles)*
$results = $sth->getrows_hashref();
@results = $sth->getrows_hashref();
Fetches the entire result set as an array of hash references.
The "getrows_hashref" method accepts optional callbacks for further
processing of the results by the caller.
getrows *(database handles)*
$results = $sth->getrows();
@results = $sth->getrows();
Alias for "getrows_hashref".
If array references are preferred, have the symbol table glob point
alias the "getrows_arrayref" method.
The "getrows" method accepts optional callbacks for further processing
of the results by the caller.
getrow_arrayref *(database handles)*
$result = $sth->getrow_arrayref();
Fetches the next row as an array reference. Returns "undef" if there are
no more rows available.
The "getrow_arrayref" method accepts optional callbacks for further
processing of the result by the caller.
getrow_hashref *(database handles)*
$result = $sth->getrow_hashref();
Fetches the next row as a hash reference. Returns "undef" if there are
no more rows available.
The "getrow_hashref" method accepts optional callbacks for further
processing of the result by the caller.
getrow *(database handles)*
$result = $sth->getrow();
Alias for "getrow_hashref".
If array references are preferred, have the symbol table glob point
alias the "getrows_arrayref" method.
The "getrow" method accepts optional callbacks for further processing of
the result by the caller.
EXPORTS
The following symbols are exported by default:
callback
To enable the namespace using this module to take advantage of the
callbacks, which are one of its main features, without the unnecessary
burden of also including the module that provides the feature *(see
Params::Callbacks for more detailed information)*.
SEE ALSO
* DBI
* Params::Callbacks
REPOSITORY
* <https://github.com/cpanic/DBIx-FlexibleBinding>
lib/DBIx/FlexibleBinding.pm view on Meta::CPAN
package # Hide from PAUSE
DBIx::FlexibleBinding::db;
our $VERSION = '2.0.4'; # VERSION
BEGIN {
*_is_hashref = \&DBIx::FlexibleBinding::_is_hashref;
*_as_list_or_ref = \&DBIx::FlexibleBinding::_as_list_or_ref;
}
use Params::Callbacks 'callbacks';
use namespace::clean;
our @ISA = 'DBI::db';
sub do
{
my ( $callbacks, $dbh, $sth, @bind_values ) = &callbacks;
my $result;
unless ( ref $sth ) {
my $attr;
$attr = shift @bind_values
if _is_hashref( $bind_values[0] );
$sth = $dbh->prepare( $sth, $attr );
return undef
if $sth->err;
}
$result = $sth->execute( @bind_values );
return undef
if $sth->err;
local $_;
$result = $callbacks->smart_transform( $_ = $result )
if @$callbacks;
return $result;
}
sub prepare
{
my ( $dbh, $stmt, @args ) = @_;
my @params;
if ( $stmt =~ m{:\w+\b} ) {
@params = $stmt =~ m{:(\w+)\b}g;
$stmt =~ s{:\w+\b}{?}g;
lib/DBIx/FlexibleBinding.pm view on Meta::CPAN
}
my $sth = $dbh->next::method( $stmt, @args );
return $sth
unless defined $sth;
$sth->_init_private_attributes( \@params );
return $sth;
}
sub getrows_arrayref
{
my ( $callbacks, $dbh, $sth, @bind_values ) = &callbacks;
unless ( ref $sth ) {
my $attr;
$attr = shift( @bind_values )
if _is_hashref( $bind_values[0] );
$sth = $dbh->prepare( $sth, $attr );
return _as_list_or_ref( undef )
if $sth->err;
}
$sth->execute( @bind_values );
return _as_list_or_ref( undef )
if $sth->err;
return $sth->getrows_arrayref( $callbacks );
}
sub getrows_hashref
{
my ( $callbacks, $dbh, $sth, @bind_values ) = &callbacks;
unless ( ref $sth ) {
my $attr;
$attr = shift( @bind_values )
if _is_hashref( $bind_values[0] );
$sth = $dbh->prepare( $sth, $attr );
return _as_list_or_ref( undef )
if $sth->err;
}
$sth->execute( @bind_values );
return _as_list_or_ref( undef )
if $sth->err;
return $sth->getrows_hashref( $callbacks );
}
sub getrow_arrayref
{
my ( $callbacks, $dbh, $sth, @bind_values ) = &callbacks;
unless ( ref $sth ) {
my $attr;
$attr = shift( @bind_values )
if _is_hashref( $bind_values[0] );
$sth = $dbh->prepare( $sth, $attr );
return undef
if $sth->err;
}
$sth->execute( @bind_values );
return undef
if $sth->err;
return $sth->getrow_arrayref( $callbacks );
}
sub getrow_hashref
{
my ( $callbacks, $dbh, $sth, @bind_values ) = &callbacks;
unless ( ref $sth ) {
my $attr;
$attr = shift( @bind_values )
if _is_hashref( $bind_values[0] );
$sth = $dbh->prepare( $sth, $attr );
return undef
if $sth->err;
}
$sth->execute( @bind_values );
return undef
if $sth->err;
return $sth->getrow_hashref( $callbacks );
}
BEGIN {
*getrows = \&getrows_hashref;
*getrow = \&getrow_hashref;
}
package # Hide from PAUSE
DBIx::FlexibleBinding::st;
our $VERSION = '2.0.4'; # VERSION
BEGIN {
*_is_arrayref = \&DBIx::FlexibleBinding::_is_arrayref;
*_is_hashref = \&DBIx::FlexibleBinding::_is_hashref;
*_as_list_or_ref = \&DBIx::FlexibleBinding::_as_list_or_ref;
}
use List::MoreUtils ( 'any' );
use Params::Callbacks ( 'callbacks' );
use namespace::clean;
use message << 'EOF';
ERR_EXP_AHR Expected a reference to a HASH or ARRAY
ERR_MISSING_BIND_ID Binding identifier is missing
ERR_BAD_BIND_ID Bad binding identifier (%s)
EOF
our @ISA = 'DBI::st';
sub _priv_attr
lib/DBIx/FlexibleBinding.pm view on Meta::CPAN
}
else {
$rows = $sth->next::method( @bind_values );
}
}
return $rows;
}
sub iterate
{
my ( $callbacks, $sth, @bind_values ) = &callbacks;
my $rows = $sth->execute( @bind_values );
return $rows unless defined $rows;
my $iter_fn = sub { $sth->getrow( $callbacks ) };
return bless( $iter_fn, 'DBIx::FlexibleBinding::Iterator' );
}
sub getrows_arrayref
{
my ( $callbacks, $sth, @args ) = &callbacks;
unless ( $sth->{Active} ) {
$sth->execute( @args );
return _as_list_or_ref( undef )
if $sth->err;
}
my $result = $sth->fetchall_arrayref;
return _as_list_or_ref( $result )
if $sth->err or not defined $result;
local $_;
$result = [ map { $callbacks->transform( $_ ) } @$result ]
if @$callbacks;
return _as_list_or_ref( $result );
}
sub getrows_hashref
{
my ( $callbacks, $sth, @args ) = &callbacks;
unless ( $sth->{Active} ) {
$sth->execute( @args );
return _as_list_or_ref( undef )
if $sth->err;
}
my $result = $sth->fetchall_arrayref( {} );
return _as_list_or_ref( $result )
if $sth->err or not defined $result;
local $_;
$result = [ map { $callbacks->transform( $_ ) } @$result ]
if @$callbacks;
return _as_list_or_ref( $result );
}
sub getrow_arrayref
{
my ( $callbacks, $sth, @args ) = &callbacks;
unless ( $sth->{Active} ) {
$sth->execute( @args );
return undef
if $sth->err;
}
my $result = $sth->fetchrow_arrayref;
return $result
if $sth->err or not defined $result;
local $_;
$result = [@$result];
$result = $callbacks->smart_transform( $_ = $result )
if @$callbacks;
return $result;
}
sub getrow_hashref
{
my ( $callbacks, $sth, @args ) = &callbacks;
unless ( $sth->{Active} ) {
$sth->execute( @args );
return undef
if $sth->err;
}
my $result = $sth->fetchrow_hashref;
return $result
if $sth->err or not defined $result;
local $_;
$result = $callbacks->smart_transform( $_ = $result )
if @$callbacks;
return $result;
}
BEGIN {
*getrows = \&getrows_hashref;
*getrow = \&getrow_hashref;
}
package # Hide from PAUSE
DBIx::FlexibleBinding::Iterator;
our $VERSION = '2.0.4'; # VERSION
use Params::Callbacks ( 'callbacks' );
use namespace::clean;
sub for_each
{
my ( $callbacks, $iter ) = &callbacks;
my @results;
local $_;
while ( my @items = $iter->() ) {
last if @items == 1 and not defined $items[0];
push @results, map { $callbacks->transform( $_ ) } @items;
}
return wantarray ? @results : \@results;
}
1;
=pod
=encoding utf8
lib/DBIx/FlexibleBinding.pm view on Meta::CPAN
$rows = $dbh->do($statement_handle) or die $dbh->errstr;
$rows = $dbh->do($statement_handle, @bind_values) or die $dbh->errstr;
Prepares (if necessary) and executes a single statement. Returns the number of
rows affected or undef on error. A return value of -1 means the number of rows
is not known, not applicable, or not available. When no rows have been affected
this method continues the C<DBI> tradition of returning C<0E0> on successful
execution and C<undef> on failure.
The C<do> method accepts optional callbacks for further processing of the result.
The C<do> implementation provided by this module allows for some minor
deviations in usage over the standard C<DBI> implementation. In spite
of this, the new method may be used just like the original.
Refer to L<http://search.cpan.org/dist/DBI/DBI.pm#do> for a more detailed
description of this method.
B<Examples>
lib/DBIx/FlexibleBinding.pm view on Meta::CPAN
$results = $dbh->getrows_arrayref($statement_string, @bind_values);
@results = $dbh->getrows_arrayref($statement_string, @bind_values);
$results = $dbh->getrows_arrayref($statement_string, \%attr, @bind_values);
@results = $dbh->getrows_arrayref($statement_string, \%attr, @bind_values);
$results = $dbh->getrows_arrayref($statement_handle, @bind_values);
@results = $dbh->getrows_arrayref($statement_handle, @bind_values);
Prepares (if necessary) and executes a single statement with the specified data
bindings and fetches the result set as an array of array references.
The C<getrows_arrayref> method accepts optional callbacks for further processing
of the results by the caller.
B<Examples>
=over
=item 1. Prepare, execute it then get the results as a reference:
$sql = << '//';
SELECT solarSystemName AS name
lib/DBIx/FlexibleBinding.pm view on Meta::CPAN
# Bourynes 1.0
# Ryddinjorn 1.0
# Luminaire 1.0
# Duripant 1.0
# Yulai 1.0
=item 3. Re-use a prepared statement, execute it then return modified results as a
reference:
We'll use the query from Example 1 but have the results returned as a list
for further processing by a caller who will be using callbacks to modify those
results.
$sth = $dbh->prepare($sql);
$systems = $dbh->getrows_arrayref($sql, minimum_security => 1.0, callback {
my ($row) = @_;
return sprintf("%-11s %.1f\n", @$row);
});
# Returns a structure something like this:
lib/DBIx/FlexibleBinding.pm view on Meta::CPAN
$results = $dbh->getrows_hashref($statement_string, @bind_values);
@results = $dbh->getrows_hashref($statement_string, @bind_values);
$results = $dbh->getrows_hashref($statement_string, \%attr, @bind_values);
@results = $dbh->getrows_hashref($statement_string, \%attr, @bind_values);
$results = $dbh->getrows_hashref($statement_handle, @bind_values);
@results = $dbh->getrows_hashref($statement_handle, @bind_values);
Prepares (if necessary) and executes a single statement with the specified data
bindings and fetches the result set as an array of hash references.
The C<getrows_hashref> method accepts optional callbacks for further processing
of the results by the caller.
B<Examples>
=over
=item 1. Prepare, execute it then get the results as a reference:
$sql = << '//';
SELECT solarSystemName AS name
lib/DBIx/FlexibleBinding.pm view on Meta::CPAN
# Bourynes 1.0
# Ryddinjorn 1.0
# Luminaire 1.0
# Duripant 1.0
# Yulai 1.0
=item 3. Re-use a prepared statement, execute it then return modified results as a
reference:
We'll use the query from Example 1 but have the results returned as a list
for further processing by a caller who will be using callbacks to modify those
results.
$sth = $dbh->prepare($sql);
$systems = $dbh->getrows_hashref($sql, minimum_security => 1.0, callback {
sprintf("%-11s %.1f\n", @{$_}{'name', 'security'}); # Hash slice
});
# Returns a structure something like this:
#
lib/DBIx/FlexibleBinding.pm view on Meta::CPAN
$results = $dbh->getrows($statement_string, \%attr, @bind_values);
@results = $dbh->getrows($statement_string, \%attr, @bind_values);
$results = $dbh->getrows($statement_handle, @bind_values);
@results = $dbh->getrows$statement_handle, @bind_values);
Alias for C<getrows_hashref>.
If array references are preferred, have the symbol table glob point alias the
C<getrows_arrayref> method.
The C<getrows> method accepts optional callbacks for further processing
of the results by the caller.
=head2 getrow_arrayref I<(database handles)>
$result = $dbh->getrow_arrayref($statement_string, @bind_values);
$result = $dbh->getrow_arrayref($statement_string, \%attr, @bind_values);
$result = $dbh->getrow_arrayref($statement_handle, @bind_values);
Prepares (if necessary) and executes a single statement with the specified data
bindings and fetches the first row as an array reference.
The C<getrow_arrayref> method accepts optional callbacks for further processing
of the result by the caller.
=head2 getrow_hashref I<(database handles)>
$result = $dbh->getrow_hashref($statement_string, @bind_values);
$result = $dbh->getrow_hashref($statement_string, \%attr, @bind_values);
$result = $dbh->getrow_hashref($statement_handle, @bind_values);
Prepares (if necessary) and executes a single statement with the specified data
bindings and fetches the first row as a hash reference.
The C<getrow_hashref> method accepts optional callbacks for further processing
of the result by the caller.
=head2 getrow I<(database handles)>
$result = $dbh->getrow($statement_string, @bind_values);
$result = $dbh->getrow($statement_string, \%attr, @bind_values);
$result = $dbh->getrow($statement_handle, @bind_values);
Alias for C<getrow_hashref>.
If array references are preferred, have the symbol table glob point alias the
C<getrows_arrayref> method.
The C<getrow> method accepts optional callbacks for further processing
of the result by the caller.
=head1 STATEMENT HANDLE METHODS
=head2 bind_param
$sth->bind_param($param_num, $bind_value)
$sth->bind_param($param_num, $bind_value, \%attr)
$sth->bind_param($param_num, $bind_value, $bind_type)
lib/DBIx/FlexibleBinding.pm view on Meta::CPAN
$iterator = $sth->iterate(@bind_values) or die $DBI::errstr;
Perform whatever processing is necessary to execute the prepared statement. An
C<undef> is returned if an error occurs. A successful call returns an iterator
which can be used to traverse the result set.
B<Examples>
=over
=item 1. Using an iterator and callbacks to process the result set:
use strict;
use warnings;
use DBIx::FlexibleBinding -subs => [ 'TestDB' ];
use Data::Dumper;
use Test::More;
$Data::Dumper::Terse = 1;
$Data::Dumper::Indent = 1;
lib/DBIx/FlexibleBinding.pm view on Meta::CPAN
is_deeply( \@rows, $expected_result, 'iterate' )
and diag( Dumper(\@rows) );
}
done_testing();
In this example, we're traversing the result set using an iterator. As we iterate
through the result set, a callback is applied to each row and we're left with
an array of transformed rows.
=item 2. Using an iterator's C<for_each> method and callbacks to process the
result set:
use strict;
use warnings;
use DBIx::FlexibleBinding -subs => [ 'TestDB' ];
use Data::Dumper;
use Test::More;
$Data::Dumper::Terse = 1;
lib/DBIx/FlexibleBinding.pm view on Meta::CPAN
=back
=head2 getrows_arrayref I<(database handles)>
$results = $sth->getrows_arrayref();
@results = $sth->getrows_arrayref();
Fetches the entire result set as an array of array references.
The C<getrows_arrayref> method accepts optional callbacks for further processing
of the results by the caller.
=head2 getrows_hashref I<(database handles)>
$results = $sth->getrows_hashref();
@results = $sth->getrows_hashref();
Fetches the entire result set as an array of hash references.
The C<getrows_hashref> method accepts optional callbacks for further processing
of the results by the caller.
=head2 getrows I<(database handles)>
$results = $sth->getrows();
@results = $sth->getrows();
Alias for C<getrows_hashref>.
If array references are preferred, have the symbol table glob point alias the
C<getrows_arrayref> method.
The C<getrows> method accepts optional callbacks for further processing
of the results by the caller.
=head2 getrow_arrayref I<(database handles)>
$result = $sth->getrow_arrayref();
Fetches the next row as an array reference. Returns C<undef> if there are no more
rows available.
The C<getrow_arrayref> method accepts optional callbacks for further processing
of the result by the caller.
=head2 getrow_hashref I<(database handles)>
$result = $sth->getrow_hashref();
Fetches the next row as a hash reference. Returns C<undef> if there are no more
rows available.
The C<getrow_hashref> method accepts optional callbacks for further processing
of the result by the caller.
=head2 getrow I<(database handles)>
$result = $sth->getrow();
Alias for C<getrow_hashref>.
If array references are preferred, have the symbol table glob point alias the
C<getrows_arrayref> method.
The C<getrow> method accepts optional callbacks for further processing
of the result by the caller.
=head1 EXPORTS
The following symbols are exported by default:
=head2 callback
To enable the namespace using this module to take advantage of the callbacks,
which are one of its main features, without the unnecessary burden of also
including the module that provides the feature I<(see L<Params::Callbacks> for
more detailed information)>.
=head1 SEE ALSO
=over 2
=item * L<DBI>