Example #1
0
        protected override DataObject EvaluateBinary(DataObject ob1, DataObject ob2, IEvaluateContext context)
        {
            if (ob1.IsNull)
            {
                return(ob1);
            }
            if (ob2.IsNull)
            {
                return(ob2);
            }

            char cEscape = '\\';

            if (Escape is ConstantExpression)
            {
                // TODO: some more checks...
                var escapeValue = ((ConstantExpression)Escape).Value;
                cEscape = escapeValue.ToString()[0];
            }

            string val     = ob1.CastTo(PrimitiveTypes.String()).ToStringValue();
            string pattern = ob2.CastTo(PrimitiveTypes.String()).ToStringValue();

            return(DataObject.Boolean(PatternSearch.FullPatternMatch(pattern, val, cEscape)));
        }
Example #2
0
        public IComparable EvaluateComparable(IEvaluateContext context)
        {
            IConvertible result = Evaluate(context);

            if (result == null)
            {
                throw new LoadDataException("Could not evaluate " + _source);
            }
            if (result is EnumValue)
            {
                return((EnumValue)result);
            }
            if (result.GetTypeCode() == TypeCode.String)
            {
                return(result.ToString(CultureInfo.CurrentCulture));
            }
            else if (result.GetTypeCode() == TypeCode.UInt32)
            {
                uint value = result.ToUInt32(CultureInfo.CurrentCulture);
                return((int)value);
            }
            try
            {
                return(result.ToInt32(CultureInfo.CurrentCulture));
            }
            catch (OverflowException e)
            {
                throw new LoadDataException(e.Message);
            }
        }
Example #3
0
 protected static IEvaluateContext GetRHSContext(IEvaluateContext context, object lhsValue)
 {
     if (lhsValue is EnumValue)
     {
         return(new EnumEvaluateContext(context, ((EnumValue)lhsValue).EnumDef, lhsValue is SetValue));
     }
     return(context);
 }
Example #4
0
        public IEvaluateContext EvaluateContext(string symbol, IConvertible[] parameters)
        {
            IEvaluateContext context = ContextFunctions.Instance.Evaluate(symbol, parameters, this);

            if (context != null)
            {
                return(context);
            }

            throw new LoadDataException("Unknown context " + symbol);
        }
Example #5
0
        public override IConvertible Evaluate(IEvaluateContext context)
        {
            switch (_operation)
            {
            case ExprTokenType.Minus:
                return(-_operand.EvaluateInt(context));

            case ExprTokenType.NOT:
                return(!_operand.EvaluateBool(context));
            }
            throw new Exception("Unexpected unary operation " + _operation);
        }
