private static Expression CreateFieldCondition( Expression rootExpr, FieldInfo fieldInfo, FilterConditionDTO condition) { object constValue; Type underlyingType = fieldInfo.FieldType; var isNullable = TryGetNullableType(underlyingType, out underlyingType); if (!ExtractConstantValue(underlyingType, condition.Value, out constValue)) { return(null); } var fieldExpr = (Expression)Expression.Field(rootExpr, fieldInfo); if (isNullable) { fieldExpr = Expression.Property(fieldExpr, "Value"); } var constExpr = Expression.Constant(constValue); Expression result; if (!CreateOperatorExpression(fieldExpr, constExpr, condition.Operator, out result)) { return(null); } if (isNullable) { result = Expression.AndAlso(Expression.NotEqual(Expression.Field(rootExpr, fieldInfo), Expression.Constant(null)), result); } return(result); }
private ParametrizedSqlPart CreateConditionWithValue( FilterConditionDTO condition, object value, ConditionGeneratorContext context) { var paramName = context.GetNextParameterName(); var sb = new StringBuilder(); sb.Append("("); sb.Append("["); sb.Append(condition.Field); sb.Append("]"); sb.Append(" "); sb.Append(GetSqlOperator(condition.Operator)); sb.Append(" "); sb.Append(paramName); sb.Append(")"); var sqlPart = new ParametrizedSqlPart() { Sql = sb.ToString(), Parameters = new List <SqlQueryParameter>() { new SqlQueryParameter(paramName, value != null ? value : DBNull.Value) } }; return(sqlPart); }
private static Expression CreatePropertyCondition( Expression rootExpr, PropertyInfo propInfo, FilterConditionDTO condition) { object constValue; Type underlyingType = propInfo.PropertyType; var isNullable = TryGetNullableType(underlyingType, out underlyingType); if (!ExtractConstantValue(underlyingType, condition.Value, out constValue)) { return(null); } var propExpr = (Expression)Expression.Property(rootExpr, propInfo); var constExpr = Expression.Constant(constValue); if (isNullable) { propExpr = Expression.Property(propExpr, "Value"); } Expression result; if (!CreateOperatorExpression(propExpr, constExpr, condition.Operator, out result)) { return(null); } if (isNullable) { result = Expression.AndAlso(Expression.NotEqual(Expression.Property(rootExpr, propInfo), Expression.Constant(null)), result); } return(result); }
private ParametrizedSqlPart CreateCondition( FilterConditionDTO condition, IDictionary <string, DbType> columnTypeMap, ConditionGeneratorContext context) { DbType dbType; if (!columnTypeMap.TryGetValue(condition.Field, out dbType)) { throw new ApplicationException("Invalid column name."); } switch (dbType) { case DbType.Boolean: return(CreateBooleanCondition(condition, context)); case DbType.String: return(CreateStringCondition(condition, context)); case DbType.Int32: return(CreateInt32Condition(condition, context)); default: throw new ApplicationException("Invalid column data-type."); } }
private ParametrizedSqlPart CreateBooleanCondition( FilterConditionDTO condition, ConditionGeneratorContext context) { var booleanValue = string.Equals( condition.Value, "true", StringComparison.InvariantCultureIgnoreCase ); return(CreateConditionWithValue(condition, booleanValue, context)); }
private ParametrizedSqlPart CreateInt32Condition( FilterConditionDTO condition, ConditionGeneratorContext context) { int intValue; if (!Int32.TryParse(condition.Value, out intValue)) { intValue = 0; } return(CreateConditionWithValue(condition, intValue, context)); }
private ParametrizedSqlPart CreateStringCondition( FilterConditionDTO condition, ConditionGeneratorContext context) { return(CreateConditionWithValue(condition, condition.Value, context)); }
protected Expression ParseCriteriaExpression(FilterConditionDTO condition, IQueryable <KeyValueDTO> queryableData) { var curType = typeof(KeyValueDTO); Expression criteriaExpression = null; var pe = Expression.Parameter(curType, "p"); var namePropInfo = curType.GetProperty("Key"); var valuePropInfo = curType.GetProperty("Value"); var nameLeftExpr = Expression.Property(pe, namePropInfo); var nameRightExpr = Expression.Constant(condition.Field); var nameExpression = Expression.Equal(nameLeftExpr, nameRightExpr); var comparisionExpr = Expression.Call(typeof(Test_Incoming_Data_v1).GetMethod("Compare", BindingFlags.Public | BindingFlags.Static), new Expression[] { Expression.Property(pe, valuePropInfo), Expression.Constant(condition.Value) }); var zero = Expression.Constant(0); var op = condition.Operator; Expression criterionExpression; switch (op) { case "eq": criterionExpression = Expression.Equal(comparisionExpr, zero); break; case "neq": criterionExpression = Expression.NotEqual(comparisionExpr, zero); break; case "gt": criterionExpression = Expression.GreaterThan(comparisionExpr, zero); break; case "gte": criterionExpression = Expression.GreaterThanOrEqual(comparisionExpr, zero); break; case "lt": criterionExpression = Expression.LessThan(comparisionExpr, zero); break; case "lte": criterionExpression = Expression.LessThanOrEqual(comparisionExpr, zero); break; default: throw new NotSupportedException($"Not supported operator: {op}"); } criteriaExpression = Expression.And(nameExpression, criterionExpression); var whereCallExpression = Expression.Call( typeof(Queryable), "Where", new[] { curType }, queryableData.Expression, Expression.Lambda <Func <KeyValueDTO, bool> >(criteriaExpression, new[] { pe }) ); return(whereCallExpression); }