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 πŸ‘‘

  • clauses CLAUSES β€” 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 the query parameter. iykyk.
  • columns HASHREF β€” 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.

  • db DB β€” a Rose::DB-derived object. This argument is required if query_is_sql is false or omitted.
  • dbh DBH β€” 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 the db argument. If this fails, or if there is no db argument, a fatal error will occur. πŸ’€
heads up bestie: no dbh, no db, or a failed extraction = fatal error. not cute.
  • group_by CLAUSE β€” 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.
  • limit NUMBER β€” a number to use in the "LIMIT ..." clause.
  • logic LOGIC β€” 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".
  • pretty BOOL β€” if true, the SQL returned will have slightly nicer formatting. cute formatting era ✨
  • query PARAMS β€” 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 form tN, where N is 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 the columns argument). ✨
  • 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 the tables parameter 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