mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-06-28 13:34:32 +08:00
Subquery.Take
This commit is contained in:
parent
79ac1f4ca6
commit
6fffa23973
@ -0,0 +1,93 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace SqlSugar
|
||||||
|
{
|
||||||
|
public class SubTake : ISubOperation
|
||||||
|
{
|
||||||
|
public bool HasWhere
|
||||||
|
{
|
||||||
|
get; set;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ExpressionContext Context
|
||||||
|
{
|
||||||
|
get; set;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Expression Expression
|
||||||
|
{
|
||||||
|
get; set;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string Name
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return "Take";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public int Sort
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
if (this.Context is SqlServerExpressionContext || this.Context.GetType().Name.Contains("Access"))
|
||||||
|
{
|
||||||
|
return 150;
|
||||||
|
}
|
||||||
|
else if (this.Context is OracleExpressionContext)
|
||||||
|
{
|
||||||
|
|
||||||
|
return 401;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return 490;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public string GetValue(Expression expression)
|
||||||
|
{
|
||||||
|
var numExp = (expression as MethodCallExpression).Arguments[0];
|
||||||
|
var num =1;
|
||||||
|
if (ExpressionTool.GetParameters(numExp).Any())
|
||||||
|
{
|
||||||
|
var copyContext = this.Context.GetCopyContextWithMapping();
|
||||||
|
copyContext.IsSingle = false;
|
||||||
|
copyContext.Resolve(numExp, ResolveExpressType.WhereMultiple);
|
||||||
|
copyContext.Result.GetString();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
num = ExpressionTool.DynamicInvoke(numExp).ObjToInt();
|
||||||
|
}
|
||||||
|
var take = (expression as MethodCallExpression);
|
||||||
|
if (this.Context is SqlServerExpressionContext || this.Context.GetType().Name.Contains("Access"))
|
||||||
|
{
|
||||||
|
return "TOP " + num;
|
||||||
|
}
|
||||||
|
else if (this.Context is OracleExpressionContext)
|
||||||
|
{
|
||||||
|
return (HasWhere ? "AND" : "WHERE") + " ROWNUM<=" + num;
|
||||||
|
}
|
||||||
|
else if (this.Context is PostgreSQLExpressionContext || this.Context?.SugarContext?.Context?.CurrentConnectionConfig?.MoreSettings?.DatabaseModel == DbType.PostgreSQL)
|
||||||
|
{
|
||||||
|
return "limit " + num;
|
||||||
|
}
|
||||||
|
else if (this.Context.GetLimit() != null)
|
||||||
|
{
|
||||||
|
return this.Context.GetLimit();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return "limit " + num;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -40,7 +40,8 @@ namespace SqlSugar
|
|||||||
new SubDistinctCount{ Context=Context },
|
new SubDistinctCount{ Context=Context },
|
||||||
new SubToList{ Context=Context},
|
new SubToList{ Context=Context},
|
||||||
new SubFirst(){ Context=Context },
|
new SubFirst(){ Context=Context },
|
||||||
new SubAsWithAttr(){ Context=Context }
|
new SubAsWithAttr(){ Context=Context },
|
||||||
|
new SubTake(){ Context=Context }
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -224,5 +224,10 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Subqueryable<T> Take(int takeNum)
|
||||||
|
{
|
||||||
|
return this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user