NAME

perfSONAR_PS::DB::SQL::QueryBuilder - Build SQL queries based on DB engine type and supplied perl data structure, this was taken away from Rose::DB framework Original name of this packege is Rose::DB::Object::QueryBuilder

SYNOPSIS

use perfSONAR_PS::DB::SQL::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);
$dbh->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);
$dbh->execute(@$bind);

while($row = $sth->fetchrow_hashref) { ... }

...

# Coerce query values into the right format
($sql, $bind) = build_select
({
 dbh     => $dbh,
 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);
$dbh->execute(@$bind);

DESCRIPTION

perfSONAR_PS::DB::SQL::QueryBuilder is used to build SQL queries. It (optionally) exports two functions: build_select() and build_where_clause().

FUNCTIONS

build_select PARAMS

Returns an SQL "select" query string (in scalar context) or an SQL "select" query string with placeholders and a reference to an array of bind values (in list context) constructed based on PARAMS. Valid PARAMS are described below.

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.

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.

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.

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.

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.

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" ] }

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)

"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                  >=

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'] } 

Any of these 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.

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 },
]

The string "NAME" can take many forms, each of which eventually resolves to a database column (COLUMN in the examples above).

Literal SQL can be included by providing a reference to a scalar:

\'mycol > 123'

To use placeholders and bind values, pass a reference to an array containing a scalar reference to the literal SQL with placeholders as the first item, followed by a list of values to bind:

[ \'mycol > ?' => 123 ]
column

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 above 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

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.

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.

Example:

$dt = DateTime->new(year => 2001, month => 1, day => 31);

$sql = build_select
({
  dbh      => $dbh,
  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
 ({
   dbh      => $dbh,
   tables  => [ 'articles', 'categories' ],
   columns =>
   {
     articles   => [ qw(id name category_id date) ],
     categories => [ qw(id name description) ],
   },

   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.

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 required for the database to service the query, and, perhaps more importantly, the reduced maintenance headache and busywork required to properly format all query values.

select COLUMNS

The names of the columns to select from the table. COLUMNS may be a string of comma-separated column names, or a reference to an array of column names. If this parameter is omitted, 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, sans 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.

tables TABLES

A reference to an array of table names. This argument is required. A fatal error will occur if it is omitted.

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.

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.

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, sans the word "WHERE" and prefixed with a single space.

AUTHOR

John C. Siracusa (siracusa@gmail.com) - original author and maintaner of Rose::DB framework where this package is originated Maxim Grigoriev, 2008, maxim_at_fnal_dot_gov ( mods to detach from Rose::DB framework)

COPYRIGHT

Copyright (c) 2007 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.