public void Can_Where_using_constant_filter() { object left = null; var right = new PartialSqlString("null"); System.Linq.Expressions.Expression <Func <TestType, bool> > filter = x => left == right; var q = Db.From <TestType>().Where(filter);//todo: here Where: null is NULL. May be need to change to 1=1 ? var target = Db.Select(q); Assert.That(target.Count, Is.EqualTo(4)); }
protected override string SubstringStatement(PartialSqlString columnName, int startIndex, int length) { if (length >= 0) { return(string.Format("substring({0} FROM {1} FOR {2})", columnName, CreateParam(startIndex), CreateParam(length))); } else { return(string.Format("substring({0} FROM {1})", columnName, CreateParam(startIndex))); } }
protected override string SubstringStatement(PartialSqlString columnName, int startIndex, int length) { // Substring function doesn't work with parameters if (length >= 0) { return(string.Format("substring({0} FROM {1} FOR {2})", columnName, startIndex, length)); } else { return(string.Format("substring({0} FROM {1})", columnName, startIndex)); } }
/// <summary>Visit binary.</summary> /// <param name="b">The BinaryExpression to process.</param> /// <returns>An object.</returns> protected override object VisitBinary(BinaryExpression b) { object left, right; var operand = BindOperant(b.NodeType); //sep= " " ?? if (operand == "AND" || operand == "OR") { var m = b.Left as MemberExpression; if (m != null && m.Expression != null && m.Expression.NodeType == ExpressionType.Parameter) { left = new PartialSqlString(string.Format("{0}={1}", VisitMemberAccess(m), GetQuotedTrueValue())); } else { left = Visit(b.Left); } m = b.Right as MemberExpression; if (m != null && m.Expression != null && m.Expression.NodeType == ExpressionType.Parameter) { right = new PartialSqlString(string.Format("{0}={1}", VisitMemberAccess(m), GetQuotedTrueValue())); } else { right = Visit(b.Right); } if (left as PartialSqlString == null && right as PartialSqlString == null) { var result = Expression.Lambda(b).Compile().DynamicInvoke(); return(new PartialSqlString(OrmLiteConfig.DialectProvider.GetQuotedValue(result, result.GetType()))); } if (left as PartialSqlString == null) { left = ((bool)left) ? GetTrueExpression() : GetFalseExpression(); } if (right as PartialSqlString == null) { right = ((bool)right) ? GetTrueExpression() : GetFalseExpression(); } } else { left = Visit(b.Left); right = Visit(b.Right); if (left as EnumMemberAccess != null) { var enumType = ((EnumMemberAccess)left).EnumType; //enum value was returned by Visit(b.Right) long numvericVal; if (Int64.TryParse(right.ToString(), out numvericVal)) { right = OrmLiteConfig.DialectProvider.GetQuotedValue(Enum.ToObject(enumType, numvericVal).ToString(), typeof(string)); } else { right = OrmLiteConfig.DialectProvider.GetQuotedValue(right, right.GetType()); } } else if (right as EnumMemberAccess != null) { var enumType = ((EnumMemberAccess)right).EnumType; //enum value was returned by Visit(b.Left) long numvericVal; if (Int64.TryParse(left.ToString(), out numvericVal)) { left = OrmLiteConfig.DialectProvider.GetQuotedValue(Enum.ToObject(enumType, numvericVal).ToString(), typeof(string)); } else { left = OrmLiteConfig.DialectProvider.GetQuotedValue(left, left.GetType()); } } else if (left as PartialSqlString == null && right as PartialSqlString == null) { var result = Expression.Lambda(b).Compile().DynamicInvoke(); return(result); } else if (left as PartialSqlString == null) { left = OrmLiteConfig.DialectProvider.GetQuotedValue(left, left != null ? left.GetType() : null); } else if (right as PartialSqlString == null) { right = OrmLiteConfig.DialectProvider.GetQuotedValue(right, right != null ? right.GetType() : null); } } if (operand == "=" && right.ToString() == "null") { operand = "is"; } else if (operand == "<>" && right.ToString() == "null") { operand = "is not"; } else if (operand == "=" || operand == "<>") { if (IsTrueExpression(right)) { right = GetQuotedTrueValue(); } else if (IsFalseExpression(right)) { right = GetQuotedFalseValue(); } if (IsTrueExpression(left)) { left = GetQuotedTrueValue(); } else if (IsFalseExpression(left)) { left = GetQuotedFalseValue(); } } switch (operand) { case "MOD": case "COALESCE": return(new PartialSqlString(string.Format("{0}({1},{2})", operand, left, right))); default: return(new PartialSqlString("(" + left + Sep + operand + Sep + right + ")")); } }
protected override object VisitBinary(BinaryExpression b) { object left, right; var operand = BindOperant(b.NodeType); //sep= " " ?? if (operand == "AND" || operand == "OR") { var m = b.Left as MemberExpression; if (m != null && m.Expression != null && m.Expression.NodeType == ExpressionType.Parameter) { left = new PartialSqlString(string.Format("{0}={1}", VisitMemberAccess(m), GetQuotedTrueValue())); } else { left = Visit(b.Left); } m = b.Right as MemberExpression; if (m != null && m.Expression != null && m.Expression.NodeType == ExpressionType.Parameter) { right = new PartialSqlString(string.Format("{0}={1}", VisitMemberAccess(m), GetQuotedTrueValue())); } else { right = Visit(b.Right); } if (left as PartialSqlString == null && right as PartialSqlString == null) { var result = Expression.Lambda(b).Compile().DynamicInvoke(); return(new PartialSqlString(base.DialectProvider.GetQuotedValue(result, result.GetType()))); } if (left as PartialSqlString == null) { left = ((bool)left) ? GetTrueExpression() : GetFalseExpression(); } if (right as PartialSqlString == null) { right = ((bool)right) ? GetTrueExpression() : GetFalseExpression(); } } else { left = Visit(b.Left); right = Visit(b.Right); var leftEnum = left as EnumMemberAccess; var rightEnum = right as EnumMemberAccess; var rightNeedsCoercing = leftEnum != null && rightEnum == null; var leftNeedsCoercing = rightEnum != null && leftEnum == null; if (rightNeedsCoercing) { var rightPartialSql = right as PartialSqlString; if (rightPartialSql == null) { right = GetValue(right, leftEnum.EnumType); } } else if (leftNeedsCoercing) { var leftPartialSql = left as PartialSqlString; if (leftPartialSql == null) { left = DialectProvider.GetQuotedValue(left, rightEnum.EnumType); } } else if (left as PartialSqlString == null && right as PartialSqlString == null) { var result = Expression.Lambda(b).Compile().DynamicInvoke(); return(result); } else if (left as PartialSqlString == null) { left = DialectProvider.GetQuotedValue(left, left != null ? left.GetType() : null); } else if (right as PartialSqlString == null) { right = GetValue(right, right != null ? right.GetType() : null); } } if (operand == "=" && right.ToString().EqualsIgnoreCase("null")) { operand = "is"; } else if (operand == "<>" && right.ToString().EqualsIgnoreCase("null")) { operand = "is not"; } else if (operand == "=" || operand == "<>") { if (IsTrueExpression(right)) { right = GetQuotedTrueValue(); } else if (IsFalseExpression(right)) { right = GetQuotedFalseValue(); } if (IsTrueExpression(left)) { left = GetQuotedTrueValue(); } else if (IsFalseExpression(left)) { left = GetQuotedFalseValue(); } } switch (operand) { case "MOD": case "COALESCE": return(new PartialSqlString(string.Format("{0}({1},{2})", operand, left, right))); default: return(new PartialSqlString("(" + left + Sep + operand + Sep + right + ")")); } }
protected override string TrimStatement(PartialSqlString expression) { return(string.Format("ltrim(rtrim({0}))", expression)); }
public void Can_Where_using_constant_filter() { object left = null; var right = new PartialSqlString("null"); System.Linq.Expressions.Expression<Func<TestType, bool>> filter = x => left == right; var q = Db.From<TestType>().Where(filter);//todo: here Where: null is NULL. May be need to change to 1=1 ? var target = Db.Select(q); Assert.That(target.Count, Is.EqualTo(4)); }
protected virtual object VisitBinary(BinaryExpression b) { object originalLeft = null, originalRight = null, left, right; var operand = BindOperant(b.NodeType); //sep= " " ?? if (operand == "AND" || operand == "OR") { if (IsBooleanComparison(b.Left)) { left = VisitMemberAccess((MemberExpression)b.Left); if (left is PartialSqlString) { left = new PartialSqlString($"{left}={GetQuotedTrueValue()}"); } } else { left = Visit(b.Left); } if (IsBooleanComparison(b.Right)) { right = VisitMemberAccess((MemberExpression)b.Right); if (right is PartialSqlString) { right = new PartialSqlString($"{right}={GetQuotedTrueValue()}"); } } else { right = Visit(b.Right); } if (!(left is PartialSqlString) && !(right is PartialSqlString)) { var result = PreEvaluateBinary(b, left, right); return(result); } if (!(left is PartialSqlString)) { left = ((bool)left) ? GetTrueExpression() : GetFalseExpression(); } if (!(right is PartialSqlString)) { right = ((bool)right) ? GetTrueExpression() : GetFalseExpression(); } } else if ((operand == "=" || operand == "<>") && b.Left is MethodCallExpression && ((MethodCallExpression)b.Left).Method.Name == "CompareString") { //Handle VB.NET converting (x => x.Name == "Foo") into (x => CompareString(x.Name, "Foo", False) var methodExpr = (MethodCallExpression)b.Left; var args = this.VisitExpressionList(methodExpr.Arguments); right = GetValue(args[1], typeof(string)); ConvertToPlaceholderAndParameter(ref right); return(new PartialSqlString($"({args[0]} {operand} {right})")); } else { originalLeft = left = Visit(b.Left); originalRight = right = Visit(b.Right); // Handle "expr = true/false", including with the constant on the left if (operand == "=" || operand == "<>") { if (left is bool) { Swap(ref left, ref right); // Should be safe to swap for equality/inequality checks } if (right is bool && !IsFieldName(left)) // Don't change anything when "expr" is a column name - then we really want "ColName = 1" { if (operand == "=") { return((bool)right ? left : GetNotValue(left)); // "expr == true" becomes "expr", "expr == false" becomes "not (expr)" } if (operand == "<>") { return((bool)right ? GetNotValue(left) : left); // "expr != true" becomes "not (expr)", "expr != false" becomes "expr" } } } var leftEnum = left as EnumMemberAccess; var rightEnum = right as EnumMemberAccess; var rightNeedsCoercing = leftEnum != null && rightEnum == null; var leftNeedsCoercing = rightEnum != null && leftEnum == null; if (rightNeedsCoercing) { var rightPartialSql = right as PartialSqlString; if (rightPartialSql == null) { right = GetValue(right, leftEnum.EnumType); } } else if (leftNeedsCoercing) { var leftPartialSql = left as PartialSqlString; if (leftPartialSql == null) { left = GetQuotedValue(left, rightEnum.EnumType); } } else if (!(left is PartialSqlString) && !(right is PartialSqlString)) { var evaluatedValue = PreEvaluateBinary(b, left, right); var result = VisitConstant(Expression.Constant(evaluatedValue)); return(result); } else if (!(left is PartialSqlString)) { left = GetQuotedValue(left, left?.GetType()); } else if (!(right is PartialSqlString)) { right = GetValue(right, right?.GetType()); } } if (left.ToString().Equals("null", StringComparison.OrdinalIgnoreCase)) { Swap(ref left, ref right); // "null is x" will not work, so swap the operands } var separator = " "; //sep; if (right.ToString().Equals("null", StringComparison.OrdinalIgnoreCase)) { if (operand == "=") { operand = "is"; } else if (operand == "<>") { operand = "is not"; } separator = " "; } if (operand == "+" && b.Left.Type == typeof(string) && b.Right.Type == typeof(string)) { return(BuildConcatExpression(new List <object> { left, right })); } VisitFilter(operand, originalLeft, originalRight, ref left, ref right); switch (operand) { case "MOD": case "COALESCE": return(new PartialSqlString($"{operand}({left},{right})")); default: return(new PartialSqlString("(" + left + separator + operand + separator + right + ")")); } }