sub get_tmpl_loop_arrayref_bind
#Given an SQL query this will return an arrayref compatible with TMPL_LOOP. Works where the query does not contain a , before the 'from' and where there is no subquery.
{
my ($dbh, $sql, @binds, $sessID) = @_;
$dbh->{RaiseError}=0;
$dbh->{PrintError}=0;
$sql=~/select\s+(.*?)from\s+.*/ig;
my $onlyfields = $1;
my @fields = split /,/, $onlyfields;
foreach my $field (@fields)
{
$field=$1 if ($field=~/.*?\s+as\s+\[(.*)\]/ig); #Fields taken with the as identifier are parsed here. Eg. "select a/b as ratio"
$field=~s/\s+//ig;
}
my $sth = $dbh->prepare($sql) || die "Could not prepare query $sql in get_tmpl_loop_arrayref";
$sth->execute || die "Could not execute query $sql in get_tmpl_loop_arrayref";
my @loop_data = ();
while (my $hash_op = $sth->fetchrow_hashref('NAME_lc'))
{
my %row_data;
foreach my $field (@fields)
{
$row_data{$field}=$hash_op->{$field}? $hash_op->{$field}:'-' ;
}
$row_data{id}=$sessID if $sessID;
push (@loop_data, \%row_data);
}
if ($dbh->errstr()) {
return $dbh->errstr();
}
else
{return \@loop_data;}
}
####
"SELECT username, name, role, email, phone, mobile, creation_date from user where creator='".$session->param('username')."'";
####
|
|
|
|
|
|
|