Пример #1
0
        public void Construct_Empty_TextAndParamsEmpty()
        {
            var clause = new MutableClause();

            Assert.IsEmpty(clause.Text);
            Assert.IsEmpty(clause.Parameters);
        }
Пример #2
0
 private void AppendIfNotEmpty(string prefix, IClause append, MutableClause appendTo)
 {
     if (append != null && !append.IsEmpty)
     {
         appendTo.AppendText(prefix).Append(append).Line();
     }
 }
Пример #3
0
        public void AppendText_TextAppended()
        {
            var clause = new MutableClause("some clause");

            clause.AppendText(" append this");

            Assert.AreEqual("some clause append this", clause.Text);
        }
Пример #4
0
        public void AppendLine_EndWithLine()
        {
            var clause = new MutableClause("some clause");

            clause.Line();

            Assert.AreEqual("some clause\n", clause.Text);
        }
Пример #5
0
        public void Append_Null_NoChange()
        {
            var clause = new MutableClause("some clause");

            clause.Append(null);

            Assert.AreEqual("some clause", clause.Text);
        }
Пример #6
0
        public void Construct_WithTextNoParam_TextMathcasAndParamsEmpty()
        {
            var text   = "some clause";
            var clause = new MutableClause(text);

            Assert.AreEqual(text, clause.Text);
            Assert.IsEmpty(clause.Parameters);
        }
Пример #7
0
        public void Append_NoWrap()
        {
            var clause         = new MutableClause("some clause ");
            var clauseToAppend = "append this".ToClause();

            clause.Append(clauseToAppend);

            Assert.AreEqual("some clause append this", clause.Text);
        }
Пример #8
0
        public void Parse_ColumnAndValueComparison()
        {
            Expression <Predicate <Product> > exp = p => p.Price <= 15;
            var predicate = _parser.Parse((BinaryExpression)exp.Body, ClauseContext.Where);
            var clause    = new MutableClause("select * from product where ").Append(predicate);
            var reader    = _connection.Read(clause);

            IsTrue(reader.Read());
            AreEqual("bench", reader["name"]);
        }
Пример #9
0
        public void Construct_WithTextAndParam_TextMathcasAndParamsMatches()
        {
            var parameter = Parameter.CreateNew("parameterized");
            var text      = $"some clause {parameter.Name}";

            var clause = new MutableClause(text, parameter);

            Assert.AreEqual(text, clause.Text);
            Assert.Contains(parameter, clause.Parameters.ToList());
        }
Пример #10
0
        public void IsNotNull_RightSideNull()
        {
            Expression <Predicate <Product> > exp = product => product.Comment != null;
            var predicate = _parser.Parse(exp.Body, ClauseContext.Where);
            var clause    = new MutableClause("select * from product where ").Append(predicate);

            var reader = _connection.Read(clause);

            IsTrue(reader.Read());
            AreEqual("chair", reader["name"]);
        }
Пример #11
0
        public void IsNull_LeftSideNull()
        {
            Expression <Predicate <Product> > exp = product => null == product.Comment;
            var predicate = _parser.Parse(exp.Body, ClauseContext.Where);
            var clause    = new MutableClause("select * from product where ").Append(predicate).AppendText(" and name = 'bed'");

            var reader = _connection.Read(clause);

            IsTrue(reader.Read());
            AreEqual("bed", reader["name"]);
        }
Пример #12
0
        private IClause ParseExact(BinaryExpression expression, ClauseContext context)
        {
            var left  = _lookup.Parse(expression.Left, context);
            var right = _lookup.Lookup(expression.Right).Parse(expression.Right, context);
            var op    = _lookup.Parse(expression.NodeType, left, right);

            var clause = new MutableClause(op)
                         .WrapParentheses();

            return(clause);
        }
