[Templates] Template::Plugin::DBI - Using an existing db hand
le
Simon Matthews
sam@knowledgepool.com
Wed, 28 Jun 2006 07:20:50 +0100
This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.
------_=_NextPart_001_01C69A7B.005A48B8
Content-Type: text/plain
-----Evan Kaufman wrote-----
From: Evan Kaufman [mailto:evan.kaufman@gmail.com]
Sent: 27 June 2006 23:15
To: templates@template-toolkit.org
Subject: [Templates] Template::Plugin::DBI - Using an existing db handle
> I'm fairly new to TT, but its significantly simplified a large project I'm
working on. I am,
> however, having issues with the DBI plugin, specifically using an already
existing connection from
> the calling script for some simple queries.
>
> The documentation for some of these plugins are fairly sparse, they show
plenty about the TT
> directives, but not much of the perl code used to implement it, so this
may well be something
> simple. I'm wanting to use an existing DBI handle for a simple predefined
query:
Andy and I are working on a new version following the split of the DBI
plugin from the core. I will see if we can add some better examples to the
documentation.
> [% USE DBI( dbh = global_dbh ) %]
> <select name="genre">
> [% FOREACH result = DBI.query($genre_query) %]
This should be [% FOREACH result = DBI.query(genre_query) %]
[% foo = "bar" %]
[% bar = "baz" %]
[% $foo %]
Prints "baz"
So in your case you are looking for the contents of the variable "SELECT ID,
Desc FROM Table;" which is empty.
> <option value="[% result.ID %]">[% result.Desc %]</option>
> [% END %]
> And then, on the perl side, I specify the dbh, and the query:
> my $dbh = DBI->connect("dsn-stuff");
> $tt->process("page.html",
> {
> global_dbh => \$dbh,
> genre_query => "SELECT ID, Desc FROM Table;"
> }
> );
Dave has already pointed out that you don't need to take a reference to
$dbh.
You should always check the return from process and see if anything broke.
You should do this:
$tt->process('page.html', { ........ }) || die $tt->error();
You may want to handle the error rather than just calling die.
> Alas, when I request the page, I get nothing, and the httpd error log has
this:
> [Tue Jun 27 17:06:44 2006] [error] [client 71.2.112.154] undef error -
Can't call method "execute"
> on an undefined value at
/usr/lib/perl5/vendor_perl/5.8.8/i386-linux/Template/Plugin/DBI.pm
> line 268., referer: http://pe6300.psx.net/catalog/
> This doesn't make sense to me, because the query() method should do a
prepare and an execute,
> right?
You found an interesting feature (bug) which I will look at in the next
release, which that the error handling seems to be suspect when the query
that is passed to the query method is invalid.
Hope this helps
S
_______________________________________________
templates mailing list
templates@template-toolkit.org
http://lists.template-toolkit.org/mailman/listinfo/templates
------_=_NextPart_001_01C69A7B.005A48B8
Content-Type: text/html
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; =
charset=3DUS-ASCII">
<META NAME=3D"Generator" CONTENT=3D"MS Exchange Server version =
5.5.2653.12">
<TITLE>RE: [Templates] Template::Plugin::DBI - Using an existing db =
handle</TITLE>
</HEAD>
<BODY>
<P><FONT SIZE=3D2>-----Evan Kaufman wrote-----</FONT>
<BR><FONT SIZE=3D2>From: Evan Kaufman [<A =
HREF=3D"mailto:evan.kaufman@gmail.com">mailto:evan.kaufman@gmail.com</A>=
] </FONT>
<BR><FONT SIZE=3D2>Sent: 27 June 2006 23:15</FONT>
<BR><FONT SIZE=3D2>To: templates@template-toolkit.org</FONT>
<BR><FONT SIZE=3D2>Subject: [Templates] Template::Plugin::DBI - Using =
an existing db handle</FONT>
</P>
<BR>
<P><FONT SIZE=3D2>> I'm fairly new to TT, but its significantly =
simplified a large project I'm working on. I am, </FONT>
<BR><FONT SIZE=3D2>> however, having issues with the DBI plugin, =
specifically using an already existing connection from </FONT>
<BR><FONT SIZE=3D2>> the calling script for some simple =
queries.</FONT>
<BR><FONT SIZE=3D2>></FONT>
<BR><FONT SIZE=3D2>> The documentation for some of these plugins are =
fairly sparse, they show plenty about the TT </FONT>
<BR><FONT SIZE=3D2>> directives, but not much of the perl code used =
to implement it, so this may well be something </FONT>
<BR><FONT SIZE=3D2>> simple. I'm wanting to use an existing =
DBI handle for a simple predefined query:</FONT>
</P>
<P><FONT SIZE=3D2>Andy and I are working on a new version following the =
split of the DBI plugin from the core. I will see if we can add =
some better examples to the documentation.</FONT></P>
<P><FONT SIZE=3D2>> [% USE DBI( dbh =3D global_dbh ) %]</FONT>
<BR><FONT SIZE=3D2>> <select name=3D"genre"></FONT>
<BR><FONT SIZE=3D2>> [% FOREACH result =3D DBI.query($genre_query) =
%]</FONT>
</P>
<P><FONT SIZE=3D2>This should be [% FOREACH result =3D =
DBI.query(genre_query) %]</FONT>
</P>
<P><FONT SIZE=3D2>[% foo =3D "bar" %] </FONT>
<BR><FONT SIZE=3D2>[% bar =3D "baz" %] </FONT>
<BR><FONT SIZE=3D2>[% $foo %] =
</FONT>
</P>
<P><FONT SIZE=3D2>Prints "baz"</FONT>
</P>
<P><FONT SIZE=3D2>So in your case you are looking for the contents of =
the variable "SELECT ID, Desc FROM Table;" which is =
empty.</FONT>
</P>
<P><FONT SIZE=3D2>> <option value=3D"[% =
result.ID %]">[% result.Desc %]</option></FONT>
<BR><FONT SIZE=3D2>> [% END %]</FONT>
</P>
<P><FONT SIZE=3D2>> And then, on the perl side, I specify the dbh, =
and the query:</FONT>
</P>
<P><FONT SIZE=3D2>> my $dbh =3D =
DBI->connect("dsn-stuff");</FONT>
<BR><FONT SIZE=3D2>> $tt->process("page.html",</FONT>
<BR><FONT SIZE=3D2>> {</FONT>
<BR><FONT SIZE=3D2>> global_dbh =3D> =
\$dbh,</FONT>
<BR><FONT SIZE=3D2>> genre_query =3D> =
"SELECT ID, Desc FROM Table;"</FONT>
<BR><FONT SIZE=3D2>> }</FONT>
<BR><FONT SIZE=3D2>> );</FONT>
</P>
<P><FONT SIZE=3D2>Dave has already pointed out that you don't need to =
take a reference to $dbh.</FONT>
</P>
<P><FONT SIZE=3D2>You should always check the return from process and =
see if anything broke. You should do this:</FONT>
</P>
<P><FONT SIZE=3D2>$tt->process('page.html', { ........ }) || die =
$tt->error();</FONT>
</P>
<P><FONT SIZE=3D2>You may want to handle the error rather than just =
calling die.</FONT>
</P>
<P><FONT SIZE=3D2>> Alas, when I request the page, I get nothing, =
and the httpd error log has this:</FONT>
</P>
<P><FONT SIZE=3D2>> [Tue Jun 27 17:06:44 2006] [error] [client =
71.2.112.154] undef error - Can't call method "execute" =
</FONT>
<BR><FONT SIZE=3D2>> on an undefined value at =
/usr/lib/perl5/vendor_perl/5.8.8/i386-linux/Template/Plugin/DBI.pm</FONT=
>
<BR><FONT SIZE=3D2>> line 268., referer: <A =
HREF=3D"http://pe6300.psx.net/catalog/" =
TARGET=3D"_blank">http://pe6300.psx.net/catalog/</A></FONT>
</P>
<P><FONT SIZE=3D2>> This doesn't make sense to me, because the =
query() method should do a prepare and an execute, </FONT>
<BR><FONT SIZE=3D2>> right?</FONT>
</P>
<P><FONT SIZE=3D2>You found an interesting feature (bug) which I will =
look at in the next release, which that the error handling seems to be =
suspect when the query that is passed to the query method is =
invalid.</FONT></P>
<P><FONT SIZE=3D2>Hope this helps</FONT>
</P>
<P><FONT SIZE=3D2>S</FONT>
</P>
<P><FONT =
SIZE=3D2>_______________________________________________</FONT>
<BR><FONT SIZE=3D2>templates mailing list</FONT>
<BR><FONT SIZE=3D2>templates@template-toolkit.org <A =
HREF=3D"http://lists.template-toolkit.org/mailman/listinfo/templates" =
TARGET=3D"_blank">http://lists.template-toolkit.org/mailman/listinfo/tem=
plates</A></FONT>
</P>
</BODY>
</HTML>
------_=_NextPart_001_01C69A7B.005A48B8--