[Templates] where to put the db logic

allan lambretta@inet.uni2.dk
Mon, 06 Jan 2003 20:41:45 +0100


hi

beeing fairly new to TT and PostgreSQL [and lazy] i hope someone can
advice with regard to where to put the logic in the code. below are
two versions of the same logic put different places. i'm just curious
which is the better performance- and maintainance-wise, or if people
do it differently i'm naturally also interested.

thanks
./allan


1) 
i found that the PostgreSQL custom functions seem to return *string
concatenations* of the results-set. so in those functions i will loop
over the results and end up with a return string that looks like this:

  $cust_id = 1; $cust_name = "allan";

in my perl code i will then do an eval like

my $ret = $sth->fetchrow_hashref->{postgres_function"};
eval($ret);

i can then effectively export those to my templates

my $vars = {
  'cust_id'   => $cust_id, 
  'cust_name' => $cust_name, 
}


# if multiple rows are concerned i would put them in a string like
$hash_name{row_id_1} = "row_name_1"; etc; and then eval that in the
perl code, and then export the \%hash_name to TT.




2)
my $sth = $DBH->prepare->{'select statement'};

my $vars = {
  'sth'   => $sth, 
}

2)
loop inside of the template
WHILE (ref = sth.fetchrow_hashref);
  ...
  ref = ref.next;   
END;