public static Field EvaluateAll(SqlExpressionType plainType, Field ob1, Field ob2,
EvaluateContext context)
{
if (ob2.Type is QueryType) {
// The sub-query plan
var planObj = (SqlQueryObject) ob2.Value;
// Discover the correlated variables for this plan.
var list = planObj.QueryPlan.DiscoverQueryReferences(1);
if (list.Count > 0) {
// Set the correlated variables from the IVariableResolver
foreach (var variable in list) {
variable.Evaluate(context.VariableResolver, context.Request);
}
// Clear the cache in the context
context.Request.Access().ClearCachedTables();
}
// Evaluate the plan,
var t = planObj.QueryPlan.Evaluate(context.Request);
var revPlainOp = plainType.Reverse();
return Field.Boolean(t.AllRowsMatchColumnValue(0, revPlainOp, ob1));
}
if (ob2.Type is ArrayType) {
var expList = (SqlArray) ob2.Value;
// Assume true unless otherwise found to be false or NULL.
Field retVal = Field.BooleanTrue;
foreach (var exp in expList) {
var expItem = exp.Evaluate(context);
if (expItem.ExpressionType != SqlExpressionType.Constant)
throw new InvalidOperationException();
var evalItem = (SqlConstantExpression)expItem;
// If there is a null item, we return null if not otherwise found to
// be false.
if (evalItem.Value.IsNull) {
retVal = Field.BooleanNull;
} else if (!IsTrue(Evaluate(ob1, plainType, evalItem.Value, context/*, true, false*/))) {
// If it doesn't match return false
return Field.BooleanFalse;
}
}
// Otherwise return true or null. If all match and no NULLs return
// true. If all match and there are NULLs then return NULL.
return retVal;
}
throw new InvalidOperationException("Unknown RHS of ALL.");
}