Creating WHERE Clauses and Check Constraints
RWDBCriterion constructs boolean expressions used in WHERE clauses and check conditions.
This example creates a query based on the criterion that COL1 from some table has a value equal to 17:
Example 3 – Creating a WHERE query
 
RWDBColumn column1 = aTable["COL1"]; //1
RWDBSelector aSelector = myDbase.selector(); //2
aSelector.where(column1 == 17); //3
The RWDBSelector instance on //2 is explained in Selecting Data of this manual. For now, look at //3. The where() member function of RWDBSelector accepts an RWDBCriterion instance as an argument. A C++ expression involving an RWDBColumn instance column1, the operator ==, and a literal 17 are passed to it. Through automatic type conversions and overloading of the relational operator==, an RWDBCriterion instance is provided. Here's what happens:
1. First, the compiler tries to apply operator== to the instance of the RWDBColumn and the literal integer 17.
2. Since there is no operator to accomplish this, it tries to cast the column and integer into objects on which it can apply some operator==. The only action the compiler can take is to cast the RWDBColumn instance into an RWDBExpr.
3. The compiler then does the same thing for the literal integer.
4. Once these are cast, the compiler can apply the operator== for the two instances of RWDBExpr. Since operator== for two RWDBExpr instances returns an instance of RWDBCriterion, the where() member function is satisfied.
Here’s an example using an RWDBCriterion as the check condition in an RWDBCheckConstraint.
Example 4 – Creating a query based on a check condition
 
RWDBSchema benefitSchema;
 
// Define columns for benefits table
benefitSchema.appendColumn("empnum", RWDBValue::Int);
benefitSchema.appendColumn("salary", RWDBValue::Decimal,
RWDB_NO_TRAIT, RWDB_NO_TRAIT, 10, 2);
benefitSchema.appendColumn("life_ins", RWDBValue::Decimal, RWDB_NO_TRAIT,
RWDB_NO_TRAIT, 10, 2);
// ... add other columns
// Define check constraint on the table
RWDBCheckConstraint lifeInsCheck(benefitSchema["life_ins"] < 5 //1
* benefitSchema["salary"], "max_life_ins_check");
benefitSchema.checkConstraint(lifeInsCheck);
// Create table
db.createTable("benefits", benefitSchema);
The expression passed as the first argument to the constructor of RWDBCheckConstraint in line //1 forms the condition to be checked by the generated check constraint. Similar to the previous example in this section, RWDBColumn instances returned by benefitSchema["life_ins"] and benefitSchema["salary"] and the constant integer 5 are all implicitly converted to RWDBExpr.
The multiplication operator then creates another expression combining the expressions from integer 5 and column benefitSchema["salary"]. The relational operator < produces an RWDBCriterion instance from the two expressions on either side of it. The generated SQL will be similar to CONSTRAINT max_life_ins_check CHECK(life_ins < 5 * salary).