Browse Source

Subquery in Subquery BUG

pull/12/MERGE
sunkaixuan 6 years ago
parent
commit
d8e0bca1c8
  1. 12
      Src/Asp.Net/SqlServerTest/Demos/1_Query.cs
  2. 1
      Src/Asp.Net/SqlSugar/ExpressionsToSql/ExpressionContext.cs
  3. 2
      Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/Items/SubAnd.cs
  4. 6
      Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/Items/SubFromTable.cs
  5. 2
      Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/Items/SubGroupBy.cs
  6. 2
      Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/Items/SubMax.cs
  7. 2
      Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/Items/SubMin.cs
  8. 2
      Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/Items/SubOrderBy.cs
  9. 2
      Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/Items/SubSum.cs
  10. 2
      Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/Items/SubWhere.cs
  11. 2
      Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/Items/SubWhereIF.cs
  12. 4
      Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/SubResolve.cs
  13. 8
      Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/SubTools.cs

12
Src/Asp.Net/SqlServerTest/Demos/1_Query.cs

@ -128,6 +128,18 @@ namespace OrmTest.Demo
name = it.Name,
id = SqlFunc.Subqueryable<Student>().WhereIF(!string.IsNullOrEmpty(name), s => s.Id == 1).Min(s => s.Id)
}).ToList();
var getAll666666 = db.Queryable<Student>()
.Where(it => SqlFunc.Subqueryable<School>().Where(s => s.Id == it.Id).Any())
.Select(it =>
new
{
name = it.Name,
id = SqlFunc.Subqueryable<Student>().Where(s=>s.Id==SqlFunc.Subqueryable<School>().Where(y=>y.Id==s.SchoolId).Select(y=>y.Id)).Min(s => s.Id),
id2 = SqlFunc.Subqueryable<Student>().Where(s => s.Id == SqlFunc.Subqueryable<School>().Where(y => y.Id == s.SchoolId).Select(y => y.Id)).Min(s => s.Id)
}).ToList();
}
private static void Async()

1
Src/Asp.Net/SqlSugar/ExpressionsToSql/ExpressionContext.cs

@ -35,6 +35,7 @@ namespace SqlSugar
_DbMehtods = value;
}
}
public int SubQueryIndex { get; set; }
public int Index { get; set; }
public int ParameterIndex { get; set; }
public string SingleTableNameSubqueryShortName{ get; set; }

2
Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/Items/SubAnd.cs

@ -42,7 +42,7 @@ namespace SqlSugar
var argExp = exp.Arguments[0];
var result = "AND " + SubTools.GetMethodValue(this.Context, argExp, ResolveExpressType.WhereMultiple);
var selfParameterName = this.Context.GetTranslationColumnName((argExp as LambdaExpression).Parameters.First().Name) + UtilConstants.Dot;
result = result.Replace(selfParameterName, string.Empty);
result = result.Replace(selfParameterName, SubTools.GetSubReplace(this.Context));
return result;
}
}

6
Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/Items/SubFromTable.cs

@ -50,7 +50,11 @@ namespace SqlSugar
this.Context.InitMappingInfo(entityType);
this.Context.RefreshMapping();
}
return "FROM "+this.Context.GetTranslationTableName(name, true);
var result= "FROM "+this.Context.GetTranslationTableName(name, true);
if (this.Context.SubQueryIndex > 0) {
result += " subTableIndex"+this.Context.SubQueryIndex;
}
return result;
}
}
}

2
Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/Items/SubGroupBy.cs

@ -42,7 +42,7 @@ namespace SqlSugar
var argExp = exp.Arguments[0];
var result = "GROUP BY " + SubTools.GetMethodValue(this.Context, argExp, ResolveExpressType.FieldSingle);
var selfParameterName = this.Context.GetTranslationColumnName((argExp as LambdaExpression).Parameters.First().Name) + UtilConstants.Dot;
result = result.Replace(selfParameterName, string.Empty);
result = result.Replace(selfParameterName, SubTools.GetSubReplace(this.Context));
return result;
}
}

2
Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/Items/SubMax.cs

