[Templates] where to put the db logic
Perrin Harkins
perrin@elem.com
Mon, 06 Jan 2003 15:09:07 -0500
allan wrote:
> 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);
Ick. You should try to avoid this kind of eval stuff if at all
possible. It's slow and hard to debug.
> 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;
That's not so hot either, because now the template knows about database
stuff, has to deal with any db errors, etc.
The most obvious way to do it is to simply query the database, put the
results into your own data structure, and pass that to the template.
There are things like Class::DBI which can automate some of that if your
database structure is appropriate.
By the way, fetchrow_hashref() is slow. You'll get much better
performance from using bind_params() with fetch().
- Perrin