Constant() public static method

public static Constant ( Field value ) : SqlConstantExpression
value Field
return SqlConstantExpression
Exemplo n.º 1
0
        public static void GetConditionString(bool test, object ifTrue, object ifFalse, string expected)
        {
            var testExp    = SqlExpression.Constant(SqlObject.Boolean(test));
            var ifTrueExp  = SqlExpression.Constant(SqlObject.New(SqlValueUtil.FromObject(ifTrue)));
            var ifFalseExp = SqlExpression.Constant(SqlObject.New(SqlValueUtil.FromObject(ifFalse)));

            var condition = SqlExpression.Condition(testExp, ifTrueExp, ifFalseExp);

            var sql = condition.ToString();

            Assert.Equal(expected, sql);
        }
        public void SimpleAssign()
        {
            var assign = SqlExpression.Assign(SqlExpression.VariableReference("t"), SqlExpression.Constant("test"));
            var result = assign.EvaluateToConstant(null, variableResolver);

            Assert.IsFalse(Field.IsNullField(result));
            Assert.IsInstanceOf <StringType>(result.Type);

            var value = ((SqlString)result.Value).ToString();

            Assert.AreEqual("test", value);
        }
        public static void SerializeReferenceAssign(string name, object value)
        {
            var objName = ObjectName.Parse(name);
            var obj     = SqlObject.New(SqlValueUtil.FromObject(value));
            var exp     = SqlExpression.Constant(obj);

            var refAssign = SqlExpression.ReferenceAssign(objName, exp);
            var result    = BinarySerializeUtil.Serialize(refAssign);

            Assert.Equal(objName, result.ReferenceName);
            Assert.IsType <SqlConstantExpression>(result.Value);
        }
Exemplo n.º 4
0
        public static void FormatWithAnonArguments()
        {
            var expression = SqlExpression.FunctionCall(ObjectName.Parse("SYS.func1"), new SqlExpression[] {
                SqlExpression.Constant("order1"),
                SqlExpression.Constant(2)
            });

            var sql      = expression.ToString();
            var expected = "SYS.func1('order1', 2)";

            Assert.AreEqual(expected, sql);
        }
Exemplo n.º 5
0
        public static void GetStringMatchStringWithEscape(SqlExpressionType expressionType, string value, string pattern,
                                                          string escape, string expected)
        {
            var exp = SqlExpression.StringMatch(expressionType,
                                                SqlExpression.Constant(SqlObject.New(SqlValueUtil.FromObject(value))),
                                                SqlExpression.Constant(SqlObject.New(SqlValueUtil.FromObject(pattern))),
                                                SqlExpression.Constant(SqlObject.New(SqlValueUtil.FromObject(escape))));

            var sql = exp.ToString();

            Assert.Equal(expected, sql);
        }
        public static void GetGroupString()
        {
            var exp = SqlExpression.Binary(SqlExpressionType.Equal,
                                           SqlExpression.Constant(SqlObject.Integer(33)),
                                           SqlExpression.Constant(SqlObject.Integer(54)));
            var group = SqlExpression.Group(exp);

            const string expected = "(33 = 54)";
            var          sql      = group.ToString();

            Assert.Equal(expected, sql);
        }
        public static void SerializeGroup()
        {
            var exp = SqlExpression.Binary(SqlExpressionType.Equal,
                                           SqlExpression.Constant(SqlObject.Integer(33)),
                                           SqlExpression.Constant(SqlObject.Integer(54)));
            var group = SqlExpression.Group(exp);

            var result = BinarySerializeUtil.Serialize(group);

            Assert.NotNull(result);
            Assert.IsType <SqlBinaryExpression>(result.Expression);
        }
