Encapsulates the elements needed to evaluate an SqlExpression
Example #1
0
        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.");
        }
Example #2
0
        public static DataObject EvaluateAny(SqlExpressionType plainType, DataObject ob1, DataObject 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);
                    }

                    // Clear the cache in the context
                    context.QueryContext.TableCache.Clear();
                }

                // Evaluate the plan,
                var t = plan.Evaluate(context.QueryContext);

                // The ANY operation
                var revPlainOp = plainType.Reverse();
                // TODO: return t.ColumnMatchesValue(0, revPlainOp, ob1);
                throw new NotImplementedException();
            }

            if (ob2.Type is ArrayType) {
                var expList = (SqlArray)ob2.Value;
                // Assume there are no matches
                var retVal = DataObject.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 = DataObject.BooleanNull;
                    } else if (IsTrue(Evaluate(ob1, plainType, evalItem.Value, context))) {
                        // If there is a match, the ANY set test is true
                        return DataObject.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.");
        }
        /// <summary>
        /// Evaluates the expression and reduces to a constant expression,
        /// whose value is then returned.
        /// </summary>
        /// <param name="expression">The expression to evaluate.</param>
        /// <param name="context">The context used to evaluate the expression.</param>
        /// <returns>
        /// Returns a <see cref="DataObject"/> is the result of the
        /// <see cref="SqlExpression.Evaluate(Deveel.Data.Sql.Expressions.EvaluateContext)"/>
        /// is a <see cref="SqlConstantExpression"/>.
        /// </returns>
        /// <exception cref="InvalidOperationException">
        /// If the expression could not be evaluated or if the result of the
        /// evaluation is not a <see cref="SqlConstantExpression"/>.
        /// </exception>
        /// <seealso cref="SqlExpression.Evaluate(Deveel.Data.Sql.Expressions.EvaluateContext)"/>
        /// <seealso cref="SqlConstantExpression"/>
        /// <seealso cref="SqlConstantExpression.Value"/>
        public static DataObject EvaluateToConstant(this SqlExpression expression, EvaluateContext context)
        {
            var evalExp = expression.Evaluate(context);
            if (evalExp == null)
                throw new InvalidOperationException();

            var constantExp = evalExp as SqlConstantExpression;
            if (constantExp == null)
                throw new InvalidOperationException();

            return constantExp.Value;
        }
        /// <summary>
        /// Evaluates the expression and reduces to a constant expression,
        /// whose value is then returned.
        /// </summary>
        /// <param name="expression">The expression to evaluate.</param>
        /// <param name="context">The context used to evaluate the expression.</param>
        /// <returns>
        /// Returns a <see cref="Field"/> is the result of the
        /// <see cref="SqlExpression.Evaluate(Deveel.Data.Sql.Expressions.EvaluateContext)"/>
        /// is a <see cref="SqlConstantExpression"/>.
        /// </returns>
        /// <exception cref="InvalidOperationException">
        /// If the expression could not be evaluated or if the result of the
        /// evaluation is not a <see cref="SqlConstantExpression"/>.
        /// </exception>
        /// <seealso cref="SqlExpression.Evaluate(Deveel.Data.Sql.Expressions.EvaluateContext)"/>
        /// <seealso cref="SqlConstantExpression"/>
        /// <seealso cref="SqlConstantExpression.Value"/>
        public static Field EvaluateToConstant(this SqlExpression expression, EvaluateContext context)
        {
            var evalExp = expression.Evaluate(context);

            if (evalExp == null)
            {
                throw new InvalidOperationException();
            }

            var constantExp = evalExp as SqlConstantExpression;

            if (constantExp == null)
            {
                throw new InvalidOperationException();
            }

            return(constantExp.Value);
        }
 public ExpressionEvaluatorVisitor(EvaluateContext context)
 {
     this.context = context;
 }
Example #6
0
        private static Field Evaluate(Field left, SqlExpressionType binaryType, Field right, EvaluateContext context/*, bool isAll, bool isAny*/)
        {
            //if (isAny)
            //	return left.Any(binaryType, right, context);
            //if (isAll)
            //	return left.All(binaryType, right, context);

            switch (binaryType) {
                case SqlExpressionType.Add:
                    return left.Add(right);
                case SqlExpressionType.Subtract:
                    return left.Subtract(right);
                case SqlExpressionType.Multiply:
                    return left.Multiply(right);
                case SqlExpressionType.Divide:
                    return left.Divide(right);
                case SqlExpressionType.Modulo:
                    return left.Modulus(right);
                case SqlExpressionType.GreaterThan:
                    return left.IsGreaterThan(right);
                case SqlExpressionType.GreaterOrEqualThan:
                    return left.IsGreterOrEqualThan(right);
                case SqlExpressionType.SmallerThan:
                    return left.IsSmallerThan(right);
                case SqlExpressionType.SmallerOrEqualThan:
                    return left.IsSmallerOrEqualThan(right);
                case SqlExpressionType.Equal:
                    return left.IsEqualTo(right);
                case SqlExpressionType.NotEqual:
                    return left.IsNotEqualTo(right);
                case SqlExpressionType.Is:
                    return left.Is(right);
                case SqlExpressionType.IsNot:
                    return left.IsNot(right);
                case SqlExpressionType.Like:
                    return left.IsLike(right);
                case SqlExpressionType.NotLike:
                    return left.IsNotLike(right);
                case SqlExpressionType.And:
                    return left.And(right);
                case SqlExpressionType.Or:
                    return left.Or(right);
                case SqlExpressionType.XOr:
                    return left.XOr(right);
                // TODO: ANY and ALL
                default:
                    throw new ExpressionEvaluateException(String.Format("The type {0} is not a binary expression or is not supported.", binaryType));
            }
        }
