mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-09-18 17:48:11 +08:00
Supquery support As
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class SubAs : ISubOperation
|
||||
{
|
||||
public bool HasWhere
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
|
||||
public string Name
|
||||
{
|
||||
get
|
||||
{
|
||||
return "AS";
|
||||
}
|
||||
}
|
||||
|
||||
public Expression Expression
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
|
||||
|
||||
public int Sort
|
||||
{
|
||||
get
|
||||
{
|
||||
return 200;
|
||||
}
|
||||
}
|
||||
|
||||
public ExpressionContext Context
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
|
||||
public string GetValue(Expression expression = null)
|
||||
{
|
||||
var exp = expression as MethodCallExpression;
|
||||
var expString= SubTools.GetMethodValue(this.Context, exp.Arguments[0], ResolveExpressType.WhereSingle)?.Trim();
|
||||
var result = this.Context.Parameters.First(it => it.ParameterName == expString).Value+"";
|
||||
return "$SubAs:"+result;
|
||||
}
|
||||
}
|
||||
}
|
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
@@ -16,6 +17,7 @@ namespace SqlSugar
|
||||
{
|
||||
List<MethodCallExpression> allMethods = new List<MethodCallExpression>();
|
||||
private ExpressionContext context = null;
|
||||
private string subKey = "$SubAs:";
|
||||
private bool hasWhere;
|
||||
public SubResolve(MethodCallExpression expression, ExpressionContext context, Expression oppsiteExpression)
|
||||
{
|
||||
@@ -85,14 +87,32 @@ namespace SqlSugar
|
||||
currentExpression = addItem;
|
||||
}
|
||||
}
|
||||
|
||||
public string GetSql()
|
||||
{
|
||||
List<string> subItems = GetSubItems();
|
||||
var sql = string.Join(UtilConstants.Space, subItems);
|
||||
var sqlItems = subItems.Where(it => !it.StartsWith(subKey)).ToList();
|
||||
var asItems = subItems.Where(it => it.StartsWith(subKey)).ToList();
|
||||
if (asItems.Any())
|
||||
{
|
||||
GetSubAs(sqlItems, asItems);
|
||||
}
|
||||
var sql = string.Join(UtilConstants.Space, sqlItems);
|
||||
return this.context.DbMehtods.Pack(sql);
|
||||
}
|
||||
|
||||
private void GetSubAs(List<string> sqlItems, List<string> asItems)
|
||||
{
|
||||
for (int i = 0; i < sqlItems.Count; i++)
|
||||
{
|
||||
if (sqlItems[i].StartsWith("FROM " + this.context.SqlTranslationLeft))
|
||||
{
|
||||
var asName = this.context.GetTranslationTableName(asItems.First().Replace(subKey, ""), false);
|
||||
var repKey = $"\\{this.context.SqlTranslationLeft}.+\\{this.context.SqlTranslationRight}";
|
||||
sqlItems[i] = Regex.Replace(sqlItems[i], repKey, asName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private List<string> GetSubItems()
|
||||
{
|
||||
var isubList = this.allMethods.Select(exp =>
|
||||
|
@@ -31,7 +31,8 @@ namespace SqlSugar
|
||||
new SubAvg(){ Context=Context },
|
||||
new SubOrderBy(){ Context=Context },
|
||||
new SubOrderByDesc(){ Context=Context },
|
||||
new SubGroupBy(){ Context=Context}
|
||||
new SubGroupBy(){ Context=Context},
|
||||
new SubAs(){Context=Context}
|
||||
};
|
||||
}
|
||||
|
||||
|
@@ -10,6 +10,10 @@ namespace SqlSugar
|
||||
public class Subqueryable<T> where T : class, new()
|
||||
{
|
||||
|
||||
public Subqueryable<T> AS(string tableName)
|
||||
{
|
||||
return this;
|
||||
}
|
||||
public Subqueryable<T> InnerJoin<JoinType>(Func<T, JoinType, bool> expression)
|
||||
{
|
||||
return this;
|
||||
|
@@ -88,6 +88,7 @@
|
||||
<Compile Include="Abstract\FilterProvider\FilterProvider.cs" />
|
||||
<Compile Include="Abstract\InsertableProvider\InsertableProvider.cs" />
|
||||
<Compile Include="Abstract\DeleteProvider\DeleteableProvider.cs" />
|
||||
<Compile Include="ExpressionsToSql\Subquery\Items\SubAs.cs" />
|
||||
<Compile Include="Interface\IParameterInsertable.cs" />
|
||||
<Compile Include="Abstract\InsertableProvider\ParameterInsertable.cs" />
|
||||
<Compile Include="Abstract\Reportable\ReportableProvider.cs" />
|
||||
|
Reference in New Issue
Block a user