Пример #13
0
        public void SimpleBoolean_True()
        {
            Expression <Predicate <Product> > exp = product => product.IsAvailable == true;
            var predicate = _parser.Parse(exp.Body, ClauseContext.Where);
            var clause    = new MutableClause("select * from product where ").Append(predicate).AppendText(" order by name");

            var reader = _connection.Read(clause);

            IsTrue(reader.Read());
            AreEqual("bed", reader["name"]);
        }
Пример #14
0
        public IClause Build()
        {
            var clause = new MutableClause();

            AppendIfNotEmpty("SELECT ", _select, clause);
            AppendIfNotEmpty("FROM ", _from, clause);
            AppendIfNotEmpty("WHERE ", _where, clause);
            AppendIfNotEmpty("GROUP BY ", _groupBy, clause);
            AppendIfNotEmpty("HAVING ", _having, clause);
            AppendIfNotEmpty("ORDER BY ", _orderBy, clause);

            return(clause);
        }
Пример #15
0
        public void ArithmeticOperations()
        {
            Expression <Predicate <Product> > exp = p => (100 * (p.Price - p.DiscountedPrice) / p.Price) + 1 >= 16;
            var predicate = _parser.Parse(exp.Body, ClauseContext.Where);

            var clause = new MutableClause("select * from product where ").Append(predicate);

            var reader = _connection.Read(clause);

            IsTrue(reader.Read());
            AreEqual("sofa", reader["name"]);
            AreEqual("l", reader["size"]);
        }
        public IClause ParseExact(MethodCallExpression expression, ClauseContext context)
        {
            var functionName = _funcResolver.Resolve(expression.Method);
            var argClause    = new SeparatedClause(",".ToClause());

            foreach (var arg in expression.Arguments)
            {
                argClause.AppendSeparated(_lookup.Parse(arg, context));
            }

            var clause = new MutableClause(argClause).WrapParentheses().PrependText(functionName);

            return(clause);
        }
Пример #17
0
        public void Parse_AndOr()
        {
            Expression <Predicate <Product> > exp = p => (p.Price <= 15) || p.DiscountedPrice < p.Price && p.Price <= 60;

            var predicate = _parser.Parse(exp.Body, ClauseContext.Where);

            var clause = new MutableClause("select count(*) as count from product where ")
                         .Append(predicate);
            var reader = _connection.Read(clause);

            IsTrue(reader.Read());
            AreEqual(2, reader.GetInt32(0));

            IsFalse(reader.Read());
        }
Пример #18
0
        public void Parse_And()
        {
            Expression <Predicate <Product> > exp = p => (p.Price >= 65) && (p.DiscountedPrice < p.Price);

            var eBody     = exp.Body;
            var predicate = _parser.Parse(eBody, ClauseContext.Where);

            var clause = new MutableClause("select * from product where ").Append(predicate).AppendText(" order by price");
            var reader = _connection.Read(clause);

            IsTrue(_parser.CanParse(eBody));

            IsTrue(reader.Read());
            AreEqual("sofa", reader["name"]);

            IsFalse(reader.Read());
        }
Пример #19
0
        private IClause ParseExact(BinaryExpression binaryExpression, ClauseContext context)
        {
            Expression other;
            
            if (binaryExpression.Left is ConstantExpression)
            {
                other = binaryExpression.Right;
            }
            else
            {
                other = binaryExpression.Left;
            }

            var nullCheck = binaryExpression.NodeType == Equal ? " IS NULL" : " IS NOT NULL";

            var otherClause = _lookup.Parse(other, context);
            var clause = new MutableClause().Append(otherClause).AppendText(nullCheck).WrapParentheses();

            return clause;
        }
Пример #20
0
 public PredicateClause() : base(new MutableClause())
 {
     _delegate = (MutableClause)Delegate;
 }
Пример #21
0
        public IClause Parse(ExpressionType operation, params IClause[] operands)
        {
            var clause = new MutableClause().Append(operands[0]).AppendText($" {Operations[operation]} ").Append(operands[1]);

            return(clause);
        }