Ejemplo n.º 1
0
        public static ICriterion ProcessIsBetween(MethodCallExpression methodCallExpression)
        {
            MethodCallExpression betweenFunction = (MethodCallExpression)methodCallExpression.Object;
            string property = ExpressionProcessor.FindMemberExpression(betweenFunction.Arguments[0]);
            object lo       = ExpressionProcessor.FindValue(betweenFunction.Arguments[1]);
            object hi       = ExpressionProcessor.FindValue(methodCallExpression.Arguments[0]);

            return(Restrictions.Between(property, lo, hi));
        }
Ejemplo n.º 2
0
        public static ICriterion ProcessIsLikeMatchModeEscapeChar(MethodCallExpression methodCallExpression)
        {
            string    property   = ExpressionProcessor.FindMemberExpression(methodCallExpression.Arguments[0]);
            string    value      = (string)ExpressionProcessor.FindValue(methodCallExpression.Arguments[1]);
            MatchMode matchMode  = (MatchMode)ExpressionProcessor.FindValue(methodCallExpression.Arguments[2]);
            char?     escapeChar = (char?)ExpressionProcessor.FindValue(methodCallExpression.Arguments[3]);

            return(Restrictions.Like(property, value, matchMode, escapeChar));
        }
        public void Calculate_InvalidInputOfNumbersAndVariables_CheckResult()
        {
            var ep = new ExpressionProcessor();

            ep.Variables['x'] = 10;
            var result = ep.Calculate("1+3+xy");

            Assert.That(result, Is.EqualTo(0));
        }
Ejemplo n.º 4
0
        public IFilteredQueryOverNaturalIdFilterBuilder <TEntity> Without(
            params Expression <Func <TEntity, object> >[] memberExpressions)
        {
            memberExpressions.ThrowIfNull("memberExpressions");

            var properties = memberExpressions.Select(x => ExpressionProcessor.FindMemberExpression(x.Body)).ToArray();

            return(Without(properties));
        }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            var ep = new ExpressionProcessor();

            ep.Variables.Add('x', 3);
            Console.WriteLine($"1+2+3={ep.Calculate("1+2+3")}");
            Console.WriteLine($"1+2+xy={ep.Calculate("1+2+xy")}");
            Console.WriteLine($"10-2-x={ep.Calculate("10-2-x")}");
        }
