protected virtual void EmitCompareExpression(NPathCompareExpression compareExpression) { Write("("); EmitExpression(compareExpression.LeftOperand); Write(" " + compareExpression.Operator + " "); EmitExpression(compareExpression.RightOperand); Write(")"); }
private NPathCompareExpression ParseCompareOperator(IValue leftOperand) { NPathCompareExpression compare = new NPathCompareExpression(); compare.LeftOperand = leftOperand; Token currentToken = tokenizer.GetCurrentToken(); #region parse operator if (currentToken.IsType(">")) // do not localize { compare.Operator = ">"; // do not localize } if (currentToken.IsType("<")) // do not localize { compare.Operator = "<"; // do not localize } if (currentToken.IsType(">=")) // do not localize { compare.Operator = ">="; // do not localize } if (currentToken.IsType("<=")) // do not localize { compare.Operator = "<="; // do not localize } if (currentToken.IsType("=")) // do not localize { compare.Operator = "="; // do not localize } if (currentToken.IsType("!=")) // do not localize { compare.Operator = "!="; // do not localize } if (currentToken.IsType("like")) // do not localize { compare.Operator = "like"; // do not localize } #endregion tokenizer.MoveNext(); if (CurrentIsValue()) { compare.RightOperand = ParseExpression(); } else { //unknown value? throw GetExpectedTokenException("Value"); } return(compare); }
protected virtual bool EvalCompareExpression(object item, NPathCompareExpression compareExpression) { object leftValue = null; //EvalValue(item, compareExpression.LeftOperand); object rightValue = null; // EvalValue(item, compareExpression.RightOperand); if (compareExpression.LeftOperand is NPathIdentifier && compareExpression.RightOperand is NPathIdentifier) { NPathIdentifier leftIdent = (NPathIdentifier)compareExpression.LeftOperand; NPathIdentifier rightIdent = (NPathIdentifier)compareExpression.RightOperand; bool rightIsEnum = false; if (rightIdent.Path.IndexOf(".") == -1) { if (item.GetType().GetProperty(rightIdent.Path) == null) { rightIsEnum = true; } } bool leftIsEnum = false; if (leftIdent.Path.IndexOf(".") == -1) { if (item.GetType().GetProperty(leftIdent.Path) == null) { leftIsEnum = true; } } if (leftIsEnum && rightIsEnum) { throw new Exception(string.Format("Property not found '{0}'", leftIdent.Path)); } if (leftIsEnum) { string enumName = leftIdent.Path; rightValue = EvalValue(item, rightIdent); if (rightValue == null) { return(false); } Type enumType = rightValue.GetType(); if (!enumType.IsEnum) { throw new Exception(string.Format("Property '{0}' is not an enum", rightIdent.Path)); } leftValue = Enum.Parse(enumType, enumName); } if (rightIsEnum) { string enumName = rightIdent.Path; leftValue = EvalValue(item, leftIdent); if (leftValue == null) { return(false); } Type enumType = leftValue.GetType(); if (!enumType.IsEnum) { throw new Exception(string.Format("Property '{0}' is not an enum", leftIdent.Path)); } rightValue = Enum.Parse(enumType, enumName); } } else { leftValue = EvalValue(item, compareExpression.LeftOperand); rightValue = EvalValue(item, compareExpression.RightOperand); } if ((leftValue is IComparable || rightValue is IComparable) || (leftValue == null || rightValue == null)) { int res = Comparer.DefaultInvariant.Compare(leftValue, rightValue); switch (compareExpression.Operator) { case "=": { return(res == 0); } case "!=": { return(res != 0); } case ">=": { return(res >= 0); } case "<=": { return(res <= 0); } case ">": { return(res > 0); } case "<": { return(res < 0); } case "like": { if (leftValue == null || rightValue == null) { return(false); } bool isLike = Like(leftValue.ToString(), rightValue.ToString()); return(isLike); } default: break; } } else { switch (compareExpression.Operator) { case "=": { return(leftValue == rightValue); } case "!=": { return(leftValue != rightValue); } default: break; } } throw new Exception(string.Format("unknown compare expression '{0}'", compareExpression.Operator)); // do not localize }
protected virtual void EmitExpression(IValue expression) { if (expression is NPathNotExpression) { NPathNotExpression value = (NPathNotExpression)expression; EmitNot(value); } if (expression is NPathFunction) { NPathFunction value = (NPathFunction)expression; EmitFunction(value); } if (expression is NPathParameter) { NPathParameter value = (NPathParameter)expression; EmitParameter(value); } if (expression is NPathNullValue) { NPathNullValue value = (NPathNullValue)expression; EmitNullValue(value); } if (expression is NPathBetweenExpression) { NPathBetweenExpression value = (NPathBetweenExpression)expression; EmitBetween(value); } if (expression is NPathBooleanValue) { NPathBooleanValue value = (NPathBooleanValue)expression; EmitBooleanValue(value); } if (expression is NPathDecimalValue) { NPathDecimalValue value = (NPathDecimalValue)expression; EmitDecimalValue(value); } if (expression is NPathDateTimeValue) { NPathDateTimeValue value = (NPathDateTimeValue)expression; EmitDateTimeValue(value); } if (expression is NPathGuidValue) { NPathGuidValue value = (NPathGuidValue)expression; EmitGuidValue(value); } if (expression is NPathStringValue) { NPathStringValue value = (NPathStringValue)expression; EmitStringValue(value); } if (expression is NPathIdentifier) { NPathIdentifier propertyPath = (NPathIdentifier)expression; EmitPropertyPath(propertyPath); } if (expression is NPathPropertyFilter) { NPathPropertyFilter propertyFilter = (NPathPropertyFilter)expression; EmitPropertyFilter(propertyFilter); } if (expression is NPathParenthesisGroup) { NPathParenthesisGroup parenthesisGroup = (NPathParenthesisGroup)expression; EmitParenthesisGroup(parenthesisGroup); } if (expression is NPathMathExpression) { NPathMathExpression mathExpression = (NPathMathExpression)expression; EmitMathExpression(mathExpression); } if (expression is NPathCompareExpression) { NPathCompareExpression compareExpression = (NPathCompareExpression)expression; EmitCompareExpression(compareExpression); } if (expression is NPathBooleanExpression) { NPathBooleanExpression boolExpression = (NPathBooleanExpression)expression; EmitBooleanExpression(boolExpression); } if (expression is NPathInExpression) { NPathInExpression value = (NPathInExpression)expression; EmitIn(value); } if (expression is NPathSearchFunction) { NPathSearchFunction value = (NPathSearchFunction)expression; EmitSearchFunction(value); } }
protected virtual bool EvalCompareExpression(object item, NPathCompareExpression compareExpression) { object leftValue = null; //EvalValue(item, compareExpression.LeftOperand); object rightValue = null; // EvalValue(item, compareExpression.RightOperand); if (compareExpression.LeftOperand is NPathIdentifier && compareExpression.RightOperand is NPathIdentifier) { NPathIdentifier leftIdent = (NPathIdentifier)compareExpression.LeftOperand; NPathIdentifier rightIdent = (NPathIdentifier)compareExpression.RightOperand ; bool rightIsEnum = false; if (rightIdent.Path.IndexOf(".") == -1) { if (item.GetType().GetProperty(rightIdent.Path) == null) rightIsEnum = true; } bool leftIsEnum = false; if (leftIdent.Path.IndexOf(".") == -1) { if (item.GetType().GetProperty(leftIdent.Path) == null) leftIsEnum = true; } if (leftIsEnum && rightIsEnum) throw new Exception(string.Format("Property not found '{0}'",leftIdent.Path) ); if (leftIsEnum) { string enumName = leftIdent.Path; rightValue = EvalValue(item,rightIdent); if (rightValue == null) return false; Type enumType = rightValue.GetType(); if (!enumType.IsEnum) throw new Exception(string.Format("Property '{0}' is not an enum",rightIdent.Path) ); leftValue = Enum.Parse(enumType,enumName); } if (rightIsEnum) { string enumName = rightIdent.Path; leftValue = EvalValue(item,leftIdent); if (leftValue == null) return false; Type enumType = leftValue.GetType(); if (!enumType.IsEnum) throw new Exception(string.Format("Property '{0}' is not an enum",leftIdent.Path) ); rightValue = Enum.Parse(enumType,enumName); } } else { leftValue = EvalValue(item, compareExpression.LeftOperand); rightValue = EvalValue(item, compareExpression.RightOperand); } if ( (leftValue is IComparable || rightValue is IComparable) || (leftValue == null || rightValue == null) ) { int res = Comparer.DefaultInvariant.Compare(leftValue, rightValue); switch (compareExpression.Operator) { case "=": { return res == 0; } case "!=": { return res != 0; } case ">=": { return res >= 0; } case "<=": { return res <= 0; } case ">": { return res > 0; } case "<": { return res < 0; } case "like": { if (leftValue == null || rightValue == null) return false; bool isLike = Like(leftValue.ToString(), rightValue.ToString()); return isLike; } default: break; } } else { switch (compareExpression.Operator) { case "=": { return leftValue == rightValue; } case "!=": { return leftValue != rightValue; } default: break; } } throw new Exception(string.Format("unknown compare expression '{0}'", compareExpression.Operator)); // do not localize }
private SqlExpression EvalCompareExpression(NPathCompareExpression compareExpression) { SqlSearchCondition search ; if (noNext) search = conditionChainOwner as SqlSearchCondition ; else search = conditionChainOwner.GetNextSqlSearchCondition() ; noNext = false; SqlExpression leftExpression = null; SqlExpression rightExpression = null; if (compareExpression.RightOperand is NPathIdentifier && !IsEnum(compareExpression.RightOperand) && IsEnum(compareExpression.LeftOperand)) { //left operand is enum rightExpression = EvalExpression(compareExpression.RightOperand); NPathIdentifier propertyPath = (NPathIdentifier)compareExpression.RightOperand; NPathIdentifier enumIdentifier = (NPathIdentifier)compareExpression.LeftOperand; Type enumType = GetTypFromPropertyPath(propertyPath); if (!enumType.IsEnum) throw new Exception(string.Format("Property '{0}' is not an Enum type",propertyPath.Path)); int enumValue = GetEnumValue(enumType,enumIdentifier.Path); leftExpression = new SqlNumericLiteral(enumValue); } if (compareExpression.LeftOperand is NPathIdentifier && !IsEnum(compareExpression.LeftOperand) && IsEnum(compareExpression.RightOperand)) { leftExpression = EvalExpression(compareExpression.LeftOperand); NPathIdentifier propertyPath = (NPathIdentifier)compareExpression.LeftOperand; NPathIdentifier enumIdentifier = (NPathIdentifier)compareExpression.RightOperand; Type enumType = GetTypFromPropertyPath(propertyPath); if (!enumType.IsEnum) throw new Exception(string.Format("Property '{0}' is not an Enum type",propertyPath.Path)); int enumValue = GetEnumValue(enumType,enumIdentifier.Path); rightExpression = new SqlNumericLiteral(enumValue); } else { leftExpression = EvalExpression(compareExpression.LeftOperand); rightExpression = EvalExpression(compareExpression.RightOperand); } SqlPredicate predicate; if (compareExpression.RightOperand is NPathNullValue) { if (compareExpression.Operator == "=") predicate = search.GetSqlIsNullPredicate(leftExpression); else if (compareExpression.Operator == "!=") // do not localize predicate = search.GetSqlIsNullPredicate(leftExpression, true); else throw new NPathException("Comparisment operand " + compareExpression.Operator + " can't be used for comparisment with Null value!"); // do not localize } else { predicate = search.GetSqlComparePredicate(leftExpression, EvalOperator(compareExpression.Operator) ,rightExpression) ; } return predicate; }
private NPathCompareExpression ParseCompareOperator(IValue leftOperand) { NPathCompareExpression compare = new NPathCompareExpression(); compare.LeftOperand = leftOperand; Token currentToken = tokenizer.GetCurrentToken(); #region parse operator if (currentToken.IsType(">")) // do not localize compare.Operator = ">"; // do not localize if (currentToken.IsType("<")) // do not localize compare.Operator = "<"; // do not localize if (currentToken.IsType(">=")) // do not localize compare.Operator = ">="; // do not localize if (currentToken.IsType("<=")) // do not localize compare.Operator = "<="; // do not localize if (currentToken.IsType("=")) // do not localize compare.Operator = "="; // do not localize if (currentToken.IsType("!=")) // do not localize compare.Operator = "!="; // do not localize if (currentToken.IsType("like")) // do not localize compare.Operator = "like"; // do not localize #endregion tokenizer.MoveNext(); if (CurrentIsValue()) { compare.RightOperand = ParseExpression(); } else { //unknown value? throw GetExpectedTokenException("Value"); } return compare; }