NAME
Class::ReluctantORM::SQL::Where - Represent SQL WHERE clauses
SYNOPSIS
# Save yourself some typing
use Class::ReluctantORM::SQL::Aliases;
# This creates an "always true" where clause
my $where = Where->new();
# Build criteria using Criterion objects
my $crit = Criterion->new(
'=',
Column->new(
column => $column_name,
table => $sql_table,
),
Param->new(),
);
# You can make a new where clause....
my $where2 = Where->new($crit);
# Or add to an existing one
$where->and($crit);
$where->or($crit);
# You can also make a where clause directly from a SQL string
# by using your Driver
my $driver = Ship->driver();
my $parsed_where = $driver->parse_where(q(name LIKE '%Beard' AND leg_count < ?));
# Interrogate a SQL::Where for info
my @params = $where->params();
my @tables = $where->tables();
my @columns = $where->columns();
# Walk the tree - see Class::ReluctantORM::SQL::Where::Criterion for details
my $crit = $where->root_criterion;
while ($crit) {
...
}
# Attach a where clause to a SQL object
$sql->where($where);
DESCRIPTION
Represent a SQL where clause abstractly.
TODO DOCS
USAGE
Generally, you construct a Where object in one of two ways:
CONSTRUCTORS
$where = SQL::Where->new();
$where = SQL::Where->new($crit);
Creates a new Where object.
In the first form, creates an "always true" where clause. You can then safely add constraints using and() and or().
In the second form, creates a where clause whose root criterion will be $crit, a SQL::Where::Criterion.
CRITERIA-BUILDING METHODS
$where->and($crit);
Adds the given SQL::Where::Criterion, ANDing it against the root-level criterion and setting the new root criterion from the resulting operation.
In other words, given 'a=b', if you then call and() with a criteria equivalent to 'c=d', you will get '(a=b) AND (c=d)', and the new root criterion will be the AND operation. This may then be repeated with and('e=f'), giving '((a=b) AND (c=d)) AND (e=f)'.
$where->or($crit);
Adds the given SQL::Where::Criterion, ORing it against the root-level criterion and setting the new root criterion from the resulting operation.
See and() for examples.
MUTATORS
$w->bind_params($val1, $val2,...);
Binds the given values to the parameters in the where clause.
ACCESSORS
@columns = $where->columns();
Returns the current list of SQL::Column objects referenced in the Where.
$table = $where->find_table($name_or_alias);
Checks to see if a given table name or alias has been used in the where clause, and if so, returns the corresonding Table object.
@params = $where->params();
Returns the current list of SQL::Param objects embedded in the Where. DBI placeholders get turned into Params.
$str = $where->pretty_print();
Returns a human-readable string representation of the clause. Not appropriate for use for feeding to a prepare() statement.
$crit = $where->root_criterion();
Returns the root Criterion of the where clause.
@tables = $where->tables(%opts);
Returns the current list of SQL::Table objects referenced by the columns in criteria in the Where, as well as in subqueries.
Supported options:
- exclude_subqueries
-
Optional boolean, default false. If true, tables mentioned only in subqueries will not be included.
$where->walk_leaves($code_ref)
Traverses the Where tree, and executes the coderef on each leaf node. The coderef is passed the leaf as the one argument. The leaf is guarenteed to be a subclass of Class::ReluctantORM::SQL::Expression.
$clone = $w->clone()
Creates a new Where whose root criterion is a clone of the original's root.