mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-09-24 13:03:43 +08:00
-
This commit is contained in:
@@ -450,14 +450,14 @@ namespace SqlSugar
|
||||
{ "AddMilliseconds",DateType.Millisecond}
|
||||
};
|
||||
|
||||
private static bool IsContainsArray(MethodCallExpression express, string methodName, bool isValidNativeMethod)
|
||||
private bool IsContainsArray(MethodCallExpression express, string methodName, bool isValidNativeMethod)
|
||||
{
|
||||
return !isValidNativeMethod && express.Method.DeclaringType.Namespace.IsIn("System.Linq", "System.Collections.Generic") && methodName == "Contains";
|
||||
}
|
||||
|
||||
private static bool IsSubMethod(MethodCallExpression express, string methodName)
|
||||
private bool IsSubMethod(MethodCallExpression express, string methodName)
|
||||
{
|
||||
return SubTools.SubItems.Any(it => it.Name == methodName) && express.Object != null && express.Object.Type.Name == "Subqueryable`1";
|
||||
return SubTools.SubItems(this.Context).Any(it => it.Name == methodName) && express.Object != null && express.Object.Type.Name == "Subqueryable`1";
|
||||
}
|
||||
private void CheckMethod(MethodCallExpression expression)
|
||||
{
|
||||
|
@@ -8,9 +8,11 @@ namespace SqlSugar
|
||||
{
|
||||
public interface ISubOperation
|
||||
{
|
||||
ExpressionContext Context { get; set; }
|
||||
string Name { get; }
|
||||
string GetValue(ExpressionContext context, Expression expression);
|
||||
string GetValue(Expression expression);
|
||||
int Sort { get; }
|
||||
Expression Expression { get; set; }
|
||||
|
||||
}
|
||||
}
|
||||
|
@@ -26,12 +26,17 @@ namespace SqlSugar
|
||||
}
|
||||
}
|
||||
|
||||
public string GetValue(ExpressionContext context, Expression expression)
|
||||
public ExpressionContext Context
|
||||
{
|
||||
get;set;
|
||||
}
|
||||
|
||||
public string GetValue(Expression expression)
|
||||
{
|
||||
var exp = expression as MethodCallExpression;
|
||||
var argExp = exp.Arguments[0];
|
||||
var result = "AND " + SubTools.GetMethodValue(context, argExp, ResolveExpressType.WhereMultiple);
|
||||
var selfParameterName = context.GetTranslationColumnName((argExp as LambdaExpression).Parameters.First().Name) + UtilConstants.Dot;
|
||||
var result = "AND " + SubTools.GetMethodValue(this.Context, argExp, ResolveExpressType.WhereMultiple);
|
||||
var selfParameterName = this.Context.GetTranslationColumnName((argExp as LambdaExpression).Parameters.First().Name) + UtilConstants.Dot;
|
||||
result = result.Replace(selfParameterName, string.Empty);
|
||||
return result;
|
||||
}
|
||||
|
@@ -8,6 +8,11 @@ namespace SqlSugar
|
||||
{
|
||||
public class SubAny : ISubOperation
|
||||
{
|
||||
public ExpressionContext Context
|
||||
{
|
||||
get;set;
|
||||
}
|
||||
|
||||
public Expression Expression
|
||||
{
|
||||
get;set;
|
||||
@@ -29,7 +34,7 @@ namespace SqlSugar
|
||||
}
|
||||
}
|
||||
|
||||
public string GetValue(ExpressionContext context, Expression expression)
|
||||
public string GetValue(Expression expression)
|
||||
{
|
||||
return "EXISTS";
|
||||
}
|
||||
|
@@ -29,7 +29,12 @@ namespace SqlSugar
|
||||
}
|
||||
}
|
||||
|
||||
public string GetValue(ExpressionContext context, Expression expression)
|
||||
public ExpressionContext Context
|
||||
{
|
||||
get;set;
|
||||
}
|
||||
|
||||
public string GetValue(Expression expression)
|
||||
{
|
||||
return "SELECT";
|
||||
}
|
||||
|
@@ -28,12 +28,18 @@ namespace SqlSugar
|
||||
return 300;
|
||||
}
|
||||
}
|
||||
public string GetValue(ExpressionContext context, Expression expression)
|
||||
|
||||
public ExpressionContext Context
|
||||
{
|
||||
get;set;
|
||||
}
|
||||
|
||||
public string GetValue(Expression expression)
|
||||
{
|
||||
var exp = expression as MethodCallExpression;
|
||||
var resType = exp.Method.ReturnType;
|
||||
var name = resType.GetGenericArguments().First().Name;
|
||||
return "FROM "+context.GetTranslationTableName(name, true);
|
||||
return "FROM "+this.Context.GetTranslationTableName(name, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -8,6 +8,11 @@ namespace SqlSugar
|
||||
{
|
||||
public class SubLeftBracket : ISubOperation
|
||||
{
|
||||
public ExpressionContext Context
|
||||
{
|
||||
get;set;
|
||||
}
|
||||
|
||||
public Expression Expression
|
||||
{
|
||||
get;set;
|
||||
@@ -29,7 +34,7 @@ namespace SqlSugar
|
||||
}
|
||||
}
|
||||
|
||||
public string GetValue(ExpressionContext context, Expression expression)
|
||||
public string GetValue(Expression expression)
|
||||
{
|
||||
return "(";
|
||||
}
|
||||
|
@@ -8,6 +8,11 @@ namespace SqlSugar
|
||||
{
|
||||
public class SubNotAny : ISubOperation
|
||||
{
|
||||
public ExpressionContext Context
|
||||
{
|
||||
get;set;
|
||||
}
|
||||
|
||||
public Expression Expression
|
||||
{
|
||||
get; set;
|
||||
@@ -29,7 +34,7 @@ namespace SqlSugar
|
||||
}
|
||||
}
|
||||
|
||||
public string GetValue(ExpressionContext context, Expression expression)
|
||||
public string GetValue(Expression expression)
|
||||
{
|
||||
return "NOT EXISTS";
|
||||
}
|
||||
|
@@ -8,6 +8,11 @@ namespace SqlSugar
|
||||
{
|
||||
public class SubRightBracket : ISubOperation
|
||||
{
|
||||
public ExpressionContext Context
|
||||
{
|
||||
get;set;
|
||||
}
|
||||
|
||||
public Expression Expression
|
||||
{
|
||||
get;set;
|
||||
@@ -29,7 +34,7 @@ namespace SqlSugar
|
||||
}
|
||||
}
|
||||
|
||||
public string GetValue(ExpressionContext context, Expression expression)
|
||||
public string GetValue(Expression expression)
|
||||
{
|
||||
return ")";
|
||||
}
|
||||
|
@@ -30,10 +30,15 @@ namespace SqlSugar
|
||||
}
|
||||
}
|
||||
|
||||
public string GetValue(ExpressionContext context, Expression expression = null)
|
||||
public ExpressionContext Context
|
||||
{
|
||||
get;set;
|
||||
}
|
||||
|
||||
public string GetValue(Expression expression = null)
|
||||
{
|
||||
var exp = expression as MethodCallExpression;
|
||||
return SubTools.GetMethodValue(context, exp.Arguments[0],ResolveExpressType.FieldSingle);
|
||||
return SubTools.GetMethodValue(this.Context, exp.Arguments[0],ResolveExpressType.FieldSingle);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -8,6 +8,11 @@ namespace SqlSugar
|
||||
{
|
||||
public class SubSelectDefault : ISubOperation
|
||||
{
|
||||
public ExpressionContext Context
|
||||
{
|
||||
get;set;
|
||||
}
|
||||
|
||||
public Expression Expression
|
||||
{
|
||||
get;set;
|
||||
@@ -28,7 +33,7 @@ namespace SqlSugar
|
||||
}
|
||||
}
|
||||
|
||||
public string GetValue(ExpressionContext context, Expression expression)
|
||||
public string GetValue(Expression expression)
|
||||
{
|
||||
return "*";
|
||||
}
|
||||
|
@@ -0,0 +1,56 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class SubTop : ISubOperation
|
||||
{
|
||||
public ExpressionContext Context
|
||||
{
|
||||
get;set;
|
||||
}
|
||||
|
||||
public Expression Expression
|
||||
{
|
||||
get;set;
|
||||
}
|
||||
|
||||
public string Name
|
||||
{
|
||||
get
|
||||
{
|
||||
return "Top";
|
||||
}
|
||||
}
|
||||
|
||||
public int Sort
|
||||
{
|
||||
get
|
||||
{
|
||||
if (this.Context is SqlServerExpressionContext)
|
||||
{
|
||||
return 150;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 450;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public string GetValue(Expression expression)
|
||||
{
|
||||
if (this.Context is SqlServerExpressionContext)
|
||||
{
|
||||
return "TOP 1";
|
||||
}
|
||||
else {
|
||||
return "limit 0,1";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -26,12 +26,17 @@ namespace SqlSugar
|
||||
}
|
||||
}
|
||||
|
||||
public string GetValue(ExpressionContext context, Expression expression)
|
||||
public ExpressionContext Context
|
||||
{
|
||||
get;set;
|
||||
}
|
||||
|
||||
public string GetValue(Expression expression)
|
||||
{
|
||||
var exp = expression as MethodCallExpression;
|
||||
var argExp= exp.Arguments[0];
|
||||
var result= "WHERE "+SubTools.GetMethodValue(context, argExp, ResolveExpressType.WhereMultiple);
|
||||
var selfParameterName =context.GetTranslationColumnName((argExp as LambdaExpression).Parameters.First().Name)+UtilConstants.Dot;
|
||||
var result= "WHERE "+SubTools.GetMethodValue(Context, argExp, ResolveExpressType.WhereMultiple);
|
||||
var selfParameterName = Context.GetTranslationColumnName((argExp as LambdaExpression).Parameters.First().Name)+UtilConstants.Dot;
|
||||
result = result.Replace(selfParameterName,string.Empty);
|
||||
return result;
|
||||
}
|
||||
|
@@ -52,19 +52,25 @@ namespace SqlSugar
|
||||
var isubList = this.allMethods.Select(exp =>
|
||||
{
|
||||
var methodName = exp.Method.Name;
|
||||
var item = SubTools.SubItems.First(s => s.Name == methodName);
|
||||
var items = SubTools.SubItems(this.context);
|
||||
var item = items.First(s => s.Name == methodName);
|
||||
if (item is SubWhere && hasWhere == false)
|
||||
{
|
||||
hasWhere = true;
|
||||
}
|
||||
else if(item is SubWhere)
|
||||
else if (item is SubWhere)
|
||||
{
|
||||
item = SubTools.SubItems.First(s => s is SubAnd);
|
||||
item = items.First(s => s is SubAnd);
|
||||
}
|
||||
item.Context = this.context;
|
||||
item.Expression = exp;
|
||||
return item;
|
||||
}).ToList();
|
||||
isubList.Insert(0, new SubBegin());
|
||||
if (isubList.Any(it => it is SubSelect))
|
||||
{
|
||||
isubList.Add(new SubTop() { Context=this.context });
|
||||
}
|
||||
if (isubList.Any(it => it is SubAny || it is SubNotAny))
|
||||
{
|
||||
isubList.Add(new SubLeftBracket());
|
||||
@@ -74,7 +80,7 @@ namespace SqlSugar
|
||||
isubList = isubList.OrderBy(it => it.Sort).ToList();
|
||||
List<string> result = isubList.Select(it =>
|
||||
{
|
||||
return it.GetValue(this.context, it.Expression);
|
||||
return it.GetValue(it.Expression);
|
||||
}).ToList();
|
||||
return result;
|
||||
}
|
||||
|
@@ -8,24 +8,28 @@ namespace SqlSugar
|
||||
{
|
||||
public class SubTools
|
||||
{
|
||||
public static List<ISubOperation> SubItems = new List<ISubOperation>()
|
||||
public static List<ISubOperation> SubItems(ExpressionContext Context)
|
||||
{
|
||||
new SubSelect(),
|
||||
new SubWhere(),
|
||||
new SubAnd(),
|
||||
new SubAny(),
|
||||
new SubNotAny(),
|
||||
new SubBegin(),
|
||||
new SubFromTable()
|
||||
};
|
||||
|
||||
public static string GetMethodValue(ExpressionContext context,Expression item,ResolveExpressType type)
|
||||
return new List<ISubOperation>()
|
||||
{
|
||||
new SubSelect() { Context=Context },
|
||||
new SubWhere(){ Context=Context },
|
||||
new SubAnd(){ Context=Context },
|
||||
new SubAny(){ Context=Context },
|
||||
new SubNotAny(){ Context=Context },
|
||||
new SubBegin(){ Context=Context },
|
||||
new SubFromTable(){ Context=Context }
|
||||
};
|
||||
}
|
||||
|
||||
public static string GetMethodValue(ExpressionContext context, Expression item, ResolveExpressType type)
|
||||
{
|
||||
var newContext = context.GetCopyContext();
|
||||
newContext.MappingColumns = context.MappingColumns;
|
||||
newContext.MappingTables = context.MappingTables;
|
||||
newContext.IgnoreComumnList = context.IgnoreComumnList;
|
||||
newContext.Resolve(item,type);
|
||||
newContext.Resolve(item, type);
|
||||
context.Index = newContext.Index;
|
||||
context.ParameterIndex = newContext.ParameterIndex;
|
||||
return newContext.Result.GetResultString();
|
||||
|
@@ -82,6 +82,7 @@
|
||||
<Compile Include="ExpressionsToSql\Subquery\Items\SubSelect.cs" />
|
||||
<Compile Include="ExpressionsToSql\Subquery\Items\SubSelectDefault.cs" />
|
||||
<Compile Include="ExpressionsToSql\Subquery\Items\SubNotAny.cs" />
|
||||
<Compile Include="ExpressionsToSql\Subquery\Items\SubTop.cs" />
|
||||
<Compile Include="ExpressionsToSql\Subquery\Subquerable.cs" />
|
||||
<Compile Include="ExpressionsToSql\Subquery\Items\SubWhere.cs" />
|
||||
<Compile Include="ExpressionsToSql\Subquery\SubResolve.cs" />
|
||||
|
Reference in New Issue
Block a user