protected override SqlBuilder Join(MemberExpression expression, SqlBuilder sqlBuilder) { var tableName = expression.Member.DeclaringType.GetTableName(sqlBuilder._dbSqlParser); sqlBuilder.SetTableAlias(tableName); string tableAlias = sqlBuilder.GetTableAlias(tableName); if (!string.IsNullOrWhiteSpace(tableAlias)) { tableAlias += "."; } if (expression.NodeType == ExpressionType.MemberAccess && expression.Type == typeof(bool)) { //添加值 var dbParamName = sqlBuilder.AddDbParameter(1); //添加到查询条件 sqlBuilder.SqlWhereStr += " " + tableAlias + expression.Member.Name; sqlBuilder.SqlWhereStr += " = "; sqlBuilder.SqlWhereStr += $"{dbParamName}"; } else { sqlBuilder.SqlJoinStr += " " + tableAlias + expression.Member.Name; } return(sqlBuilder); }
protected override SqlBuilder Where(UnaryExpression expression, SqlBuilder sqlBuilder) { //解析x => !x.isDeletion这样的 if (expression.NodeType == ExpressionType.Not && expression.Operand is MemberExpression && expression.Type == typeof(bool)) { var dbParamName = sqlBuilder.AddDbParameter(0); var memberExpression = expression.Operand as MemberExpression; //添加到查询条件 var tableName = memberExpression.Member.DeclaringType.GetTableName(sqlBuilder._dbSqlParser); sqlBuilder.SetTableAlias(tableName); string tableAlias = sqlBuilder.GetTableAlias(tableName); if (!string.IsNullOrWhiteSpace(tableAlias)) { tableAlias += "."; } sqlBuilder.SqlWhereStr += " " + tableAlias + memberExpression.Member.Name; sqlBuilder.SqlWhereStr += " = "; sqlBuilder.SqlWhereStr += $"{dbParamName}"; } else { SqlProvider.Where(expression.Operand, sqlBuilder); } return(sqlBuilder); }
protected override SqlBuilder ThenByDescending(MemberExpression expression, SqlBuilder sqlBuilder) { var tableName = expression.Member.DeclaringType.GetTableName(sqlBuilder._dbSqlParser); sqlBuilder.SetTableAlias(tableName); sqlBuilder.SqlOrderStr += "," + sqlBuilder.GetTableAlias(tableName) + "." + expression.Member.Name + " desc"; return(sqlBuilder); }
protected override SqlBuilder OrderBy(MemberExpression expression, SqlBuilder sqlBuilder) { var tableName = expression.Member.DeclaringType.GetTableName(sqlBuilder._dbSqlParser); sqlBuilder.SetTableAlias(tableName); if (sqlBuilder.SqlOrderStr.Length > 10) { sqlBuilder.SqlOrderStr += ","; } sqlBuilder.SqlOrderStr += sqlBuilder.GetTableAlias(tableName) + "." + expression.Member.Name; return(sqlBuilder); }
private SqlBuilder AggregateFunctionParser(MemberExpression expression, SqlBuilder sqlBuilder, string functionName) { string aggregateFunctionName = functionName; string tableName = expression.Member.DeclaringType.GetTableName(sqlBuilder._dbSqlParser); string columnName = expression.Member.Name; sqlBuilder.SetTableAlias(tableName); string tableAlias = sqlBuilder.GetTableAlias(tableName); if (!string.IsNullOrWhiteSpace(tableAlias)) { tableName += " " + tableAlias; columnName = tableAlias + "." + columnName; } sqlBuilder.SqlCalculateStr = $"select {aggregateFunctionName}({columnName}) from {tableName}"; return(sqlBuilder); }
protected override SqlBuilder GroupBy(MemberExpression expression, SqlBuilder sqlBuilder) { var tableName = expression.Member.DeclaringType.GetTableName(sqlBuilder._dbSqlParser); sqlBuilder.SetTableAlias(tableName); var tableAliaName = sqlBuilder.GetTableAlias(tableName); if (!sqlBuilder.IsAddGroupBy) { sqlBuilder.SqlGroupByStr += $" group by {tableAliaName + "." + expression.Member.Name}"; } else { sqlBuilder.SqlGroupByStr += $" ,{tableAliaName + "." + expression.Member.Name}"; } return(sqlBuilder); }
protected override SqlBuilder Select(MemberExpression expression, SqlBuilder sqlBuilder) { //排除非数据列 PropertyInfo propertyInfo = expression.Member as PropertyInfo; if (propertyInfo.IsNotColumn(expression.Member.DeclaringType)) { return(sqlBuilder); } var tableName = expression.Member.DeclaringType.GetTableName(sqlBuilder._dbSqlParser); sqlBuilder.SetTableAlias(tableName); string tableAlias = sqlBuilder.GetTableAlias(tableName); if (!string.IsNullOrWhiteSpace(tableAlias)) { tableAlias += "."; } sqlBuilder.SelectFields.Add(tableAlias + expression.Member.Name); return(sqlBuilder); }
protected override SqlBuilder Where(MemberExpression expression, SqlBuilder sqlBuilder) { //获取table名称和别名 var tableName = expression.Member.DeclaringType.GetTableName(sqlBuilder._dbSqlParser); sqlBuilder.SetTableAlias(tableName); string tableAlias = sqlBuilder.GetTableAlias(tableName); if (!string.IsNullOrWhiteSpace(tableAlias)) { tableAlias += "."; } if (expression.Expression.NodeType == ExpressionType.Constant) { object value = GetValue(expression); var dbParamName = sqlBuilder.AddDbParameter(value); sqlBuilder.SqlWhereStr += $" {dbParamName}"; } //解析x => x.isDeletion这样的 else if (expression.NodeType == ExpressionType.MemberAccess && expression.Type == typeof(bool)) { //添加值 var dbParamName = sqlBuilder.AddDbParameter(1); //添加到查询条件 sqlBuilder.SqlWhereStr += " " + tableAlias + expression.Member.Name; sqlBuilder.SqlWhereStr += " = "; sqlBuilder.SqlWhereStr += $"{dbParamName}"; } else if (expression.Expression.NodeType == ExpressionType.Parameter) { sqlBuilder.SqlWhereStr += " " + tableAlias + expression.Member.Name; } return(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); }