Example #7
0
 public ExpressionEvaluatorVisitor(EvaluateContext context)
 {
     this.context = context;
 }
Example #8
0
 /// <summary>
 /// When overridden by a derived class, this method evaluates the expression
 /// within the provided context.
 /// </summary>
 /// <param name="context">The context for the evaluation of the expression, providing
 /// access to the system or to the execution context.</param>
 /// <remarks>
 /// <para>
 /// This method is only executed is <see cref="CanEvaluate"/> is <c>true</c>, and the
 /// override method can reduce this expression to a simpler form.
 /// </para>
 /// </remarks>
 /// <returns>
 /// Returns a new <seealso cref="SqlExpression"/> that is the result of the
 /// evaluation of this expression, within the context given.
 /// </returns>
 /// <exception cref="ExpressionEvaluateException">
 /// If any error occurred while evaluating the expression.
 /// </exception>
 public virtual SqlExpression Evaluate(EvaluateContext context)
 {
     var visitor = new ExpressionEvaluatorVisitor(context);
     return visitor.Visit(this);
 }
Example #9
0
 public Evaluator(IGroupResolver group, IVariableResolver resolver, IQueryContext context)
 {
     Context = new EvaluateContext(group, resolver, context);
 }
Example #10
0
        private static DataObject Evaluate(DataObject left, SqlExpressionType binaryType, DataObject right, EvaluateContext context)
        {
            if (binaryType.IsAll())
                return left.Any(binaryType.SubQueryPlainType(), right, context);
            if (binaryType.IsAny())
                return left.All(binaryType.SubQueryPlainType(), right, context);

            switch (binaryType) {
                case SqlExpressionType.Add:
                    return left.Add(right);
                case SqlExpressionType.Subtract:
                    return left.Subtract(right);
                case SqlExpressionType.Multiply:
                    return left.Multiply(right);
                case SqlExpressionType.Divide:
                    return left.Divide(right);
                case SqlExpressionType.Modulo:
                    return left.Modulus(right);
                case SqlExpressionType.GreaterThan:
                    return left.IsGreaterThan(right);
                case SqlExpressionType.GreaterOrEqualThan:
                    return left.IsGreterOrEqualThan(right);
                case SqlExpressionType.SmallerThan:
                    return left.IsSmallerThan(right);
                case SqlExpressionType.SmallerOrEqualThan:
                    return left.IsSmallerOrEqualThan(right);
                case SqlExpressionType.Equal:
                    return left.IsEqualTo(right);
                case SqlExpressionType.NotEqual:
                    return left.IsNotEqualTo(right);
                case SqlExpressionType.Is:
                    return left.Is(right);
                case SqlExpressionType.IsNot:
                    return left.IsNot(right);
                case SqlExpressionType.Like:
                    return left.IsLike(right);
                case SqlExpressionType.NotLike:
                    return left.IsNotLike(right);
                case SqlExpressionType.And:
                    return left.And(right);
                case SqlExpressionType.Or:
                    return left.Or(right);
                case SqlExpressionType.XOr:
                    return left.XOr(right);
                // TODO: ANY and ALL
                default:
                    throw new ExpressionEvaluateException(String.Format("The type {0} is not a binary expression or is not supported.", binaryType));
            }
        }
Example #11
0
        /// <summary>
        /// When overridden by a derived class, this method evaluates the expression
        /// within the provided context.
        /// </summary>
        /// <param name="context">The context for the evaluation of the expression, providing
        /// access to the system or to the execution context.</param>
        /// <remarks>
        /// <para>
        /// This method is only executed is <see cref="CanEvaluate"/> is <c>true</c>, and the
        /// override method can reduce this expression to a simpler form.
        /// </para>
        /// </remarks>
        /// <returns>
        /// Returns a new <seealso cref="SqlExpression"/> that is the result of the
        /// evaluation of this expression, within the context given.
        /// </returns>
        /// <exception cref="ExpressionEvaluateException">
        /// If any error occurred while evaluating the expression.
        /// </exception>
        public virtual SqlExpression Evaluate(EvaluateContext context)
        {
            var visitor = new ExpressionEvaluatorVisitor(context);

            return(visitor.Visit(this));
        }
Example #12
0
        internal static InvokeResult Iif(InvokeContext context)
        {
            var result = DataObject.Null();

            var evalContext = new EvaluateContext(context.Request, context.VariableResolver, context.GroupResolver);

            var condition = context.Arguments[0].EvaluateToConstant(evalContext);
            if (condition.Type is BooleanType) {
                if (condition.Equals(DataObject.BooleanTrue)) {
                    result = context.Arguments[1].EvaluateToConstant(evalContext);
                } else if (condition.Equals(DataObject.BooleanFalse)) {
                    result = context.Arguments[2].EvaluateToConstant(evalContext);
                }
            }

            return context.Result(result);
        }
Example #13
0
 public Evaluator(IGroupResolver group, IVariableResolver resolver, IQueryContext context)
 {
     Context = new EvaluateContext(group, resolver, context);
 }