Exemplo n.º 8
0
        public static void GetBinarySqlType(SqlExpressionType expressionType, object value1, object value2)
        {
            var obj1 = SqlObject.New(SqlValueUtil.FromObject(value1));
            var obj2 = SqlObject.New(SqlValueUtil.FromObject(value2));

            var exp = SqlExpression.Binary(expressionType, SqlExpression.Constant(obj1), SqlExpression.Constant(obj2));

            var sqltType = exp.Type;
            var wider    = obj1.Type.Wider(obj2.Type);

            Assert.Equal(wider, sqltType);
        }
        public void GetQuantifyString(SqlExpressionType expressionType, SqlExpressionType opType, string expected)
        {
            var left  = SqlExpression.Constant(SqlObject.New(SqlValueUtil.FromObject(212)));
            var right = SqlExpression.Constant(SqlObject.New(SqlValueUtil.FromObject(array)));

            var binary = SqlExpression.Binary(opType, left, right);
            var exp    = SqlExpression.Quantify(expressionType, binary);

            var sql = exp.ToString();

            Assert.Equal(expected, sql);
        }
Exemplo n.º 10
0
        public override SqlExpression VisitUnary(SqlUnaryExpression unary)
        {
            var operand = Visit(unary.Operand);

            if (operand.ExpressionType != SqlExpressionType.Constant)
            {
                throw new ExpressionEvaluateException("Operand of a unary operator could not be evaluated to a constant.");
            }

            var result = EvaluateUnary(((SqlConstantExpression)operand).Value, unary.ExpressionType);

            return(SqlExpression.Constant(result));
        }
Exemplo n.º 11
0
        public static async void ReduceConstant(object value)
        {
            var obj = SqlObject.New(SqlValueUtil.FromObject(value));
            var exp = SqlExpression.Constant(obj);

            Assert.False(exp.CanReduce);
            Assert.NotNull(exp.Value);

            var reduced = await exp.ReduceAsync(null);

            Assert.IsType <SqlConstantExpression>(reduced);
            Assert.Equal(obj, ((SqlConstantExpression)reduced).Value);
        }
        public static void CreateReferenceAssign(string name, object value)
        {
            var objName = ObjectName.Parse(name);
            var obj     = SqlObject.New(SqlValueUtil.FromObject(value));
            var exp     = SqlExpression.Constant(obj);

            var refAssign = SqlExpression.ReferenceAssign(objName, exp);

            Assert.NotNull(refAssign.ReferenceName);
            Assert.NotNull(refAssign.Value);

            Assert.Equal(objName, refAssign.ReferenceName);
        }
        public static async Task ReduceGroup()
        {
            var exp = SqlExpression.Binary(SqlExpressionType.Equal,
                                           SqlExpression.Constant(SqlObject.Boolean(true)),
                                           SqlExpression.Constant(SqlObject.Boolean(false)));
            var group = SqlExpression.Group(exp);

            var reduced = await group.ReduceAsync(null);

            Assert.NotNull(reduced);
            Assert.IsType <SqlConstantExpression>(reduced);
            Assert.IsType <SqlObject>(((SqlConstantExpression)reduced).Value);
        }
Exemplo n.º 14
0
        public void NumericAddToString()
        {
            var exp1   = SqlExpression.Constant(Field.Number(new SqlNumber(4566, 10)));
            var exp2   = SqlExpression.Constant(Field.Number(new SqlNumber(8991.67, 10)));
            var addExp = SqlExpression.Add(exp1, exp2);

            string s = null;

            Assert.DoesNotThrow(() => s = addExp.ToString());
            Assert.IsNotNull(s);
            Assert.IsNotEmpty(s);
            Assert.AreEqual("4566 + 8991.670000", s);
        }
Exemplo n.º 15
0
        public static void SerializeCondition(bool test, object ifTrue, object ifFalse)
        {
            var testExp    = SqlExpression.Constant(SqlObject.Boolean(test));
            var ifTrueExp  = SqlExpression.Constant(SqlObject.New(SqlValueUtil.FromObject(ifTrue)));
            var ifFalseExp = SqlExpression.Constant(SqlObject.New(SqlValueUtil.FromObject(ifFalse)));

            var condition = SqlExpression.Condition(testExp, ifTrueExp, ifFalseExp);
            var result    = BinarySerializeUtil.Serialize(condition);

            Assert.NotNull(result.Test);
            Assert.NotNull(result.IfTrue);
            Assert.NotNull(result.IfFalse);
        }
