make WebGUI::SQL a little faster

This commit is contained in:
Graham Knop 2008-11-24 04:01:01 +00:00
parent a922eca6bb
commit 848cf3b6db

View file

@ -14,8 +14,8 @@ package WebGUI::SQL;
=cut =cut
use DBI;
use strict; use strict;
use DBI;
use Tie::IxHash; use Tie::IxHash;
use WebGUI::SQL::ResultSet; use WebGUI::SQL::ResultSet;
use WebGUI::Utility; use WebGUI::Utility;
@ -99,19 +99,9 @@ An array reference containing values for any placeholder params used in the SQL
=cut =cut
sub buildArray { sub buildArray {
my $self = shift; my $self = shift;
my $sql = shift; my $arrayRef = $self->buildArrayRef(@_);
my $params = shift; return @{ $arrayRef };
my ($sth, $data, @array, $i);
$sth = $self->prepare($sql);
$sth->execute($params);
$i=0;
while (($data) = $sth->array) {
$array[$i] = $data;
$i++;
}
$sth->finish;
return @array;
} }
@ -135,14 +125,19 @@ sub buildArrayRef {
my $self = shift; my $self = shift;
my $sql = shift; my $sql = shift;
my $params = shift; my $params = shift;
my @array = $self->buildArray($sql,$params); my $sth = $self->prepare($sql);
return \@array; $sth->execute($params);
my @array;
while (my $data = $sth->arrayRef) {
push @array, @$data;
}
return \@array;
} }
#------------------------------------------------------------------- #-------------------------------------------------------------------
=head2 buildHash ( sql, params ) =head2 buildHash ( sql, params, options )
Builds a hash of data from a series of rows. Builds a hash of data from a series of rows.
@ -154,30 +149,28 @@ An SQL query. The query should select at least two columns of data, the first be
An array reference containing values for any placeholder params used in the SQL query. An array reference containing values for any placeholder params used in the SQL query.
=head3 options
A hash reference of options
=head4 noOrder
By default, buildHash returns the result tied to Tie::IxHash to maintain
order. Setting this option will prevent the tie, so the result will be a
straight hash that is faster but does not maintain order.
=cut =cut
sub buildHash { sub buildHash {
my $self = shift; my $self = shift;
my $sql = shift; my $hashRef = $self->buildHashRef(@_);
my $params = shift; return %{ $hashRef };
my ($sth, %hash, @data);
tie %hash, "Tie::IxHash";
$sth = $self->prepare($sql);
$sth->execute($params);
while (@data = $sth->array) {
my $value = pop @data;
my $key = join(":",@data); # if more than two columns is selected, join them together with :
$key = $value unless ($key); # if only one column is selected, then it is both the key and the value
$hash{$key} = $value;
}
$sth->finish;
return %hash;
} }
#------------------------------------------------------------------- #-------------------------------------------------------------------
=head2 buildHashRef ( sql ) =head2 buildHashRef ( sql, params, options )
Builds a hash reference of data from a series of rows. Builds a hash reference of data from a series of rows.
@ -189,16 +182,37 @@ An SQL query. The query should select at least two columns of data, the first be
An array reference containing values for any placeholder params used in the SQL query. An array reference containing values for any placeholder params used in the SQL query.
=head3 options
A hash reference of options
=head4 noOrder
By default, buildHashRef returns the result tied to Tie::IxHash to maintain
order. Setting this option will prevent the tie, so the result will be a
straight hash that is faster but does not maintain order.
=cut =cut
sub buildHashRef { sub buildHashRef {
my $self = shift; my $self = shift;
my $sql = shift; my $sql = shift;
my $params = shift; my $params = shift;
my ($sth, %hash); my $options = shift || {};
tie %hash, "Tie::IxHash"; my %hash;
%hash = $self->buildHash($sql, $params); unless ($options->{noOrder}) {
return \%hash; tie %hash, "Tie::IxHash";
}
my $sth = $self->prepare($sql);
$sth->execute($params);
while (my @data = $sth->array) {
my $value = pop @data;
my $key = join(":", @data); # if more than two columns is selected, join them together with :
$key = $value unless ($key); # if only one column is selected, then it is both the key and the value
$hash{$key} = $value;
}
$sth->finish;
return \%hash;
} }
@ -220,16 +234,16 @@ An array reference containing values for any placeholder params used in the SQL
=cut =cut
sub buildArrayRefOfHashRefs { sub buildArrayRefOfHashRefs {
my @array; my $self = shift;
my $class = shift; my $sql = shift;
my $sql = shift; my $params = shift;
my $params = shift; my @array;
my $sth = $class->read($sql,$params); my $sth = $self->read($sql, $params);
while (my $data = $sth->hashRef) { while (my $data = $sth->hashRef) {
push(@array,$data); push @array, $data;
} }
$sth->finish; $sth->finish;
return \@array; return \@array;
} }
@ -296,15 +310,14 @@ Which column of the result set to use as the key when creating the hashref.
=cut =cut
sub buildHashRefOfHashRefs { sub buildHashRefOfHashRefs {
my %hash; my $self = shift;
my $class = shift;
my $sql = shift; my $sql = shift;
my $params = shift; my $params = shift;
my $key = shift; my $key = shift;
my $sth = $class->read($sql,$params); my $sth = $self->read($sql, $params);
my $data; my %hash;
tie %hash, "Tie::IxHash"; tie %hash, "Tie::IxHash";
while ($data = $sth->hashRef) { while (my $data = $sth->hashRef) {
$hash{$data->{$key}} = $data; $hash{$data->{$key}} = $data;
} }
$sth->finish; $sth->finish;
@ -623,13 +636,9 @@ An array reference containing values for any placeholder params used in the SQL
sub quickArray { sub quickArray {
my $self = shift; my $self = shift;
my $sql = shift; my $sql = shift;
my $params = shift; my $params = shift || [];
my ($sth, @data); my $data = $self->dbh->selectrow_arrayref($sql, {}, @{ $params }) || [];
$sth = $self->prepare($sql); return @{ $data };
$sth->execute($params);
@data = $sth->array;
$sth->finish;
return @data;
} }