Subquery.Take

This commit is contained in:
sunkaixuan 2025-05-18 19:20:55 +08:00
parent 79ac1f4ca6
commit 6fffa23973
3 changed files with 100 additions and 1 deletions

View File

@ -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;
}
}
}
}

View File

@ -40,7 +40,8 @@ namespace SqlSugar
new SubDistinctCount{ Context=Context },
new SubToList{ Context=Context},
new SubFirst(){ Context=Context },
new SubAsWithAttr(){ Context=Context }
new SubAsWithAttr(){ Context=Context },
new SubTake(){ Context=Context }
};
}

View File

@ -224,5 +224,10 @@ namespace SqlSugar
{
return this;
}
public Subqueryable<T> Take(int takeNum)
{
return this;
}
}
}