NAME

Template::Plugin::DBI - Template Plugin interface to the DBI.pm module

SYNOPSIS

[% USE DBI('dbi:driver:database', 'username', 'password') %]

[% FOREACH row = DBI.query( 'select col from table' ) %]

Here comes the data [% row.col %]

[% END %]

DESCRIPTION

This plugin provides an interface between a Template processor and DBI. This provides a simple method of including data from a DBI data source into a template to be processed by the Template module.

The DBI object can be initialised as follows:

[% USE DBI %]

This creates an uninitialised DBI object. It can be initialised when it is created by passing parameters that will be passed to the DBI connect call.

[% USE DBI('dbi:driver:database','username','password') %]

This will create a fully initialised DBI object.

OBJECT METHODS

connect

connect(data_source, username, password) connect(data_source = 'dbi:driver:database' username = 'foo' password = 'bar' )

Establishes a database connection.

The connect method accepts both the positional and named parameter syntax as shown.

The connect method provides for connecting to a data source explicitly, this can be used to reconnect an exisiting object to a different data source.

query( sql query string )

The query method returns data from the database given the provided sql query string. It does this in an efficient manner by only retrieving a single row at a time and returning this data to the Template through the Template::Iterator interface. This means that you can do this:

[% FOREACH DBI.query('select * from users') %]

Each row can be processed here

[% END %]

prepare

Prepare a query for later execution. This will return a clone of the current Template::Plugin::DBI object. You can use this thus:

[% user_query = DBI.prepare("select * from users where id = ?") %]

In order to get data back from the database for the query you should use execute (see below)

execute

Execute a previously prepared query. Given the example above you would call this with a FOREACH to select each of the records retuned.

[% FOREACH user = user_query.execute('sam') %] [% user.name %] [% END %]

do

Do executes a sql statement where there will be no records returned. It will return true if the statement was successful

[% IF DBI.do("delete from users where uid = 'sam'") %] Oh No the user was deleted [% END %]

quote

REQUIRES

Perl 5.005, Template-Toolkit 1.00, DBI

SEE ALSO

perldoc Template

perldoc DBI

AUTHOR

Simon A Matthews, sam@knowledgepool.com

COPYRIGHT

Copyright (C) 1999 Simon Matthews. All Rights Reserved

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

STILL TO DO

FIXUP Handlers - how to install - ALL type to process all - UNDEF magic should be optional ?

DBI method access - commit ? - rollback ?

Tests

DBI errors being thrown

Statement and DBH methods