PAB3::DB::Max - Additional functions to PAB3::DB


PAB3 documentation Contained in the PAB3 distribution.

Index


Code Index:

NAME

Top

PAB3::DB::Max - Additional functions to PAB3::DB

SYNOPSIS

Top

  use PAB3::DB::Max;

  $data = $res->fetchall_arrayref();
  $data = $res->fetchall_arrayref( {} );
  $data = $stmt->fetchall_arrayref();
  $data = $stmt->fetchall_arrayref( {} );

  $data = $res->fetchall_hashref( $key );
  $data = $stmt->fetchall_hashref( $key );

  @row = $res->fetchrow_array();
  @row = $stmt->fetchrow_array();

  %row = $res->fetchrow_hash();
  %row = $stmt->fetchrow_hash();

  $row = $res->fetchrow_arrayref();
  $row = $stmt->fetchrow_arrayref();

  $row = $res->fetchrow_hashref();
  $row = $stmt->fetchrow_hashref();

  $data = $db->selectall_arrayref( $statement );
  $data = $db->selectall_arrayref( $statement, @bind_values );
  $data = $db->selectall_arrayref( $statement, {} );
  $data = $db->selectall_arrayref( $statement, {}, @bind_values );

  $data = $db->selectall_hashref( $statement, $key );
  $data = $db->selectall_hashref( $statement, $key, @bind_values );

  @row = $db->selectrow_array( $statement );
  @row = $db->selectrow_array( $statement, @bind_values );

  $row = $db->selectrow_arrayref( $statement );
  $row = $db->selectrow_arrayref( $statement, @bind_values );

  %row = $db->selectrow_hash( $statement );
  %row = $db->selectrow_hash( $statement, @bind_values );

  $row = $db->selectrow_hashref( $statement );
  $row = $db->selectrow_hashref( $statement, @bind_values );




DESCRIPTION

Top

PAB3::DB::Max provides additional functions to PAB3::DB. Once it has been loaded all functions becomes available to PAB3::DB.

EXAMPLES

  use PAB3::DB::Max;

  $db = PAB3::DB->connect( ... );

  $row = $db->selectrow_hashref( 'select * from table' );
  print $row->{'foo'}, "\n";

METHODS

Top

$res -> fetchrow_array ()
$stmt -> fetchrow_array ()

fetchrow_array() is a synonym for fetch_row()

$res -> fetchrow_hash ()
$stmt -> fetchrow_hash ()

fetchrow_hash() is a synonym for fetch_hash()

$res -> fetchrow_arrayref ()
$stmt -> fetchrow_arrayref ()

Fetches the next row of data and returns a reference to an array holding the field values or NULL if there are no more rows in result set. Null fields are returned as undef values in the array.

$res -> fetchrow_hashref ()
$stmt -> fetchrow_hashref ()

Fetches the next row of data and returns it as a reference to a hash containing field name and field value pairs or NULL if there are no more rows in result set. Null fields are returned as undef values in the hash.

$res -> fetchall_arrayref ()
$res -> fetchall_arrayref ( {} )
$stmt -> fetchall_arrayref ()
$stmt -> fetchall_arrayref ( {} )

Fetch all the data to be returned from a result or statement.

Parameters

{}

Fetch all fields of every row as a hash ref.

Return Values

It returns a reference to an array that contains one reference per row or NULL if there is no data.

$res -> fetchall_hashref ( $key )
$stmt -> fetchall_hashref ( $key )

Fetch all the data to be returned from a result or statement class as a reference to a hash containing a key for each distinct value of the $key column that was fetched.

Parameters

$key

Provides the name of the field that holds the value to be used for the key for the returned hash. For example:

  $res = $db->query( 'select id, name from table' );
  $data = $res->fetchall_hashref( 'id' );
  # print name of id = 2
  print $data->{2}->{'name'};

For queries returing more than one 'key' column, you can specify multiple column names by passing $key as a reference to an array containing one or more key column names. For example:

  $res = $db->query( 'select id1, id2, name from table' );
  $data = $res->fetchall_hashref( [ qw(id1 id2) ] );
  # print name of id1 = 2 and id2 = 10
  print $data->{2}->{10}->{'name'};




$db -> selectrow_array ( $statement )
$db -> selectrow_array ( $statement, @bind_values )

If @bind_values are not used, this method combines query() and fetchrow_array() into a single call.

If @bind_values are used, it combines prepare(), execute() and fetchrow_array().

