Rose::...uilder(3pm)
okay bestie, this is Rose::DB::Object::QueryBuilder(3pm) from User Contributed Perl Documentation, and itβs literally the SQL-building bestie you didnβt know you needed π
πΈ NAME πΈ
Rose::DB::Object::QueryBuilder β builds SQL queries on behalf of Rose::DB::Object::Manager. No cap, thatβs the whole assignment.
β¨ SYNOPSIS β¨
okay so hereβs the vibe in code, bestie π«Ά
use Rose::DB::Object::QueryBuilder qw(build_select);
# Build simple query
$sql = build_select
(
dbh => $dbh,
select => 'COUNT(*)',
tables => [ 'articles' ],
columns => { articles => [ qw(id category type title date) ] },
query =>
[
category => [ 'sports', 'science' ],
type => 'news',
title => { like => [ '%million%',
'%resident%' ] },
],
query_is_sql => 1);
$sth = $dbh->prepare($sql);
$sth->execute;
$count = $sth->fetchrow_array;
...
# Return query with placeholders, plus bind values
($sql, $bind) = build_select
(
dbh => $dbh,
tables => [ 'articles' ],
columns => { articles => [ qw(id category type title date) ] },
query =>
[
category => [ 'sports', 'science' ],
type => 'news',
title => { like => [ '%million%',
'%resident%' ] },
],
query_is_sql => 1,
sort_by => 'title DESC, category',
limit => 5);
$sth = $dbh->prepare($sql);
$sth->execute(@$bind);
while($row = $sth->fetchrow_hashref) { ... }
...
# Coerce query values into the right format
($sql, $bind) = build_select
(
db => $db,
tables => [ 'articles' ],
columns => { articles => [ qw(id category type title date) ] },
classes => { articles => 'Article' },
query =>
[
type => 'news',
date => { lt => 'now' },
date => { gt => DateTime->new(...) },
],
sort_by => 'title DESC, category',
limit => 5);
$sth = $dbh->prepare($sql);
$sth->execute(@$bind);
π¦ DESCRIPTION π¦
Rose::DB::Object::QueryBuilder is used to build SQL queries, primarily in service of the Rose::DB::Object::Manager class. It (optionally) exports two functions: build_select() and build_where_clause(). Slay.
π FUNCTIONS π
π build_select PARAMS π
Returns an SQL "select" query string in scalar context. In list context, it returns an SQL "select" query string with placeholders plus a reference to an array of bind values. Either way, itβs constructed based on PARAMS. Valid PARAMS are described below, bestie π
clausesCLAUSES β a reference to an array of extra SQL clauses to add to the "WHERE" portion of the query string. This is the obligatory "escape hatch" for clauses that are not supported by arguments to thequeryparameter. iykyk.columnsHASHREF β a reference to a hash keyed by table name, each of which points to a reference to an array of the names of the columns in that table. Example:
$sql = build_select(columns =>
{
table1 => [ 'col1', 'col2', ... ],
table2 => [ 'col1', 'col2', ... ],
...
});
This argument is required. Periodt.
dbDB β aRose::DB-derived object. This argument is required ifquery_is_sqlis false or omitted.dbhDBH β a DBI database handle already connected to the correct database. If this argument is omitted, an attempt will be made to extract a database handle from thedbargument. If this fails, or if there is nodbargument, a fatal error will occur. π
heads up bestie: nodbh, nodb, or a failed extraction = fatal error. not cute.
group_byCLAUSE β a fully formed SQL "GROUP BY ..." clause, sans the words "GROUP BY", or a reference to an array of strings to be joined with a comma and appended to the "GROUP BY" clause.limitNUMBER β a number to use in the "LIMIT ..." clause.logicLOGIC β a string indicating the logic that will be used to join the statements in the WHERE clause. Valid values for LOGIC are "AND" and "OR". If omitted, it defaults to "AND".prettyBOOL β if true, the SQL returned will have slightly nicer formatting. cute formatting era β¨queryPARAMS β the query parameters, passed as a reference to an array of name/value pairs, scalar references, or array references. PARAMS may include an arbitrary list of selection parameters used to modify the "WHERE" clause of the SQL select statement. Any query parameter that is not in one of the forms described below will cause a fatal error.
Valid selection parameters are described below, along with the SQL clause they add to the select statement. letβs go π₯
π Simple equality π
'NAME' => "foo" # COLUMN = 'foo'
'!NAME' => "foo" # NOT(COLUMN = 'foo')
'NAME' => [ "a", "b" ] # COLUMN IN ('a', 'b')
'!NAME' => [ "a", "b" ] # COLUMN NOT(IN ('a', 'b'))
π§ Is/is not null π§
'NAME' => undef # COLUMN IS NULL
'!NAME' => undef # COLUMN IS NOT NULL
'NAME' => { eq => undef } # COLUMN IS NULL
'NAME' => { ne => undef } # COLUMN IS NOT NULL
π Comparisons π
NAME => { OP => "foo" } # COLUMN OP 'foo'
# (COLUMN OP 'foo' OR COLUMN OP 'goo')
NAME => { OP => [ "foo", "goo" ] }
"OP" can be any of the following:
OP SQL operator
------------- ------------
similar SIMILAR TO
match ~
imatch ~*
regex, regexp REGEXP
like LIKE
ilike ILIKE
rlike RLIKE
is IS
is_not IS NOT
ne <>
eq =
lt <
gt >
le <=
ge >=
π Ranges π
NAME => { between => [ 1, 99 ] } # COLUMN BETWEEN 1 AND 99
NAME => { gt_lt => [ 1, 99 ] } # (COLUMN > 1 AND < 99)
NAME => { gt_le => [ 1, 99 ] } # (COLUMN > 1 AND <= 99)
NAME => { ge_lt => [ 1, 99 ] } # (COLUMN >= 1 AND < 99)
NAME => { ge_le => [ 1, 99 ] } # (COLUMN >= 1 AND <= 99)
If a value is a reference to a scalar, that scalar is "inlined" without any quoting.
'NAME' => \"foo" # COLUMN = foo
'NAME' => [ "a", \"b" ] # COLUMN IN ('a', b)
Undefined values are translated to the keyword NULL when included in a multi-value comparison.
'NAME' => [ "a", undef ] # COLUMN IN ('a', NULL)
π Set operations π
### Informix (default) ###
# A IN COLUMN
'NAME' => { in_set => 'A' }
# NOT(A IN COLUMN)
'!NAME' => { in_set => 'A' }
# (A IN COLUMN OR B IN COLUMN)
'NAME' => { in_set => [ 'A', 'B'] }
'NAME' => { any_in_set => [ 'A', 'B'] }
# NOT(A IN COLUMN) AND NOT(B IN COLUMN)
'!NAME' => { in_set => [ 'A', 'B'] }
'!NAME' => { any_in_set => [ 'A', 'B'] }
# (A IN COLUMN AND B IN COLUMN)
'NAME' => { all_in_set => [ 'A', 'B'] }
# NOT(A IN COLUMN AND B IN COLUMN)
'!NAME' => { all_in_set => [ 'A', 'B'] }
### MySQL (requires db parameter) ###
# FIND_IN_SET(A, COLUMN) > 0
'NAME' => { in_set => 'A' }
# NOT(FIND_IN_SET(A, COLUMN) > 0)
'!NAME' => { in_set => 'A' }
# (FIND_IN_SET(A, COLUMN) > 0 OR FIND_IN_SET(B, COLUMN) > 0)
'NAME' => { in_set => [ 'A', 'B'] }
'NAME' => { any_in_set => [ 'A', 'B'] }
# NOT(FIND_IN_SET(A, COLUMN) > 0) AND NOT(FIND_IN_SET(B, COLUMN) > 0)
'!NAME' => { in_set => [ 'A', 'B'] }
'!NAME' => { any_in_set => [ 'A', 'B'] }
# (FIND_IN_SET(A, COLUMN) > 0 AND FIND_IN_SET(B, COLUMN) > 0)
'NAME' => { all_in_set => [ 'A', 'B'] }
# NOT(FIND_IN_SET(A, COLUMN) > 0 AND FIND_IN_SET(B, COLUMN) > 0)
'!NAME' => { all_in_set => [ 'A', 'B'] }
π¦ Array operations π¦
# A = ANY(COLUMN)
'NAME' => { in_array => 'A' }
# NOT(A = ANY(COLUMN))
'!NAME' => { in_array => 'A' }
# (A = ANY(COLUMN) OR B = ANY(COLUMN))
'NAME' => { in_array => [ 'A', 'B'] }
'NAME' => { any_in_array => [ 'A', 'B'] }
# NOT(A = ANY(COLUMN) OR B = ANY(COLUMN))
'!NAME' => { in_array => [ 'A', 'B'] }
'!NAME' => { any_in_array => [ 'A', 'B'] }
# (A = ANY(COLUMN) AND B = ANY(COLUMN))
'NAME' => { all_in_array => [ 'A', 'B'] }
# NOT(A = ANY(COLUMN) AND B = ANY(COLUMN))
'!NAME' => { all_in_array => [ 'A', 'B'] }
πΈ PostgreSQL ltree operations πΈ
OP SQL operator
------------- ------------
ltree_ancestor @>
ltree_descendant <@
ltree_query ~
ltree_ltxtquery @
ltree_concat ||
Any of the operations described above can have _sql appended to indicate that the corresponding values are to be "inlined" (i.e., included in the SQL query as-is, with no quoting of any kind). This is useful for comparing two columns. For example, this query:
query => [ legs => { gt_sql => 'eyes' } ]
would produce this SQL:
SELECT ... FROM animals WHERE legs > eyes
where "legs" and "eyes" are both left unquoted. no cap, thatβs the whole point π«Ά
The same NAME string may be repeated multiple times. (This is the primary reason that the query is a reference to an array of name/value pairs, rather than a reference to a hash, which would only allow each NAME once.) Example:
query =>
[
age => { gt => 10 },
age => { lt => 20 },
]
okay so the NAME string is kinda the main character here, and it can show up in a bunch of different fits β but every single one eventually resolves to a database column (thatβs COLUMN in the examples). π
Wanna drop in literal SQL? Pass a reference to a scalar:
\'mycol > 123'
Wanna use placeholders and bind values? Pass a reference to an array where the first item is a scalar reference to the literal SQL with placeholders, and the rest are the values to bind:
[ \'mycol > ?' => 123 ]
Now for the actual NAME forms, bestie:
"column"β just a bare column name. If the query includes more than one table, the column name may be ambiguous if it appears in two or more tables. In that case, a fatal error will occur. To solve this, use one of the less ambiguous forms below."table.column"β a column name and a table name joined by a dot. This is the βfully qualifiedβ column name."tN.column"β a column name and a table alias joined by a dot. The table alias is in the formtN, whereNis a number starting from 1. See the documentation for tables parameter below to learn how table aliases are assigned to tables.- Any of the above prefixed with
!β this indicates the negation of the specified condition.
If query_is_sql is false or omitted, then NAME can also take on these additional forms:
"method"β a get_set column method name from a Rose::DB::Object-derived class fronting one of the tables being queried. There may be ambiguity here if the same method name is defined on more than one of the classes involved in the query. In such a case, the method will be mapped to the first Rose::DB::Object-derived class that contains a method by that name, considered in the order that the tables are provided in the tables parameter."!method"β this indicates the negation of the specified condition.
Un-prefixed column or method names that are ambiguous (i.e., exist in more than one of the tables being queried) are considered to be part of the primary table ("t1"). π«Ά
Finally, in the case of apparently intractable ambiguity, like when a table name is the same as another tableβs alias, remember that you can always use the "tn_"-prefixed column name aliases, which are unique within a given query. iconic, honestly.
All of these clauses are joined by logic (default: "AND") in the final query. Example:
$sql = build_select
(
dbh => $dbh,
select => 'id, title',
tables => [ 'articles' ],
columns => { articles => [ qw(id category type title) ] },
query =>
[
category => [ 'sports', 'science' ],
type => 'news',
title => { like => [ '%million%',
'%resident%' ] },
],
query_is_sql => 1);
The above returns an SQL statement something like this:
SELECT id, title FROM articles WHERE
category IN ('sports', 'science')
AND
type = 'news'
AND
(title LIKE '%million%' OR title LIKE '%resident%')
LIMIT 5
Nested boolean logic is possible using the special keywords "and" and "or" (case insensitive). Example:
$sql = build_select
(
dbh => $dbh,
select => 'id, title',
tables => [ 'articles' ],
columns => { articles => [ qw(id category type title) ] },
query =>
[
or =>
[
and => [ category => undef, type => 'aux' ],
category => [ 'sports', 'science' ],
],
type => 'news',
title => { like => [ '%million%',
'%resident%' ] },
],
query_is_sql => 1);
which returns an SQL statement something like this:
SELECT id, title FROM articles WHERE
(
(
category IS NULL AND
type = 'aux'
)
OR category IN ('sports', 'science')
)
AND
type = 'news'
AND
(title LIKE '%million%' OR title LIKE '%resident%')
The "and" and "or" keywords can be used multiple times within a query (just like all other NAME specifiers described earlier) and can be arbitrarily nested. β¨
If you have a column named "and" or "or", youβll have to use the fully-qualified (table.column) or alias-qualified (tN.column) forms in order to address that column. no cap.
If query_is_sql is false or omitted, all of the parameter values are passed through the parse_value() and format_value() methods of their corresponding Rose::DB::Object::Metadata::Column-derived column objects. π§
If a column object returns true from its manager_uses_method() method, then its parameter value is passed through the corresponding Rose::DB::Object-derived object method instead. π
Example:
$dt = DateTime->new(year => 2001, month => 1, day => 31);
$sql = build_select
(
db => $db,
select => 'id, category',
tables => [ 'articles' ],
columns => { articles => [ qw(id category type date) ] },
classes => { articles => 'Article' },
query =>
[
type => 'news',
date => { lt => '12/25/2003 8pm' },
date => { gt => $dt },
],
sort_by => 'id DESC, category',
limit => 5);
The above returns an SQL statement something like this:
SELECT id, category FROM articles WHERE
type = 'news'
AND
date < '2003-12-25 20:00:00'
AND
date > '2001-01-31 00:00:00'
ORDER BY id DESC, category
LIMIT 5
Finally, hereβs an example using more than one table:
$dt = DateTime->new(year => 2001, month => 1, day => 31);
$sql = build_select
(
db => $db,
tables => [ 'articles', 'categories' ],
columns =>
{
articles => [ qw(id name category_id date) ],
categories => [ qw(id name description) ],
},
classes =>
{
articles => 'Article',
categories => 'Category',
},
query =>
[
'!t1.name' => { like => '%foo%' },
t2.name => 'news',
date => { lt => '12/25/2003 8pm' },
date => { gt => $dt },
],
clauses =>
[
't1.category_id = t2.id',
],
sort_by => 'articles.name DESC, t2.name',
limit => 5);
The above returns an SQL statement something like this:
SELECT
t1.id,
t1.name,
t1.category_id,
t1.date,
t2.id,
t2.name,
t2.description
FROM
articles t1,
categories t2
WHERE
t1.category_id = t2.id
AND
NOT(t1.name LIKE '%foo%')
AND
t2.name = 'news'
AND
t1.date < '2003-12-25 20:00:00'
AND
t1.date > '2001-01-31 00:00:00'
ORDER BY articles.name DESC, t2.name
LIMIT 5
query_is_sql BOOL β if omitted, this boolean flag is false. If true, then the values of the query parameters are taken as literal strings that are suitable for direct use in SQL queries. Example:
$sql = build_select
(
query_is_sql => 1,
query =>
[
date => { lt => '2003-12-25 20:00:00' },
],
...
);
Here the date value "2003-12-25 20:00:00" must be in the format that the current database expects for columns of that data type. π
But if query_is_sql is false or omitted, then any query value that can be handled by the Rose::DB::Object-derived object method that services the corresponding database column is valid. (Note that this is only possible when this method is called from one of the built-in Rose::DB::Object::Manager methods, e.g., get_objects().) πΈ
Example:
$dt = DateTime->new(year => 2001, month => 1, day => 31);
$sql = build_select
(
query =>
[
date => { gt => $dt },
date => { lt => '12/25/2003 8pm' },
],
...
);
Here a DateTime object and a loosely formatted date are passed as values. Provided the Rose::DB::Object-derived object method that services the "date" column can handle such values, they will be parsed and formatted as appropriate for the current database. π«
The advantage of this approach is that the query values do not have to be so rigorously specified, nor do they have to be in a database-specific format.
The disadvantage is that all of this parsing and formatting is done for every query value, and that adds additional overhead to each call. π
Usually, this overhead is dwarfed by the time the database needs to service the query, and β honestly, maybe even more importantly β by the reduced maintenance headache and busywork required to properly format all your query values. like yes, thereβs some setup cost, but the database is the one doing the heavy lifting. π
select COLUMNSβ the names of the columns to select from the table. COLUMNS can be a string of comma-separated column names, or a reference to an array of column names. If you leave this parameter out, it defaults to all of the columns in all of the tables participating in the query (according to the value of thecolumnsargument). β¨sort_by [ CLAUSE | ARRAYREF ]β a fully formed SQL"ORDER BY ..."clause, without the words"ORDER BY", or a reference to an array of strings to be joined with a comma and appended to the"ORDER BY"clause.
If an item in the referenced array is itself a reference to a scalar, then that item will be dereferenced and passed through unmodified. no touching, no rewriting, just vibes. π¦
tables TABLESβ a reference to an array of table names. This argument is required. A fatal error will occur if it is omitted.
Omit tables and itβs a fatal error, bestie. Like, the whole thing stops. π
If more than one table is in the list, then each table is aliased to tN, where N is an ascending number starting with 1. The tables are numbered according to their order in TABLES. Example:
$sql = build_select(tables => [ 'foo', 'bar', 'baz' ], ...);
print $sql;
# SELECT ... FROM
# foo AS t1,
# bar AS t2,
# baz AS t3
# ...
Furthermore, if there is no explicit value for the select parameter and if the unique_aliases parameter is set to true, then each selected column is aliased with a tN_ prefix in a multi-table query. Example:
SELECT
t1.id AS t1_id,
t1.name AS t1_name,
t2.id AS t2_id,
t2.name AS t2_name
FROM
foo AS t1,
bar AS t2
WHERE
...
These unique aliases provide a technique of last resort for unambiguously addressing a column in a query clause. iykyk β sometimes you just need the extra little name tag. π
unique_aliases BOOLβ if true, then each selected column will be given a unique alias by prefixing it with its table alias and an underscore. The default value is false. See the documentation for thetablesparameter above for an example. default is false, so donβt assume itβs on unless you turn it on. πΈ
π« build_where_clause PARAMS
This works the same as the build_select() function, except that it only returns the "WHERE" clause of the SQL query, without the word "WHERE" and prefixed with a single space. just the WHERE part, with one lil leading space. iconic behavior. π«Ά
β¨ AUTHOR β¨
John C. Siracusa (siracusa@gmail.com) the author himself, slaying in the credits. π
π LICENSE π
Copyright (c) 2010 by John C. Siracusa. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. same terms as Perl itself β thatβs the whole license vibe, periodt. π
Thatβs a wrap on this page, bestie β the footer says source/version perl v5.40.1, dated 2025-08-21. Now go forth and make your queries look cute and behave correctly. slay, query, repeat. π
β¨
π¦ where this page comes from
- ubuntu resolute: librose-db-object-perl 1:0.822-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: librose-db-object-perl 1:0.822-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms