WebGUI::SQL as DBI subclass

This commit is contained in:
Graham Knop 2010-04-13 19:25:42 -05:00
parent 5b25692561
commit 0bff8a0fa4
2 changed files with 445 additions and 559 deletions

View file

@ -26,8 +26,6 @@ This class provides methods for working with SQL result sets. If you're used to
=head1 SYNOPSIS
use WebGUI::SQL::ResultSet;
my $result = WebGUI::SQL::ResultSet->prepare($query, $db);
$result->execute([ @values ]);
@ -44,160 +42,6 @@ This class provides methods for working with SQL result sets. If you're used to
These methods are available from this package:
=cut
#-------------------------------------------------------------------
=head2 array ( )
Returns the next row of data as an array.
=cut
sub array {
my $self = shift;
return $self->sth->fetchrow_array() or $self->db->session->errorHandler->fatal("Couldn't fetch array. ".$self->errorMessage);
}
#-------------------------------------------------------------------
=head2 arrayRef ( )
Returns the next row of data as an array reference. Note that this is 12% faster than array().
=cut
sub arrayRef {
my $self = shift;
return $self->sth->fetchrow_arrayref() or $self->db->session->errorHandler->fatal("Couldn't fetch array. ".$self->errorMessage);
}
#-------------------------------------------------------------------
=head2 db ( )
A reference to the current WebGUI::SQL object.
=cut
sub db {
my $self = shift;
return $self->{_db};
}
#-------------------------------------------------------------------
=head2 errorCode {
Returns an error code for the current handler.
=cut
sub errorCode {
my $self = shift;
return $self->sth->err;
}
#-------------------------------------------------------------------
=head2 errorMessage {
Returns a text error message for the current handler.
=cut
sub errorMessage {
my $self = shift;
return $self->sth->errstr;
}
#-------------------------------------------------------------------
=head2 execute ( [ placeholders ] )
Executes a prepared SQL statement. For SELECT queries, returns a true value on success. For
other queries, returns the number of rows effected. Return value will always evaluate as true
even if zero rows were effected.
=head3 placeholders
An array reference containing a list of values to be used in the placeholders defined in the SQL statement.
=cut
sub execute {
my $self = shift;
my $placeholders = shift || [];
my $sql = $self->{_sql};
$self->sth->execute(@{ $placeholders }) or $self->session->errorHandler->fatal("Couldn't execute prepared statement: $sql : With place holders: ".join(", ", @{$placeholders}).". Root cause: ". $self->errorMessage);
}
#-------------------------------------------------------------------
=head2 finish ( )
Releases the result set. Should be called to complete any statement handler.
=cut
sub finish {
my $self = shift;
return $self->sth->finish;
}
#-------------------------------------------------------------------
=head2 getColumnNames
Returns an array of column names. Use with a "read" method.
=cut
sub getColumnNames {
my $self = shift;
return @{$self->sth->{NAME}} if (ref $self->sth->{NAME} eq 'ARRAY');
}
#-------------------------------------------------------------------
=head2 hash ( )
Returns the next row of data in the form of a hash.
=cut
sub hash {
my $self = shift;
my ($hashRef);
$hashRef = $self->sth->fetchrow_hashref();
if (defined $hashRef) {
return %{$hashRef};
} else {
return ();
}
}
#-------------------------------------------------------------------
=head2 hashRef ( )
Returns the next row of data in the form of a hash reference.
=cut
sub hashRef {
my $self = shift;
return $self->sth->fetchrow_hashref();
}
#-------------------------------------------------------------------
=head2 prepare ( sql, db )
@ -215,14 +59,12 @@ A WebGUI::SQL database handler.
=cut
sub prepare {
my $class = shift;
my $sql = shift;
my $db = shift;
my $sth = $db->dbh->prepare($sql) or $db->session->errorHandler->fatal("Couldn't prepare statement: ".$sql." : ". $db->dbh->errstr);
bless {_sth => $sth, _sql => $sql, _db=>$db}, $class;
my $class = shift;
my $sql = shift;
my $db = shift;
return $db->prepare($sql);
}
#-------------------------------------------------------------------
=head2 read ( sql, db, placeholders )
@ -245,43 +87,13 @@ An array reference containing a list of values to be used in the placeholders de
=cut
sub read {
my $class = shift;
my $sql = shift;
my $db = shift;
my $placeholders = shift;
my $self = $db->prepare($sql, $db);
$self->execute($placeholders);
return $self;
my $class = shift;
my $sql = shift;
my $db = shift;
my $placeholders = shift;
return $db->read($sql, $placeholders);
}
#-------------------------------------------------------------------
=head2 rows ( )
Returns the number of rows in the result set.
=cut
sub rows {
my $self = shift;
return $self->sth->rows;
}
#-------------------------------------------------------------------
=head2 sth ( )
Returns the working DBI statement handler for this result set.
=cut
sub sth {
my $self = shift;
return $self->{_sth};
}
#-------------------------------------------------------------------
=head2 unconditionalRead ( sql, db, placeholders )
@ -303,19 +115,161 @@ An array reference containing a list of values to be used in the placeholders de
=cut
sub unconditionalRead {
my $class = shift;
my $sql = shift;
my $db = shift;
my $placeholders = shift;
my $errorHandler = $db->session->errorHandler;
$errorHandler->query($sql,$placeholders);
my $sth = $db->dbh->prepare($sql) or $errorHandler->warn("Unconditional read failed: ".$sql." : ".$db->dbh->errstr);
if ($sth) {
$sth->execute(@$placeholders) or $errorHandler->warn("Unconditional read failed: ".$sql." : ".$sth->errstr);
bless {_sql=>$sql, _db=>$db, _sth=>$sth}, $class;
} else {
return undef;
}
my $class = shift;
my $sql = shift;
my $db = shift;
my $placeholders = shift;
return $db->unconditionalRead($sql, $placeholders);
}
package WebGUI::SQL::st;
our @ISA = qw(DBI::st);
#-------------------------------------------------------------------
=head2 array ( )
Returns the next row of data as an array.
=cut
sub array {
my $self = shift;
return $self->fetchrow_array;
}
#-------------------------------------------------------------------
=head2 arrayRef ( )
Returns the next row of data as an array reference. Note that this is 12% faster than array().
=cut
sub arrayRef {
my $self = shift;
return $self->fetchrow_arrayref;
}
#-------------------------------------------------------------------
=head2 db ( )
A reference to the current WebGUI::SQL object.
=cut
sub db {
my $self = shift;
return $self->{Database};
}
#-------------------------------------------------------------------
=head2 errorCode {
Returns an error code for the current handler.
=cut
sub errorCode {
my $self = shift;
return $self->err;
}
#-------------------------------------------------------------------
=head2 errorMessage {
Returns a text error message for the current handler.
=cut
sub errorMessage {
my $self = shift;
return $self->errstr;
}
#-------------------------------------------------------------------
=head2 execute ( [ placeholders ] )
Executes a prepared SQL statement. For SELECT queries, returns a true value on success. For
other queries, returns the number of rows effected. Return value will always evaluate as true
even if zero rows were effected.
=head3 placeholders
An array reference containing a list of values to be used in the placeholders defined in the SQL statement.
=cut
sub execute {
my $self = shift;
my $placeholders =
( @_ == 1 && ref $_[0] eq 'ARRAY' ) ? $_[0]
: \@_;
return $self->SUPER::execute(@$placeholders);
}
#-------------------------------------------------------------------
=head2 getColumnNames
Returns an array of column names. Use with a "read" method.
=cut
sub getColumnNames {
my $self = shift;
return @{ $self->{NAME} }
if (ref $self->{NAME} eq 'ARRAY');
return;
}
#-------------------------------------------------------------------
=head2 hash ( )
Returns the next row of data in the form of a hash.
=cut
sub hash {
my $self = shift;
my $hashRef = $self->fetchrow_hashref || {};
return %$hashRef;
}
#-------------------------------------------------------------------
=head2 hashRef ( )
Returns the next row of data in the form of a hash reference.
=cut
sub hashRef {
my $self = shift;
return $self->fetchrow_hashref;
}
#-------------------------------------------------------------------
=head2 sth ( )
Returns the working DBI statement handler for this result set.
=cut
sub sth {
my $self = shift;
return $self;
}
1;