$db -> selectrow_hash ( $statement )
$db -> selectrow_hash ( $statement, @bind_values )

If @bind_values are not used, this method combines query() and fetchrow_hash() into a single call.

If @bind_values are used, it combines prepare(), execute() and fetchrow_hash().

$db -> selectrow_arrayref ( $statement )
$db -> selectrow_arrayref ( $statement, @bind_values )

If @bind_values are not used, this method combines query() and fetchrow_arrayref() into a single call.

If @bind_values are used, it combines prepare(), execute() and fetchrow_arrayref().

$db -> selectrow_hashref ( $statement )
$db -> selectrow_hashref ( $statement, @bind_values )

If @bind_values are not used, this method combines query() and fetchrow_hashref() into a single call.

If @bind_values are used, it combines prepare(), execute() and fetchrow_hashref().

$db -> selectall_arrayref ( $statement )
$db -> selectall_arrayref ( $statement, {} )
$db -> selectall_arrayref ( $statement, @bind_values )
$db -> selectall_arrayref ( $statement, {}, @bind_values )

If @bind_values are not used, this method combines query() and fetchall_arrayref() into a single call.

If @bind_values are used, it combines prepare(), execute() and fetchall_arrayref().

$db -> selectall_arrayref ( $statement, $key )
$db -> selectall_arrayref ( $statement, $key, @bind_values )

If @bind_values are not used, this method combines query() and fetchall_hashref() into a single call.

If @bind_values are used, it combines prepare(), execute() and fetchall_hashref().

SEE ALSO

Top

Interface for database communication PAB3::DB.

AUTHORS

Top

Christian Mueller <christian_at_hbr1.com>

COPYRIGHT

Top


PAB3 documentation Contained in the PAB3 distribution.

package PAB3::DB::Max;
# =============================================================================
# Perl Application Builder
# Module: PAB3::DB::Max
# Additional functionality to PAB3::DB
# =============================================================================
use strict;

use vars qw($VERSION);

use PAB3::DB ();

BEGIN {
	$VERSION = $PAB3::DB::VERSION;
}

package PAB3::DB;

no strict 'refs';
no strict 'vars';

BEGIN {
	*fetchrow_array = \&fetch_row;
	*fetchrow_hash = \&fetch_hash;
}

1;

sub fetchall_hashref {
	my( $this, $key ) = @_;
	my( @row, @names, %names, @key, $pkg, $i, @index, $ref, $rows, $query_id );
	$query_id = $this->[$DB_QUERYID];
	$pkg = $this->[$DB_PKG];
	@key = ref( $key ) ? @$key : ( $key );
	@names = &{"$${pkg}::fetch_names"}( $query_id )
		or return $this->_set_db_error();
	$i = 0;
	%names = map{ $_ => $i ++ } @names;
	foreach( @key ) {
		$i = $names{$_};
		if( ! defined $i ) {
			return $this->_set_db_error(
				"Field '$_' does not exist (not one of @names)"
			);
		}
		push @index, $i;
	}
	$rows = {};
	&{"$${pkg}::row_seek"}( $query_id, 0 );
	while( @row = &{"$${pkg}::fetch_row"}( $query_id ) ) {
	    $ref = $rows;
	    $ref = $ref->{$row[$_]} ||= {} foreach @index;
	    @{$ref}{@names} = @row;
	}
	return $rows;
}

sub fetchall_arrayref {
	my( $this ) = @_;
	my( $pkg, @rows, $i, $query_id );
	$query_id = $this->[$DB_QUERYID];
	@rows = ();
	$pkg = $this->[$DB_PKG];
	&{"$${pkg}::row_seek"}( $query_id, 0 );
	$i = 0;
	if( ref( $_[1] ) eq 'HASH' ) {
		my %row;
		while( %row = &{"$${pkg}::fetch_hash"}( $query_id ) ) {
			push @rows, { %row };
			$i ++;
		}
	}
	else {
		my @row;
		while( @row = &{"$${pkg}::fetch_row"}( $query_id ) ) {
			push @rows, [ @row ];
			$i ++;
		}
	}
	return $i ? \@rows : undef;
}

sub fetchrow_hashref {
	my( $this, $query_id ) = @_;
	my( $pkg, %row );
	$query_id = $query_id->[$DB_QUERYID] if ref( $query_id );
	$query_id ||= $this->[$DB_QUERYID];
	$pkg = $this->[$DB_PKG];
	%row = &{"$${pkg}::fetch_hash"}( $query_id ) or return undef;
	return \%row;
}