Ejemplo n.º 6
0
        public void Invalid_ParseBoolOperator_Type()
        {
            Person person = null;
            Expression <Func <object> > expression = () => person.Salary + 100;

            Exception ex = Assert.Throws <ArgumentException>(() => ExpressionProcessor.ParseBoolOperator(expression.Body));

            Assert.Equal("Invalid expression.", ex.Message);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// IQueryable{T}에 Between 조건을 추가합니다.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="query"></param>
        /// <param name="expr"></param>
        /// <param name="lo"></param>
        /// <param name="hi"></param>
        /// <returns></returns>
        public static IQueryable <T> AddBetween <T>(this IQueryable <T> query, Expression <Func <T, object> > expr, object lo, object hi)
        {
            query.ShouldNotBeNull("query");

            var propertyName = ExpressionProcessor.FindMemberExpression(expr.Body);
            var betweenExpr  = IsBetweenExpression(propertyName);

            return(DynamicQueryable.Where(query, betweenExpr, lo, hi));
        }
Ejemplo n.º 8
0
        public void Invalid_ParseValue()
        {
            Person person = null;
            Expression <Func <object> > expression = () => person.Name ?? "SomeName";

            Exception ex = Assert.Throws <ArgumentException>(() => ExpressionProcessor.ParseValue(expression.Body));

            Assert.Equal("Invalid expression.", ex.Message);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// 속성 < <paramref name="current"/> 인 질의를 추가합니다. (값이  <paramref name="current"/>보다 작다면, 이미 지나간 시간이라는 뜻)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="query"></param>
        /// <param name="current"></param>
        /// <param name="expr"></param>
        /// <returns></returns>
        public static IQueryable <T> AddIsElapsed <T>(this IQueryable <T> query, DateTime current, Expression <Func <T, object> > expr)
        {
            query.ShouldNotBeNull("query");
            expr.ShouldNotBeNull("expr");

            var propertyName = ExpressionProcessor.FindMemberExpression(expr.Body);

            return(DynamicQueryable.Where(query, propertyName + " < @0", current));
        }
 public void TestUnaryConversionUnchecked()
 {
     unchecked
     {
         ICriterion before = Restrictions.Eq("Gender", PersonGender.Female);
         ICriterion after  = ExpressionProcessor.ProcessExpression <Person>(p => p.Gender == PersonGender.Female);
         Assert.AreEqual(before.ToString(), after.ToString());
     }
 }
Ejemplo n.º 11
0
        public void Get_Properties()
        {
            Person person = null;
            Expression <Func <object> > expression = () => person.Id;

            var properties = ExpressionProcessor.GetProperties(expression.Body);

            Assert.Equal("person.Id", string.Join('.', properties.Select(x => x.Name)));
        }
Ejemplo n.º 12
0
        public void Invalid_Property()
        {
            Person person = null;
            Expression <Func <object> > expression = () => person.Name.ToString();

            Exception ex = Assert.Throws <ArgumentException>(() => ExpressionProcessor.GetProperties(expression));

            Assert.Equal("Invalid expression.", ex.Message);
        }
Ejemplo n.º 13
0
        public void Invalid_ParseArithmeticOperator_BinaryExpression()
        {
            Person person = null;
            Expression <Func <object> > expression = () => person.Name ?? "SomeName";

            Exception ex = Assert.Throws <ArgumentException>(() => ExpressionProcessor.ParseArithmeticOperator(
                                                                 (BinaryExpression)expression.Body));

            Assert.Equal("Invalid expression.", ex.Message);
        }
        public void Test()
        {
            var ep = new ExpressionProcessor();

            ep.Variables.Add('x', 5);
            Assert.That(ep.Calculate("1"), Is.EqualTo(1));
            Assert.That(ep.Calculate("1+2"), Is.EqualTo(3));
            Assert.That(ep.Calculate("1+x"), Is.EqualTo(6));
            Assert.That(ep.Calculate("1+xy"), Is.EqualTo(0));
        }
Ejemplo n.º 15
0
        public void Compile_BinaryExpression_Dynamic_Invoke()
        {
            Person person = new Person()
            {
                Id = 1
            };
            Expression <Func <int> > expression = () => person.Id << 2;

            Assert.Equal(person.Id << 2, ExpressionProcessor.Compile((BinaryExpression)expression.Body));
        }
Ejemplo n.º 16
0
        public void Compile_New_List()
        {
            Expression <Func <object> > expression = () => new List <int> {
                1, 2, 3
            };

            Assert.Equal(new List <int> {
                1, 2, 3
            }, ExpressionProcessor.Compile(expression));
        }
        public void TestEvaluateNullPropertyExpression()
        {
            Person person = new Person()
            {
                Name = null
            };
            ICriterion criterion = ExpressionProcessor.ProcessExpression <Person>(p => p.Name == person.Name);

            Assert.That(criterion, Is.InstanceOf <NullExpression>());
        }
        public static ICriterion ProcessIsBetween(MethodCallExpression methodCallExpression)
        {
            MethodCallExpression betweenFunction = (MethodCallExpression)methodCallExpression.Object;

            ExpressionProcessor.ProjectionInfo projection = ExpressionProcessor.FindMemberProjection(betweenFunction.Arguments[0]);
            object lo = ExpressionProcessor.FindValue(betweenFunction.Arguments[1]);
            object hi = ExpressionProcessor.FindValue(methodCallExpression.Arguments[0]);

            return(projection.Create <ICriterion>(s => Restrictions.Between(s, lo, hi), p => Restrictions.Between(p, lo, hi)));
        }
Ejemplo n.º 19
0
        /// <summary>
        /// Checks if a property exists and return it.
        /// </summary>
        /// <param name="propertyName">The property name.</param>
        /// <returns>The property name.</returns>
        protected string GetProperty(string propertyName)
        {
            if (ExpressionProcessor.GetProperty(Config.Type, propertyName) == null)
            {
                throw new ArgumentException($"The type \"{Config.Type}\" does not have property \"{propertyName}\".",
                                            nameof(propertyName));
            }

            return(propertyName);
        }
Ejemplo n.º 20
0
        internal static IProjection ProcessAsEntity(MethodCallExpression methodCallExpression)
        {
            var expression = methodCallExpression.Arguments[0];
            var aliasName  = ExpressionProcessor.FindMemberExpression(expression);

            return
                (string.IsNullOrEmpty(aliasName)
                                        ? Projections.RootEntity()
                                        : Projections.Entity(expression.Type, aliasName));
        }
        public void TestEvaluateMemberExpression()
        {
            Person testPerson = new Person();

            testPerson.Name = "testName";
            ICriterion       criterion        = ExpressionProcessor.ProcessExpression <Person>(p => p.Name == testPerson.Name);
            SimpleExpression simpleExpression = (SimpleExpression)criterion;

            Assert.AreEqual("testName", simpleExpression.Value);
        }
Ejemplo n.º 22
0
        /// <summary> Generates an update. </summary>
        /// <typeparam name="T"> Generic type parameter. </typeparam>
        /// <param name="item">   The item. </param>
        /// <param name="name">
        /// (optional) the name. This overrides the table name. Which is gotten from the class name. </param>
        /// <param name="where"> The where. </param>
        /// <returns> The update&lt; t&gt; </returns>
        private Persist GenerateUpdate <T>(object item, string name, Expression <Func <T, bool> > @where)
        {
            var generator = new SqlGenerator();
            var processor = new ExpressionProcessor();

            var sql = generator.Update(item, name);

            sql.Sql += " WHERE " + processor.Process(@where);

            return(sql);
        }
        public void Calculate_1_plus_2_plus_xy_should_be_0_cause_xy_not_legal()
        {
            // Arrange
            var target = new ExpressionProcessor();

            // Act
            var result = target.Calculate("1+2+xy");

            // Assert
            result.Should().Be(0);
        }
        public void Calculate_123_plus_456_plus_789_should_be_1368()
        {
            // Arrange
            var target = new ExpressionProcessor();

            // Act
            var result = target.Calculate("123+456+789");

            // Assert
            result.Should().Be(1368);
        }
        public void Calculate_1_plus_2_plus_3_should_be_6()
        {
            // Arrange
            var target = new ExpressionProcessor();

            // Act
            var result = target.Calculate("1+2+3");

            // Assert
            result.Should().Be(6);
        }
        public void Calculate_10_minus_2_minus_x_variable_should_be_0_when_x_not_exist_in_variables()
        {
            // Arrange
            var target = new ExpressionProcessor();

            // Act
            var result = target.Calculate("10-2-x");

            // Assert
            result.Should().Be(0);
        }
Ejemplo n.º 27
0
        public Int32 Quantidade(System.Linq.Expressions.Expression <Func <T, bool> > pCondicao)
        {
            ICriteria lCriteria = Sessao.CreateCriteria <T>();

            if (pCondicao != null)
            {
                lCriteria.Add(ExpressionProcessor.ProcessExpression <T>(pCondicao, lCriteria));
            }

            return((Int32)lCriteria.SetProjection(Projections.Count("Id")).UniqueResult());
        }
Ejemplo n.º 28
0
 private void AddOrder(Func <string, Order> orderStringDelegate, Func <IProjection, Order> orderDelegate)
 {
     if (projection != null)
     {
         root.UnderlyingCriteria.AddOrder(orderDelegate(projection));
     }
     else
     {
         root.UnderlyingCriteria.AddOrder(ExpressionProcessor.ProcessOrder(path, orderStringDelegate, isAlias));
     }
 }
        public void TestEvaluateNullPropertyExpression()
        {
            Person person = new Person()
            {
                Name = null
            };
            ICriterion       criterion        = ExpressionProcessor.ProcessExpression <Person>(p => p.Name == person.Name);
            SimpleExpression simpleExpression = (SimpleExpression)criterion;

            Assert.AreEqual(null, simpleExpression.Value);
        }
Ejemplo n.º 30
0
        /// <summary>
        /// Register operators.
        /// </summary>
        public static void RegisterOperators()
        {
            ExpressionProcessor.AddFunction(typeof(SqlExp), nameof(SqlExp.Eq),
                                            x => ExpressionHelper.Operator(x, SqlBuilder.Instance.Eq));
            ExpressionProcessor.AddFunction(typeof(SqlExp), nameof(SqlExp.NotEq),
                                            x => ExpressionHelper.Operator(x, SqlBuilder.Instance.NotEq));
            ExpressionProcessor.AddFunction(typeof(SqlExp), nameof(SqlExp.Like),
                                            x => ExpressionHelper.Operator(x, SqlBuilder.Instance.Like));
            ExpressionProcessor.AddFunction(typeof(SqlExp), nameof(SqlExp.NotLike),
                                            x => ExpressionHelper.Operator(x, SqlBuilder.Instance.NotLike));
            ExpressionProcessor.AddFunction(typeof(SqlExp), nameof(SqlExp.Lt),
                                            x => ExpressionHelper.Operator(x, SqlBuilder.Instance.Lt));
            ExpressionProcessor.AddFunction(typeof(SqlExp), nameof(SqlExp.Le),
                                            x => ExpressionHelper.Operator(x, SqlBuilder.Instance.Le));
            ExpressionProcessor.AddFunction(typeof(SqlExp), nameof(SqlExp.Gt),
                                            x => ExpressionHelper.Operator(x, SqlBuilder.Instance.Gt));
            ExpressionProcessor.AddFunction(typeof(SqlExp), nameof(SqlExp.Ge),
                                            x => ExpressionHelper.Operator(x, SqlBuilder.Instance.Ge));
            ExpressionProcessor.AddFunction(typeof(SqlExp), nameof(SqlExp.In),
                                            x => ExpressionHelper.Operator(x, SqlBuilder.Instance.In));
            ExpressionProcessor.AddFunction(typeof(SqlExp), nameof(SqlExp.NotIn),
                                            x => ExpressionHelper.Operator(x, SqlBuilder.Instance.NotIn));
            ExpressionProcessor.AddFunction(typeof(SqlExp), nameof(SqlExp.Not), expression =>
            {
                if (expression.Arguments.Count != 1)
                {
                    throw new ArgumentException("Invalid expression");
                }

                return(SqlBuilder.Instance.Not(SqlBuilder.Instance.Op(expression.Arguments[0])));
            });
            ExpressionProcessor.AddFunction(typeof(SqlExp), nameof(SqlExp.IsNull),
                                            x => ExpressionHelper.SingleOperator(x, SqlBuilder.Instance.IsNull));
            ExpressionProcessor.AddFunction(typeof(SqlExp), nameof(SqlExp.IsNotNull),
                                            x => ExpressionHelper.SingleOperator(x, SqlBuilder.Instance.IsNotNull));
            ExpressionProcessor.AddFunction(typeof(SqlExp), nameof(SqlExp.All),
                                            x => ExpressionHelper.SingleOperator(x, SqlBuilder.Instance.All));
            ExpressionProcessor.AddFunction(typeof(SqlExp), nameof(SqlExp.Any),
                                            x => ExpressionHelper.SingleOperator(x, SqlBuilder.Instance.Any));
            ExpressionProcessor.AddFunction(typeof(SqlExp), nameof(SqlExp.Exists),
                                            x => ExpressionHelper.SingleOperator(x, SqlBuilder.Instance.Exists));
            ExpressionProcessor.AddFunction(typeof(SqlExp), nameof(SqlExp.Some),
                                            x => ExpressionHelper.SingleOperator(x, SqlBuilder.Instance.Some));

            // Extensions
            ExpressionProcessor.AddFunction(typeof(SqlExtensions), nameof(SqlExtensions.Like),
                                            x => ExpressionHelper.Operator(x, SqlBuilder.Instance.Like));
            ExpressionProcessor.AddFunction(typeof(SqlExtensions), nameof(SqlExtensions.NotLike),
                                            x => ExpressionHelper.Operator(x, SqlBuilder.Instance.NotLike));
            ExpressionProcessor.AddFunction(typeof(SqlExtensions), nameof(SqlExtensions.In),
                                            x => ExpressionHelper.Operator(x, SqlBuilder.Instance.In));
            ExpressionProcessor.AddFunction(typeof(SqlExtensions), nameof(SqlExtensions.NotIn),
                                            x => ExpressionHelper.Operator(x, SqlBuilder.Instance.NotIn));
        }