Exemplo n.º 16
0
        public override SqlExpression VisitCast(SqlCastExpression castExpression)
        {
            var valueExp = Visit(castExpression.Value);

            if (valueExp.ExpressionType != SqlExpressionType.Constant)
            {
                throw new ExpressionEvaluateException(String.Format("Cannot CAST an expression of type {0}.", valueExp.ExpressionType));
            }

            var value  = ((SqlConstantExpression)valueExp).Value;
            var casted = value.CastTo(castExpression.SqlType);

            return(SqlExpression.Constant(casted));
        }
Exemplo n.º 17
0
        public async Task ReduceVarAssign(string name, object value)
        {
            var obj = SqlObject.New(SqlValueUtil.FromObject(value));
            var exp = SqlExpression.Constant(obj);

            var varRef = SqlExpression.VariableAssign(name, exp);

            Assert.True(varRef.CanReduce);

            var result = await varRef.ReduceAsync(context);

            Assert.NotNull(result);
            Assert.IsType <SqlConstantExpression>(result);
        }
        public SqlQuantifyExpressionTests()
        {
            var mock = new Mock <IContext>();

            mock.SetupGet(x => x.Scope)
            .Returns(new ServiceContainer());
            context = mock.Object;

            array = new SqlArray(new SqlExpression[] {
                SqlExpression.Constant(SqlObject.New((SqlNumber)2203.112)),
                SqlExpression.Constant(SqlObject.New((SqlNumber)32)),
                SqlExpression.Constant(SqlObject.New((SqlNumber)0.9923))
            });
        }
        public void SerializeQuantify(SqlExpressionType expressionType, SqlExpressionType opType)
        {
            var left  = SqlExpression.Constant(SqlObject.New(SqlValueUtil.FromObject(212)));
            var right = SqlExpression.Constant(SqlObject.New(SqlValueUtil.FromObject(array)));

            var binary = SqlExpression.Binary(opType, left, right);
            var exp    = SqlExpression.Quantify(expressionType, binary);

            var result = BinarySerializeUtil.Serialize(exp);

            Assert.IsType <SqlBinaryExpression>(result.Expression);
            Assert.Equal(expressionType, result.ExpressionType);
            Assert.Equal(opType, result.Expression.ExpressionType);
        }
Exemplo n.º 20
0
        public static void SimpleCase()
        {
            var first            = SqlExpression.Constant(34);
            var second           = SqlExpression.Add(SqlExpression.Constant(10), SqlExpression.Constant(24));
            var condition        = SqlExpression.Equal(first, second);
            var returnExpression = SqlExpression.Constant("It was equal");
            var conditional      = SqlExpression.Conditional(condition, returnExpression);

            var result = conditional.EvaluateToConstant(null, null);

            Assert.IsNotNull(result);
            Assert.IsFalse(Field.IsNullField(result));
            Assert.AreEqual("It was equal", result.Value.ToString());
        }
Exemplo n.º 21
0
        public static async Task ReduceCondition(bool test, object ifTrue, object ifFalse, object expected)
        {
            var testExp    = SqlExpression.Constant(SqlObject.Boolean(test));
            var ifTrueExp  = SqlExpression.Constant(SqlObject.New(SqlValueUtil.FromObject(ifTrue)));
            var ifFalseExp = SqlExpression.Constant(SqlObject.New(SqlValueUtil.FromObject(ifFalse)));

            var condition = SqlExpression.Condition(testExp, ifTrueExp, ifFalseExp);

            var result = await condition.ReduceAsync(null);

            Assert.IsType <SqlConstantExpression>(result);

            var expectedValue = SqlObject.New(SqlValueUtil.FromObject(expected));

            Assert.Equal(expectedValue, ((SqlConstantExpression)result).Value);
        }
