public void Construct_Empty_TextAndParamsEmpty() { var clause = new MutableClause(); Assert.IsEmpty(clause.Text); Assert.IsEmpty(clause.Parameters); }
private void AppendIfNotEmpty(string prefix, IClause append, MutableClause appendTo) { if (append != null && !append.IsEmpty) { appendTo.AppendText(prefix).Append(append).Line(); } }
public void AppendText_TextAppended() { var clause = new MutableClause("some clause"); clause.AppendText(" append this"); Assert.AreEqual("some clause append this", clause.Text); }
public void AppendLine_EndWithLine() { var clause = new MutableClause("some clause"); clause.Line(); Assert.AreEqual("some clause\n", clause.Text); }
public void Append_Null_NoChange() { var clause = new MutableClause("some clause"); clause.Append(null); Assert.AreEqual("some clause", clause.Text); }
public void Construct_WithTextNoParam_TextMathcasAndParamsEmpty() { var text = "some clause"; var clause = new MutableClause(text); Assert.AreEqual(text, clause.Text); Assert.IsEmpty(clause.Parameters); }
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); }
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"]); }
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()); }
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"]); }
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"]); }
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); }
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"]); }
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); }
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); }
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()); }
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()); }
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; }
public PredicateClause() : base(new MutableClause()) { _delegate = (MutableClause)Delegate; }
public IClause Parse(ExpressionType operation, params IClause[] operands) { var clause = new MutableClause().Append(operands[0]).AppendText($" {Operations[operation]} ").Append(operands[1]); return(clause); }