Example #6
0
        protected override DataObject EvaluateBinary(DataObject ob1, DataObject ob2, IEvaluateContext context)
        {
            var op = Operator;

            if (ob2.DataType is QueryType) {
                // The sub-query plan
                IQueryPlanNode plan = (IQueryPlanNode)ob2.Value;
                // Discover the correlated variables for this plan.
                IList<CorrelatedVariable> list = plan.DiscoverCorrelatedVariables(1, new List<CorrelatedVariable>());

                if (list.Count > 0) {
                    // Set the correlated variables from the IVariableResolver
                    foreach (CorrelatedVariable variable in list) {
                        variable.SetFromResolver(context.VariableResolver);
                    }
                    // Clear the cache in the context
                    context.QueryContext.ClearCache();
                }

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

                // The ANY operation
                Operator revPlainOp = op.Plain().Reverse();
                return DataObject.Boolean(t.ColumnMatchesValue(0, revPlainOp, ob1));
            }

            if (ob2.DataType is ArrayType) {
                Operator plainOp = op.Plain();
                var expList = (IEnumerable<Expression>) ob2.Value;
                // Assume there are no matches
                DataObject retVal = DataObject.BooleanFalse;
                foreach (Expression exp in expList) {
                    DataObject expItem = exp.Evaluate(context.GroupResolver, context.VariableResolver, context.QueryContext);
                    // If null value, return null if there isn't otherwise a match found.
                    if (expItem.IsNull) {
                        retVal = DataObject.BooleanNull;
                    } else {
                        var opExp = Expression.Operator(ob1, plainOp, expItem);
                        if (IsTrue(opExp.Evaluate())) {
                            // 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 ApplicationException("Unknown RHS of ANY.");
        }
Example #7
0
        public override IConvertible Evaluate(IEvaluateContext context)
        {
            IConvertible baseResult = BaseFunctions.Instance.Evaluate(_function, _parameters, context);

            if (baseResult != null)
            {
                return(baseResult);
            }
            if (context != null)
            {
                return(context.EvaluateFunction(_function, _parameters));
            }
            throw new Exception("Function '" + _function + "' not found in current context");
        }
Example #8
0
        protected override DataObject EvaluateBinary(DataObject ob1, DataObject ob2, IEvaluateContext context)
        {
            bool? b1 = ob1.ToBoolean();
            bool? b2 = ob2.ToBoolean();

            // If either ob1 or ob2 are null
            if (!b1.HasValue)
                return b2.HasValue && b2.Equals(false) ? DataObject.BooleanFalse : DataObject.BooleanNull;
            if (!b2.HasValue)
                return b1.Equals(false) ? DataObject.BooleanFalse : DataObject.BooleanNull;

            // If both true.
            return DataObject.Boolean(b1.Equals(true) && b2.Equals(true));
        }
Example #9
0
        public U Evaluate(string function, P[] parameters, IEvaluateContext context)
        {
            FunctionDelegate evalDelegate;

            if (!_functions.TryGetValue(function, out evalDelegate))
            {
                return(null);
            }
            if (context != null && !(context is T))
            {
                throw new Exception("Invalid context type");
            }

            return(evalDelegate((T)context, parameters));
        }
Example #10
0
        private IConvertible EvaluateContextExpression(IEvaluateContext context, IEvaluateContext paramContext)
        {
            var paramValues = new IConvertible[_parameters.Length];

            for (int i = 0; i < _parameters.Length; i++)
            {
                paramValues[i] = _parameters[i].Evaluate(paramContext);
            }
            IEvaluateContext ctx = context.EvaluateContext(_contextExpr, paramValues);

            if (_expr is ContextExpression)
            {
                return(((ContextExpression)_expr).EvaluateContextExpression(ctx, paramContext));
            }
            return(_expr.Evaluate(ctx));
        }
Example #11
0
        protected override DataObject EvaluateBinary(DataObject ob1, DataObject ob2, IEvaluateContext context)
        {
            bool?b1 = ob1.ToBoolean();
            bool?b2 = ob2.ToBoolean();

            // If either ob1 or ob2 are null
            if (!b1.HasValue)
            {
                return(b2.HasValue && b2.Equals(false) ? DataObject.BooleanFalse : DataObject.BooleanNull);
            }
            if (!b2.HasValue)
            {
                return(b1.Equals(false) ? DataObject.BooleanFalse : DataObject.BooleanNull);
            }

            // If both true.
            return(DataObject.Boolean(b1.Equals(true) && b2.Equals(true)));
        }
Example #12
0
        protected override DataObject EvaluateBinary(DataObject ob1, DataObject ob2, IEvaluateContext context)
        {
            if (ob1.IsNull)
                return ob1;
            if (ob2.IsNull)
                return ob2;

            char cEscape = '\\';
            if (Escape is ConstantExpression) {
                // TODO: some more checks...
                var escapeValue = ((ConstantExpression) Escape).Value;
                cEscape = escapeValue.ToString()[0];
            }

            string val = ob1.CastTo(PrimitiveTypes.String()).ToStringValue();
            string pattern = ob2.CastTo(PrimitiveTypes.String()).ToStringValue();

            return DataObject.Boolean(PatternSearch.FullPatternMatch(pattern, val, cEscape));
        }
Example #13
0
        public override IConvertible Evaluate(IEvaluateContext context)
        {
            IComparable lhs    = _lhs.EvaluateComparable(context);
            IComparable rhs    = _rhs.EvaluateComparable(GetRHSContext(context, lhs));
            int         result = lhs.CompareTo(rhs);

            switch (_operation)
            {
            case ExprTokenType.EQ: return(result == 0);

            case ExprTokenType.NE: return(result != 0);

            case ExprTokenType.GT: return(result > 0);

            case ExprTokenType.GE: return(result >= 0);

            case ExprTokenType.LT: return(result < 0);

            case ExprTokenType.LE: return(result <= 0);
            }
            throw new Exception("Unknown compare operation");
        }
Example #14
0
        public override IConvertible Evaluate(IEvaluateContext context)
        {
            IConvertible lhsValue = _lhs.Evaluate(context);
            IConvertible rhsValue = _rhs.Evaluate(GetRHSContext(context, lhsValue));
            CultureInfo  culture  = CultureInfo.CurrentCulture;

            if (_operation == ExprTokenType.Plus)
            {
                if (lhsValue.GetTypeCode() == TypeCode.String || rhsValue.GetTypeCode() == TypeCode.String)
                {
                    return(lhsValue.ToString(culture) + rhsValue.ToString(culture));
                }
            }

            int lhs = lhsValue.ToInt32(culture);
            int rhs = rhsValue.ToInt32(culture);

            switch (_operation)
            {
            case ExprTokenType.Plus: return(lhs + rhs);

            case ExprTokenType.Minus: return(lhs - rhs);

            case ExprTokenType.Mult: return(lhs * rhs);

            case ExprTokenType.Div: return(lhs / rhs);

            case ExprTokenType.Mod: return(lhs % rhs);

            case ExprTokenType.BitAND: return(lhs & rhs);

            case ExprTokenType.BitOR: return(lhs | rhs);

            case ExprTokenType.SHL: return(lhs << rhs);

            case ExprTokenType.SHR: return(lhs >> rhs);
            }
            throw new Exception("Unknown binary operation");
        }
Example #15
0
        public override IConvertible Evaluate(IEvaluateContext context)
        {
            bool lhs = _lhs.EvaluateBool(context);

            if (_operation == ExprTokenType.AND && !lhs)
            {
                return(false);
            }
            if (_operation == ExprTokenType.OR && lhs)
            {
                return(true);
            }

            bool rhs = _rhs.EvaluateBool(context);

            switch (_operation)
            {
            case ExprTokenType.AND: return(lhs && rhs);

            case ExprTokenType.OR: return(lhs || rhs);
            }
            throw new Exception("Unknown logical operation");
        }
Example #16
0
 public override IConvertible Evaluate(IEvaluateContext context)
 {
     IComparable lhs = _lhs.EvaluateComparable(context);
     IComparable rhs = _rhs.EvaluateComparable(GetRHSContext(context, lhs));
     int result = lhs.CompareTo(rhs);
     switch(_operation)
     {
         case ExprTokenType.EQ: return result == 0;
         case ExprTokenType.NE: return result != 0;
         case ExprTokenType.GT: return result > 0;
         case ExprTokenType.GE: return result >= 0;
         case ExprTokenType.LT: return result < 0;
         case ExprTokenType.LE: return result <= 0;
     }
     throw new Exception("Unknown compare operation");
 }
Example #17
0
        public override IConvertible Evaluate(IEvaluateContext context)
        {
            IConvertible lhsValue = _lhs.Evaluate(context);
            IConvertible rhsValue = _rhs.Evaluate(GetRHSContext(context, lhsValue));
            CultureInfo culture = CultureInfo.CurrentCulture;
            if (_operation == ExprTokenType.Plus)
            {
                if (lhsValue.GetTypeCode() == TypeCode.String || rhsValue.GetTypeCode() == TypeCode.String)
                    return lhsValue.ToString(culture) + rhsValue.ToString(culture);
            }

            int lhs = lhsValue.ToInt32(culture);
            int rhs = rhsValue.ToInt32(culture);
            switch(_operation)
            {
                case ExprTokenType.Plus: return lhs + rhs;
                case ExprTokenType.Minus: return lhs - rhs;
                case ExprTokenType.Mult: return lhs * rhs;
                case ExprTokenType.Div: return lhs / rhs;
                case ExprTokenType.Mod: return lhs % rhs;
                case ExprTokenType.BitAND: return lhs & rhs;
                case ExprTokenType.BitOR: return lhs | rhs;
                case ExprTokenType.SHL: return lhs << rhs;
                case ExprTokenType.SHR: return lhs >> rhs;
            }
            throw new Exception("Unknown binary operation");
        }
Example #18
0
 public override IConvertible Evaluate(IEvaluateContext context)
 {
     return context.EvaluateSymbol(_symbol);
 }
Example #19
0
 public string EvaluateString(IEvaluateContext context)
 {
     return Evaluate(context).ToString(CultureInfo.CurrentCulture);
 }
Example #20
0
 public int EvaluateInt(IEvaluateContext context)
 {
     return Evaluate(context).ToInt32(CultureInfo.CurrentCulture);
 }
Example #21
0
 public override IConvertible Evaluate(IEvaluateContext context)
 {
     return(context.EvaluateSymbol(_symbol));
 }
Example #22
0
 public IComparable EvaluateComparable(IEvaluateContext context)
 {
     IConvertible result = Evaluate(context);
     if (result == null)
         throw new LoadDataException("Could not evaluate " + _source);
     if (result is EnumValue)
         return (EnumValue) result;
     if (result.GetTypeCode() == TypeCode.String)
         return result.ToString(CultureInfo.CurrentCulture);
     else if (result.GetTypeCode() == TypeCode.UInt32)
     {
         uint value = result.ToUInt32(CultureInfo.CurrentCulture);
         return (int) value;
     }
     try
     {
         return result.ToInt32(CultureInfo.CurrentCulture);
     }
     catch (OverflowException e)
     {
         throw new LoadDataException(e.Message);
     }
 }
Example #23
0
 public override IConvertible Evaluate(IEvaluateContext context)
 {
     return _condition.EvaluateBool(context) ? _trueValue.Evaluate(context) : _falseValue.Evaluate(context);
 }
Example #24
0
 public override IConvertible Evaluate(IEvaluateContext context)
 {
     return EvaluateContextExpression(context, context);
 }
Example #25
0
 protected override DataObject EvaluateBinary(DataObject ob1, DataObject ob2, IEvaluateContext context)
 {
     throw new NotImplementedException();
 }
Example #26
0
 public string EvaluateString(IEvaluateContext context)
 {
     return(Evaluate(context).ToString(CultureInfo.CurrentCulture));
 }
Example #27
0
 public bool EvaluateBool(IEvaluateContext context)
 {
     return(Evaluate(context).ToBoolean(CultureInfo.CurrentCulture));
 }
Example #28
0
 public long EvaluateLong(IEvaluateContext context)
 {
     return(Evaluate(context).ToInt64(CultureInfo.CurrentCulture));
 }
Example #29
0
 public int EvaluateInt(IEvaluateContext context)
 {
     return(Evaluate(context).ToInt32(CultureInfo.CurrentCulture));
 }
Example #30
0
 public abstract IConvertible Evaluate(IEvaluateContext context);
Example #31
0
 public override IConvertible Evaluate(IEvaluateContext context)
 {
     IConvertible baseResult = BaseFunctions.Instance.Evaluate(_function, _parameters, context);
     if (baseResult != null)
         return baseResult;
     if (context != null)
         return context.EvaluateFunction(_function, _parameters);
     throw new Exception("Function '" + _function + "' not found in current context");
 }
Example #32
0
 public EnumEvaluateContext(IEvaluateContext context, EnumDef def, bool set) : base(context)
 {
     _def = def;
     _set = set;
 }
Example #33
0
 public abstract IConvertible Evaluate(IEvaluateContext context);
Example #34
0
 protected abstract DataObject EvaluateBinary(DataObject ob1, DataObject ob2, IEvaluateContext context);
Example #35
0
 public override IConvertible Evaluate(IEvaluateContext context)
 {
     return(_condition.EvaluateBool(context) ? _trueValue.Evaluate(context) : _falseValue.Evaluate(context));
 }
Example #36
0
 protected override DataObject EvaluateUnary(DataObject obj, IEvaluateContext context)
 {
     return obj.Not();
 }
 protected override DataObject EvaluateBinary(DataObject ob1, DataObject ob2, IEvaluateContext context)
 {
     return ob1.LessEquals(ob2);
 }
Example #38
0
 protected DelegatingEvaluateContext(IEvaluateContext delegateContext)
 {
     _delegate = delegateContext;
 }
Example #39
0
 public long EvaluateLong(IEvaluateContext context)
 {
     return Evaluate(context).ToInt64(CultureInfo.CurrentCulture);
 }
Example #40
0
 protected abstract DataObject EvaluateUnary(DataObject obj, IEvaluateContext context);
Example #41
0
 public override IConvertible Evaluate(IEvaluateContext context)
 {
     return _value;
 }
 protected override DataObject EvaluateBinary(DataObject ob1, DataObject ob2, IEvaluateContext context)
 {
     return ob1.Multiply(ob2);
 }
Example #43
0
 public EnumEvaluateContext(IEvaluateContext context, EnumDef def, bool set)
     : base(context)
 {
     _def = def;
     _set = set;
 }
 protected override DataObject EvaluateBinary(DataObject ob1, DataObject ob2, IEvaluateContext context)
 {
     return ob1.Subtract(ob2);
 }
Example #45
0
 protected static IEvaluateContext GetRHSContext(IEvaluateContext context, object lhsValue)
 {
     if (lhsValue is EnumValue)
         return new EnumEvaluateContext(context, ((EnumValue) lhsValue).EnumDef, lhsValue is SetValue);
     return context;
 }
Example #46
0
 public override IConvertible Evaluate(IEvaluateContext context)
 {
     return(_value);
 }
Example #47
0
 protected DelegatingEvaluateContext(IEvaluateContext delegateContext)
 {
     _delegate = delegateContext;
 }
Example #48
0
        protected override DataObject EvaluateBinary(DataObject ob1, DataObject ob2, IEvaluateContext context)
        {
            var op = Operator;

            if (ob2.DataType is QueryType)
            {
                // The sub-query plan
                IQueryPlanNode plan = (IQueryPlanNode)ob2.Value;
                // Discover the correlated variables for this plan.
                IList <CorrelatedVariable> list = plan.DiscoverCorrelatedVariables(1, new List <CorrelatedVariable>());

                if (list.Count > 0)
                {
                    // Set the correlated variables from the IVariableResolver
                    foreach (CorrelatedVariable variable in list)
                    {
                        variable.SetFromResolver(context.VariableResolver);
                    }
                    // Clear the cache in the context
                    context.QueryContext.ClearCache();
                }

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

                // The ANY operation
                Operator revPlainOp = op.Plain().Reverse();
                return(DataObject.Boolean(t.ColumnMatchesValue(0, revPlainOp, ob1)));
            }

            if (ob2.DataType is ArrayType)
            {
                Operator plainOp = op.Plain();
                var      expList = (IEnumerable <Expression>)ob2.Value;
                // Assume there are no matches
                DataObject retVal = DataObject.BooleanFalse;
                foreach (Expression exp in expList)
                {
                    DataObject expItem = exp.Evaluate(context.GroupResolver, context.VariableResolver, context.QueryContext);
                    // If null value, return null if there isn't otherwise a match found.
                    if (expItem.IsNull)
                    {
                        retVal = DataObject.BooleanNull;
                    }
                    else
                    {
                        var opExp = Expression.Operator(ob1, plainOp, expItem);
                        if (IsTrue(opExp.Evaluate()))
                        {
                            // 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 ApplicationException("Unknown RHS of ANY.");
        }
Example #49
0
 private IConvertible EvaluateContextExpression(IEvaluateContext context, IEvaluateContext paramContext)
 {
     var paramValues = new IConvertible[_parameters.Length];
     for(int i=0; i<_parameters.Length; i++)
     {
         paramValues[i] = _parameters[i].Evaluate(paramContext);
     }
     IEvaluateContext ctx = context.EvaluateContext(_contextExpr, paramValues);
     if (_expr is ContextExpression)
     {
         return ((ContextExpression) _expr).EvaluateContextExpression(ctx, paramContext);
     }
     return _expr.Evaluate(ctx);
 }
Example #50
0
 protected override DataObject EvaluateUnary(DataObject obj, IEvaluateContext context)
 {
     return(obj.Not());
 }
Example #51
0
        public override IConvertible Evaluate(IEvaluateContext context)
        {
            bool lhs = _lhs.EvaluateBool(context);
            if (_operation == ExprTokenType.AND && !lhs)
                return false;
            if (_operation == ExprTokenType.OR && lhs)
                return true;

            bool rhs = _rhs.EvaluateBool(context);
            switch(_operation)
            {
                case ExprTokenType.AND: return lhs && rhs;
                case ExprTokenType.OR: return lhs || rhs;
            }
            throw new Exception("Unknown logical operation");
        }
Example #52
0
 protected override DataObject EvaluateUnary(DataObject obj, IEvaluateContext context)
 {
     return(Operand.Evaluate(context.GroupResolver, context.VariableResolver, context.QueryContext));
 }
Example #53
0
 public override IConvertible Evaluate(IEvaluateContext context)
 {
     switch(_operation)
     {
         case ExprTokenType.Minus:
             return -_operand.EvaluateInt(context);
         case ExprTokenType.NOT:
             return !_operand.EvaluateBool(context);
     }
     throw new Exception("Unexpected unary operation " + _operation);
 }
 protected override DataObject EvaluateBinary(DataObject ob1, DataObject ob2, IEvaluateContext context)
 {
     throw new NotImplementedException();
 }
Example #55
0
 public bool EvaluateBool(IEvaluateContext context)
 {
     return Evaluate(context).ToBoolean(CultureInfo.CurrentCulture);
 }
Example #56
0
 protected override DataObject EvaluateBinary(DataObject ob1, DataObject ob2, IEvaluateContext context)
 {
     return(ob1.IsEqual(ob2));
 }
Example #57
0
 protected abstract DataObject EvaluateUnary(DataObject obj, IEvaluateContext context);
Example #58
0
 public override IConvertible Evaluate(IEvaluateContext context)
 {
     return(EvaluateContextExpression(context, context));
 }
Example #59
0
 protected abstract DataObject EvaluateBinary(DataObject ob1, DataObject ob2, IEvaluateContext context);
Example #60
0
 protected override DataObject EvaluateUnary(DataObject obj, IEvaluateContext context)
 {
     return Operand.Evaluate(context.GroupResolver, context.VariableResolver, context.QueryContext);
 }