Oracle Subquery bug

This commit is contained in:
sunkaixuan 2018-02-09 15:42:49 +08:00
parent b2cae43fa4
commit 99e40df3af
21 changed files with 156 additions and 1 deletions

View File

@ -27,6 +27,7 @@ namespace OrmTest.Demo
Enum(); Enum();
Simple(); Simple();
SqlTest(); SqlTest();
Subqueryable();
} }
private static void SqlTest() private static void SqlTest()
@ -34,7 +35,55 @@ namespace OrmTest.Demo
var db = GetInstance(); var db = GetInstance();
var x = db.Ado.ExecuteCommand("select '@id' as id from student where id=@id",new { id=1}); var x = db.Ado.ExecuteCommand("select '@id' as id from student where id=@id",new { id=1});
} }
private static void Subqueryable()
{
var db = GetInstance();
var i = 0;
var getAll11 = db.Queryable<Student>().Where(it => SqlFunc.Subqueryable<School>().Where(s => s.Id == it.Id).Max(s => s.Id) == i).ToList();
var getAll12 = db.Queryable<Student>().Where(it => SqlFunc.Subqueryable<School>().Where(s => s.Id == it.Id).Max(s => s.Id) == 1).ToList();
var getAll7 = db.Queryable<Student>().Where(it => SqlFunc.Subqueryable<School>().Where(s => s.Id == it.Id).Any()).ToList();
var getAll9 = db.Queryable<Student>().Where(it => SqlFunc.Subqueryable<School>().Where(s => s.Id == it.Id).Count() == 1).ToList();
//var getAll10 = db.Queryable<Student>().Where(it => SqlFunc.Subqueryable<School>().Where(s => s.Id == it.Id).OrderBy(s => s.Id).Select(s => s.Id) == 1).ToList();
//var getAll14 = db.Queryable<Student>().Where(it => SqlFunc.Subqueryable<School>().Where(s => s.Id == it.Id).OrderByDesc(s => s.Id).Select(s => s.Id) == 1).ToList();
var getAll8 = db.Queryable<Student>().Where(it => SqlFunc.Subqueryable<School>().Where(s => s.Id == it.Id).Where(s => s.Name == it.Name).NotAny()).ToList();
var getAll1 = db.Queryable<Student>().Where(it => it.Id == SqlFunc.Subqueryable<School>().Where(s => s.Id == it.Id).Select(s => s.Id)).ToList();
var getAll2 = db.Queryable<Student, School>((st, sc) => new object[] {
JoinType.Left,st.Id==sc.Id
})
.Where(st => st.Id == SqlFunc.Subqueryable<School>().Where(s => s.Id == st.Id).Select(s => s.Id))
.ToList();
var getAll3 = db.Queryable<Student, School>((st, sc) => new object[] {
JoinType.Left,st.Id==sc.Id
})
.Select(st =>
new
{
name = st.Name,
id = SqlFunc.Subqueryable<School>().Where(s => s.Id == st.Id).Select(s => s.Id)
})
.ToList();
var getAll4 = db.Queryable<Student>().Select(it =>
new
{
name = it.Name,
id = SqlFunc.Subqueryable<School>().Where(s => s.Id == it.Id).Select(s => s.Id)
}).ToList();
var getAll5 = db.Queryable<Student>().Select(it =>
new Student
{
Name = it.Name,
Id = SqlFunc.Subqueryable<School>().Where(s => s.Id == it.Id).Select(s => s.Id)
}).ToList();
}
private static void Simple() private static void Simple()
{ {
//SqlSugarClient //SqlSugarClient

View File

@ -13,5 +13,6 @@ namespace SqlSugar
string GetValue(Expression expression); string GetValue(Expression expression);
int Sort { get; } int Sort { get; }
Expression Expression { get; set; } Expression Expression { get; set; }
bool HasWhere { get; set; }
} }
} }

View File

