DBIx::ContextualFetch(User Contributed Perl DocumentDBIx::ContextualFetch(3pm))
DBIx::ContextualFetch - Add contextual fetches to DBI
| Use Case | Command | Description |
|---|---|---|
| Connect with RootClass | DBI->connect($dsn, $user, $pass, { RootClass => "DBIx::ContextualFetch" }) | π§ Enables contextual fetch methods for all statement handles |
| Execute with bind values and columns | $sth->execute(\@bind_values, \@bind_cols) | π Combines bind_param, execute, and bind_columns in one call |
| Contextβsensitive fetch | $row_ref = $sth->fetch; @row = $sth->fetch; | π₯ Scalar β arrayref, list β array |
| Fetch hash (scalar or list) | $row_ref = $sth->fetch_hash; %row = $sth->fetch_hash; | π Scalar β hashref, list β hash |
| Fetch all rows | $rows_ref = $sth->fetchall; @rows = $sth->fetchall; | π¦ Scalar β arrayref of arrayrefs, list β list of arrayrefs |
| Fetch all rows as hashes | $rows_ref = $sth->fetchall_hash; @rows = $sth->fetchall_hash; | π Scalar β arrayref of hashrefs, list β list of hashrefs |
my $dbh = DBI->connect(...., { RootClass => "DBIx::ContextualFetch" });
# Modified statement handle methods.
my $rv = $sth->execute;
my $rv = $sth->execute(@bind_values);
my $rv = $sth->execute(\@bind_values, \@bind_cols);
# In addition to the normal DBI sth methods...
my $row_ref = $sth->fetch;
my @row = $sth->fetch;
my $row_ref = $sth->fetch_hash;
my %row = $sth->fetch_hash;
my $rows_ref = $sth->fetchall;
my @rows = $sth->fetchall;
my $rows_ref = $sth->fetchall_hash;
my @tbl = $sth->fetchall_hash;
It always struck me odd that DBI didn't take much advantage of Perl's context sensitivity. DBIx::ContextualFetch redefines some of the various fetch methods to fix this oversight. It also adds a few new methods for convenience (though not necessarily efficiency).
my $dbh = DBIx::ContextualFetch->connect(@info);
my $dbh = DBI->connect(@info, { RootClass => "DBIx::ContextualFetch" });
To use this method, you can either make sure that everywhere you normally call DBI->connect() you either call it on DBIx::ContextualFetch, or that you pass this as your RootClass. After this DBI will Do The Right Thing and pass all its calls through us.
$rv = $sth->execute;
$rv = $sth->execute(@bind_values);
$rv = $sth->execute(\@bind_values, \@bind_cols);
execute() is enhanced slightly:
$sth->execute(@bind_values);
$sth->bind_columns(undef, @bind_cols);
In addition, execute will accept tainted @bind_values. I can't think of what a malicious user could do with a tainted bind value (in the general case. Your application may vary.)
Thus a typical idiom would be:
$sth->execute([$this, $that], [\($foo, $bar)]);
Of course, this method provides no way of passing bind attributes through to bind_param or bind_columns. If that is necessary, then you must perform the bind_param, execute, bind_col sequence yourself.
$row_ref = $sth->fetch;
@row = $sth->fetch;
A context sensitive version of fetch(). When in scalar context, it will act as fetchrow_arrayref. In list context it will use fetchrow_array.
$row_ref = $sth->fetch_hash;
%row = $sth->fetch_hash;
A modification on fetchrow_hashref. When in scalar context, it acts just as fetchrow_hashref() does. In list context it returns the complete hash.
$rows_ref = $sth->fetchall;
@rows = $sth->fetchall;
A modification on fetchall_arrayref. In scalar context it acts as fetchall_arrayref. In list it returns an array of references to rows fetched.
$rows_ref = $sth->fetchall_hash;
@rows = $sth->fetchall_hash;
A mating of fetchall_arrayref() with fetchrow_hashref(). It gets all rows from the hash, each as hash references. In scalar context it returns a reference to an array of hash references. In list context it returns a list of hash references.
Michael G Schwern as part of Ima::DBI
Tony Bowden <tony AT tmtm.com>
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
DBI. Ima::DBI. Class::DBI.
perl v5.32.0 2020-12-28 DBIx::ContextualFetch(3pm)
Generated by phpman v4.9.26-1-g511901d Author: Che Dong Under GNU General Public License
2026-08-09 09:01 @2600:1f28:365:80b0:50b3:453e:ff52:20f7
CrawledBy CCBot/2.0 (https://commoncrawl.org/faq/)
Enhanced by LLM: deepseek-v4-flash / taotoken.net / www.chedong.com - original format