protected override SqlBuilder Select(NewExpression expression, SqlBuilder sqlBuilder) { for (int i = 0; i < expression.Arguments.Count; i++) { var argument = expression.Arguments[i]; var mermberExpression = ((MemberExpression)argument); PropertyInfo property = mermberExpression.Member as PropertyInfo; //排除非数据列 if (property.IsNotColumn(mermberExpression.Expression.Type)) { continue; } SqlProvider.Select(argument, sqlBuilder); var fieldName = expression.Members[i].Name; sqlBuilder.SelectFieldsAlias.Add($"[{fieldName}]"); } return(sqlBuilder); }
protected override SqlBuilder Select(ParameterExpression expression, SqlBuilder sqlBuilder) { SqlProvider.Select(expression, sqlBuilder); return(sqlBuilder); }
protected override SqlBuilder OrderBy(ParameterExpression expression, SqlBuilder sqlBuilder) { SqlProvider.OrderBy(expression, sqlBuilder); return(sqlBuilder); }
protected override SqlBuilder Where(ParameterExpression expression, SqlBuilder sqlBuilder) { SqlProvider.Where(expression, sqlBuilder); return(sqlBuilder); }
protected override SqlBuilder Max(UnaryExpression expression, SqlBuilder sqlBuilder) { SqlProvider.Max(expression.Operand, sqlBuilder); return(sqlBuilder); }
protected override SqlBuilder ThenByDescending(UnaryExpression expression, SqlBuilder sqlBuilder) { SqlProvider.ThenByDescending(expression.Operand, sqlBuilder); return(sqlBuilder); }
private static void InnerIn(MethodCallExpression expression, SqlBuilder sqlBuilder) { SqlProvider.Where(expression.Arguments[0], sqlBuilder); sqlBuilder.SqlWhereStr += " in"; SqlProvider.In(expression.Arguments[1], sqlBuilder); }
protected override SqlBuilder Where(BinaryExpression expression, SqlBuilder sqlBuilder) { bool hasHandle = false; if (CheckMemberIsNotColumn(expression, sqlBuilder)) { return(sqlBuilder); } //添加Where表达式左侧sql if (IsNeedsParentheses(expression, expression.Left)) { sqlBuilder.SqlWhereStr += "("; } if (expression.Left is MemberExpression && expression.Left.Type == typeof(bool) && expression.Left.NodeType == ExpressionType.MemberAccess && (expression.NodeType == ExpressionType.AndAlso || expression.Right is ConstantExpression)) { //解析(m=>m.IsAdmin) var memberExpression = expression.Left as MemberExpression; var tableName = memberExpression.Member.DeclaringType.GetTableName(sqlBuilder._dbSqlParser); sqlBuilder.SetTableAlias(tableName); string tableAlias = sqlBuilder.GetTableAlias(tableName); if (!string.IsNullOrWhiteSpace(tableAlias)) { tableAlias += "."; } //添加参数 var dbParamName = string.Empty; if (expression.Right is ConstantExpression) { hasHandle = true; var value = Convert.ToBoolean(((ConstantExpression)expression.Right).Value) ? 1 : 0; dbParamName = sqlBuilder.AddDbParameter(value); } else { dbParamName = sqlBuilder.AddDbParameter(1); } //添加关联条件 sqlBuilder.SqlWhereStr += " " + tableAlias + memberExpression.Member.Name; sqlBuilder.SqlWhereStr += " = "; sqlBuilder.SqlWhereStr += $"{dbParamName}"; } else { SqlProvider.Where(expression.Left, sqlBuilder); } if (IsNeedsParentheses(expression, expression.Left)) { sqlBuilder.SqlWhereStr += ")"; } if (!hasHandle) { //添加Where操作符 var whereOperator = " ="; if ((expression.Right is ConstantExpression) && ((ConstantExpression)expression.Right).Value == null) { whereOperator = OperatorParser(expression.NodeType, true); } else { whereOperator = OperatorParser(expression.NodeType); } sqlBuilder.SqlWhereStr += $"{whereOperator}"; //添加Where表达式右侧sql if (IsNeedsParentheses(expression, expression.Right)) { sqlBuilder.SqlWhereStr += "("; } if (expression.Right is MemberExpression && expression.Right.Type == typeof(bool) && expression.Right.NodeType == ExpressionType.MemberAccess && expression.NodeType == ExpressionType.AndAlso) { //解析(m=>m.IsAdmin) var memberExpression = expression.Right as MemberExpression; var tableName = memberExpression.Member.DeclaringType.GetTableName(sqlBuilder._dbSqlParser); sqlBuilder.SetTableAlias(tableName); string tableAlias = sqlBuilder.GetTableAlias(tableName); if (!string.IsNullOrWhiteSpace(tableAlias)) { tableAlias += "."; } //添加参数 var dbParamName = sqlBuilder.AddDbParameter(1); //添加关联条件 sqlBuilder.SqlWhereStr += " " + tableAlias + memberExpression.Member.Name; sqlBuilder.SqlWhereStr += " = "; sqlBuilder.SqlWhereStr += $"{dbParamName}"; } else { SqlProvider.Where(expression.Right, sqlBuilder); } if (IsNeedsParentheses(expression, expression.Right)) { sqlBuilder.SqlWhereStr += ")"; } } return(sqlBuilder); }