@ -31,6 +31,11 @@ namespace SqlSugar
get;set; get;set;
} }
public bool HasWhere
{
get; set;
}
public string GetValue(Expression expression) public string GetValue(Expression expression)
{ {
var exp = expression as MethodCallExpression; var exp = expression as MethodCallExpression;

View File

@ -18,6 +18,11 @@ namespace SqlSugar
get;set; get;set;
} }
public bool HasWhere
{
get;set;
}
public string Name public string Name
{ {
get get

View File

@ -8,6 +8,11 @@ namespace SqlSugar
{ {
public class SubAvg: ISubOperation public class SubAvg: ISubOperation
{ {
public bool HasWhere
{
get; set;
}
public string Name public string Name
{ {
get get

View File

@ -8,6 +8,11 @@ namespace SqlSugar
{ {
public class SubBegin : ISubOperation public class SubBegin : ISubOperation
{ {
public bool HasWhere
{
get; set;
}
public string Name public string Name
{ {
get get

View File

@ -8,6 +8,11 @@ namespace SqlSugar
{ {
public class SubCount: ISubOperation public class SubCount: ISubOperation
{ {
public bool HasWhere
{
get; set;
}
public string Name public string Name
{ {
get get

View File

@ -8,6 +8,11 @@ namespace SqlSugar
{ {
public class SubFromTable : ISubOperation public class SubFromTable : ISubOperation
{ {
public bool HasWhere
{
get; set;
}
public string Name public string Name
{ {
get get

View File

@ -8,6 +8,11 @@ namespace SqlSugar
{ {
public class SubGroupBy : ISubOperation public class SubGroupBy : ISubOperation
{ {
public bool HasWhere
{
get; set;
}
public string Name public string Name
{ {
get { return "GroupBy"; } get { return "GroupBy"; }

View File

@ -8,6 +8,11 @@ namespace SqlSugar
{ {
public class SubLeftBracket : ISubOperation public class SubLeftBracket : ISubOperation
{ {
public bool HasWhere
{
get; set;
}
public ExpressionContext Context public ExpressionContext Context
{ {
get;set; get;set;

View File

@ -8,6 +8,11 @@ namespace SqlSugar
{ {
public class SubMax:ISubOperation public class SubMax:ISubOperation
{ {
public bool HasWhere
{
get; set;
}
public string Name public string Name
{ {
get get

View File

@ -8,6 +8,11 @@ namespace SqlSugar
{ {
public class SubMin: ISubOperation public class SubMin: ISubOperation
{ {
public bool HasWhere
{
get; set;
}
public string Name public string Name
{ {
get get

View File

@ -8,6 +8,11 @@ namespace SqlSugar
{ {
public class SubNotAny : ISubOperation public class SubNotAny : ISubOperation
{ {
public bool HasWhere
{
get; set;
}
public ExpressionContext Context public ExpressionContext Context
{ {
get;set; get;set;

View File

@ -8,6 +8,11 @@ namespace SqlSugar
{ {
public class SubOrderBy : ISubOperation public class SubOrderBy : ISubOperation
{ {
public bool HasWhere
{
get; set;
}
public string Name public string Name
{ {
get { return "OrderBy"; } get { return "OrderBy"; }
@ -33,6 +38,10 @@ namespace SqlSugar
public string GetValue(Expression expression) public string GetValue(Expression expression)
{ {
if (this.Context is OracleExpressionContext)
{
throw new Exception("Oracle Subquery can't OrderBy");
}
var exp = expression as MethodCallExpression; var exp = expression as MethodCallExpression;
var argExp = exp.Arguments[0]; var argExp = exp.Arguments[0];
var result = "ORDER BY " + SubTools.GetMethodValue(this.Context, argExp, ResolveExpressType.FieldSingle); var result = "ORDER BY " + SubTools.GetMethodValue(this.Context, argExp, ResolveExpressType.FieldSingle);
@ -43,6 +52,11 @@ namespace SqlSugar
} }
public class SubOrderByDesc : ISubOperation public class SubOrderByDesc : ISubOperation
{ {
public bool HasWhere
{
get; set;
}
public string Name public string Name
{ {
get { return "OrderByDesc"; } get { return "OrderByDesc"; }

View File

@ -8,6 +8,11 @@ namespace SqlSugar
{ {
public class SubRightBracket : ISubOperation public class SubRightBracket : ISubOperation
{ {
public bool HasWhere
{
get; set;
}
public ExpressionContext Context public ExpressionContext Context
{ {
get;set; get;set;

View File

@ -8,6 +8,11 @@ namespace SqlSugar
{ {
public class SubSelect : ISubOperation public class SubSelect : ISubOperation
{ {
public bool HasWhere
{
get; set;
}
public string Name public string Name
{ {
get get

View File

@ -8,6 +8,11 @@ namespace SqlSugar
{ {
public class SubSelectDefault : ISubOperation public class SubSelectDefault : ISubOperation
{ {
public bool HasWhere
{
get; set;
}
public ExpressionContext Context public ExpressionContext Context
{ {
get;set; get;set;

View File

@ -8,6 +8,11 @@ namespace SqlSugar
{ {
public class SubSum:ISubOperation public class SubSum:ISubOperation
{ {
public bool HasWhere
{
get; set;
}
public string Name public string Name
{ {
get get

View File

@ -8,6 +8,11 @@ namespace SqlSugar
{ {
public class SubTop : ISubOperation public class SubTop : ISubOperation
{ {
public bool HasWhere
{
get; set;
}
public ExpressionContext Context public ExpressionContext Context
{ {
get; set; get; set;
@ -34,6 +39,10 @@ namespace SqlSugar
{ {
return 150; return 150;
} }
else if (this.Context is OracleExpressionContext) {
return 401;
}
else else
{ {
return 490; return 490;
@ -50,7 +59,7 @@ namespace SqlSugar
} }
else if (this.Context is OracleExpressionContext) else if (this.Context is OracleExpressionContext)
{ {
return "ROWNUM=1"; return (HasWhere?"AND":"WHERE")+ " ROWNUM=1";
} }
else else
{ {

View File

@ -8,6 +8,11 @@ namespace SqlSugar
{ {
public class SubWhere: ISubOperation public class SubWhere: ISubOperation
{ {
public bool HasWhere
{
get; set;
}
public string Name public string Name
{ {
get { return "Where"; } get { return "Where"; }

View File

@ -99,8 +99,10 @@ namespace SqlSugar
isubList.Add(new SubSelectDefault()); isubList.Add(new SubSelectDefault());
} }
isubList = isubList.OrderBy(it => it.Sort).ToList(); isubList = isubList.OrderBy(it => it.Sort).ToList();
var isHasWhere = isubList.Where(it => it is SubWhere).Any();
List<string> result = isubList.Select(it => List<string> result = isubList.Select(it =>
{ {
it.HasWhere = isHasWhere;
return it.GetValue(it.Expression); return it.GetValue(it.Expression);
}).ToList(); }).ToList();
return result; return result;