@ -51,7 +51,7 @@ namespace SqlSugar
}
var result = "MAX(" + SubTools.GetMethodValue(Context, argExp, ResolveExpressType.WhereMultiple) + ")";
var selfParameterName = Context.GetTranslationColumnName(parametres.First().Name) + UtilConstants.Dot;
result = result.Replace(selfParameterName, string.Empty);
result = result.Replace(selfParameterName, SubTools.GetSubReplace(this.Context));
return result;
}
}

2
Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/Items/SubMin.cs

@ -51,7 +51,7 @@ namespace SqlSugar
}
var result = "MIN(" + SubTools.GetMethodValue(Context, argExp, ResolveExpressType.WhereMultiple) + ")";
var selfParameterName = Context.GetTranslationColumnName(parametres.First().Name) + UtilConstants.Dot;
result = result.Replace(selfParameterName, string.Empty);
result = result.Replace(selfParameterName, SubTools.GetSubReplace(this.Context));
return result;
}
}

2
Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/Items/SubOrderBy.cs

@ -46,7 +46,7 @@ namespace SqlSugar
var argExp = exp.Arguments[0];
var result = "ORDER BY " + SubTools.GetMethodValue(this.Context, argExp, ResolveExpressType.FieldSingle);
var selfParameterName = this.Context.GetTranslationColumnName((argExp as LambdaExpression).Parameters.First().Name) + UtilConstants.Dot;
result = result.Replace(selfParameterName, string.Empty);
result = result.Replace(selfParameterName, SubTools.GetSubReplace(this.Context));
return result;
}
}

2
Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/Items/SubSum.cs

@ -51,7 +51,7 @@ namespace SqlSugar
}
var result = "SUM(" + SubTools.GetMethodValue(Context, argExp, ResolveExpressType.WhereMultiple)+")";
var selfParameterName = Context.GetTranslationColumnName(parametres.First().Name) + UtilConstants.Dot;
result = result.Replace(selfParameterName, string.Empty);
result = result.Replace(selfParameterName, SubTools.GetSubReplace(this.Context));
return result;
}
}

2
Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/Items/SubWhere.cs

@ -42,7 +42,7 @@ namespace SqlSugar
var argExp= exp.Arguments[0];
var result= "WHERE "+SubTools.GetMethodValue(Context, argExp, ResolveExpressType.WhereMultiple);;
var selfParameterName = Context.GetTranslationColumnName((argExp as LambdaExpression).Parameters.First().Name)+UtilConstants.Dot;
result = result.Replace(selfParameterName,string.Empty);
result = result.Replace(selfParameterName,SubTools.GetSubReplace(this.Context));
return result;
}
}

2
Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/Items/SubWhereIF.cs

@ -55,7 +55,7 @@ namespace SqlSugar
var argExp = exp.Arguments[1];
var result = "WHERE " + SubTools.GetMethodValue(Context, argExp, ResolveExpressType.WhereMultiple); ;
var selfParameterName = Context.GetTranslationColumnName((argExp as LambdaExpression).Parameters.First().Name) + UtilConstants.Dot;
result = result.Replace(selfParameterName, string.Empty);
result = result.Replace(selfParameterName, SubTools.GetSubReplace(this.Context));
return result;
}
}

4
Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/SubResolve.cs

@ -52,11 +52,15 @@ namespace SqlSugar
Check.Exception(true, "I'm sorry I can't parse the current expression");
}
}
var subIndex = this.context.SubQueryIndex;
while (currentExpression != null)
{
var addItem = currentExpression.Object as MethodCallExpression;
if (addItem != null)
allMethods.Add(addItem);
if (subIndex==this.context.SubQueryIndex&&addItem !=null&&addItem.Arguments.HasValue()&&addItem.Arguments.Any(it=>it.ToString().Contains("Subqueryable()"))) {
this.context.SubQueryIndex++;
}
currentExpression = addItem;
}
}

8
Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/SubTools.cs

@ -32,6 +32,14 @@ namespace SqlSugar
};
}
public static string GetSubReplace(ExpressionContext context)
{
if (context.SubQueryIndex == 0)
return string.Empty;
else
return "subTableIndex"+context.SubQueryIndex+".";
}
public static List<ISubOperation> SubItemsConst = SubItems(null);
public static string GetMethodValue(ExpressionContext context, Expression item, ResolveExpressType type)

Loading…
Cancel
Save