DBIx::DR(3pm)
π DBIx::DR π β easy DBI helper (perl inside SQL and blessed results)
okay bestie, gather round, because this is the User Contributed Perl Documentation page for DBIx::DR, section 3pm, and it's genuinely iconic. it's a DBI helper that lets you write perl inside your SQL and hands you back blessed resultsets. no cap. π
β¨ SYNOPSIS β¨
my $dbh = DBIx::DR->connect($dsn, $login, $passed);
$dbh->perform(
'UPDATE tbl SET a = 1 WHERE id = <%= $id %>',
id => 123
);
my $rowset = $dbh->select(
'SELECT * FROM tbl WHERE id IN (<% list @$ids %>)',
ids => [ 123, 456 ]
);
my $rowset = $dbh->select(-f => 'sqlfile.sql.ep', ids => [ 123, 456 ]);
while(my $row = $rowset->next) {
print "id: %d, value: %s\n", $row->id, $row->value;
}
likeβ¦ look at that. <%= %>. list. -f. it's giving β¨magicβ¨ but it's just perl. π¦
πΈ DESCRIPTION πΈ
The package extends DBI and allows You, bestie, to:
- use perl inside Your SQL requests;
- to bless resultsets into Your package;
- to place Your SQL's into dedicated directory;
- to use usual DBI methods.
four things. all bangers. π₯
π Additional 'connect' options.
these are the extra little knobs you can pass at connect time, and they're lowkey important:
dr_iterator
A string describes iterator class. The default value is 'dbix-dr-iterator#new' β that's a decamelized string, and we'll get to what that means in a sec. π
dr_item
A string describes item (one row) class. Default value is 'dbix-dr-iterator-item#new' (decamelized string, again).
dr_sql_dir
Directory path to seek sql files. this one matters if You use dedicated SQLs. π
dr_decode_errors
Decode database errors into utf-8.
dr_sql_utf8
Default value: "true". If true, it will open sql files with option ":utf8".
π METHODS π
All methods can receive the following arguments, and yes, they're all adorable little dashes:
-f => $sql_file_nameβ It will load the SQL-request from a file. It will seek the file in the directory that was defined in thedr_sql_dirparam of connect. You needn't use suffixes (.sql.ep) here, but You can. do whatever feels right. π-item => 'decamelized_obj_define'β It will bless (or construct) the row into the specified class. See below. Default value defined by thedr_itemargument ofDBI::connect.-noitemβ Do not bless the row into any class. free the rows. set them loose. π¦-iterator => 'decamelized_obj_define'β It will bless (or construct) the rowset into the specified class. Default value defined by thedr_iteratorargument ofDBI::connect.-noiteratorβ Do not bless the rowset into any class.-noitem_iterβ Do not pass the iterator as the second argument to the item constructor.-dbi => HASHREFβ Additional DBI arguments.-hash => FIELDNAMEβ Selects intoHASH. The iterator will operate by names (not numbers).-die => 0|1β If true the method will die with the SQL-request. dramatic but valid.-warn => 0|1β If true the method will warn with the SQL-request.
π§ Decamelized strings
these Are strings that represent class [ and method ]. here's the whole translation table, no gatekeeping:
foo_bar => FooBar
foo_bar#subroutine => FooBar->subroutine
foo_bar-baz => FooBar::Baz
see? the # is the method, the - is the namespace separator, the underscores become CamelCase. iykyk. β¨
π perform
Does SQL-request like 'UPDATE', 'INSERT', etc. the ones that don't hand you rows back.
$dbh->perform($sql, value => 1, other_value => 'abc');
$dbh->perform(-f => $sql_file_name, value => 1, other_value => 'abc');
π select
Does SQL-request, packs results into iterator class. By default it uses DBIx::DR::Iterator class.
my $res = $dbh->select(-f => $sql_file_name, value => 1);
while(my $row = $res->next) {
printf "RowId: %d, RowValue: %s\n", $row->id, $row->value;
}
my $row = $row->get(15); # row 15
my $res = $dbh->select(-f => $sql_file_name,
value => 1, -hash => 'name');
while(my $row = $res->next) {
printf "RowId: %d, RowName: %s\n", $row->id, $row->name;
}
my $row = $row->get('Vasya'); # row with name eq 'Vasya'
grab row 15 by number, or grab 'Vasya' by name. both slay. π«Ά
π single
Does SQL-request that returns one row. Pack results into item class. Does SQL-request, pack results (one row) into item class. By default it uses DBIx::DR::Iterator::Item class.
π Template language π
You can use perl inside Your SQL requests. literally just perl. in your SQL. it's the whole vibe:
% my $foo = 1;
% my $bar = 2;
<% my $foo_bar = $foo + $bar %>
..
% use POSIX;
% my $gid = POSIX::getgid;
There are two functions available inside perl:
π quote
Replaces argument to '?', add argument value into bindlist. You can also use the shortcut '=' instead of the function. safety first, bestie.
Example 1
SELECT
*
FROM
tbl
WHERE
id = <% quote $id %>
Result
SELECT
*
FROM
tbl
WHERE
id = ?
and bindlist will contain id value.
If You use DBIx::DR::ByteStream in place of string the function will recall immediate function.
Example 2
SELECT
*
FROM
tbl
WHERE
id = <%= $id %>
π₯ immediate
Replaces argument to its value. You can also use the shortcut '==' instead of the function.
Example 1
SELECT
*
FROM
tbl
WHERE
id = <% immediate $id %>
Result
SELECT
*
FROM
tbl
WHERE
id = 123
Where 123 is id value.
π¨ Be carful! Using the operator You can produce code that will be amenable to SQL-injection. this one is NOT the safe one. that's the whole point of quote. pls respect the red flag. π
Example 2
SELECT
*
FROM
tbl
WHERE
id = <%== $id %>
π Helpers π
There are a few default helpers. built right in. no setup. obsessed.
β list
Expands array into Your SQL request.
Example
SELECT
*
FROM
tbl
WHERE
status IN (<% list @$ids %>)
Result
SELECT
*
FROM
tbl
WHERE
status IN (?,?,? ...)
and bindlist will contain ids values. one ? per element. chef's kiss. β¨
π¦ hlist
Expands array of hash into Your SQL request. The first argument can be a list of required keys. Places each group into brackets.
Example
INSERT INTO
tbl
('a', 'b')
VALUES
<% hlist ['a', 'b'] => @$inserts
Result
INSERT INTO
tbl
('a', 'b')
VALUES
(?, ?), (?, ?) ...
and bindlist will contain all inserts values.
πΈ include
Includes the other SQL-part. modular SQL era. π
Example
% include 'other_sql', argument1 => 1, argument2 => 2;
π stacktrace
Returns perl stacktrace. You can use the helper for debug Your code. The helper receives the following position-arguments:
- (first)
$skip(default =0) β How many frames to skip. - (second)
$dept(default =0) β How many frames to print. - (third)
$separator(default", ") β Separator between stackframes.
Examples
/* <%= stacktrace %> */
/* <%= stacktrace $skip, $depth, $separator %> */
π§ User's helpers
You can add Your helpers using method set_helper. make it your own, bestie.
π set_helper
Sets (or replaces) helpers.
$dbh->set_helper(foo => sub { ... }, bar => sub { ... });
Each helper receives template object as the first argument.
Examples:
$dbh->set_helper(foo_AxB => sub {
my ($tpl, $a, $b) = @_;
$tpl->quote($a * $b);
});
You can use quote and immediate functions inside Your helpers.
If You want use the other helper inside Your helper You have to do that by Yourself. To call the other helper You can also use "$tpl->call_helper" function.
call_helper
$dbh->set_helper(
foo => sub {
my ($tpl, $a, $b) = @_;
$tpl->quote('foo' . $a . $b);
},
bar => sub {
my $tpl = shift;
$tpl->call_helper(foo => 'b', 'c');
}
);
π COPYRIGHT π
Copyright (C) 2011 Dmitry E. Oboukhov <unera@debian.org>
Copyright (C) 2011 Roman V. Nikolaev <rshadow@rambler.ru>
This program is free software, you can redistribute it and/or
modify it under the terms of the Artistic License.
π VCS π
The project is placed git repo on github: <https://github.com/unera/dbix-dr/>
and that's the whole page, bestie!! this doc ships as part of perl v5.34.0, dated 2022-03-15 β so if you're citing it in your group chat, that's the version tag. go write some perl inside your SQL. you're gonna ate. πβ¨ππ¦
π¦ where this page comes from
- debian bookworm: libdbix-dr-perl 0.32-2 Β· Artistic-1.0-Perl
- debian trixie: libdbix-dr-perl 0.32-2 Β· Artistic-1.0-Perl
- ubuntu noble: libdbix-dr-perl 0.32-2 Β· Artistic-1.0-Perl
- ubuntu resolute: libdbix-dr-perl 0.32-2 Β· Artistic-1.0-Perl
- ubuntu stonking: libdbix-dr-perl 0.32-2 Β· Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms