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();
Simple();
SqlTest();
Subqueryable();
}
private static void SqlTest()
@ -34,7 +35,55 @@ namespace OrmTest.Demo
var db = GetInstance();
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()
{
//SqlSugarClient

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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