sub fetchrow_arrayref {
	my( $this, $query_id ) = @_;
	my( $pkg, @row );
	$query_id = $query_id->[$DB_QUERYID] if ref( $query_id );
	$query_id ||= $this->[$DB_QUERYID];
	$pkg = $this->[$DB_PKG];
	@row = &{"$${pkg}::fetch_row"}( $query_id ) or return undef;
	return \@row;
}

sub selectrow_array {
	my( $this, $sql ) = ( shift, shift );
	my( $pkg, @row, $res, $stmt );
	$pkg = $this->[$DB_PKG];
	if( @_ ) {
		$stmt = $this->prepare( $sql )
			or return undef;
		$res = $stmt->execute( @_ )
			or return undef;
	}
	else {
		$res = $this->query( $sql )
			or return undef;
	}
	return &{"$${pkg}::fetch_row"}( $res->[$DB_QUERYID] );
}

sub selectrow_arrayref {
	my( $this, $sql ) = ( shift, shift );
	my( $pkg, @row, $res, $stmt );
	$pkg = $this->[$DB_PKG];
	if( @_ ) {
		$stmt = $this->prepare( $sql )
			or return undef;
		$res = $stmt->execute( @_ )
			or return undef;
	}
	else {
		$res = $this->query( $sql )
			or return undef;
	}
	return &{"$${pkg}::fetch_row"}( $res->[$DB_QUERYID] );
}

sub selectrow_hash {
	my( $this, $sql ) = ( shift, shift );
	my( $pkg, $query_id, %row, $res, $stmt );
	$pkg = $this->[$DB_PKG];
	if( @_ ) {
		$stmt = $this->prepare( $sql )
			or return undef;
		$res = $stmt->execute( @_ )
			or return undef;
	}
	else {
		$res = $this->query( $sql )
			or return undef;
	}
	return &{"$${pkg}::fetch_hash"}( $res->[$DB_QUERYID] );
}

sub selectrow_hashref {
	my( $this, $sql ) = ( shift, shift );
	my( $pkg, %row, $res, $stmt );
	$pkg = $this->[$DB_PKG];
	if( @_ ) {
		$stmt = $this->prepare( $sql )
			or return undef;
		$res = $stmt->execute( @_ )
			or return undef;
	}
	else {
		$res = $this->query( $sql )
			or return undef;
	}
	%row = &{"$${pkg}::fetch_hash"}( $res->[$DB_QUERYID] ) or return undef;
	return \%row;
}

sub selectall_arrayref {
	my( $this, $sql ) = ( shift, shift );
	my( $res, $rows, $stmt, $st1 );
	$st1 = shift if ref( $_[0] );
	if( @_ ) {
		$stmt = $this->prepare( $sql )
			or return undef;
		$res = $stmt->execute( @_ )
			or return undef;
	}
	else {
		$res = $this->query( $sql )
			or return undef;
	}
	return $res->fetchall_arrayref( $st1 );
}

sub selectall_hashref {
	my( $this, $sql, $key ) = ( shift, shift, shift );
	my( $res, $rows, $stmt );
	if( @_ ) {
		$stmt = $this->prepare( $sql )
			or return undef;
		$res = $stmt->execute( @_ )
			or return undef;
	}
	else {
		$res = $this->query( $sql )
			or return undef;
	}
	return $res->fetchall_hashref( $key );
}

sub selectcol_arrayref {
	my( $this, $sql ) = ( shift, shift );
	my( $res, @col, $stmt );
	if( @_ ) {
		$stmt = $this->prepare( $sql )
			or return undef;
		$res = $stmt->execute( @_ )
			or return undef;
	}
	else {
		$res = $this->query( $sql )
			or return undef;
	}
	@col = $res->fetch_col( $res ) or return undef;
	return \@col;
}

package PAB3::DB::RES_;

BEGIN {
	*fetchall_arrayref = \&PAB3::DB::fetchall_arrayref;
	*fetchall_hashref = \&PAB3::DB::fetchall_hashref;
	*fetchrow_arrayref = \&PAB3::DB::fetchrow_arrayref;
	*fetchrow_hashref = \&PAB3::DB::fetchrow_hashref;
	*fetchrow_array = \&PAB3::DB::fetch_row;
	*fetchrow_hash = \&PAB3::DB::fetch_hash;
}

__END__