NAME

Tao::DBI - Portable support for named placeholders in DBI statements

SYNOPSIS

use Tao::DBI;

$dbh = Tao::DBI->connect({ dsn => $dsn, user => $user, pass => $pass });
$sql = q{UPDATE T set a = :a, b = :b where k = :k};
$stmt = $dbh->prepare($sql);
$rc = $stmt->execute({ k => $k, a => $a, b => $b });

DESCRIPTION

THIS IS PRE-ALPHA SOFTWARE! MANY BUGS LURKING AROUND!

perldoc DBI - section "Placeholders and Bind Values"

"Some drivers also allow placeholders like :name and :n (e.g., :1, :2, and so on) in addition to ?, but their use is not portable."

connect
my $dbh = Tao::DBI->connect($args);

Returns a new database connection built from the arguments in hash ref $args.

Note. The previous dbi_prepare function which was exported on demand was deprecated in favor of this class method that looks more DBIsh.

dbi_prepare
my $sth = dbi_prepare($args);

Returns a new prepared statement. This statement supports named placeholders (like :a) whether the driver supports it or not. (However, the driver has to support ? placeholders.)

You don't have to import this function if you plan to create DBI connections via Tao::DBI::dbi_connect, because these will automatically support SQL with named placeholders in prepare.

EXPORT

dbi_connect and dbi_prepare can be exported on demand. dbi_connect was deprecated. I think dbi_prepare will have the same fate soon.

PRINCIPLES OF THIS TAO

Every constructor is designed to accept named parameters.

my $o = new Tao::DBI::o($hashref);
# or
my $o = new Tao::DBI::o(k1 => $v1, k2 => $v2);

This is one Tao, not the only one. As long as it aims to perfection, it is Tao. It does not need to aim to uniqueness. (This is not Python. TIMTOWTDI.)

TAO STATEMENTS

Tao statements are DBI statements.

For Tao statements, both sets of parameters/placeholders and rows may be represented as hash refs. The main point of this usage is to represent an ensemble of values as one.

$stmt->execute($params);
while ($row = $stmt->fetch_hashref()) {
  ...
}

If parameters are added, removed or modified (eg. by changing data types), the code often stays the same.

The emphasis on naming parts (hash keys) also has to with this approach. Not relying on artificial indices is good. Self-documenting with well-chosen names is good too.

One intentional benefit of the uniform treatment of statement parameters and selected rows is the possibility of extracting data via SELECT statements and injecting it (possibly after transformation) via INSERT and UPDATE statements. One example is in order, but I am just too lazy today.

SEE ALSO

DBI

BUGS

Please report bugs via CPAN RT http://rt.cpan.org/NoAuth/Bugs.html?Dist=Tao-DBI.

AUTHOR

Adriano R. Ferreira, <ferreira@cpan.org>

COPYRIGHT AND LICENSE

Copyright (C) 2005-2007 by Adriano R. Ferreira

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