Files
SqlSugar/Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/Items/SubWhere.cs

109 lines
3.6 KiB
C#
Raw Normal View History

2017-09-16 01:49:23 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
2019-05-14 17:01:49 +08:00
using System.Text.RegularExpressions;
2017-09-16 01:49:23 +08:00
2017-09-17 00:37:12 +08:00
namespace SqlSugar
2017-09-16 01:49:23 +08:00
{
2020-12-29 13:55:46 +08:00
public class SubWhere : ISubOperation
2017-09-16 01:49:23 +08:00
{
2018-02-09 15:42:49 +08:00
public bool HasWhere
{
get; set;
}
2017-09-16 01:49:23 +08:00
public string Name
{
get { return "Where"; }
}
2017-09-16 16:01:26 +08:00
public Expression Expression
{
get; set;
}
2017-09-16 01:49:23 +08:00
public int Sort
{
get
{
return 400;
}
}
2017-09-17 01:18:56 +08:00
public ExpressionContext Context
{
2020-12-29 13:55:46 +08:00
get; set;
2017-09-17 01:18:56 +08:00
}
public string GetValue(Expression expression)
2017-09-16 01:49:23 +08:00
{
2017-09-16 16:01:26 +08:00
var exp = expression as MethodCallExpression;
2021-12-25 14:25:48 +08:00
if (Regex.Matches( expression.ToString(), "Subqueryable").Count >= 2)
{
new SubSelect() { Context = this.Context }.SetShortName(exp, "+");
}
2020-12-29 13:55:46 +08:00
var argExp = exp.Arguments[0];
2022-11-22 13:24:44 +08:00
var copyContext = this.Context;
if (this.Context.JoinIndex > 0)
{
copyContext = this.Context.GetCopyContextWithMapping();
copyContext.IsSingle = false;
}
var result = "WHERE " + SubTools.GetMethodValue(copyContext, argExp, ResolveExpressType.WhereMultiple);
2019-05-14 17:01:49 +08:00
2022-11-22 13:33:32 +08:00
if (this.Context.JoinIndex > 0)
{
this.Context.Parameters.AddRange(copyContext.Parameters);
2022-11-23 11:35:36 +08:00
this.Context.Index = copyContext.Index;
this.Context.ParameterIndex = copyContext.ParameterIndex;
2022-11-22 13:33:32 +08:00
}
2021-12-25 14:25:48 +08:00
2019-05-14 17:01:49 +08:00
var regex = @"^WHERE (\@Const\d+) $";
2019-06-04 20:01:28 +08:00
if (this.Context is OracleExpressionContext)
{
regex = @"^WHERE (\:Const\d+) $";
}
2020-10-31 20:00:59 +08:00
if (this.Context is DmExpressionContext)
{
regex = @"^WHERE (\:Const\d+) $";
}
2019-05-14 17:01:49 +08:00
if (Regex.IsMatch(result, regex))
{
2021-09-10 18:08:26 +08:00
var value = GetValue(result, regex);
2021-09-10 18:18:41 +08:00
if (value is Expression)
{
var p = this.Context.Parameters.First(it => it.ParameterName == Regex.Match(result, regex).Groups[1].Value);
result = "WHERE " + SubTools.GetMethodValue(Context, value as Expression, ResolveExpressType.WhereMultiple);
argExp = value as Expression;
2021-09-10 18:32:31 +08:00
this.Context.Parameters.Remove(p);
2021-09-10 18:18:41 +08:00
}
else
{
result = "WHERE " + value;
return result;
}
2019-05-14 17:01:49 +08:00
}
2020-12-29 13:55:46 +08:00
var selfParameterName = Context.GetTranslationColumnName((argExp as LambdaExpression).Parameters.First().Name) + UtilConstants.Dot;
2023-01-12 19:35:08 +08:00
if (this.Context.JoinIndex == 0&&result.Contains(" FROM "))
{
2023-02-20 20:45:58 +08:00
this.Context.CurrentShortName= selfParameterName.ObjToString().TrimEnd('.');
2023-01-12 19:35:08 +08:00
}
else if (this.Context.JoinIndex == 0)
2020-12-29 13:55:46 +08:00
result = result.Replace(selfParameterName, SubTools.GetSubReplace(this.Context));
2022-11-05 10:34:22 +08:00
if (!string.IsNullOrEmpty(selfParameterName) && this.Context.IsSingle&& this.Context.JoinIndex == 0)
{
this.Context.CurrentShortName = selfParameterName.TrimEnd('.');
}
2017-09-16 21:27:31 +08:00
return result;
2017-09-16 01:49:23 +08:00
}
2021-09-10 17:59:47 +08:00
private object GetValue(string result, string regex)
{
return this.Context.Parameters.First(it => it.ParameterName == Regex.Match(result, regex).Groups[1].Value).Value;
}
2017-09-16 01:49:23 +08:00
}
}