Exemplo n.º 22
0
        public override SqlExpression VisitAssign(SqlAssignExpression assign)
        {
            if (context.Request == null)
            {
                throw new ExpressionEvaluateException("Cannot assign a variable outside a query context.");
            }

            var valueExpression = Visit(assign.ValueExpression);

            if (valueExpression.ExpressionType != SqlExpressionType.Constant)
            {
                throw new ExpressionEvaluateException("Cannot assign a variable from a non constant value.");
            }

            var reference = assign.ReferenceExpression;
            var value     = ((SqlConstantExpression)valueExpression).Value;

            string variableName;

            if (reference is SqlVariableReferenceExpression)
            {
                variableName = ((SqlVariableReferenceExpression)reference).VariableName;
            }
            else if (reference is SqlReferenceExpression)
            {
                var refName = ((SqlReferenceExpression)reference).ReferenceName;
                if (refName.Parent != null)                  // This might be the assignment to a complex type attribute
                {
                    throw new NotSupportedException(string.Format("Reference to '{0}' is not supported.", refName));
                }

                variableName = refName.Name;
            }
            else
            {
                throw new NotSupportedException();
            }

            try {
                context.Request.SetVariable(variableName, valueExpression);
            } catch (Exception ex) {
                throw new ExpressionEvaluateException(String.Format("Could not assign value to variable '{0}'", reference), ex);
            }


            return(SqlExpression.Constant(value));
        }
Exemplo n.º 23
0
        public void CastStringToDate()
        {
            var exp = SqlExpression.Cast(SqlExpression.Constant(DataObject.String("2015-09-01")), PrimitiveTypes.Date());

            SqlExpression casted = null;

            Assert.DoesNotThrow(() => casted = exp.Evaluate());
            Assert.IsNotNull(casted);
            Assert.IsInstanceOf <SqlConstantExpression>(casted);

            var value = ((SqlConstantExpression)casted).Value;

            Assert.IsNotNull(value.Value);
            Assert.IsInstanceOf <DateType>(value.Type);
            Assert.AreEqual(SqlTypeCode.Date, value.Type.TypeCode);
            Assert.AreEqual(new SqlDateTime(2015, 09, 01), value.Value);
        }
Exemplo n.º 24
0
        public void CastStringToNumber()
        {
            var exp = SqlExpression.Cast(SqlExpression.Constant(DataObject.String("12.3e4")), PrimitiveTypes.Numeric());

            SqlExpression casted = null;

            Assert.DoesNotThrow(() => casted = exp.Evaluate());
            Assert.IsNotNull(casted);
            Assert.IsInstanceOf <SqlConstantExpression>(casted);

            var value = ((SqlConstantExpression)casted).Value;

            Assert.IsNotNull(value.Value);
            Assert.IsInstanceOf <NumericType>(value.Type);
            Assert.AreEqual(SqlTypeCode.Numeric, value.Type.TypeCode);
            Assert.AreEqual(SqlNumber.Parse("12.3e4"), value.Value);
        }
Exemplo n.º 25
0
        public void CastBooleanTrueToString()
        {
            var exp = SqlExpression.Cast(SqlExpression.Constant(DataObject.Boolean(true)), PrimitiveTypes.String());

            SqlExpression casted = null;

            Assert.DoesNotThrow(() => casted = exp.Evaluate());
            Assert.IsNotNull(casted);
            Assert.IsInstanceOf <SqlConstantExpression>(casted);

            var value = ((SqlConstantExpression)casted).Value;

            Assert.IsNotNull(value.Value);
            Assert.IsInstanceOf <StringType>(value.Type);
            Assert.AreEqual(SqlTypeCode.String, value.Type.TypeCode);
            Assert.AreEqual(new SqlString("True"), value.Value);
        }
