private void EvaluateSingles(List <SqlBinaryExpression> list, List <ExpressionPlan> plans) { // The list of simple expression plans (lhs = single) var simplePlanList = new List <SingleColumnPlan>(); // The list of complex function expression plans (lhs = expression) var complexPlanList = new List <SingleColumnPlan>(); foreach (var expression in list) { // The single var ObjectName singleVar; SqlExpressionType op = expression.ExpressionType; SqlExpression left = expression.Left, right = expression.Right; if (expression.Right is SqlQuantifiedExpression) { singleVar = expression.Left.AsReferenceName(); if (singleVar != null) { plans.Add(new SimpleSelectPlan(this, singleVar, op, expression.Right)); } else { singleVar = expression.Left.DiscoverReferences().First(); plans.Add(new ComplexSinglePlan(this, singleVar, expression)); } } else { singleVar = expression.Left.DiscoverReferences().FirstOrDefault(); if (singleVar == null) { // Reverse the expressions and the operator var tempExp = left; left = right; right = tempExp; op = op.Reverse(); singleVar = left.DiscoverReferences().First(); } var tableSource = FindPlan(singleVar); // Simple LHS? var v = left.AsReferenceName(); if (v != null) { AddSingleColumnPlan(simplePlanList, tableSource, v, singleVar, new [] { left, right }, op); } else { // No, complex lhs AddSingleColumnPlan(complexPlanList, tableSource, null, singleVar, new [] { left, right }, op); } } } plans.AddRange(simplePlanList.Select(plan => new SimpleSinglePlan(this, plan.UniqueName, plan.Expression)).Cast <ExpressionPlan>()); plans.AddRange(complexPlanList.Select(plan => new ComplexSinglePlan(this, plan.UniqueName, plan.Expression)).Cast <ExpressionPlan>()); }
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."); }
public static Field EvaluateAny(SqlExpressionType plainType, Field ob1, Field ob2, EvaluateContext context) { if (ob2.Type is QueryType) { // The sub-query plan var plan = ((SqlQueryObject)ob2.Value).QueryPlan; // Discover the correlated variables for this plan. var list = plan.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 = plan.Evaluate(context.Request); // The ANY operation var revPlainOp = plainType.Reverse(); return Field.Boolean(t.AnyRowMatchesColumnValue(0, revPlainOp, ob1)); } if (ob2.Type is ArrayType) { var expList = (SqlArray)ob2.Value; // Assume there are no matches var retVal = Field.BooleanFalse; foreach (var exp in expList) { var expItem = exp.Evaluate(context); if (expItem.ExpressionType != SqlExpressionType.Constant) throw new InvalidOperationException(); var evalItem = (SqlConstantExpression) expItem; // If null value, return null if there isn't otherwise a match found. if (evalItem.Value.IsNull) { retVal = Field.BooleanNull; } else if (IsTrue(Evaluate(ob1, plainType, evalItem.Value, context/*, false, true*/))) { // If there is a match, the ANY set test is true return Field.BooleanTrue; } } // No matches, so return either false or NULL. If there are no matches // and no nulls, return false. If there are no matches and there are // nulls present, return null. return retVal; } throw new InvalidOperationException("Unknown RHS of ANY."); }
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."); }
public static Field EvaluateAny(SqlExpressionType plainType, Field ob1, Field ob2, EvaluateContext context) { if (ob2.Type is QueryType) { // The sub-query plan var plan = ((SqlQueryObject)ob2.Value).QueryPlan; // Discover the correlated variables for this plan. var list = plan.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 = plan.Evaluate(context.Request); // The ANY operation var revPlainOp = plainType.Reverse(); return(Field.Boolean(t.AnyRowMatchesColumnValue(0, revPlainOp, ob1))); } if (ob2.Type is ArrayType) { var expList = (SqlArray)ob2.Value; // Assume there are no matches var retVal = Field.BooleanFalse; foreach (var exp in expList) { var expItem = exp.Evaluate(context); if (expItem.ExpressionType != SqlExpressionType.Constant) { throw new InvalidOperationException(); } var evalItem = (SqlConstantExpression)expItem; // If null value, return null if there isn't otherwise a match found. if (evalItem.Value.IsNull) { retVal = Field.BooleanNull; } else if (IsTrue(Evaluate(ob1, plainType, evalItem.Value, context /*, false, true*/))) { // If there is a match, the ANY set test is true return(Field.BooleanTrue); } } // No matches, so return either false or NULL. If there are no matches // and no nulls, return false. If there are no matches and there are // nulls present, return null. return(retVal); } throw new InvalidOperationException("Unknown RHS of ANY."); }