protected override System.Linq.Expressions.Expression VisitMember(MemberExpression node) { var memberInitExpression = node; var entity = ((ConstantExpression)TrimExpression.Trim(memberInitExpression)).Value; var properties = memberInitExpression.Type.GetProperties(); foreach (var item in properties) { if (item.CustomAttributes.Any(b => b.AttributeType == typeof(KeyAttribute))) { continue; } if (_sqlCmd.Length > 0) { _sqlCmd.Append(","); } var paramName = item.Name; var value = item.GetValue(entity); var fieldName = _providerOption.CombineFieldName(item.GetColumnAttributeName()); SetParam(fieldName, paramName, value); } return(node); }
public QuerySet <T> Where(T model) { ParameterExpression parameter = Expression.Parameter(typeof(T), "parameter"); var entityType = EntityCache.QueryEntity(typeof(T)); foreach (var item in entityType.Properties) { string name = item.Name; object value = item.GetValue(model, null); if (value != null) { if (item.PropertyType == typeof(int) || item.PropertyType == typeof(double) || item.PropertyType == typeof(decimal) || item.PropertyType == typeof(long)) { if (Convert.ToDecimal(value) == 0) { continue; } } else if (item.PropertyType == typeof(DateTime)) { var time = Convert.ToDateTime(value); if (time == DateTime.MinValue) { continue; } } var whereExpress = Expression.Equal(Expression.Property(parameter, name), Expression.Constant(value)); WhereExpressionList.Add(Expression.Lambda <Func <T, bool> >(TrimExpression.Trim(whereExpress), parameter)); } } return(this); }
/// <inheritdoc /> /// <summary> /// 执行解析 /// </summary> /// <param name="expression"></param> /// <param name="prefix">字段前缀</param> /// <param name="providerOption"></param> /// <returns></returns> public WhereExpression(LambdaExpression expression, string tableAlias, DynamicParameters para) : base(para) { _tempFieldName = "P_" + tableAlias + GetHashCode() + "_"; if (!string.IsNullOrEmpty(tableAlias)) { _tableAlias = tableAlias + "."; } var exp = TrimExpression.Trim(expression, true); Visit(exp); }
public JoinOrderExpression(LambdaExpression expression, Dictionary <string, string> m, DynamicParameters para, ISqlProvider sqlProvider) : base(para, sqlProvider) { _tempFieldName = "PJO_" + GetHashCode() + "_"; foreach (var v in m) { var key = string.IsNullOrEmpty(v.Key) ? expression.Parameters[0].Name : (expression.Parameters[0].Name + "." + v.Key); _map.Add(key, v.Value); } var exp = TrimExpression.Trim(expression, false); Visit(exp); }
public JoinExpression(LambdaExpression expression, Dictionary <string, string> m, DynamicParameters para) : base(para) { _tempFieldName = "PJ_" + GetHashCode() + "_"; foreach (var v in m) { var key = string.IsNullOrEmpty(v.Key) ? expression.Parameters[0].Name : (expression.Parameters[0].Name + "." + v.Key); _map.Add(key, v.Value); } var exp = TrimExpression.Trim(expression, false); Visit(exp); if (_sqlCmd.Length > 0) { JoinDic.Add("", _sqlCmd.ToString()); } }
/// <inheritdoc /> /// <summary> /// 执行解析 /// </summary> /// <param name="expression"></param> /// <param name="prefix">字段前缀</param> /// <param name="providerOption"></param> /// <returns></returns> public JoinWhereExpression(LambdaExpression expression, Dictionary <string, string> map, DynamicParameters para) : base(para) { if (expression == null) { return; } foreach (var v in map) { var key = string.IsNullOrEmpty(v.Key) ? expression.Parameters[0].Name : (expression.Parameters[0].Name + "." + v.Key); _map.Add(key, v.Value); } _tempFieldName = "PJW_" + GetHashCode() + "_"; var exp = TrimExpression.Trim(expression, true); Visit(exp); }
public JoinSelectExpression(LambdaExpression expression, Dictionary <string, string> map, DynamicParameters para) : base(para) { foreach (var v in map) { var key = string.IsNullOrEmpty(v.Key) ? expression.Parameters[0].Name : (expression.Parameters[0].Name + "." + v.Key); _map.Add(key, v.Value); } _tempFieldName = "PJS_" + GetHashCode() + "_"; var exp = TrimExpression.Trim(expression); Visit(exp); if (_sqlCmd.Length > 0) { var sql = _sqlCmd.ToString(); if (!sql.Contains(".")) { sql += ".*"; } QueryColumns.Add(sql); _sqlCmd.Clear(); } }
protected override System.Linq.Expressions.Expression VisitMember(MemberExpression node) { var memberInitExpression = node; var entity = ((ConstantExpression)TrimExpression.Trim(memberInitExpression)).Value; var properties = memberInitExpression.Type.GetPropertiesInDb(false); foreach (var item in properties) { if (item.CustomAttributes.Any(b => b.AttributeType == typeof(KeyAttribute))) { continue; } if (item.CustomAttributes.Any(b => b.AttributeType == typeof(NoUpdateAttribute))) { continue; } if (SkipByOtherCondition(entity, item)) { continue; } if (_sqlCmd.Length > 0) { _sqlCmd.Append(","); } var paramName = item.Name; var value = item.GetValue(entity); var c = item.GetColumnAttributeName(Dialect); SetParam(c, paramName, value); } return(node); }
/// <summary> /// 动态化查讯(转换成表达式树集合) 注意,int参数不会判断为0的值 /// </summary> /// <typeparam name="T">对应表</typeparam> /// <param name="dynamicTree"></param> /// <returns></returns> public QuerySet <T> Where(Dictionary <string, DynamicTree> dynamicTree) { if (dynamicTree != null) { foreach (var key in dynamicTree.Keys) { DynamicTree tree = dynamicTree[key]; if (tree != null && !string.IsNullOrEmpty(tree.Value)) { Type tableType = typeof(T); if (!string.IsNullOrEmpty(tree.Table)) { tableType = EntityCache.QueryEntity(tree.Table).Type; } //如果不存在对应表就使用默认表 ParameterExpression param = Expression.Parameter(tableType, "param"); object value = tree.Value; if (value == null) { continue; } else if (tree.ValueType == DbType.DateTime) { value = Convert.ToDateTime(value); } else if (tree.ValueType == DbType.String) { value = Convert.ToString(value); if ("" == value.ToString()) { continue; } } else if (tree.ValueType == DbType.Int32) { int number = Convert.ToInt32(value); value = number; if (0 == number) { continue; } } Expression whereExpress = null; switch (tree.Operators) { case ExpressionType.Equal: //等于 whereExpress = Expression.Equal(Expression.Property(param, tree.Field), Expression.Constant(value)); break; case ExpressionType.GreaterThanOrEqual: //大于等于 whereExpress = Expression.GreaterThanOrEqual(Expression.Property(param, tree.Field), Expression.Constant(value)); break; case ExpressionType.LessThanOrEqual: //小于等于 whereExpress = Expression.LessThanOrEqual(Expression.Property(param, tree.Field), Expression.Constant(value)); break; case ExpressionType.Call: //模糊查询 var method = typeof(string).GetMethodss().FirstOrDefault(x => x.Name.Equals("Contains")); whereExpress = Expression.Call(Expression.Property(param, tree.Field), method, new Expression[] { Expression.Constant(value) }); break; default: whereExpress = Expression.Equal(Expression.Property(param, tree.Field), Expression.Constant(value)); break; } WhereExpressionList.Add(Expression.Lambda(TrimExpression.Trim(whereExpress), param)); } } } return(this); }