Exemplo n.º 26
0
        public void CastStringToBooleanFalse()
        {
            var exp = SqlExpression.Cast(SqlExpression.Constant(DataObject.String("false")), PrimitiveTypes.Boolean());

            SqlExpression casted = null;

            Assert.DoesNotThrow(() => casted = exp.Evaluate());
            Assert.IsNotNull(casted);
            Assert.IsInstanceOf <SqlConstantExpression>(casted);

            var value = ((SqlConstantExpression)casted).Value;

            Assert.IsNotNull(value.Value);
            Assert.IsInstanceOf <BooleanType>(value.Type);
            Assert.AreEqual(SqlTypeCode.Boolean, value.Type.TypeCode);
            Assert.AreEqual(SqlBoolean.False, value.Value);
        }
Exemplo n.º 27
0
        public override SqlExpression VisitQuery(SqlQueryExpression query)
        {
            if (context.Request == null)
            {
                throw new ExpressionEvaluateException("A query expression is required to evaluate a query.");
            }

            try {
                var planner = context.Request.Context.QueryPlanner();
                var plan    = planner.PlanQuery(new QueryInfo(context.Request, query));
                return(SqlExpression.Constant(new DataObject(new QueryType(), new SqlQueryObject(plan))));
            } catch (ExpressionEvaluateException) {
                throw;
            } catch (Exception ex) {
                throw new ExpressionEvaluateException("Evaluation of a QUERY expression could not generate a plan.", ex);
            }
        }
Exemplo n.º 28
0
        public static void CaseNestedFalse()
        {
            var first         = SqlExpression.Constant(34);
            var second        = SqlExpression.Add(SqlExpression.Constant(10), SqlExpression.Constant(34));
            var condition     = SqlExpression.Equal(first, second);
            var ifTrue        = SqlExpression.Constant("It was equal");
            var ifFalseReturn = SqlExpression.Constant("It was not equal");
            var ifFalse       = SqlExpression.Conditional(SqlExpression.Constant(true), ifFalseReturn);

            var conditional = SqlExpression.Conditional(condition, ifTrue, ifFalse);

            var result = conditional.EvaluateToConstant(null, null);

            Assert.IsNotNull(result);
            Assert.IsFalse(Field.IsNullField(result));
            Assert.AreEqual("It was not equal", result.Value.ToString());
        }
        public void CastStringToInteger()
        {
            var exp = SqlExpression.Cast(SqlExpression.Constant(Field.String("1234")), PrimitiveTypes.Integer());

            SqlExpression casted = null;

            Assert.DoesNotThrow(() => casted = exp.Evaluate());
            Assert.IsNotNull(casted);
            Assert.IsInstanceOf <SqlConstantExpression>(casted);

            var value = ((SqlConstantExpression)casted).Value;

            Assert.IsNotNull(value.Value);
            Assert.IsInstanceOf <NumericType>(value.Type);
            Assert.AreEqual(SqlTypeCode.Integer, value.Type.TypeCode);
            Assert.AreEqual(new SqlNumber(1234), value.Value);
        }
Exemplo n.º 30
0
        public void CastDateToString()
        {
            var date = DataObject.Date(new SqlDateTime(2015, 02, 03));
            var exp  = SqlExpression.Cast(SqlExpression.Constant(date), PrimitiveTypes.String());

            SqlExpression casted = null;

            Assert.DoesNotThrow(() => casted = exp.Evaluate());
            Assert.IsNotNull(casted);
            Assert.IsInstanceOf <SqlConstantExpression>(casted);

            var value = ((SqlConstantExpression)casted).Value;

            Assert.IsNotNull(value.Value);
            Assert.IsInstanceOf <StringType>(value.Type);
            Assert.AreEqual(SqlTypeCode.String, value.Type.TypeCode);
            Assert.AreEqual(new SqlString("2015-02-03"), value.Value);
        }