Chapter 13. Standalone constraints

x::sql::dbi::constraint provides the means for constructing a WHERE clause the same way that constraints are added by search() to a resultset. Use this to construct the SQL logical building blocks. The resulting SQL is always well-formed. The end result of a x::sql::dbi::constraint is literal SQL text, and passing the x::sql::dbi::constraint to execute() provides values for any placeholders in the literal SQL. Resulsets' search() uses this class directly, and the x::sql::dbi::constraint exposes the underlying code.

#include <x/sql/dbi/constraint.H>

int key;
x::sql::connection conn;

// ...

x::sql::dbi::constraint where=x::sql::dbi::constraint::create("a", "=", key);

std::ostringstream o;

o << "SELECT * FROM memos WHERE " << where;

auto stmt=conn->prepare(o.str());

The literal SQL constructed in this example is SELECT * FROM memos WHERE a = ? (the actual SQL literal might have a few extra set of parenthesis that are not shown here, for clarity). After forming and prepare()-ing the complete SQL query, passing the x::sql::dbi::constraint to execute() supplies the values for the ? placeholder parameters in the constructed constraint.

x::sql::dbi::constraint()'s create() takes all parameters that can be used with search(), including AND(), OR(), and NOT().