mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-10-15 18:55:07 +08:00
Synchronization code
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class MapperSql
|
||||
{
|
||||
public string Sql { get; set; }
|
||||
}
|
||||
|
||||
public class MapperExpressionInfo
|
||||
{
|
||||
public Type Type { get; set; }
|
||||
public EntityInfo EntityInfo { get; set; }
|
||||
public string FieldName { get; set; }
|
||||
public string FieldString { get; set; }
|
||||
}
|
||||
}
|
@@ -6,16 +6,11 @@ using System.Reflection;
|
||||
using System.Text;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class BaseResolve
|
||||
/// <summary>
|
||||
/// BaseResolve-Append
|
||||
/// </summary>
|
||||
public partial class BaseResolve
|
||||
{
|
||||
#region Property
|
||||
protected Expression Expression { get; set; }
|
||||
protected Expression ExactExpression { get; set; }
|
||||
public ExpressionContext Context { get; set; }
|
||||
public bool? IsLeft { get; set; }
|
||||
public int ContentIndex { get { return this.Context.Index; } }
|
||||
public int Index { get; set; }
|
||||
public ExpressionParameter BaseParameter { get; set; }
|
||||
|
||||
private BaseResolve()
|
||||
{
|
||||
@@ -30,17 +25,9 @@ namespace SqlSugar
|
||||
|
||||
public BaseResolve Start()
|
||||
{
|
||||
Context.Index++;
|
||||
Expression expression = this.Expression;
|
||||
ExpressionParameter parameter = new ExpressionParameter()
|
||||
{
|
||||
Context = this.Context,
|
||||
CurrentExpression = expression,
|
||||
IsLeft = this.IsLeft,
|
||||
BaseExpression = this.ExactExpression,
|
||||
BaseParameter = this.BaseParameter,
|
||||
Index = Context.Index
|
||||
};
|
||||
Expression expression;
|
||||
ExpressionParameter parameter;
|
||||
SetParameter(out expression, out parameter);
|
||||
if (expression is LambdaExpression)
|
||||
{
|
||||
return new LambdaExpressionResolve(parameter);
|
||||
@@ -111,842 +98,5 @@ namespace SqlSugar
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Append
|
||||
protected void AppendMember(ExpressionParameter parameter, bool? isLeft, object appendValue)
|
||||
{
|
||||
|
||||
Context.ParameterIndex++;
|
||||
if (isLeft == true)
|
||||
{
|
||||
appendValue += ExpressionConst.ExpressionReplace + parameter.BaseParameter.Index;
|
||||
}
|
||||
if (this.Context.Result.Contains(ExpressionConst.FormatSymbol))
|
||||
{
|
||||
this.Context.Result.Replace(ExpressionConst.FormatSymbol, appendValue.ObjToString());
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Context.Result.Append(appendValue);
|
||||
}
|
||||
}
|
||||
protected void AppendValue(ExpressionParameter parameter, bool? isLeft, object value)
|
||||
{
|
||||
if (parameter.BaseExpression is BinaryExpression || parameter.BaseExpression == null)
|
||||
{
|
||||
var oppoSiteExpression = isLeft == true ? parameter.BaseParameter.RightExpression : parameter.BaseParameter.LeftExpression;
|
||||
|
||||
if (value is MapperSql)
|
||||
{
|
||||
var sql = ((MapperSql)value).Sql;
|
||||
if (isLeft == true)
|
||||
{
|
||||
sql += ExpressionConst.ExpressionReplace + parameter.BaseParameter.Index;
|
||||
}
|
||||
if (this.Context.Result.Contains(ExpressionConst.FormatSymbol))
|
||||
{
|
||||
this.Context.Result.Replace(ExpressionConst.FormatSymbol, sql);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Context.Result.Append(sql);
|
||||
}
|
||||
}
|
||||
else if (parameter.CurrentExpression is MethodCallExpression || parameter.CurrentExpression is ConditionalExpression || parameter.CurrentExpression.NodeType == ExpressionType.Coalesce)
|
||||
{
|
||||
var appendValue = value;
|
||||
if (this.Context.Result.Contains(ExpressionConst.FormatSymbol))
|
||||
{
|
||||
this.Context.Result.Replace(ExpressionConst.FormatSymbol, appendValue.ObjToString());
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Context.Result.Append(appendValue);
|
||||
}
|
||||
this.AppendOpreator(parameter, isLeft);
|
||||
}
|
||||
else if (oppoSiteExpression is MemberExpression)
|
||||
{
|
||||
string appendValue = Context.SqlParameterKeyWord
|
||||
+ ((MemberExpression)oppoSiteExpression).Member.Name
|
||||
+ Context.ParameterIndex;
|
||||
if (IsNullValue(parameter, value))
|
||||
{
|
||||
appendValue = $" NULL ";
|
||||
parameter.BaseParameter.ValueIsNull = true;
|
||||
}
|
||||
else if (value.ObjToString() != "NULL" && !parameter.ValueIsNull)
|
||||
{
|
||||
EntityColumnInfo columnInfo = GetColumnInfo(oppoSiteExpression);
|
||||
if (columnInfo != null && columnInfo.SqlParameterDbType != null && columnInfo.SqlParameterDbType is System.Data.DbType)
|
||||
{
|
||||
var p = new SugarParameter(appendValue, value, (System.Data.DbType)columnInfo.SqlParameterDbType);
|
||||
if (columnInfo.SqlParameterSize != null)
|
||||
{
|
||||
p.Size = columnInfo.SqlParameterSize.ObjToInt();
|
||||
}
|
||||
this.Context.Parameters.Add(p);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Context.Parameters.Add(new SugarParameter(appendValue, value));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
appendValue = value.ObjToString();
|
||||
}
|
||||
Context.ParameterIndex++;
|
||||
appendValue = string.Format(" {0} ", appendValue);
|
||||
if (isLeft == true)
|
||||
{
|
||||
appendValue += ExpressionConst.ExpressionReplace + parameter.BaseParameter.Index;
|
||||
}
|
||||
if (this.Context.Result.Contains(ExpressionConst.FormatSymbol))
|
||||
{
|
||||
this.Context.Result.Replace(ExpressionConst.FormatSymbol, appendValue);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Context.Result.Append(appendValue);
|
||||
}
|
||||
}
|
||||
else if ((oppoSiteExpression is UnaryExpression && (oppoSiteExpression as UnaryExpression).Operand is MemberExpression))
|
||||
{
|
||||
string appendValue = Context.SqlParameterKeyWord
|
||||
+ ((MemberExpression)(oppoSiteExpression as UnaryExpression).Operand).Member.Name
|
||||
+ Context.ParameterIndex;
|
||||
if (value.ObjToString() != "NULL" && !parameter.ValueIsNull)
|
||||
{
|
||||
value = this.Context.TableEnumIsString == true ? value.ToString() : value;
|
||||
this.Context.Parameters.Add(new SugarParameter(appendValue, value));
|
||||
}
|
||||
else
|
||||
{
|
||||
appendValue = value.ObjToString();
|
||||
}
|
||||
Context.ParameterIndex++;
|
||||
appendValue = string.Format(" {0} ", appendValue);
|
||||
if (isLeft == true)
|
||||
{
|
||||
appendValue += ExpressionConst.ExpressionReplace + parameter.BaseParameter.Index;
|
||||
}
|
||||
if (this.Context.Result.Contains(ExpressionConst.FormatSymbol))
|
||||
{
|
||||
this.Context.Result.Replace(ExpressionConst.FormatSymbol, appendValue);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Context.Result.Append(appendValue);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var appendValue = this.Context.SqlParameterKeyWord + ExpressionConst.Const + Context.ParameterIndex;
|
||||
Context.ParameterIndex++;
|
||||
if (value != null && value.GetType().IsEnum())
|
||||
{
|
||||
if (this.Context.TableEnumIsString == true)
|
||||
{
|
||||
value = value.ToString();
|
||||
}
|
||||
else
|
||||
{
|
||||
value = Convert.ToInt64(value);
|
||||
}
|
||||
}
|
||||
this.Context.Parameters.Add(new SugarParameter(appendValue, value));
|
||||
appendValue = string.Format(" {0} ", appendValue);
|
||||
if (isLeft == true)
|
||||
{
|
||||
appendValue += ExpressionConst.ExpressionReplace + parameter.BaseParameter.Index;
|
||||
}
|
||||
if (this.Context.Result.Contains(ExpressionConst.FormatSymbol))
|
||||
{
|
||||
this.Context.Result.Replace(ExpressionConst.FormatSymbol, appendValue);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Context.Result.Append(appendValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
protected void AppendOpreator(ExpressionParameter parameter, bool? isLeft)
|
||||
{
|
||||
if (isLeft == true)
|
||||
{
|
||||
this.Context.Result.Append(" " + ExpressionConst.ExpressionReplace + parameter.BaseParameter.Index);
|
||||
}
|
||||
}
|
||||
protected string AppendParameter(object paramterValue)
|
||||
{
|
||||
string parameterName = this.Context.SqlParameterKeyWord + "constant" + this.Context.ParameterIndex;
|
||||
this.Context.ParameterIndex++; ;
|
||||
this.Context.Parameters.Add(new SugarParameter(parameterName, paramterValue));
|
||||
return parameterName;
|
||||
}
|
||||
protected void AppendNot(object Value)
|
||||
{
|
||||
var isAppend = !this.Context.Result.Contains(ExpressionConst.FormatSymbol);
|
||||
var lastCharIsSpace = this.Context.Result.LastCharIsSpace;
|
||||
if (isAppend)
|
||||
{
|
||||
this.Context.Result.Append(lastCharIsSpace ? "NOT" : " NOT");
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Context.Result.Replace(ExpressionConst.FormatSymbol, "NOT");
|
||||
}
|
||||
}
|
||||
protected void AppendNegate(object Value)
|
||||
{
|
||||
var isAppend = !this.Context.Result.Contains(ExpressionConst.FormatSymbol);
|
||||
var lastCharIsSpace = this.Context.Result.LastCharIsSpace;
|
||||
if (isAppend)
|
||||
{
|
||||
this.Context.Result.Append(lastCharIsSpace ? "-" : " -");
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Context.Result.Replace(ExpressionConst.FormatSymbol, "-");
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region New Expression
|
||||
|
||||
public string GetNewExpressionValue(Expression item)
|
||||
{
|
||||
var newContext = this.Context.GetCopyContextWithMapping();
|
||||
newContext.SugarContext = this.Context.SugarContext;
|
||||
newContext.Resolve(item, this.Context.IsJoin ? ResolveExpressType.WhereMultiple : ResolveExpressType.WhereSingle);
|
||||
this.Context.Index = newContext.Index;
|
||||
this.Context.ParameterIndex = newContext.ParameterIndex;
|
||||
if (newContext.Parameters.HasValue())
|
||||
{
|
||||
this.Context.Parameters.AddRange(newContext.Parameters);
|
||||
}
|
||||
if (this.Context.SingleTableNameSubqueryShortName == "Subqueryable()")
|
||||
{
|
||||
this.Context.SingleTableNameSubqueryShortName = newContext.SingleTableNameSubqueryShortName;
|
||||
}
|
||||
else if (newContext.SingleTableNameSubqueryShortName!=null&& newContext.Result !=null && newContext.Result.Contains(this.Context.SqlTranslationLeft+ newContext.SingleTableNameSubqueryShortName+ this.Context.SqlTranslationRight))
|
||||
{
|
||||
this.Context.SingleTableNameSubqueryShortName = newContext.SingleTableNameSubqueryShortName;
|
||||
}
|
||||
return newContext.Result.GetResultString();
|
||||
}
|
||||
public string GetNewExpressionValue(Expression item, ResolveExpressType type)
|
||||
{
|
||||
var newContext = this.Context.GetCopyContextWithMapping();
|
||||
newContext.SugarContext = this.Context.SugarContext;
|
||||
newContext.Resolve(item, type);
|
||||
this.Context.Index = newContext.Index;
|
||||
this.Context.ParameterIndex = newContext.ParameterIndex;
|
||||
if (newContext.Parameters.HasValue())
|
||||
{
|
||||
this.Context.Parameters.AddRange(newContext.Parameters);
|
||||
}
|
||||
return newContext.Result.GetResultString();
|
||||
}
|
||||
protected void ResolveNewExpressions(ExpressionParameter parameter, Expression item, string asName)
|
||||
{
|
||||
if (item is ConstantExpression)
|
||||
{
|
||||
this.Expression = item;
|
||||
this.Start();
|
||||
string parameterName = this.Context.SqlParameterKeyWord + "constant" + this.Context.ParameterIndex;
|
||||
this.Context.ParameterIndex++;
|
||||
parameter.Context.Result.Append(this.Context.GetAsString(asName, parameterName));
|
||||
this.Context.Parameters.Add(new SugarParameter(parameterName, parameter.CommonTempData));
|
||||
}
|
||||
else if ((item is MemberExpression) && ((MemberExpression)item).Expression == null)
|
||||
{
|
||||
var paramterValue = ExpressionTool.GetPropertyValue(item as MemberExpression);
|
||||
string parameterName = this.Context.SqlParameterKeyWord + "constant" + this.Context.ParameterIndex;
|
||||
this.Context.ParameterIndex++;
|
||||
parameter.Context.Result.Append(this.Context.GetAsString(asName, parameterName));
|
||||
this.Context.Parameters.Add(new SugarParameter(parameterName, paramterValue));
|
||||
}
|
||||
else if ((item is MemberExpression) && ((MemberExpression)item).Expression.NodeType == ExpressionType.Constant)
|
||||
{
|
||||
this.Expression = item;
|
||||
this.Start();
|
||||
string parameterName = this.Context.SqlParameterKeyWord + "constant" + this.Context.ParameterIndex;
|
||||
this.Context.ParameterIndex++;
|
||||
parameter.Context.Result.Append(this.Context.GetAsString(asName, parameterName));
|
||||
this.Context.Parameters.Add(new SugarParameter(parameterName, parameter.CommonTempData));
|
||||
}
|
||||
else if (item is MemberExpression)
|
||||
{
|
||||
if (this.Context.Result.IsLockCurrentParameter == false)
|
||||
{
|
||||
this.Context.Result.CurrentParameter = parameter;
|
||||
this.Context.Result.IsLockCurrentParameter = true;
|
||||
parameter.IsAppendTempDate();
|
||||
this.Expression = item;
|
||||
if (IsBoolValue(item))
|
||||
{
|
||||
this.Expression = (item as MemberExpression).Expression;
|
||||
}
|
||||
this.Start();
|
||||
parameter.IsAppendResult();
|
||||
this.Context.Result.Append(this.Context.GetAsString(asName, parameter.CommonTempData.ObjToString()));
|
||||
this.Context.Result.CurrentParameter = null;
|
||||
}
|
||||
}
|
||||
else if (item is UnaryExpression && ((UnaryExpression)item).Operand is MemberExpression)
|
||||
{
|
||||
if (this.Context.Result.IsLockCurrentParameter == false)
|
||||
{
|
||||
var expression = ((UnaryExpression)item).Operand as MemberExpression;
|
||||
var isDateTimeNow = ((UnaryExpression)item).Operand.ToString() == "DateTime.Now";
|
||||
if (expression.Expression == null && !isDateTimeNow)
|
||||
{
|
||||
this.Context.Result.CurrentParameter = parameter;
|
||||
this.Context.Result.IsLockCurrentParameter = true;
|
||||
parameter.IsAppendTempDate();
|
||||
this.Expression = item;
|
||||
this.Start();
|
||||
parameter.IsAppendResult();
|
||||
this.Context.Result.Append(this.Context.GetAsString(asName, parameter.CommonTempData.ObjToString()));
|
||||
this.Context.Result.CurrentParameter = null;
|
||||
}
|
||||
else if (expression.Expression is ConstantExpression || isDateTimeNow)
|
||||
{
|
||||
string parameterName = this.Context.SqlParameterKeyWord + "constant" + this.Context.ParameterIndex;
|
||||
this.Context.ParameterIndex++;
|
||||
parameter.Context.Result.Append(this.Context.GetAsString(asName, parameterName));
|
||||
this.Context.Parameters.Add(new SugarParameter(parameterName, ExpressionTool.GetMemberValue(expression.Member, expression)));
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Context.Result.CurrentParameter = parameter;
|
||||
this.Context.Result.IsLockCurrentParameter = true;
|
||||
parameter.IsAppendTempDate();
|
||||
this.Expression = expression;
|
||||
this.Start();
|
||||
parameter.IsAppendResult();
|
||||
this.Context.Result.Append(this.Context.GetAsString(asName, parameter.CommonTempData.ObjToString()));
|
||||
this.Context.Result.CurrentParameter = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (item is UnaryExpression && ((UnaryExpression)item).Operand is ConstantExpression)
|
||||
{
|
||||
if (this.Context.Result.IsLockCurrentParameter == false)
|
||||
{
|
||||
this.Expression = ((UnaryExpression)item).Operand;
|
||||
this.Start();
|
||||
string parameterName = this.Context.SqlParameterKeyWord + "constant" + this.Context.ParameterIndex;
|
||||
this.Context.ParameterIndex++;
|
||||
parameter.Context.Result.Append(this.Context.GetAsString(asName, parameterName));
|
||||
this.Context.Parameters.Add(new SugarParameter(parameterName, parameter.CommonTempData));
|
||||
}
|
||||
}
|
||||
else if (item is BinaryExpression)
|
||||
{
|
||||
if (this.Context.Result.IsLockCurrentParameter == false)
|
||||
{
|
||||
var newContext = this.Context.GetCopyContextWithMapping();
|
||||
var resolveExpressType = this.Context.IsSingle ? ResolveExpressType.WhereSingle : ResolveExpressType.WhereMultiple;
|
||||
newContext.Resolve(item, resolveExpressType);
|
||||
this.Context.Index = newContext.Index;
|
||||
this.Context.ParameterIndex = newContext.ParameterIndex;
|
||||
if (newContext.Parameters.HasValue())
|
||||
{
|
||||
this.Context.Parameters.AddRange(newContext.Parameters);
|
||||
}
|
||||
this.Context.Result.Append(this.Context.GetAsString(asName, newContext.Result.GetString()));
|
||||
this.Context.Result.CurrentParameter = null;
|
||||
if (this.Context.SingleTableNameSubqueryShortName.IsNullOrEmpty() && newContext.SingleTableNameSubqueryShortName.HasValue())
|
||||
{
|
||||
this.Context.SingleTableNameSubqueryShortName = newContext.SingleTableNameSubqueryShortName;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (item.Type.IsClass())
|
||||
{
|
||||
var mappingKeys = GetMappingColumns(parameter.CurrentExpression);
|
||||
var isSameType = mappingKeys.Keys.Count > 0;
|
||||
CallContextThread<Dictionary<string, string>>.SetData("Exp_Select_Mapping_Key", mappingKeys);
|
||||
CallContextAsync<Dictionary<string, string>>.SetData("Exp_Select_Mapping_Key", mappingKeys);
|
||||
this.Expression = item;
|
||||
if (this.Context.IsJoin&& (item is MemberInitExpression|| item is NewExpression))
|
||||
{
|
||||
List<NewExpressionInfo> newExpressionInfos = new List<NewExpressionInfo>();
|
||||
if (item is MemberInitExpression)
|
||||
{
|
||||
newExpressionInfos = ExpressionTool.GetNewexpressionInfos(item,this.Context,this);
|
||||
}
|
||||
else
|
||||
{
|
||||
newExpressionInfos = ExpressionTool.GetNewDynamicexpressionInfos(item, this.Context,this);
|
||||
}
|
||||
foreach (NewExpressionInfo newExpressionInfo in newExpressionInfos)
|
||||
{
|
||||
//var property=item.Type.GetProperties().Where(it => it.Name == newExpressionInfo.l).First();
|
||||
//asName = GetAsName(item, newExpressionInfo.ShortName, property);
|
||||
if (newExpressionInfo.Type == nameof(ConstantExpression))
|
||||
{
|
||||
parameter.Context.Result.Append(
|
||||
newExpressionInfo.RightDbName +" AS "+
|
||||
this.Context.SqlTranslationLeft + asName + "." + newExpressionInfo.LeftNameName + this.Context.SqlTranslationRight
|
||||
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
parameter.Context.Result.Append(this.Context.GetAsString(
|
||||
this.Context.SqlTranslationLeft + asName + "." + newExpressionInfo.LeftNameName + this.Context.SqlTranslationRight,
|
||||
newExpressionInfo.ShortName + "." + newExpressionInfo.RightDbName
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (!this.Context.IsJoin && (item is MemberInitExpression || item is NewExpression))
|
||||
{
|
||||
List<NewExpressionInfo> newExpressionInfos = new List<NewExpressionInfo>();
|
||||
if (item is MemberInitExpression)
|
||||
{
|
||||
newExpressionInfos = ExpressionTool.GetNewexpressionInfos(item, this.Context,this);
|
||||
}
|
||||
else
|
||||
{
|
||||
newExpressionInfos = ExpressionTool.GetNewDynamicexpressionInfos(item, this.Context,this);
|
||||
}
|
||||
//mappingKeys = new Dictionary<string, string>();
|
||||
foreach (NewExpressionInfo newExpressionInfo in newExpressionInfos)
|
||||
{
|
||||
//var property=item.Type.GetProperties().Where(it => it.Name == newExpressionInfo.l).First();
|
||||
//asName = GetAsName(item, newExpressionInfo.ShortName, property);
|
||||
mappingKeys.Add("Single_" + newExpressionInfo.LeftNameName, asName + "." + newExpressionInfo.LeftNameName);
|
||||
if (newExpressionInfo.Type == nameof(ConstantExpression))
|
||||
{
|
||||
CallContextThread<Dictionary<string, string>>.SetData("Exp_Select_Mapping_Key", mappingKeys);
|
||||
CallContextAsync<Dictionary<string, string>>.SetData("Exp_Select_Mapping_Key", mappingKeys);
|
||||
parameter.Context.Result.Append($" {newExpressionInfo.RightDbName} AS { this.Context.SqlTranslationLeft}{asName}.{newExpressionInfo.LeftNameName}{ this.Context.SqlTranslationRight} ");
|
||||
}
|
||||
else
|
||||
{
|
||||
CallContextThread<Dictionary<string, string>>.SetData("Exp_Select_Mapping_Key", mappingKeys);
|
||||
CallContextAsync<Dictionary<string, string>>.SetData("Exp_Select_Mapping_Key", mappingKeys);
|
||||
parameter.Context.Result.Append(this.Context.GetAsString(
|
||||
this.Context.SqlTranslationLeft + asName + "." + newExpressionInfo.LeftNameName + this.Context.SqlTranslationRight,
|
||||
newExpressionInfo.RightDbName
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (IsExtSqlFuncObj(item))
|
||||
{
|
||||
var value = GetNewExpressionValue(item);
|
||||
parameter.Context.Result.Append($" {value} AS {asName} ");
|
||||
}
|
||||
else
|
||||
{
|
||||
asName = GetAsNameResolveAnObject(parameter, item, asName, isSameType);
|
||||
}
|
||||
}
|
||||
else if (item.Type == UtilConstants.BoolType && item is MethodCallExpression && IsNotCaseExpression(item))
|
||||
{
|
||||
this.Expression = item;
|
||||
this.Start();
|
||||
var sql = this.Context.DbMehtods.IIF(new MethodCallExpressionModel()
|
||||
{
|
||||
Args = new List<MethodCallExpressionArgs>() {
|
||||
new MethodCallExpressionArgs() {
|
||||
IsMember=true,
|
||||
MemberName=parameter.CommonTempData.ObjToString()
|
||||
},
|
||||
new MethodCallExpressionArgs() {
|
||||
IsMember=true,
|
||||
MemberName=1
|
||||
},
|
||||
new MethodCallExpressionArgs() {
|
||||
IsMember=true,
|
||||
MemberName=0
|
||||
}
|
||||
}
|
||||
});
|
||||
parameter.Context.Result.Append(this.Context.GetAsString(asName, sql));
|
||||
}
|
||||
else if (item.NodeType == ExpressionType.Not
|
||||
&& (item as UnaryExpression).Operand is MethodCallExpression
|
||||
&& ((item as UnaryExpression).Operand as MethodCallExpression).Method.Name.IsIn("IsNullOrEmpty", "IsNullOrWhiteSpace"))
|
||||
{
|
||||
var asValue = GetAsNamePackIfElse(GetNewExpressionValue(item)).ObjToString();
|
||||
parameter.Context.Result.Append(this.Context.GetAsString(asName, asValue));
|
||||
}
|
||||
else if (item is MethodCallExpression && (item as MethodCallExpression).Method.Name.IsIn("Count", "Any") && !item.ToString().StartsWith("Subqueryable"))
|
||||
{
|
||||
if (this.Context.IsSingle && this.Context.SingleTableNameSubqueryShortName == null)
|
||||
{
|
||||
this.Context.SingleTableNameSubqueryShortName = item.ToString().Split('.').First();
|
||||
}
|
||||
parameter.Context.Result.Append(this.Context.GetAsString(asName, GetNewExpressionValue(item)));
|
||||
}
|
||||
else if (item is MethodCallExpression || item is UnaryExpression || item is ConditionalExpression || item.NodeType == ExpressionType.Coalesce)
|
||||
{
|
||||
this.Expression = item;
|
||||
this.Start();
|
||||
parameter.Context.Result.Append(this.Context.GetAsString(asName, parameter.CommonTempData.ObjToString()));
|
||||
}
|
||||
else
|
||||
{
|
||||
Check.ThrowNotSupportedException(item.GetType().Name);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Helper
|
||||
|
||||
protected void SetNavigateResult()
|
||||
{
|
||||
if (this.Context != null)
|
||||
{
|
||||
if (this.Context.Result != null)
|
||||
{
|
||||
this.Context.Result.IsNavicate = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
private string GetAsName(Expression item, object shortName, PropertyInfo property)
|
||||
{
|
||||
string asName;
|
||||
var propertyName = property.Name;
|
||||
var dbColumnName = propertyName;
|
||||
var mappingInfo = this.Context.MappingColumns.FirstOrDefault(it => it.EntityName == item.Type.Name && it.PropertyName.Equals(propertyName, StringComparison.CurrentCultureIgnoreCase));
|
||||
if (mappingInfo.HasValue())
|
||||
{
|
||||
dbColumnName = mappingInfo.DbColumnName;
|
||||
}
|
||||
asName = this.Context.GetTranslationText(item.Type.Name + "." + propertyName);
|
||||
if (Context.IsJoin)
|
||||
{
|
||||
this.Context.Result.Append(Context.GetAsString(asName, dbColumnName, shortName.ObjToString()));
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Context.Result.Append(Context.GetAsString(asName, dbColumnName));
|
||||
}
|
||||
|
||||
return asName;
|
||||
}
|
||||
private string GetAsNameAndShortName(Expression item, object shortName, PropertyInfo property)
|
||||
{
|
||||
string asName;
|
||||
var propertyName = property.Name;
|
||||
var dbColumnName = propertyName;
|
||||
var mappingInfo = this.Context.MappingColumns.FirstOrDefault(it => it.EntityName == item.Type.Name && it.PropertyName.Equals(propertyName, StringComparison.CurrentCultureIgnoreCase));
|
||||
if (mappingInfo.HasValue())
|
||||
{
|
||||
dbColumnName = mappingInfo.DbColumnName;
|
||||
}
|
||||
if (shortName != null && shortName.ObjToString().Contains(this.Context.SqlTranslationLeft) && this.Context.IsSingle)
|
||||
{
|
||||
asName = this.Context.GetTranslationText(item.Type.Name + "." + propertyName);
|
||||
}
|
||||
else
|
||||
{
|
||||
asName = this.Context.GetTranslationText(shortName + "." + item.Type.Name + "." + propertyName);
|
||||
}
|
||||
if (Context.IsJoin)
|
||||
{
|
||||
this.Context.Result.Append(Context.GetAsString(asName, dbColumnName, shortName.ObjToString()));
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Context.Result.Append(Context.GetAsString(asName, dbColumnName));
|
||||
}
|
||||
|
||||
return asName;
|
||||
}
|
||||
private EntityColumnInfo GetColumnInfo(Expression oppoSiteExpression)
|
||||
{
|
||||
var oppsite = (oppoSiteExpression as MemberExpression);
|
||||
if (oppsite == null) return null;
|
||||
if (this.Context.SugarContext == null) return null;
|
||||
if (this.Context.SugarContext.Context == null) return null;
|
||||
if (oppsite.Expression == null) return null;
|
||||
var columnInfo = this.Context.SugarContext.Context.EntityMaintenance
|
||||
.GetEntityInfo(oppsite.Expression.Type).Columns.FirstOrDefault(it => it.PropertyName == oppsite.Member.Name);
|
||||
return columnInfo;
|
||||
}
|
||||
protected MethodCallExpressionArgs GetMethodCallArgs(ExpressionParameter parameter, Expression item,string name=null)
|
||||
{
|
||||
var newContext = this.Context.GetCopyContext();
|
||||
newContext.MappingColumns = this.Context.MappingColumns;
|
||||
newContext.MappingTables = this.Context.MappingTables;
|
||||
newContext.IgnoreComumnList = this.Context.IgnoreComumnList;
|
||||
newContext.IsSingle = this.Context.IsSingle;
|
||||
newContext.SqlFuncServices = this.Context.SqlFuncServices;
|
||||
newContext.MethodName = name;
|
||||
newContext.Resolve(item, this.Context.IsJoin ? ResolveExpressType.WhereMultiple : ResolveExpressType.WhereSingle);
|
||||
this.Context.Index = newContext.Index;
|
||||
this.Context.ParameterIndex = newContext.ParameterIndex;
|
||||
if (newContext.Parameters.HasValue())
|
||||
{
|
||||
this.Context.Parameters.AddRange(newContext.Parameters);
|
||||
}
|
||||
if (newContext.SingleTableNameSubqueryShortName.HasValue())
|
||||
{
|
||||
this.Context.SingleTableNameSubqueryShortName = newContext.SingleTableNameSubqueryShortName;
|
||||
}
|
||||
var methodCallExpressionArgs = new MethodCallExpressionArgs()
|
||||
{
|
||||
IsMember = true,
|
||||
MemberName = newContext.Result.GetResultString()
|
||||
};
|
||||
return methodCallExpressionArgs;
|
||||
}
|
||||
private string GetAsNameResolveAnObject(ExpressionParameter parameter, Expression item, string asName, bool isSameType)
|
||||
{
|
||||
this.Start();
|
||||
var shortName = parameter.CommonTempData;
|
||||
var listProperties = item.Type.GetProperties().Cast<PropertyInfo>().ToList();
|
||||
foreach (var property in listProperties)
|
||||
{
|
||||
var hasIgnore = this.Context.IgnoreComumnList != null && this.Context.IgnoreComumnList.Any(it => it.EntityName.Equals(item.Type.Name, StringComparison.CurrentCultureIgnoreCase) && it.PropertyName.Equals(property.Name, StringComparison.CurrentCultureIgnoreCase));
|
||||
if (hasIgnore)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (property.PropertyType.IsClass())
|
||||
{
|
||||
var comumnInfo = property.GetCustomAttribute<SugarColumn>();
|
||||
if (comumnInfo != null && comumnInfo.IsJson && isSameType)
|
||||
{
|
||||
asName = GetAsNameAndShortName(item, shortName, property);
|
||||
}
|
||||
else if (comumnInfo != null && comumnInfo.IsJson)
|
||||
{
|
||||
asName = GetAsName(item, shortName, property);
|
||||
}
|
||||
else if (comumnInfo != null && this.Context.SugarContext != null && this.Context.SugarContext.Context != null)
|
||||
{
|
||||
var entityInfo = this.Context.SugarContext.Context.EntityMaintenance.GetEntityInfo(item.Type);
|
||||
var entityColumn = entityInfo.Columns.FirstOrDefault(it => it.PropertyName == property.Name);
|
||||
if (entityColumn != null && entityColumn.IsJson)
|
||||
{
|
||||
asName = GetAsName(item, shortName, property);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (isSameType)
|
||||
{
|
||||
asName = GetAsNameAndShortName(item, shortName, property);
|
||||
}
|
||||
else
|
||||
{
|
||||
asName = GetAsName(item, shortName, property);
|
||||
}
|
||||
}
|
||||
|
||||
return asName;
|
||||
}
|
||||
public object GetAsNamePackIfElse(object methodValue)
|
||||
{
|
||||
methodValue = this.Context.DbMehtods.CaseWhen(new List<KeyValuePair<string, string>>() {
|
||||
new KeyValuePair<string, string>("IF",methodValue.ObjToString()),
|
||||
new KeyValuePair<string, string>("Return","1"),
|
||||
new KeyValuePair<string, string>("End","0")
|
||||
});
|
||||
return methodValue;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Validate
|
||||
|
||||
private bool IsExtSqlFuncObj(Expression item)
|
||||
{
|
||||
return this.Context.SqlFuncServices != null && item is MethodCallExpression && this.Context.SqlFuncServices.Any(it => it.UniqueMethodName == ExpressionTool.GetMethodName(item));
|
||||
}
|
||||
private bool IsNullValue(ExpressionParameter parameter, object value)
|
||||
{
|
||||
return value == null
|
||||
&& !parameter.ValueIsNull
|
||||
&& parameter.BaseParameter != null
|
||||
&& parameter.BaseParameter.OperatorValue.IsIn("=","<>")
|
||||
&& this.Context.ResolveType.IsIn(ResolveExpressType.WhereMultiple, ResolveExpressType.WhereSingle);
|
||||
}
|
||||
private static bool IsNotCaseExpression(Expression item)
|
||||
{
|
||||
if ((item as MethodCallExpression).Method.Name == "IIF")
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if ((item as MethodCallExpression).Method.Name == "IsNull")
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if ((item as MethodCallExpression).Method.Name == "End" && item.ToString().Contains("IF("))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if ((item as MethodCallExpression).Method.Name == "AggregateMax")
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if ((item as MethodCallExpression).Method.Name == "AggregateMin")
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if ((item as MethodCallExpression).Method.Name == "AggregateSum")
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if ((item as MethodCallExpression).Method.Name == "ToBool")
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if ((item as MethodCallExpression).Method.Name == "ToBoolean")
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if ((item as MethodCallExpression).Method.Name == "Select" && item.ToString().Contains("Subqueryable()"))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
private static bool IsBoolValue(Expression item)
|
||||
{
|
||||
return item.Type == UtilConstants.BoolType &&
|
||||
(item is MemberExpression) &&
|
||||
(item as MemberExpression).Expression != null &&
|
||||
(item as MemberExpression).Expression.Type == typeof(bool?) &&
|
||||
(item as MemberExpression).Member.Name == "Value";
|
||||
}
|
||||
protected static bool IsConvert(Expression item)
|
||||
{
|
||||
return item is UnaryExpression && item.NodeType == ExpressionType.Convert;
|
||||
}
|
||||
protected static bool IsNotMember(Expression item)
|
||||
{
|
||||
return item is UnaryExpression &&
|
||||
item.Type == UtilConstants.BoolType &&
|
||||
(item as UnaryExpression).NodeType == ExpressionType.Not &&
|
||||
(item as UnaryExpression).Operand is MemberExpression &&
|
||||
((item as UnaryExpression).Operand as MemberExpression).Expression != null &&
|
||||
((item as UnaryExpression).Operand as MemberExpression).Expression.NodeType == ExpressionType.Parameter;
|
||||
}
|
||||
protected static bool IsNotParameter(Expression item)
|
||||
{
|
||||
return item is UnaryExpression &&
|
||||
item.Type == UtilConstants.BoolType &&
|
||||
(item as UnaryExpression).NodeType == ExpressionType.Not &&
|
||||
(item as UnaryExpression).Operand is MemberExpression &&
|
||||
((item as UnaryExpression).Operand as MemberExpression).Expression != null &&
|
||||
((item as UnaryExpression).Operand as MemberExpression).Expression.NodeType == ExpressionType.MemberAccess;
|
||||
}
|
||||
protected bool IsSubMethod(MethodCallExpression express)
|
||||
{
|
||||
return SubTools.SubItemsConst.Any(it => express.Object != null && express.Object.Type.Name.StartsWith("Subqueryable`"));
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Dictionary
|
||||
private Dictionary<string, string> GetMappingColumns(Expression currentExpression)
|
||||
{
|
||||
Dictionary<string, string> result = new Dictionary<string, string>();
|
||||
if (currentExpression == null)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
List<Type> types = new List<Type>();
|
||||
int i = 0;
|
||||
if (currentExpression is NewExpression)
|
||||
{
|
||||
i = (currentExpression as NewExpression).Arguments.Count;
|
||||
foreach (var item in (currentExpression as NewExpression).Arguments)
|
||||
{
|
||||
if (item.Type.IsClass())
|
||||
{
|
||||
types.Add(item.Type);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (currentExpression is MemberInitExpression)
|
||||
{
|
||||
i = (currentExpression as MemberInitExpression).Bindings.Count;
|
||||
foreach (var item in (currentExpression as MemberInitExpression).Bindings)
|
||||
{
|
||||
MemberAssignment memberAssignment = (MemberAssignment)item;
|
||||
if (memberAssignment.Expression.Type.IsClass())
|
||||
{
|
||||
types.Add(memberAssignment.Expression.Type);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (types.Count == i && (types.Count == types.Distinct().Count()))
|
||||
{
|
||||
return result;
|
||||
}
|
||||
var array = currentExpression.ToString().Split(',');
|
||||
foreach (var item in array)
|
||||
{
|
||||
var itemArray = item.Split('=').ToArray();
|
||||
var last = itemArray.Last().Trim().Split('.').First().TrimEnd(')').TrimEnd('}');
|
||||
var first = itemArray.First().Trim();
|
||||
if (first.Contains("{"))
|
||||
{
|
||||
first = first.Split('{').Last().Trim();
|
||||
}
|
||||
if (first.Contains("("))
|
||||
{
|
||||
first = first.Split('(').Last().Trim();
|
||||
}
|
||||
if (!result.ContainsKey(first))
|
||||
{
|
||||
result.Add(first, last);
|
||||
}
|
||||
else
|
||||
{
|
||||
//future
|
||||
}
|
||||
}
|
||||
return result; ;
|
||||
}
|
||||
protected static Dictionary<string, string> MethodMapping = new Dictionary<string, string>() {
|
||||
{ "ToString","ToString"},
|
||||
{ "ToInt32","ToInt32"},
|
||||
{ "ToInt16","ToInt32"},
|
||||
{ "ToInt64","ToInt64"},
|
||||
{ "ToDecimal","ToDecimal"},
|
||||
{ "ToDateTime","ToDate"},
|
||||
{ "ToBoolean","ToBool"},
|
||||
{ "ToDouble","ToDouble"},
|
||||
{ "Length","Length"},
|
||||
{ "Replace","Replace"},
|
||||
{ "Contains","Contains"},
|
||||
{ "ContainsArray","ContainsArray"},
|
||||
{ "EndsWith","EndsWith"},
|
||||
{ "StartsWith","StartsWith"},
|
||||
{ "HasValue","HasValue"},
|
||||
{ "Trim","Trim"},
|
||||
{ "Equals","Equals"},
|
||||
{ "ToLower","ToLower"},
|
||||
{ "ToUpper","ToUpper"},
|
||||
{ "Substring","Substring"},
|
||||
{ "DateAdd","DateAdd"}
|
||||
};
|
||||
protected static Dictionary<string, DateType> MethodTimeMapping = new Dictionary<string, DateType>() {
|
||||
{ "AddYears",DateType.Year},
|
||||
{ "AddMonths",DateType.Month},
|
||||
{ "AddDays",DateType.Day},
|
||||
{ "AddHours",DateType.Hour},
|
||||
{ "AddMinutes",DateType.Minute},
|
||||
{ "AddSeconds",DateType.Second},
|
||||
{ "AddMilliseconds",DateType.Millisecond}
|
||||
};
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,238 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
namespace SqlSugar
|
||||
{
|
||||
/// <summary>
|
||||
/// BaseResolve-Append
|
||||
/// </summary>
|
||||
public partial class BaseResolve
|
||||
{
|
||||
protected void AppendMember(ExpressionParameter parameter, bool? isLeft, object appendValue)
|
||||
{
|
||||
|
||||
Context.ParameterIndex++;
|
||||
if (isLeft == true)
|
||||
{
|
||||
appendValue += ExpressionConst.ExpressionReplace + parameter.BaseParameter.Index;
|
||||
}
|
||||
if (this.Context.Result.Contains(ExpressionConst.FormatSymbol))
|
||||
{
|
||||
this.Context.Result.Replace(ExpressionConst.FormatSymbol, appendValue.ObjToString());
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Context.Result.Append(appendValue);
|
||||
}
|
||||
}
|
||||
protected void AppendValue(ExpressionParameter parameter, bool? isLeft, object value)
|
||||
{
|
||||
if (parameter.BaseExpression is BinaryExpression || parameter.BaseExpression == null)
|
||||
{
|
||||
var oppoSiteExpression = isLeft == true ? parameter.BaseParameter.RightExpression : parameter.BaseParameter.LeftExpression;
|
||||
|
||||
if (value is MapperSql)
|
||||
{
|
||||
ApppendMapperSql(parameter, isLeft, value);
|
||||
}
|
||||
else if (parameter.CurrentExpression is MethodCallExpression || parameter.CurrentExpression is ConditionalExpression || parameter.CurrentExpression.NodeType == ExpressionType.Coalesce)
|
||||
{
|
||||
AppendMethod(parameter, isLeft, value);
|
||||
}
|
||||
else if (oppoSiteExpression is MemberExpression)
|
||||
{
|
||||
AppendMember(parameter, isLeft, value, oppoSiteExpression);
|
||||
}
|
||||
else if ((oppoSiteExpression is UnaryExpression && (oppoSiteExpression as UnaryExpression).Operand is MemberExpression))
|
||||
{
|
||||
value = AppendUnaryExp(parameter, isLeft, value, oppoSiteExpression);
|
||||
}
|
||||
else
|
||||
{
|
||||
value = AppendOther(parameter, isLeft, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
private object AppendOther(ExpressionParameter parameter, bool? isLeft, object value)
|
||||
{
|
||||
var appendValue = this.Context.SqlParameterKeyWord + ExpressionConst.Const + Context.ParameterIndex;
|
||||
Context.ParameterIndex++;
|
||||
if (value != null && value.GetType().IsEnum())
|
||||
{
|
||||
if (this.Context.TableEnumIsString == true)
|
||||
{
|
||||
value = value.ToString();
|
||||
}
|
||||
else
|
||||
{
|
||||
value = Convert.ToInt64(value);
|
||||
}
|
||||
}
|
||||
this.Context.Parameters.Add(new SugarParameter(appendValue, value));
|
||||
appendValue = string.Format(" {0} ", appendValue);
|
||||
if (isLeft == true)
|
||||
{
|
||||
appendValue += ExpressionConst.ExpressionReplace + parameter.BaseParameter.Index;
|
||||
}
|
||||
if (this.Context.Result.Contains(ExpressionConst.FormatSymbol))
|
||||
{
|
||||
this.Context.Result.Replace(ExpressionConst.FormatSymbol, appendValue);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Context.Result.Append(appendValue);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
private object AppendUnaryExp(ExpressionParameter parameter, bool? isLeft, object value, Expression oppoSiteExpression)
|
||||
{
|
||||
string appendValue = Context.SqlParameterKeyWord
|
||||
+ ((MemberExpression)(oppoSiteExpression as UnaryExpression).Operand).Member.Name
|
||||
+ Context.ParameterIndex;
|
||||
if (value.ObjToString() != "NULL" && !parameter.ValueIsNull)
|
||||
{
|
||||
value = this.Context.TableEnumIsString == true ? value.ToString() : value;
|
||||
this.Context.Parameters.Add(new SugarParameter(appendValue, value));
|
||||
}
|
||||
else
|
||||
{
|
||||
appendValue = value.ObjToString();
|
||||
}
|
||||
Context.ParameterIndex++;
|
||||
appendValue = string.Format(" {0} ", appendValue);
|
||||
if (isLeft == true)
|
||||
{
|
||||
appendValue += ExpressionConst.ExpressionReplace + parameter.BaseParameter.Index;
|
||||
}
|
||||
if (this.Context.Result.Contains(ExpressionConst.FormatSymbol))
|
||||
{
|
||||
this.Context.Result.Replace(ExpressionConst.FormatSymbol, appendValue);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Context.Result.Append(appendValue);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
private void AppendMember(ExpressionParameter parameter, bool? isLeft, object value, Expression oppoSiteExpression)
|
||||
{
|
||||
string appendValue = Context.SqlParameterKeyWord
|
||||
+ ((MemberExpression)oppoSiteExpression).Member.Name
|
||||
+ Context.ParameterIndex;
|
||||
if (IsNullValue(parameter, value))
|
||||
{
|
||||
appendValue = $" NULL ";
|
||||
parameter.BaseParameter.ValueIsNull = true;
|
||||
}
|
||||
else if (value.ObjToString() != "NULL" && !parameter.ValueIsNull)
|
||||
{
|
||||
EntityColumnInfo columnInfo = GetColumnInfo(oppoSiteExpression);
|
||||
if (columnInfo != null && columnInfo.SqlParameterDbType != null && columnInfo.SqlParameterDbType is System.Data.DbType)
|
||||
{
|
||||
var p = new SugarParameter(appendValue, value, (System.Data.DbType)columnInfo.SqlParameterDbType);
|
||||
if (columnInfo.SqlParameterSize != null)
|
||||
{
|
||||
p.Size = columnInfo.SqlParameterSize.ObjToInt();
|
||||
}
|
||||
this.Context.Parameters.Add(p);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Context.Parameters.Add(new SugarParameter(appendValue, value));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
appendValue = value.ObjToString();
|
||||
}
|
||||
Context.ParameterIndex++;
|
||||
appendValue = string.Format(" {0} ", appendValue);
|
||||
if (isLeft == true)
|
||||
{
|
||||
appendValue += ExpressionConst.ExpressionReplace + parameter.BaseParameter.Index;
|
||||
}
|
||||
if (this.Context.Result.Contains(ExpressionConst.FormatSymbol))
|
||||
{
|
||||
this.Context.Result.Replace(ExpressionConst.FormatSymbol, appendValue);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Context.Result.Append(appendValue);
|
||||
}
|
||||
}
|
||||
private void AppendMethod(ExpressionParameter parameter, bool? isLeft, object value)
|
||||
{
|
||||
var appendValue = value;
|
||||
if (this.Context.Result.Contains(ExpressionConst.FormatSymbol))
|
||||
{
|
||||
this.Context.Result.Replace(ExpressionConst.FormatSymbol, appendValue.ObjToString());
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Context.Result.Append(appendValue);
|
||||
}
|
||||
this.AppendOpreator(parameter, isLeft);
|
||||
}
|
||||
private void ApppendMapperSql(ExpressionParameter parameter, bool? isLeft, object value)
|
||||
{
|
||||
var sql = ((MapperSql)value).Sql;
|
||||
if (isLeft == true)
|
||||
{
|
||||
sql += ExpressionConst.ExpressionReplace + parameter.BaseParameter.Index;
|
||||
}
|
||||
if (this.Context.Result.Contains(ExpressionConst.FormatSymbol))
|
||||
{
|
||||
this.Context.Result.Replace(ExpressionConst.FormatSymbol, sql);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Context.Result.Append(sql);
|
||||
}
|
||||
}
|
||||
protected void AppendOpreator(ExpressionParameter parameter, bool? isLeft)
|
||||
{
|
||||
if (isLeft == true)
|
||||
{
|
||||
this.Context.Result.Append(" " + ExpressionConst.ExpressionReplace + parameter.BaseParameter.Index);
|
||||
}
|
||||
}
|
||||
protected string AppendParameter(object paramterValue)
|
||||
{
|
||||
string parameterName = this.Context.SqlParameterKeyWord + "constant" + this.Context.ParameterIndex;
|
||||
this.Context.ParameterIndex++; ;
|
||||
this.Context.Parameters.Add(new SugarParameter(parameterName, paramterValue));
|
||||
return parameterName;
|
||||
}
|
||||
protected void AppendNot(object Value)
|
||||
{
|
||||
var isAppend = !this.Context.Result.Contains(ExpressionConst.FormatSymbol);
|
||||
var lastCharIsSpace = this.Context.Result.LastCharIsSpace;
|
||||
if (isAppend)
|
||||
{
|
||||
this.Context.Result.Append(lastCharIsSpace ? "NOT" : " NOT");
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Context.Result.Replace(ExpressionConst.FormatSymbol, "NOT");
|
||||
}
|
||||
}
|
||||
protected void AppendNegate(object Value)
|
||||
{
|
||||
var isAppend = !this.Context.Result.Contains(ExpressionConst.FormatSymbol);
|
||||
var lastCharIsSpace = this.Context.Result.LastCharIsSpace;
|
||||
if (isAppend)
|
||||
{
|
||||
this.Context.Result.Append(lastCharIsSpace ? "-" : " -");
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Context.Result.Replace(ExpressionConst.FormatSymbol, "-");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,188 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
namespace SqlSugar
|
||||
{
|
||||
/// <summary>
|
||||
/// BaseResolve-Helper
|
||||
/// </summary>
|
||||
public partial class BaseResolve
|
||||
{
|
||||
#region Set Method
|
||||
protected void SetNavigateResult()
|
||||
{
|
||||
if (this.Context != null)
|
||||
{
|
||||
if (this.Context.Result != null)
|
||||
{
|
||||
this.Context.Result.IsNavicate = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
private void SetParameter(out Expression expression, out ExpressionParameter parameter)
|
||||
{
|
||||
Context.Index++;
|
||||
expression = this.Expression;
|
||||
parameter = new ExpressionParameter()
|
||||
{
|
||||
Context = this.Context,
|
||||
CurrentExpression = expression,
|
||||
IsLeft = this.IsLeft,
|
||||
BaseExpression = this.ExactExpression,
|
||||
BaseParameter = this.BaseParameter,
|
||||
Index = Context.Index
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Get Mehtod
|
||||
private string GetAsName(Expression item, object shortName, PropertyInfo property)
|
||||
{
|
||||
string asName;
|
||||
var propertyName = property.Name;
|
||||
var dbColumnName = propertyName;
|
||||
var mappingInfo = this.Context.MappingColumns.FirstOrDefault(it => it.EntityName == item.Type.Name && it.PropertyName.Equals(propertyName, StringComparison.CurrentCultureIgnoreCase));
|
||||
if (mappingInfo.HasValue())
|
||||
{
|
||||
dbColumnName = mappingInfo.DbColumnName;
|
||||
}
|
||||
asName = this.Context.GetTranslationText(item.Type.Name + "." + propertyName);
|
||||
if (Context.IsJoin)
|
||||
{
|
||||
this.Context.Result.Append(Context.GetAsString(asName, dbColumnName, shortName.ObjToString()));
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Context.Result.Append(Context.GetAsString(asName, dbColumnName));
|
||||
}
|
||||
|
||||
return asName;
|
||||
}
|
||||
private string GetAsNameAndShortName(Expression item, object shortName, PropertyInfo property)
|
||||
{
|
||||
string asName;
|
||||
var propertyName = property.Name;
|
||||
var dbColumnName = propertyName;
|
||||
var mappingInfo = this.Context.MappingColumns.FirstOrDefault(it => it.EntityName == item.Type.Name && it.PropertyName.Equals(propertyName, StringComparison.CurrentCultureIgnoreCase));
|
||||
if (mappingInfo.HasValue())
|
||||
{
|
||||
dbColumnName = mappingInfo.DbColumnName;
|
||||
}
|
||||
if (shortName != null && shortName.ObjToString().Contains(this.Context.SqlTranslationLeft) && this.Context.IsSingle)
|
||||
{
|
||||
asName = this.Context.GetTranslationText(item.Type.Name + "." + propertyName);
|
||||
}
|
||||
else
|
||||
{
|
||||
asName = this.Context.GetTranslationText(shortName + "." + item.Type.Name + "." + propertyName);
|
||||
}
|
||||
if (Context.IsJoin)
|
||||
{
|
||||
this.Context.Result.Append(Context.GetAsString(asName, dbColumnName, shortName.ObjToString()));
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Context.Result.Append(Context.GetAsString(asName, dbColumnName));
|
||||
}
|
||||
|
||||
return asName;
|
||||
}
|
||||
private EntityColumnInfo GetColumnInfo(Expression oppoSiteExpression)
|
||||
{
|
||||
var oppsite = (oppoSiteExpression as MemberExpression);
|
||||
if (oppsite == null) return null;
|
||||
if (this.Context.SugarContext == null) return null;
|
||||
if (this.Context.SugarContext.Context == null) return null;
|
||||
if (oppsite.Expression == null) return null;
|
||||
var columnInfo = this.Context.SugarContext.Context.EntityMaintenance
|
||||
.GetEntityInfo(oppsite.Expression.Type).Columns.FirstOrDefault(it => it.PropertyName == oppsite.Member.Name);
|
||||
return columnInfo;
|
||||
}
|
||||
protected MethodCallExpressionArgs GetMethodCallArgs(ExpressionParameter parameter, Expression item, string name = null)
|
||||
{
|
||||
var newContext = this.Context.GetCopyContext();
|
||||
newContext.MappingColumns = this.Context.MappingColumns;
|
||||
newContext.MappingTables = this.Context.MappingTables;
|
||||
newContext.IgnoreComumnList = this.Context.IgnoreComumnList;
|
||||
newContext.IsSingle = this.Context.IsSingle;
|
||||
newContext.SqlFuncServices = this.Context.SqlFuncServices;
|
||||
newContext.MethodName = name;
|
||||
newContext.Resolve(item, this.Context.IsJoin ? ResolveExpressType.WhereMultiple : ResolveExpressType.WhereSingle);
|
||||
this.Context.Index = newContext.Index;
|
||||
this.Context.ParameterIndex = newContext.ParameterIndex;
|
||||
if (newContext.Parameters.HasValue())
|
||||
{
|
||||
this.Context.Parameters.AddRange(newContext.Parameters);
|
||||
}
|
||||
if (newContext.SingleTableNameSubqueryShortName.HasValue())
|
||||
{
|
||||
this.Context.SingleTableNameSubqueryShortName = newContext.SingleTableNameSubqueryShortName;
|
||||
}
|
||||
var methodCallExpressionArgs = new MethodCallExpressionArgs()
|
||||
{
|
||||
IsMember = true,
|
||||
MemberName = newContext.Result.GetResultString()
|
||||
};
|
||||
return methodCallExpressionArgs;
|
||||
}
|
||||
private string GetAsNameResolveAnObject(ExpressionParameter parameter, Expression item, string asName, bool isSameType)
|
||||
{
|
||||
this.Start();
|
||||
var shortName = parameter.CommonTempData;
|
||||
var listProperties = item.Type.GetProperties().Cast<PropertyInfo>().ToList();
|
||||
foreach (var property in listProperties)
|
||||
{
|
||||
var hasIgnore = this.Context.IgnoreComumnList != null && this.Context.IgnoreComumnList.Any(it => it.EntityName.Equals(item.Type.Name, StringComparison.CurrentCultureIgnoreCase) && it.PropertyName.Equals(property.Name, StringComparison.CurrentCultureIgnoreCase));
|
||||
if (hasIgnore)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (property.PropertyType.IsClass())
|
||||
{
|
||||
var comumnInfo = property.GetCustomAttribute<SugarColumn>();
|
||||
if (comumnInfo != null && comumnInfo.IsJson && isSameType)
|
||||
{
|
||||
asName = GetAsNameAndShortName(item, shortName, property);
|
||||
}
|
||||
else if (comumnInfo != null && comumnInfo.IsJson)
|
||||
{
|
||||
asName = GetAsName(item, shortName, property);
|
||||
}
|
||||
else if (comumnInfo != null && this.Context.SugarContext != null && this.Context.SugarContext.Context != null)
|
||||
{
|
||||
var entityInfo = this.Context.SugarContext.Context.EntityMaintenance.GetEntityInfo(item.Type);
|
||||
var entityColumn = entityInfo.Columns.FirstOrDefault(it => it.PropertyName == property.Name);
|
||||
if (entityColumn != null && entityColumn.IsJson)
|
||||
{
|
||||
asName = GetAsName(item, shortName, property);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (isSameType)
|
||||
{
|
||||
asName = GetAsNameAndShortName(item, shortName, property);
|
||||
}
|
||||
else
|
||||
{
|
||||
asName = GetAsName(item, shortName, property);
|
||||
}
|
||||
}
|
||||
|
||||
return asName;
|
||||
}
|
||||
public object GetAsNamePackIfElse(object methodValue)
|
||||
{
|
||||
methodValue = this.Context.DbMehtods.CaseWhen(new List<KeyValuePair<string, string>>() {
|
||||
new KeyValuePair<string, string>("IF",methodValue.ObjToString()),
|
||||
new KeyValuePair<string, string>("Return","1"),
|
||||
new KeyValuePair<string, string>("End","0")
|
||||
});
|
||||
return methodValue;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
@@ -0,0 +1,268 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
namespace SqlSugar
|
||||
{
|
||||
/// <summary>
|
||||
///BaseResolve New Expression
|
||||
/// </summary>
|
||||
public partial class BaseResolve
|
||||
{
|
||||
private void ResloveOtherMUC(ExpressionParameter parameter, Expression item, string asName)
|
||||
{
|
||||
this.Expression = item;
|
||||
this.Start();
|
||||
parameter.Context.Result.Append(this.Context.GetAsString(asName, parameter.CommonTempData.ObjToString()));
|
||||
}
|
||||
|
||||
private void ResloveCountAny(ExpressionParameter parameter, Expression item, string asName)
|
||||
{
|
||||
if (this.Context.IsSingle && this.Context.SingleTableNameSubqueryShortName == null)
|
||||
{
|
||||
this.Context.SingleTableNameSubqueryShortName = item.ToString().Split('.').First();
|
||||
}
|
||||
parameter.Context.Result.Append(this.Context.GetAsString(asName, GetNewExpressionValue(item)));
|
||||
}
|
||||
|
||||
private void ResloveNot(ExpressionParameter parameter, Expression item, string asName)
|
||||
{
|
||||
var asValue = GetAsNamePackIfElse(GetNewExpressionValue(item)).ObjToString();
|
||||
parameter.Context.Result.Append(this.Context.GetAsString(asName, asValue));
|
||||
}
|
||||
|
||||
private void ResloveBoolMethod(ExpressionParameter parameter, Expression item, string asName)
|
||||
{
|
||||
this.Expression = item;
|
||||
this.Start();
|
||||
var sql = this.Context.DbMehtods.IIF(new MethodCallExpressionModel()
|
||||
{
|
||||
Args = new List<MethodCallExpressionArgs>() {
|
||||
new MethodCallExpressionArgs() {
|
||||
IsMember=true,
|
||||
MemberName=parameter.CommonTempData.ObjToString()
|
||||
},
|
||||
new MethodCallExpressionArgs() {
|
||||
IsMember=true,
|
||||
MemberName=1
|
||||
},
|
||||
new MethodCallExpressionArgs() {
|
||||
IsMember=true,
|
||||
MemberName=0
|
||||
}
|
||||
}
|
||||
});
|
||||
parameter.Context.Result.Append(this.Context.GetAsString(asName, sql));
|
||||
}
|
||||
|
||||
private string ResolveClass(ExpressionParameter parameter, Expression item, string asName)
|
||||
{
|
||||
var mappingKeys = GetMappingColumns(parameter.CurrentExpression);
|
||||
var isSameType = mappingKeys.Keys.Count > 0;
|
||||
CallContextThread<Dictionary<string, string>>.SetData("Exp_Select_Mapping_Key", mappingKeys);
|
||||
CallContextAsync<Dictionary<string, string>>.SetData("Exp_Select_Mapping_Key", mappingKeys);
|
||||
this.Expression = item;
|
||||
if (this.Context.IsJoin && (item is MemberInitExpression || item is NewExpression))
|
||||
{
|
||||
List<NewExpressionInfo> newExpressionInfos = new List<NewExpressionInfo>();
|
||||
if (item is MemberInitExpression)
|
||||
{
|
||||
newExpressionInfos = ExpressionTool.GetNewexpressionInfos(item, this.Context, this);
|
||||
}
|
||||
else
|
||||
{
|
||||
newExpressionInfos = ExpressionTool.GetNewDynamicexpressionInfos(item, this.Context, this);
|
||||
}
|
||||
foreach (NewExpressionInfo newExpressionInfo in newExpressionInfos)
|
||||
{
|
||||
//var property=item.Type.GetProperties().Where(it => it.Name == newExpressionInfo.l).First();
|
||||
//asName = GetAsName(item, newExpressionInfo.ShortName, property);
|
||||
if (newExpressionInfo.Type == nameof(ConstantExpression))
|
||||
{
|
||||
parameter.Context.Result.Append(
|
||||
newExpressionInfo.RightDbName + " AS " +
|
||||
this.Context.SqlTranslationLeft + asName + "." + newExpressionInfo.LeftNameName + this.Context.SqlTranslationRight
|
||||
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
parameter.Context.Result.Append(this.Context.GetAsString(
|
||||
this.Context.SqlTranslationLeft + asName + "." + newExpressionInfo.LeftNameName + this.Context.SqlTranslationRight,
|
||||
newExpressionInfo.ShortName + "." + newExpressionInfo.RightDbName
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (!this.Context.IsJoin && (item is MemberInitExpression || item is NewExpression))
|
||||
{
|
||||
List<NewExpressionInfo> newExpressionInfos = new List<NewExpressionInfo>();
|
||||
if (item is MemberInitExpression)
|
||||
{
|
||||
newExpressionInfos = ExpressionTool.GetNewexpressionInfos(item, this.Context, this);
|
||||
}
|
||||
else
|
||||
{
|
||||
newExpressionInfos = ExpressionTool.GetNewDynamicexpressionInfos(item, this.Context, this);
|
||||
}
|
||||
//mappingKeys = new Dictionary<string, string>();
|
||||
foreach (NewExpressionInfo newExpressionInfo in newExpressionInfos)
|
||||
{
|
||||
//var property=item.Type.GetProperties().Where(it => it.Name == newExpressionInfo.l).First();
|
||||
//asName = GetAsName(item, newExpressionInfo.ShortName, property);
|
||||
mappingKeys.Add("Single_" + newExpressionInfo.LeftNameName, asName + "." + newExpressionInfo.LeftNameName);
|
||||
if (newExpressionInfo.Type == nameof(ConstantExpression))
|
||||
{
|
||||
CallContextThread<Dictionary<string, string>>.SetData("Exp_Select_Mapping_Key", mappingKeys);
|
||||
CallContextAsync<Dictionary<string, string>>.SetData("Exp_Select_Mapping_Key", mappingKeys);
|
||||
parameter.Context.Result.Append($" {newExpressionInfo.RightDbName} AS {this.Context.SqlTranslationLeft}{asName}.{newExpressionInfo.LeftNameName}{this.Context.SqlTranslationRight} ");
|
||||
}
|
||||
else
|
||||
{
|
||||
CallContextThread<Dictionary<string, string>>.SetData("Exp_Select_Mapping_Key", mappingKeys);
|
||||
CallContextAsync<Dictionary<string, string>>.SetData("Exp_Select_Mapping_Key", mappingKeys);
|
||||
parameter.Context.Result.Append(this.Context.GetAsString(
|
||||
this.Context.SqlTranslationLeft + asName + "." + newExpressionInfo.LeftNameName + this.Context.SqlTranslationRight,
|
||||
newExpressionInfo.RightDbName
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (IsExtSqlFuncObj(item))
|
||||
{
|
||||
var value = GetNewExpressionValue(item);
|
||||
parameter.Context.Result.Append($" {value} AS {asName} ");
|
||||
}
|
||||
else
|
||||
{
|
||||
asName = GetAsNameResolveAnObject(parameter, item, asName, isSameType);
|
||||
}
|
||||
|
||||
return asName;
|
||||
}
|
||||
|
||||
private void ResolveBinary(Expression item, string asName)
|
||||
{
|
||||
if (this.Context.Result.IsLockCurrentParameter == false)
|
||||
{
|
||||
var newContext = this.Context.GetCopyContextWithMapping();
|
||||
var resolveExpressType = this.Context.IsSingle ? ResolveExpressType.WhereSingle : ResolveExpressType.WhereMultiple;
|
||||
newContext.Resolve(item, resolveExpressType);
|
||||
this.Context.Index = newContext.Index;
|
||||
this.Context.ParameterIndex = newContext.ParameterIndex;
|
||||
if (newContext.Parameters.HasValue())
|
||||
{
|
||||
this.Context.Parameters.AddRange(newContext.Parameters);
|
||||
}
|
||||
this.Context.Result.Append(this.Context.GetAsString(asName, newContext.Result.GetString()));
|
||||
this.Context.Result.CurrentParameter = null;
|
||||
if (this.Context.SingleTableNameSubqueryShortName.IsNullOrEmpty() && newContext.SingleTableNameSubqueryShortName.HasValue())
|
||||
{
|
||||
this.Context.SingleTableNameSubqueryShortName = newContext.SingleTableNameSubqueryShortName;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ResolveUnaryExpConst(ExpressionParameter parameter, Expression item, string asName)
|
||||
{
|
||||
if (this.Context.Result.IsLockCurrentParameter == false)
|
||||
{
|
||||
this.Expression = ((UnaryExpression)item).Operand;
|
||||
this.Start();
|
||||
string parameterName = this.Context.SqlParameterKeyWord + "constant" + this.Context.ParameterIndex;
|
||||
this.Context.ParameterIndex++;
|
||||
parameter.Context.Result.Append(this.Context.GetAsString(asName, parameterName));
|
||||
this.Context.Parameters.Add(new SugarParameter(parameterName, parameter.CommonTempData));
|
||||
}
|
||||
}
|
||||
|
||||
private void ResolveUnaryExpMem(ExpressionParameter parameter, Expression item, string asName)
|
||||
{
|
||||
if (this.Context.Result.IsLockCurrentParameter == false)
|
||||
{
|
||||
var expression = ((UnaryExpression)item).Operand as MemberExpression;
|
||||
var isDateTimeNow = ((UnaryExpression)item).Operand.ToString() == "DateTime.Now";
|
||||
if (expression.Expression == null && !isDateTimeNow)
|
||||
{
|
||||
this.Context.Result.CurrentParameter = parameter;
|
||||
this.Context.Result.IsLockCurrentParameter = true;
|
||||
parameter.IsAppendTempDate();
|
||||
this.Expression = item;
|
||||
this.Start();
|
||||
parameter.IsAppendResult();
|
||||
this.Context.Result.Append(this.Context.GetAsString(asName, parameter.CommonTempData.ObjToString()));
|
||||
this.Context.Result.CurrentParameter = null;
|
||||
}
|
||||
else if (expression.Expression is ConstantExpression || isDateTimeNow)
|
||||
{
|
||||
string parameterName = this.Context.SqlParameterKeyWord + "constant" + this.Context.ParameterIndex;
|
||||
this.Context.ParameterIndex++;
|
||||
parameter.Context.Result.Append(this.Context.GetAsString(asName, parameterName));
|
||||
this.Context.Parameters.Add(new SugarParameter(parameterName, ExpressionTool.GetMemberValue(expression.Member, expression)));
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Context.Result.CurrentParameter = parameter;
|
||||
this.Context.Result.IsLockCurrentParameter = true;
|
||||
parameter.IsAppendTempDate();
|
||||
this.Expression = expression;
|
||||
this.Start();
|
||||
parameter.IsAppendResult();
|
||||
this.Context.Result.Append(this.Context.GetAsString(asName, parameter.CommonTempData.ObjToString()));
|
||||
this.Context.Result.CurrentParameter = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ResolveMemberOther(ExpressionParameter parameter, Expression item, string asName)
|
||||
{
|
||||
if (this.Context.Result.IsLockCurrentParameter == false)
|
||||
{
|
||||
this.Context.Result.CurrentParameter = parameter;
|
||||
this.Context.Result.IsLockCurrentParameter = true;
|
||||
parameter.IsAppendTempDate();
|
||||
this.Expression = item;
|
||||
if (IsBoolValue(item))
|
||||
{
|
||||
this.Expression = (item as MemberExpression).Expression;
|
||||
}
|
||||
this.Start();
|
||||
parameter.IsAppendResult();
|
||||
this.Context.Result.Append(this.Context.GetAsString(asName, parameter.CommonTempData.ObjToString()));
|
||||
this.Context.Result.CurrentParameter = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void ResolveMemberConst(ExpressionParameter parameter, Expression item, string asName)
|
||||
{
|
||||
this.Expression = item;
|
||||
this.Start();
|
||||
string parameterName = this.Context.SqlParameterKeyWord + "constant" + this.Context.ParameterIndex;
|
||||
this.Context.ParameterIndex++;
|
||||
parameter.Context.Result.Append(this.Context.GetAsString(asName, parameterName));
|
||||
this.Context.Parameters.Add(new SugarParameter(parameterName, parameter.CommonTempData));
|
||||
}
|
||||
|
||||
private void ResolveMember(ExpressionParameter parameter, Expression item, string asName)
|
||||
{
|
||||
var paramterValue = ExpressionTool.GetPropertyValue(item as MemberExpression);
|
||||
string parameterName = this.Context.SqlParameterKeyWord + "constant" + this.Context.ParameterIndex;
|
||||
this.Context.ParameterIndex++;
|
||||
parameter.Context.Result.Append(this.Context.GetAsString(asName, parameterName));
|
||||
this.Context.Parameters.Add(new SugarParameter(parameterName, paramterValue));
|
||||
}
|
||||
|
||||
private void ResolveConst(ExpressionParameter parameter, Expression item, string asName)
|
||||
{
|
||||
this.Expression = item;
|
||||
this.Start();
|
||||
string parameterName = this.Context.SqlParameterKeyWord + "constant" + this.Context.ParameterIndex;
|
||||
this.Context.ParameterIndex++;
|
||||
parameter.Context.Result.Append(this.Context.GetAsString(asName, parameterName));
|
||||
this.Context.Parameters.Add(new SugarParameter(parameterName, parameter.CommonTempData));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,109 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
namespace SqlSugar
|
||||
{
|
||||
/// <summary>
|
||||
///BaseResolve New Expression
|
||||
/// </summary>
|
||||
public partial class BaseResolve
|
||||
{
|
||||
public string GetNewExpressionValue(Expression item)
|
||||
{
|
||||
var newContext = this.Context.GetCopyContextWithMapping();
|
||||
newContext.SugarContext = this.Context.SugarContext;
|
||||
newContext.Resolve(item, this.Context.IsJoin ? ResolveExpressType.WhereMultiple : ResolveExpressType.WhereSingle);
|
||||
this.Context.Index = newContext.Index;
|
||||
this.Context.ParameterIndex = newContext.ParameterIndex;
|
||||
if (newContext.Parameters.HasValue())
|
||||
{
|
||||
this.Context.Parameters.AddRange(newContext.Parameters);
|
||||
}
|
||||
if (this.Context.SingleTableNameSubqueryShortName == "Subqueryable()")
|
||||
{
|
||||
this.Context.SingleTableNameSubqueryShortName = newContext.SingleTableNameSubqueryShortName;
|
||||
}
|
||||
else if (newContext.SingleTableNameSubqueryShortName!=null&& newContext.Result !=null && newContext.Result.Contains(this.Context.SqlTranslationLeft+ newContext.SingleTableNameSubqueryShortName+ this.Context.SqlTranslationRight))
|
||||
{
|
||||
this.Context.SingleTableNameSubqueryShortName = newContext.SingleTableNameSubqueryShortName;
|
||||
}
|
||||
return newContext.Result.GetResultString();
|
||||
}
|
||||
|
||||
public string GetNewExpressionValue(Expression item, ResolveExpressType type)
|
||||
{
|
||||
var newContext = this.Context.GetCopyContextWithMapping();
|
||||
newContext.SugarContext = this.Context.SugarContext;
|
||||
newContext.Resolve(item, type);
|
||||
this.Context.Index = newContext.Index;
|
||||
this.Context.ParameterIndex = newContext.ParameterIndex;
|
||||
if (newContext.Parameters.HasValue())
|
||||
{
|
||||
this.Context.Parameters.AddRange(newContext.Parameters);
|
||||
}
|
||||
return newContext.Result.GetResultString();
|
||||
}
|
||||
|
||||
protected void ResolveNewExpressions(ExpressionParameter parameter, Expression item, string asName)
|
||||
{
|
||||
if (item is ConstantExpression)
|
||||
{
|
||||
ResolveConst(parameter, item, asName);
|
||||
}
|
||||
else if ((item is MemberExpression) && ((MemberExpression)item).Expression == null)
|
||||
{
|
||||
ResolveMember(parameter, item, asName);
|
||||
}
|
||||
else if ((item is MemberExpression) && ((MemberExpression)item).Expression.NodeType == ExpressionType.Constant)
|
||||
{
|
||||
ResolveMemberConst(parameter, item, asName);
|
||||
}
|
||||
else if (item is MemberExpression)
|
||||
{
|
||||
ResolveMemberOther(parameter, item, asName);
|
||||
}
|
||||
else if (item is UnaryExpression && ((UnaryExpression)item).Operand is MemberExpression)
|
||||
{
|
||||
ResolveUnaryExpMem(parameter, item, asName);
|
||||
}
|
||||
else if (item is UnaryExpression && ((UnaryExpression)item).Operand is ConstantExpression)
|
||||
{
|
||||
ResolveUnaryExpConst(parameter, item, asName);
|
||||
}
|
||||
else if (item is BinaryExpression)
|
||||
{
|
||||
ResolveBinary(item, asName);
|
||||
}
|
||||
else if (item.Type.IsClass())
|
||||
{
|
||||
asName = ResolveClass(parameter, item, asName);
|
||||
}
|
||||
else if (item.Type == UtilConstants.BoolType && item is MethodCallExpression && IsNotCaseExpression(item))
|
||||
{
|
||||
ResloveBoolMethod(parameter, item, asName);
|
||||
}
|
||||
else if (item.NodeType == ExpressionType.Not
|
||||
&& (item as UnaryExpression).Operand is MethodCallExpression
|
||||
&& ((item as UnaryExpression).Operand as MethodCallExpression).Method.Name.IsIn("IsNullOrEmpty", "IsNullOrWhiteSpace"))
|
||||
{
|
||||
ResloveNot(parameter, item, asName);
|
||||
}
|
||||
else if (item is MethodCallExpression && (item as MethodCallExpression).Method.Name.IsIn("Count", "Any") && !item.ToString().StartsWith("Subqueryable"))
|
||||
{
|
||||
ResloveCountAny(parameter, item, asName);
|
||||
}
|
||||
else if (item is MethodCallExpression || item is UnaryExpression || item is ConditionalExpression || item.NodeType == ExpressionType.Coalesce)
|
||||
{
|
||||
ResloveOtherMUC(parameter, item, asName);
|
||||
}
|
||||
else
|
||||
{
|
||||
Check.ThrowNotSupportedException(item.GetType().Name);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,117 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public partial class BaseResolve
|
||||
{
|
||||
#region Property
|
||||
protected Expression Expression { get; set; }
|
||||
protected Expression ExactExpression { get; set; }
|
||||
public ExpressionContext Context { get; set; }
|
||||
public bool? IsLeft { get; set; }
|
||||
public int ContentIndex { get { return this.Context.Index; } }
|
||||
public int Index { get; set; }
|
||||
public ExpressionParameter BaseParameter { get; set; }
|
||||
#endregion
|
||||
|
||||
#region Dictionary
|
||||
private Dictionary<string, string> GetMappingColumns(Expression currentExpression)
|
||||
{
|
||||
Dictionary<string, string> result = new Dictionary<string, string>();
|
||||
if (currentExpression == null)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
List<Type> types = new List<Type>();
|
||||
int i = 0;
|
||||
if (currentExpression is NewExpression)
|
||||
{
|
||||
i = (currentExpression as NewExpression).Arguments.Count;
|
||||
foreach (var item in (currentExpression as NewExpression).Arguments)
|
||||
{
|
||||
if (item.Type.IsClass())
|
||||
{
|
||||
types.Add(item.Type);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (currentExpression is MemberInitExpression)
|
||||
{
|
||||
i = (currentExpression as MemberInitExpression).Bindings.Count;
|
||||
foreach (var item in (currentExpression as MemberInitExpression).Bindings)
|
||||
{
|
||||
MemberAssignment memberAssignment = (MemberAssignment)item;
|
||||
if (memberAssignment.Expression.Type.IsClass())
|
||||
{
|
||||
types.Add(memberAssignment.Expression.Type);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (types.Count == i && (types.Count == types.Distinct().Count()))
|
||||
{
|
||||
return result;
|
||||
}
|
||||
var array = currentExpression.ToString().Split(',');
|
||||
foreach (var item in array)
|
||||
{
|
||||
var itemArray = item.Split('=').ToArray();
|
||||
var last = itemArray.Last().Trim().Split('.').First().TrimEnd(')').TrimEnd('}');
|
||||
var first = itemArray.First().Trim();
|
||||
if (first.Contains("{"))
|
||||
{
|
||||
first = first.Split('{').Last().Trim();
|
||||
}
|
||||
if (first.Contains("("))
|
||||
{
|
||||
first = first.Split('(').Last().Trim();
|
||||
}
|
||||
if (!result.ContainsKey(first))
|
||||
{
|
||||
result.Add(first, last);
|
||||
}
|
||||
else
|
||||
{
|
||||
//future
|
||||
}
|
||||
}
|
||||
return result; ;
|
||||
}
|
||||
protected static Dictionary<string, string> MethodMapping = new Dictionary<string, string>() {
|
||||
{ "ToString","ToString"},
|
||||
{ "ToInt32","ToInt32"},
|
||||
{ "ToInt16","ToInt32"},
|
||||
{ "ToInt64","ToInt64"},
|
||||
{ "ToDecimal","ToDecimal"},
|
||||
{ "ToDateTime","ToDate"},
|
||||
{ "ToBoolean","ToBool"},
|
||||
{ "ToDouble","ToDouble"},
|
||||
{ "Length","Length"},
|
||||
{ "Replace","Replace"},
|
||||
{ "Contains","Contains"},
|
||||
{ "ContainsArray","ContainsArray"},
|
||||
{ "EndsWith","EndsWith"},
|
||||
{ "StartsWith","StartsWith"},
|
||||
{ "HasValue","HasValue"},
|
||||
{ "Trim","Trim"},
|
||||
{ "Equals","Equals"},
|
||||
{ "ToLower","ToLower"},
|
||||
{ "ToUpper","ToUpper"},
|
||||
{ "Substring","Substring"},
|
||||
{ "DateAdd","DateAdd"}
|
||||
};
|
||||
protected static Dictionary<string, DateType> MethodTimeMapping = new Dictionary<string, DateType>() {
|
||||
{ "AddYears",DateType.Year},
|
||||
{ "AddMonths",DateType.Month},
|
||||
{ "AddDays",DateType.Day},
|
||||
{ "AddHours",DateType.Hour},
|
||||
{ "AddMinutes",DateType.Minute},
|
||||
{ "AddSeconds",DateType.Second},
|
||||
{ "AddMilliseconds",DateType.Millisecond}
|
||||
};
|
||||
#endregion
|
||||
}
|
||||
}
|
@@ -0,0 +1,105 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
namespace SqlSugar
|
||||
{
|
||||
/// <summary>
|
||||
/// BaseResolve-Validate
|
||||
/// </summary>
|
||||
public partial class BaseResolve
|
||||
{
|
||||
|
||||
private bool IsExtSqlFuncObj(Expression item)
|
||||
{
|
||||
return this.Context.SqlFuncServices != null && item is MethodCallExpression && this.Context.SqlFuncServices.Any(it => it.UniqueMethodName == ExpressionTool.GetMethodName(item));
|
||||
}
|
||||
private bool IsNullValue(ExpressionParameter parameter, object value)
|
||||
{
|
||||
return value == null
|
||||
&& !parameter.ValueIsNull
|
||||
&& parameter.BaseParameter != null
|
||||
&& parameter.BaseParameter.OperatorValue.IsIn("=", "<>")
|
||||
&& this.Context.ResolveType.IsIn(ResolveExpressType.WhereMultiple, ResolveExpressType.WhereSingle);
|
||||
}
|
||||
private static bool IsNotCaseExpression(Expression item)
|
||||
{
|
||||
if ((item as MethodCallExpression).Method.Name == "IIF")
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if ((item as MethodCallExpression).Method.Name == "IsNull")
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if ((item as MethodCallExpression).Method.Name == "End" && item.ToString().Contains("IF("))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if ((item as MethodCallExpression).Method.Name == "AggregateMax")
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if ((item as MethodCallExpression).Method.Name == "AggregateMin")
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if ((item as MethodCallExpression).Method.Name == "AggregateSum")
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if ((item as MethodCallExpression).Method.Name == "ToBool")
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if ((item as MethodCallExpression).Method.Name == "ToBoolean")
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if ((item as MethodCallExpression).Method.Name == "Select" && item.ToString().Contains("Subqueryable()"))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
private static bool IsBoolValue(Expression item)
|
||||
{
|
||||
return item.Type == UtilConstants.BoolType &&
|
||||
(item is MemberExpression) &&
|
||||
(item as MemberExpression).Expression != null &&
|
||||
(item as MemberExpression).Expression.Type == typeof(bool?) &&
|
||||
(item as MemberExpression).Member.Name == "Value";
|
||||
}
|
||||
protected static bool IsConvert(Expression item)
|
||||
{
|
||||
return item is UnaryExpression && item.NodeType == ExpressionType.Convert;
|
||||
}
|
||||
protected static bool IsNotMember(Expression item)
|
||||
{
|
||||
return item is UnaryExpression &&
|
||||
item.Type == UtilConstants.BoolType &&
|
||||
(item as UnaryExpression).NodeType == ExpressionType.Not &&
|
||||
(item as UnaryExpression).Operand is MemberExpression &&
|
||||
((item as UnaryExpression).Operand as MemberExpression).Expression != null &&
|
||||
((item as UnaryExpression).Operand as MemberExpression).Expression.NodeType == ExpressionType.Parameter;
|
||||
}
|
||||
protected static bool IsNotParameter(Expression item)
|
||||
{
|
||||
return item is UnaryExpression &&
|
||||
item.Type == UtilConstants.BoolType &&
|
||||
(item as UnaryExpression).NodeType == ExpressionType.Not &&
|
||||
(item as UnaryExpression).Operand is MemberExpression &&
|
||||
((item as UnaryExpression).Operand as MemberExpression).Expression != null &&
|
||||
((item as UnaryExpression).Operand as MemberExpression).Expression.NodeType == ExpressionType.MemberAccess;
|
||||
}
|
||||
protected bool IsSubMethod(MethodCallExpression express)
|
||||
{
|
||||
return SubTools.SubItemsConst.Any(it => express.Object != null && express.Object.Type.Name.StartsWith("Subqueryable`"));
|
||||
}
|
||||
}
|
||||
}
|
@@ -12,22 +12,9 @@ namespace SqlSugar
|
||||
{
|
||||
var expression = base.Expression as ConstantExpression;
|
||||
var isLeft = parameter.IsLeft;
|
||||
object value = ExpressionTool.GetValue(expression.Value,this.Context);
|
||||
if (this.Context.TableEnumIsString == true
|
||||
&& value != null
|
||||
&& value.IsInt()
|
||||
&& base.BaseParameter?.OppsiteExpression != null)
|
||||
{
|
||||
if (base.BaseParameter?.OppsiteExpression is UnaryExpression)
|
||||
{
|
||||
var oppsiteExpression = base.BaseParameter?.OppsiteExpression as UnaryExpression;
|
||||
var oppsiteValue = oppsiteExpression.Operand;
|
||||
if (oppsiteValue.Type.IsEnum())
|
||||
{
|
||||
value = UtilMethods.ChangeType2(value, oppsiteValue.Type).ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
object value = ExpressionTool.GetValue(expression.Value, this.Context);
|
||||
if (IsEnumString(value))
|
||||
value = ConvertEnum(value);
|
||||
var baseParameter = parameter.BaseParameter;
|
||||
baseParameter.ChildExpression = expression;
|
||||
var isSetTempData = baseParameter.CommonTempData.HasValue() && baseParameter.CommonTempData.Equals(CommonTempDataType.Result);
|
||||
@@ -81,5 +68,28 @@ namespace SqlSugar
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private object ConvertEnum(object value)
|
||||
{
|
||||
if (base.BaseParameter?.OppsiteExpression is UnaryExpression)
|
||||
{
|
||||
var oppsiteExpression = base.BaseParameter?.OppsiteExpression as UnaryExpression;
|
||||
var oppsiteValue = oppsiteExpression.Operand;
|
||||
if (oppsiteValue.Type.IsEnum())
|
||||
{
|
||||
value = UtilMethods.ChangeType2(value, oppsiteValue.Type).ToString();
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
private bool IsEnumString(object value)
|
||||
{
|
||||
return this.Context.TableEnumIsString == true
|
||||
&& value != null
|
||||
&& value.IsInt()
|
||||
&& base.BaseParameter?.OppsiteExpression != null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -325,17 +325,4 @@ namespace SqlSugar
|
||||
Check.Exception(isError, ErrorMessage.GetThrowMessage(expression.ToString() + "no support", "不支持表达式" + expression.ToString() + " ,查看导航是否配置正确等 "));
|
||||
}
|
||||
}
|
||||
|
||||
public class MapperSql
|
||||
{
|
||||
public string Sql { get; set; }
|
||||
}
|
||||
|
||||
public class MapperExpressionInfo
|
||||
{
|
||||
public Type Type { get; set; }
|
||||
public EntityInfo EntityInfo { get; set; }
|
||||
public string FieldName { get; set; }
|
||||
public string FieldString { get; set; }
|
||||
}
|
||||
}
|
@@ -21,27 +21,38 @@ namespace SqlSugar
|
||||
case ResolveExpressType.Update:
|
||||
case ResolveExpressType.SelectSingle:
|
||||
case ResolveExpressType.SelectMultiple:
|
||||
if (value != null && value.GetType().IsEnum())
|
||||
{
|
||||
value = Convert.ToInt64(value);
|
||||
}
|
||||
parameter.BaseParameter.CommonTempData = value;
|
||||
value = Select(parameter, value);
|
||||
break;
|
||||
case ResolveExpressType.WhereSingle:
|
||||
case ResolveExpressType.WhereMultiple:
|
||||
if (isSetTempData)
|
||||
{
|
||||
baseParameter.CommonTempData = value;
|
||||
}
|
||||
else
|
||||
{
|
||||
AppendValue(parameter, isLeft, value);
|
||||
}
|
||||
Where(parameter, isLeft, value, baseParameter, isSetTempData);
|
||||
break;
|
||||
case ResolveExpressType.FieldSingle:
|
||||
case ResolveExpressType.FieldMultiple:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void Where(ExpressionParameter parameter, bool? isLeft, object value, ExpressionParameter baseParameter, bool isSetTempData)
|
||||
{
|
||||
if (isSetTempData)
|
||||
{
|
||||
baseParameter.CommonTempData = value;
|
||||
}
|
||||
else
|
||||
{
|
||||
AppendValue(parameter, isLeft, value);
|
||||
}
|
||||
}
|
||||
|
||||
private static object Select(ExpressionParameter parameter, object value)
|
||||
{
|
||||
if (value != null && value.GetType().IsEnum())
|
||||
{
|
||||
value = Convert.ToInt64(value);
|
||||
}
|
||||
parameter.BaseParameter.CommonTempData = value;
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -10,6 +10,7 @@ namespace SqlSugar
|
||||
public class MemberExpressionResolve : BaseResolve
|
||||
{
|
||||
public ExpressionParameter Parameter { get; set; }
|
||||
|
||||
public MemberExpressionResolve(ExpressionParameter parameter) : base(parameter)
|
||||
{
|
||||
ExpressionParameter baseParameter;
|
||||
@@ -60,61 +61,11 @@ namespace SqlSugar
|
||||
}
|
||||
else if (IsConvertMemberName(expression))
|
||||
{
|
||||
var memParameter = (expression.Expression as UnaryExpression).Operand as ParameterExpression;
|
||||
var name = ExpressionTool.GetMemberName(expression);
|
||||
if (this.Context.IsSingle)
|
||||
{
|
||||
AppendMember(parameter, isLeft, this.Context.GetTranslationColumnName(name));
|
||||
}
|
||||
else
|
||||
{
|
||||
AppendMember(parameter, isLeft, this.Context.GetTranslationColumnName(memParameter.Name + "." + name));
|
||||
}
|
||||
ResolveConvertMemberName(parameter, expression, isLeft);
|
||||
}
|
||||
else if (isMemberValue)
|
||||
{
|
||||
var nav = new OneToOneNavgateExpression(this.Context?.SugarContext?.Context);
|
||||
var navN = new OneToOneNavgateExpressionN(this.Context?.SugarContext?.Context);
|
||||
if (nav.IsNavgate(expression))
|
||||
{
|
||||
if (this.Context?.SugarContext?.QueryBuilder?.JoinQueryInfos != null)
|
||||
{
|
||||
var p=expression.Expression.ObjToString();
|
||||
var querybuilder = this.Context?.SugarContext?.QueryBuilder;
|
||||
var joinInfos = querybuilder.JoinQueryInfos;
|
||||
var joinInfo=joinInfos.FirstOrDefault(it => $"{querybuilder.TableShortName}.{ it.ShortName.Replace("pnv_","")}" == p);
|
||||
if (joinInfo != null)
|
||||
{
|
||||
var columnInfo=nav.ProPertyEntity.Columns.FirstOrDefault(it => it.PropertyName == nav.MemberName);
|
||||
var value =new MapperSql() { Sql = joinInfo.ShortName + "." + columnInfo.DbColumnName };
|
||||
|
||||
if (isSetTempData)
|
||||
{
|
||||
baseParameter.CommonTempData = value;
|
||||
}
|
||||
else
|
||||
{
|
||||
AppendValue(parameter, isLeft, value);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
DefaultOneToOne(parameter, baseParameter, isLeft, isSetTempData, nav);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
DefaultOneToOne(parameter, baseParameter, isLeft, isSetTempData, nav);
|
||||
}
|
||||
}
|
||||
else if (navN.IsNavgate(expression))
|
||||
{
|
||||
DefaultOneToOneN(parameter, baseParameter, isLeft, isSetTempData, navN);
|
||||
}
|
||||
else
|
||||
{
|
||||
ResolveMemberValue(parameter, baseParameter, isLeft, isSetTempData, expression);
|
||||
}
|
||||
ResolveMemberValue(parameter, baseParameter, expression, isLeft, isSetTempData);
|
||||
}
|
||||
else if (fieldIsBool && !isField && !isSelectField)
|
||||
{
|
||||
@@ -126,6 +77,7 @@ namespace SqlSugar
|
||||
}
|
||||
}
|
||||
|
||||
#region Navigate
|
||||
private void DefaultOneToOneN(ExpressionParameter parameter, ExpressionParameter baseParameter, bool? isLeft, bool isSetTempData, OneToOneNavgateExpressionN navN)
|
||||
{
|
||||
var value = navN.GetMemberSql();
|
||||
@@ -156,12 +108,7 @@ namespace SqlSugar
|
||||
}
|
||||
}
|
||||
|
||||
private static bool IsConvertMemberName(MemberExpression expression)
|
||||
{
|
||||
return expression.Expression is UnaryExpression && (expression.Expression as UnaryExpression).Operand is ParameterExpression;
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region Resolve default
|
||||
private void ResolveDefault(ExpressionParameter parameter, ExpressionParameter baseParameter, MemberExpression expression, bool? isLeft, bool isSetTempData, bool isSingle)
|
||||
@@ -307,6 +254,67 @@ namespace SqlSugar
|
||||
#endregion
|
||||
|
||||
#region Resolve special member
|
||||
|
||||
private void ResolveMemberValue(ExpressionParameter parameter, ExpressionParameter baseParameter, MemberExpression expression, bool? isLeft, bool isSetTempData)
|
||||
{
|
||||
var nav = new OneToOneNavgateExpression(this.Context?.SugarContext?.Context);
|
||||
var navN = new OneToOneNavgateExpressionN(this.Context?.SugarContext?.Context);
|
||||
if (nav.IsNavgate(expression))
|
||||
{
|
||||
if (this.Context?.SugarContext?.QueryBuilder?.JoinQueryInfos != null)
|
||||
{
|
||||
var p = expression.Expression.ObjToString();
|
||||
var querybuilder = this.Context?.SugarContext?.QueryBuilder;
|
||||
var joinInfos = querybuilder.JoinQueryInfos;
|
||||
var joinInfo = joinInfos.FirstOrDefault(it => $"{querybuilder.TableShortName}.{it.ShortName.Replace("pnv_", "")}" == p);
|
||||
if (joinInfo != null)
|
||||
{
|
||||
var columnInfo = nav.ProPertyEntity.Columns.FirstOrDefault(it => it.PropertyName == nav.MemberName);
|
||||
var value = new MapperSql() { Sql = joinInfo.ShortName + "." + columnInfo.DbColumnName };
|
||||
|
||||
if (isSetTempData)
|
||||
{
|
||||
baseParameter.CommonTempData = value;
|
||||
}
|
||||
else
|
||||
{
|
||||
AppendValue(parameter, isLeft, value);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
DefaultOneToOne(parameter, baseParameter, isLeft, isSetTempData, nav);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
DefaultOneToOne(parameter, baseParameter, isLeft, isSetTempData, nav);
|
||||
}
|
||||
}
|
||||
else if (navN.IsNavgate(expression))
|
||||
{
|
||||
DefaultOneToOneN(parameter, baseParameter, isLeft, isSetTempData, navN);
|
||||
}
|
||||
else
|
||||
{
|
||||
ResolveMemberValue(parameter, baseParameter, isLeft, isSetTempData, expression);
|
||||
}
|
||||
}
|
||||
|
||||
private void ResolveConvertMemberName(ExpressionParameter parameter, MemberExpression expression, bool? isLeft)
|
||||
{
|
||||
var memParameter = (expression.Expression as UnaryExpression).Operand as ParameterExpression;
|
||||
var name = ExpressionTool.GetMemberName(expression);
|
||||
if (this.Context.IsSingle)
|
||||
{
|
||||
AppendMember(parameter, isLeft, this.Context.GetTranslationColumnName(name));
|
||||
}
|
||||
else
|
||||
{
|
||||
AppendMember(parameter, isLeft, this.Context.GetTranslationColumnName(memParameter.Name + "." + name));
|
||||
}
|
||||
}
|
||||
|
||||
private void ResolveDayOfWeek(ExpressionParameter parameter, bool? isLeft, MemberExpression expression)
|
||||
{
|
||||
var exp = expression.Expression;
|
||||
@@ -600,6 +608,11 @@ namespace SqlSugar
|
||||
#endregion
|
||||
|
||||
#region Helper
|
||||
private static bool IsConvertMemberName(MemberExpression expression)
|
||||
{
|
||||
return expression.Expression is UnaryExpression && (expression.Expression as UnaryExpression).Operand is ParameterExpression;
|
||||
}
|
||||
|
||||
private static bool IsDateDiff(MemberExpression expression)
|
||||
{
|
||||
return
|
||||
|
@@ -12,19 +12,11 @@ namespace SqlSugar
|
||||
{
|
||||
var expression = base.Expression as MemberExpression;
|
||||
var isLeft = parameter.IsLeft;
|
||||
object value = null;
|
||||
var isField = expression.Member is System.Reflection.FieldInfo;
|
||||
var isProperty = expression.Member is System.Reflection.PropertyInfo;
|
||||
var baseParameter = parameter.BaseParameter;
|
||||
var isSetTempData = baseParameter.CommonTempData.HasValue() && baseParameter.CommonTempData.Equals(CommonTempDataType.Result);
|
||||
if (isField)
|
||||
{
|
||||
value = ExpressionTool.GetFiledValue(expression);
|
||||
}
|
||||
else if (isProperty)
|
||||
{
|
||||
value = ExpressionTool.GetPropertyValue(expression);
|
||||
}
|
||||
object value = GetValue(expression, isField, isProperty);
|
||||
switch (base.Context.ResolveType)
|
||||
{
|
||||
case ResolveExpressType.WhereSingle:
|
||||
@@ -32,15 +24,8 @@ namespace SqlSugar
|
||||
case ResolveExpressType.SelectSingle:
|
||||
case ResolveExpressType.SelectMultiple:
|
||||
case ResolveExpressType.Update:
|
||||
if (isSetTempData)
|
||||
{
|
||||
baseParameter.CommonTempData = value;
|
||||
}
|
||||
else
|
||||
{
|
||||
AppendValue(parameter, isLeft, value);
|
||||
}
|
||||
break;
|
||||
Update(parameter, isLeft, baseParameter, isSetTempData, value);
|
||||
break;
|
||||
case ResolveExpressType.FieldSingle:
|
||||
break;
|
||||
case ResolveExpressType.FieldMultiple:
|
||||
@@ -49,6 +34,33 @@ namespace SqlSugar
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void Update(ExpressionParameter parameter, bool? isLeft, ExpressionParameter baseParameter, bool isSetTempData, object value)
|
||||
{
|
||||
if (isSetTempData)
|
||||
{
|
||||
baseParameter.CommonTempData = value;
|
||||
}
|
||||
else
|
||||
{
|
||||
AppendValue(parameter, isLeft, value);
|
||||
}
|
||||
}
|
||||
|
||||
private static object GetValue(MemberExpression expression, bool isField, bool isProperty)
|
||||
{
|
||||
object value = null;
|
||||
if (isField)
|
||||
{
|
||||
value = ExpressionTool.GetFiledValue(expression);
|
||||
}
|
||||
else if (isProperty)
|
||||
{
|
||||
value = ExpressionTool.GetPropertyValue(expression);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,244 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
/// <summary>
|
||||
/// MethodCall base DateFomat
|
||||
/// </summary>
|
||||
public partial class MethodCallExpressionResolve : BaseResolve
|
||||
{
|
||||
public string GeDateFormat(string formatString, string value)
|
||||
{
|
||||
if (IsOracle() && formatString == "yyyy-MM-dd HH:mm:ss")
|
||||
{
|
||||
return $"to_char({value},'yyyy-MM-dd HH:mi:ss') ";
|
||||
}
|
||||
else if (IsOracle() || IsPg())
|
||||
{
|
||||
if (formatString.HasValue() && formatString.Contains("hh:mm"))
|
||||
{
|
||||
formatString = formatString.Replace("hh:mm", "hh:mi");
|
||||
}
|
||||
else if (formatString.HasValue() && formatString.Contains("hhmm"))
|
||||
{
|
||||
formatString = formatString.Replace("hhmm", "hhmi");
|
||||
}
|
||||
else if (formatString.HasValue() && formatString.Contains("HH:mm"))
|
||||
{
|
||||
formatString = formatString.Replace("HH:mm", "HH:mi");
|
||||
}
|
||||
else if (formatString.HasValue() && formatString.Contains("HHmm"))
|
||||
{
|
||||
formatString = formatString.Replace("HHmm", "HHmi");
|
||||
}
|
||||
return $"to_char({value},'{formatString}') ";
|
||||
}
|
||||
else if (IsSqlite() && formatString == "yyyy-MM-dd")
|
||||
{
|
||||
return $"strftime('%Y-%m-%d', {value})";
|
||||
}
|
||||
else if (IsSqlite() && formatString == "yyyy-MM-dd HH:mm:ss")
|
||||
{
|
||||
return $"strftime('%Y-%m-%d %H:%M:%S', {value})";
|
||||
}
|
||||
else if (IsSqlite() && formatString == "yyyy-MM-dd hh:mm:ss")
|
||||
{
|
||||
return $"strftime('%Y-%m-%d %H:%M:%S', {value})";
|
||||
}
|
||||
else if (IsSqlite() && formatString == "yyyy-MM")
|
||||
{
|
||||
return $"strftime('%Y-%m', {value})";
|
||||
}
|
||||
else if (IsSqlite() && formatString == "yyyyMM")
|
||||
{
|
||||
return $"strftime('%Y%m', {value})";
|
||||
}
|
||||
else if (IsSqlite() && formatString == "yyyyMMdd")
|
||||
{
|
||||
return $"strftime('%Y%m%d', {value})";
|
||||
}
|
||||
else if (IsSqlite() && formatString.Contains("%"))
|
||||
{
|
||||
return $"strftime('{formatString}', {value})";
|
||||
}
|
||||
else if (IsMySql() && formatString == "yyyy-MM-dd")
|
||||
{
|
||||
return $"DATE_FORMAT({value}, '%Y-%m-%d')";
|
||||
}
|
||||
else if (IsMySql() && formatString == "yyyy-MM")
|
||||
{
|
||||
return $"DATE_FORMAT({value}, '%Y-%m')";
|
||||
}
|
||||
else if (IsMySql() && formatString == "yyyyMM")
|
||||
{
|
||||
return $"DATE_FORMAT({value}, '%Y%m')";
|
||||
}
|
||||
else if (IsMySql() && formatString == "yyyyMMdd")
|
||||
{
|
||||
return $"DATE_FORMAT({value}, '%Y%m%d')";
|
||||
}
|
||||
else if (IsMySql() && formatString == "yyyy-MM-dd HH:mm:ss")
|
||||
{
|
||||
return $"DATE_FORMAT({value}, '%Y-%m-%d %H:%i:%S')";
|
||||
}
|
||||
else if (IsMySql() && formatString == "yyyy-MM-dd hh:mm:ss")
|
||||
{
|
||||
return $"DATE_FORMAT({value}, '%Y-%m-%d %H:%i:%S')";
|
||||
}
|
||||
else if (IsMySql() && formatString.Contains("%"))
|
||||
{
|
||||
return $"DATE_FORMAT({value}, '{formatString}')";
|
||||
}
|
||||
else if (formatString == "yyyy-MM-dd" && IsSqlServer())
|
||||
{
|
||||
return $"CONVERT(varchar(100),convert(datetime,{value}), 23)";
|
||||
}
|
||||
else if (formatString == "yyyy-MM" && IsSqlServer())
|
||||
{
|
||||
return $"CONVERT(varchar(7),convert(datetime,{value}), 23)";
|
||||
}
|
||||
else if (formatString == "yyyy-MM-dd HH:mm:ss" && IsSqlServer())
|
||||
{
|
||||
return $"CONVERT(varchar(100),convert(datetime,{value}), 120)";
|
||||
}
|
||||
else if (formatString == "yyyy-MM-dd hh:mm:ss" && IsSqlServer())
|
||||
{
|
||||
return $"CONVERT(varchar(100),convert(datetime,{value}), 120)";
|
||||
}
|
||||
else if (formatString == "yyyy-MM-dd HH:mm" && IsSqlServer())
|
||||
{
|
||||
return $"CONVERT(varchar(16),convert(datetime,{value}), 120)";
|
||||
}
|
||||
else if (formatString == "yyyy-MM-dd hh:mm" && IsSqlServer())
|
||||
{
|
||||
return $"CONVERT(varchar(16),convert(datetime,{value}), 120)";
|
||||
}
|
||||
else if (formatString == "yyyy-MM-dd hh:mm:ss.ms" && IsSqlServer())
|
||||
{
|
||||
return $"CONVERT(varchar(100),convert(datetime,{value}), 121)";
|
||||
}
|
||||
else if (IsSqlServer() && formatString != null && formatString.IsInt())
|
||||
{
|
||||
return string.Format("CONVERT(varchar(100),convert(datetime,{0}), {1})", value, formatString);
|
||||
}
|
||||
else if (IsSqlServer())
|
||||
{
|
||||
return string.Format("FORMAT({0},'{1}','en-US')", value, formatString);
|
||||
}
|
||||
var parameter = new MethodCallExpressionArgs() { IsMember = true, MemberValue = DateType.Year };
|
||||
var parameter2 = new MethodCallExpressionArgs() { IsMember = true, MemberName = value };
|
||||
var parameters = new MethodCallExpressionModel() { Args = new List<MethodCallExpressionArgs>() { parameter2, parameter } };
|
||||
var begin = @"^";
|
||||
var end = @"$";
|
||||
formatString = formatString.Replace("yyyy", begin + UtilMethods.ConvertStringToNumbers(this.GetMethodValue("DateValue", parameters).ObjToString()) + end);
|
||||
formatString = formatString.Replace("yy", begin + UtilMethods.ConvertStringToNumbers(this.GetMethodValue("DateValue", parameters).ObjToString()) + end);
|
||||
|
||||
parameters.Args.Last().MemberValue = DateType.Month;
|
||||
if (IsMySql())
|
||||
{
|
||||
formatString = formatString.Replace("MM", begin + UtilMethods.ConvertStringToNumbers("LPAD(" + this.GetMethodValue("DateValue", parameters).ObjToString() + ",2,0)") + end);
|
||||
}
|
||||
else if (IsSqlite())
|
||||
{
|
||||
formatString = formatString.Replace("MM", begin + UtilMethods.ConvertStringToNumbers("SUBSTR('00' ||" + this.GetMethodValue("DateValue", parameters).ObjToString() + ", -2, 2)") + end);
|
||||
}
|
||||
else if (IsPg())
|
||||
{
|
||||
formatString = formatString.Replace("MM", begin + UtilMethods.ConvertStringToNumbers("lpad(cast(" + this.GetMethodValue("DateValue", parameters).ObjToString() + " as varchar(20)),2,'0')") + end);
|
||||
}
|
||||
else if (IsOracle())
|
||||
{
|
||||
formatString = formatString.Replace("MM", begin + UtilMethods.ConvertStringToNumbers("lpad(cast(" + this.GetMethodValue("DateValue", parameters).ObjToString() + " as varchar(20)),2,'0')") + end);
|
||||
}
|
||||
else
|
||||
{
|
||||
formatString = formatString.Replace("MM", begin + UtilMethods.ConvertStringToNumbers(this.GetMethodValue("DateValue", parameters).ObjToString()) + end);
|
||||
}
|
||||
formatString = formatString.Replace("M", begin + UtilMethods.ConvertStringToNumbers(this.GetMethodValue("DateValue", parameters).ObjToString()) + end);
|
||||
|
||||
parameters.Args.Last().MemberValue = DateType.Day;
|
||||
if (IsSqlServer())
|
||||
{
|
||||
formatString = formatString.Replace("dd", begin + UtilMethods.ConvertStringToNumbers(string.Format("CASE WHEN LEN({0})=1 THEN '0'+ {0} else {0} end", this.GetMethodValue("DateValue", parameters))) + end);
|
||||
}
|
||||
formatString = formatString.Replace("dd", begin + UtilMethods.ConvertStringToNumbers(this.GetMethodValue("DateValue", parameters).ObjToString()) + end);
|
||||
formatString = formatString.Replace("d", begin + UtilMethods.ConvertStringToNumbers(this.GetMethodValue("DateValue", parameters).ObjToString()) + end);
|
||||
|
||||
parameters.Args.Last().MemberValue = DateType.Hour;
|
||||
formatString = Regex.Replace(formatString, "hh", begin + UtilMethods.ConvertStringToNumbers(this.GetMethodValue("DateValue", parameters).ObjToString()) + end, RegexOptions.IgnoreCase);
|
||||
formatString = Regex.Replace(formatString, "h", begin + UtilMethods.ConvertStringToNumbers(this.GetMethodValue("DateValue", parameters).ObjToString()) + end, RegexOptions.IgnoreCase);
|
||||
|
||||
parameters.Args.Last().MemberValue = DateType.Minute;
|
||||
formatString = formatString.Replace("mm", begin + UtilMethods.ConvertStringToNumbers(this.GetMethodValue("DateValue", parameters).ObjToString()) + end);
|
||||
formatString = formatString.Replace("m", begin + UtilMethods.ConvertStringToNumbers(this.GetMethodValue("DateValue", parameters).ObjToString()) + end);
|
||||
|
||||
parameters.Args.Last().MemberValue = DateType.Second;
|
||||
formatString = formatString.Replace("ss", begin + UtilMethods.ConvertStringToNumbers(this.GetMethodValue("DateValue", parameters).ObjToString()) + end);
|
||||
formatString = formatString.Replace("s", begin + UtilMethods.ConvertStringToNumbers(this.GetMethodValue("DateValue", parameters).ObjToString()) + end);
|
||||
|
||||
if (!IsSqlite())
|
||||
{
|
||||
parameters.Args.Last().MemberValue = DateType.Millisecond;
|
||||
formatString = formatString.Replace("ms", begin + UtilMethods.ConvertStringToNumbers(this.GetMethodValue("DateValue", parameters).ObjToString()) + end);
|
||||
}
|
||||
|
||||
var items = Regex.Matches(formatString, @"\^\d+\$").Cast<Match>().ToList();
|
||||
foreach (var item in items)
|
||||
{
|
||||
formatString = formatString.Replace(item.Value, "$@" + UtilMethods.ConvertNumbersToString(item.Value.TrimStart('^').TrimEnd('$')) + "$");
|
||||
}
|
||||
var strings = formatString.TrimStart('$').TrimEnd('$').Split('$');
|
||||
var joinStringParameter = new MethodCallExpressionModel()
|
||||
{
|
||||
Args = new List<MethodCallExpressionArgs>()
|
||||
};
|
||||
foreach (var r in strings)
|
||||
{
|
||||
if (r != "" && r.Substring(0, 1) == "@")
|
||||
{
|
||||
joinStringParameter.Args.Add(new MethodCallExpressionArgs()
|
||||
{
|
||||
MemberName = r.TrimStart('@')
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
var name = base.AppendParameter(r);
|
||||
joinStringParameter.Args.Add(new MethodCallExpressionArgs()
|
||||
{
|
||||
MemberName = name
|
||||
});
|
||||
}
|
||||
}
|
||||
return this.GetMethodValue("MergeString", joinStringParameter).ObjToString();
|
||||
}
|
||||
private bool IsSqlServer()
|
||||
{
|
||||
return this.Context is SqlServerExpressionContext;
|
||||
}
|
||||
private bool IsMySql()
|
||||
{
|
||||
var name = this.Context.GetType().Name;
|
||||
var result = (name == "MySqlExpressionContext");
|
||||
return result;
|
||||
}
|
||||
private bool IsSqlite()
|
||||
{
|
||||
return this.Context is SqliteExpressionContext;
|
||||
}
|
||||
private bool IsPg()
|
||||
{
|
||||
return this.Context is PostgreSQLExpressionContext;
|
||||
}
|
||||
private bool IsOracle()
|
||||
{
|
||||
return this.Context is OracleExpressionContext;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,869 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
/// <summary>
|
||||
///MethodCall Helper
|
||||
/// </summary>
|
||||
public partial class MethodCallExpressionResolve : BaseResolve
|
||||
{
|
||||
private void CusMethod(ExpressionParameter parameter, MethodCallExpression express, bool? isLeft)
|
||||
{
|
||||
try
|
||||
{
|
||||
OneToManyNavgateExpression nav = new OneToManyNavgateExpression(this.Context?.SugarContext?.Context, this);
|
||||
if (nav.IsNavgate(express))
|
||||
{
|
||||
var sql = nav.GetSql();
|
||||
SetNavigateResult();
|
||||
this.Context.SingleTableNameSubqueryShortName = nav.ShorName;
|
||||
base.AppendValue(parameter, isLeft, sql);
|
||||
return;
|
||||
}
|
||||
|
||||
OneToManyNavgateExpressionN nav2 = new OneToManyNavgateExpressionN(this.Context?.SugarContext?.Context, this);
|
||||
if (nav2.IsNavgate(express))
|
||||
{
|
||||
var sql = nav2.GetSql();
|
||||
SetNavigateResult();
|
||||
this.Context.SingleTableNameSubqueryShortName = nav2.shorName;
|
||||
base.AppendValue(parameter, isLeft, sql);
|
||||
return;
|
||||
}
|
||||
|
||||
var constValue = ExpressionTool.DynamicInvoke(express);
|
||||
if (constValue is MapperSql)
|
||||
{
|
||||
constValue = (constValue as MapperSql).Sql;
|
||||
base.AppendValue(parameter, isLeft, constValue);
|
||||
return;
|
||||
}
|
||||
parameter.BaseParameter.CommonTempData = constValue;
|
||||
var parameterName = base.AppendParameter(constValue);
|
||||
if (parameter.BaseParameter.CommonTempData != null && parameter.BaseParameter.CommonTempData.Equals(CommonTempDataType.Result))
|
||||
{
|
||||
this.Context.Result.Append(parameterName);
|
||||
}
|
||||
else
|
||||
{
|
||||
base.AppendValue(parameter, isLeft, parameterName);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (ex is SqlSugarException)
|
||||
{
|
||||
Check.Exception(true, string.Format(ex.Message, express.Method.Name));
|
||||
}
|
||||
else
|
||||
{
|
||||
Check.Exception(true, string.Format(ErrorMessage.MethodError, express.Method.Name));
|
||||
}
|
||||
}
|
||||
}
|
||||
private static bool MethodValueIsTrue(object methodValue)
|
||||
{
|
||||
return methodValue != null && methodValue.ToString().Contains("THEN true ELSE false END");
|
||||
}
|
||||
private object packIfElse(object methodValue)
|
||||
{
|
||||
methodValue = this.Context.DbMehtods.CaseWhen(new List<KeyValuePair<string, string>>() {
|
||||
new KeyValuePair<string, string>("IF",methodValue.ObjToString()),
|
||||
new KeyValuePair<string, string>("Return","1"),
|
||||
new KeyValuePair<string, string>("End","0")
|
||||
});
|
||||
return methodValue;
|
||||
}
|
||||
private void SetShortName(Expression exp)
|
||||
{
|
||||
var lamExp = (exp as LambdaExpression);
|
||||
if (lamExp.Parameters != null && lamExp.Parameters.Count == 1)
|
||||
{
|
||||
if (this.Context.SingleTableNameSubqueryShortName == null)
|
||||
{
|
||||
this.Context.SingleTableNameSubqueryShortName = lamExp.Parameters.First().Name;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void AppendItem(ExpressionParameter parameter, string name, IEnumerable<Expression> args, MethodCallExpressionModel model, Expression item)
|
||||
{
|
||||
if (ExpressionTool.IsUnConvertExpress(item))
|
||||
{
|
||||
item = (item as UnaryExpression).Operand;
|
||||
}
|
||||
if (this.Context.IsSingle && args.Any(it => ExpressionTool.IsSubQuery(it)) && base.BaseParameter?.BaseParameter?.BaseParameter?.CurrentExpression != null)
|
||||
{
|
||||
var exp = base.BaseParameter?.BaseParameter?.BaseParameter?.CurrentExpression;
|
||||
if (exp is LambdaExpression)
|
||||
{
|
||||
SetShortName(exp);
|
||||
}
|
||||
else if (exp is UnaryExpression)
|
||||
{
|
||||
exp = base.BaseParameter?.BaseParameter?.BaseParameter?.BaseParameter?.CurrentExpression;
|
||||
if (exp is LambdaExpression)
|
||||
{
|
||||
SetShortName(exp);
|
||||
}
|
||||
}
|
||||
}
|
||||
var isBinaryExpression = item is BinaryExpression || item is MethodCallExpression;
|
||||
var isConst = item is ConstantExpression;
|
||||
var isIIF = name == "IIF";
|
||||
var isSubIIF = (isIIF && item.ToString().StartsWith("IIF"));
|
||||
var isIFFBoolMember = isIIF && (item is MemberExpression) && (item as MemberExpression).Type == UtilConstants.BoolType;
|
||||
var isIFFUnary = isIIF && (item is UnaryExpression) && (item as UnaryExpression).Operand.Type == UtilConstants.BoolType;
|
||||
var isIFFBoolBinary = isIIF && (item is BinaryExpression) && (item as BinaryExpression).Type == UtilConstants.BoolType;
|
||||
var isIFFBoolMethod = isIIF && (item is MethodCallExpression) && (item as MethodCallExpression).Type == UtilConstants.BoolType;
|
||||
var isFirst = item == args.First();
|
||||
var isBoolValue = item.Type == UtilConstants.BoolType && item.ToString().StartsWith("value(");
|
||||
if (isFirst && isIIF && isConst)
|
||||
{
|
||||
var value = (item as ConstantExpression).Value.ObjToBool() ? this.Context.DbMehtods.True() : this.Context.DbMehtods.False();
|
||||
var methodCallExpressionArgs = new MethodCallExpressionArgs()
|
||||
{
|
||||
IsMember = true,
|
||||
MemberName = value,
|
||||
MemberValue = value
|
||||
};
|
||||
model.Args.Add(methodCallExpressionArgs);
|
||||
}
|
||||
else if (isIFFUnary && !isFirst)
|
||||
{
|
||||
AppendModelByIIFMember(parameter, model, (item as UnaryExpression).Operand);
|
||||
}
|
||||
else if (isIFFBoolMember && !isFirst)
|
||||
{
|
||||
AppendModelByIIFMember(parameter, model, item);
|
||||
|
||||
}
|
||||
else if (isIFFBoolBinary && !isFirst)
|
||||
{
|
||||
var binaryExp = item as BinaryExpression;
|
||||
var binaryExpEqual = binaryExp != null && ExpressionTool.IsComparisonOperatorBool(binaryExp);
|
||||
if (binaryExpEqual)
|
||||
{
|
||||
var expValue = GetNewExpressionValue(item);
|
||||
expValue = this.Context.DbMehtods.IIF(new MethodCallExpressionModel()
|
||||
{
|
||||
Name = "IIF",
|
||||
Args = new List<MethodCallExpressionArgs>()
|
||||
{
|
||||
new MethodCallExpressionArgs(){
|
||||
IsMember=true,
|
||||
MemberName=expValue
|
||||
},
|
||||
new MethodCallExpressionArgs(){
|
||||
IsMember=true,
|
||||
MemberName= Context.DbMehtods.TrueValue()
|
||||
},
|
||||
new MethodCallExpressionArgs(){
|
||||
IsMember=true,
|
||||
MemberName= Context.DbMehtods.FalseValue()
|
||||
}
|
||||
}
|
||||
});
|
||||
model.Args.Add(new MethodCallExpressionArgs()
|
||||
{
|
||||
IsMember = false,
|
||||
MemberName = expValue,
|
||||
MemberValue = expValue
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
AppendModelByIIFBinary(parameter, model, item);
|
||||
}
|
||||
|
||||
}
|
||||
else if (isIFFBoolMethod && !isFirst)
|
||||
{
|
||||
AppendModelByIIFMethod(parameter, model, item);
|
||||
}
|
||||
else if (isBinaryExpression)
|
||||
{
|
||||
model.Args.Add(GetMethodCallArgs(parameter, item, name));
|
||||
}
|
||||
else if (isSubIIF)
|
||||
{
|
||||
model.Args.Add(GetMethodCallArgs(parameter, item));
|
||||
}
|
||||
else if (isBoolValue && !isIIF && item is MemberExpression)
|
||||
{
|
||||
model.Args.Add(GetMethodCallArgs(parameter, (item as MemberExpression).Expression));
|
||||
}
|
||||
else if (isBoolValue && isIIF && item is MemberExpression)
|
||||
{
|
||||
var argItem = GetMethodCallArgs(parameter, (item as MemberExpression).Expression);
|
||||
if (argItem.IsMember)
|
||||
{
|
||||
var pName = this.Context.SqlParameterKeyWord + "true_0";
|
||||
if (!this.Context.Parameters.Any(it => it.ParameterName == pName))
|
||||
this.Context.Parameters.Add(new SugarParameter(pName, true));
|
||||
argItem.MemberName = $" {argItem.MemberName}={pName} ";
|
||||
}
|
||||
model.Args.Add(argItem);
|
||||
}
|
||||
else
|
||||
{
|
||||
AppendModel(parameter, model, item);
|
||||
}
|
||||
}
|
||||
private void AppendModelByIIFMember(ExpressionParameter parameter, MethodCallExpressionModel model, Expression item)
|
||||
{
|
||||
parameter.CommonTempData = CommonTempDataType.Result;
|
||||
base.Expression = item;
|
||||
base.Start();
|
||||
var methodCallExpressionArgs = new MethodCallExpressionArgs()
|
||||
{
|
||||
IsMember = parameter.ChildExpression is MemberExpression,
|
||||
MemberName = parameter.CommonTempData
|
||||
};
|
||||
if (methodCallExpressionArgs.IsMember && parameter.ChildExpression != null && parameter.ChildExpression.ToString() == "DateTime.Now")
|
||||
{
|
||||
methodCallExpressionArgs.IsMember = false;
|
||||
}
|
||||
var value = methodCallExpressionArgs.MemberName;
|
||||
if (methodCallExpressionArgs.IsMember)
|
||||
{
|
||||
var childExpression = parameter.ChildExpression as MemberExpression;
|
||||
if (childExpression.Expression != null && childExpression.Expression is ConstantExpression)
|
||||
{
|
||||
methodCallExpressionArgs.IsMember = false;
|
||||
}
|
||||
}
|
||||
if (methodCallExpressionArgs.IsMember == false)
|
||||
{
|
||||
var parameterName = this.Context.SqlParameterKeyWord + ExpressionConst.MethodConst + this.Context.ParameterIndex;
|
||||
this.Context.ParameterIndex++;
|
||||
methodCallExpressionArgs.MemberName = parameterName;
|
||||
methodCallExpressionArgs.MemberValue = value;
|
||||
this.Context.Parameters.Add(new SugarParameter(parameterName, value));
|
||||
}
|
||||
model.Args.Add(methodCallExpressionArgs);
|
||||
parameter.ChildExpression = null;
|
||||
}
|
||||
private void AppendModelByIIFBinary(ExpressionParameter parameter, MethodCallExpressionModel model, Expression item)
|
||||
{
|
||||
Check.Exception(true, "The SqlFunc.IIF(arg1,arg2,arg3) , {0} argument do not support ", item.ToString());
|
||||
}
|
||||
private void AppendModelByIIFMethod(ExpressionParameter parameter, MethodCallExpressionModel model, Expression item)
|
||||
{
|
||||
var methodExpression = item as MethodCallExpression;
|
||||
if (methodExpression.Method.Name.IsIn("ToBool", "ToBoolean", "IIF"))
|
||||
{
|
||||
model.Args.Add(base.GetMethodCallArgs(parameter, item));
|
||||
}
|
||||
else
|
||||
{
|
||||
Check.Exception(true, "The SqlFunc.IIF(arg1,arg2,arg3) , {0} argument do not support ", item.ToString());
|
||||
}
|
||||
}
|
||||
private void AppendModel(ExpressionParameter parameter, MethodCallExpressionModel model, Expression item)
|
||||
{
|
||||
parameter.CommonTempData = CommonTempDataType.Result;
|
||||
base.Expression = item;
|
||||
if (item.Type == UtilConstants.DateType && parameter.CommonTempData.ObjToString() == CommonTempDataType.Result.ToString() && item.ToString() == "DateTime.Now.Date")
|
||||
{
|
||||
parameter.CommonTempData = DateTime.Now.Date;
|
||||
}
|
||||
else if (item is ConditionalExpression)
|
||||
{
|
||||
parameter.CommonTempData = GetNewExpressionValue(item);
|
||||
}
|
||||
else if (IsNot(item))
|
||||
{
|
||||
parameter.CommonTempData = GetNewExpressionValue(item);
|
||||
}
|
||||
else if (IsDateDate(item))
|
||||
{
|
||||
parameter.CommonTempData = GetNewExpressionValue(item);
|
||||
}
|
||||
else if (IsDateValue(item))
|
||||
{
|
||||
parameter.CommonTempData = GetNewExpressionValue(item);
|
||||
}
|
||||
else if (model.Name == "ToString" && item is ConstantExpression && (item as ConstantExpression).Type.IsEnum())
|
||||
{
|
||||
parameter.CommonTempData = item.ToString();
|
||||
}
|
||||
else if (IsDateItemValue(item))
|
||||
{
|
||||
parameter.CommonTempData = GetNewExpressionValue(item);
|
||||
}
|
||||
else
|
||||
{
|
||||
base.Start();
|
||||
}
|
||||
var methodCallExpressionArgs = new MethodCallExpressionArgs()
|
||||
{
|
||||
IsMember = parameter.ChildExpression is MemberExpression && !ExpressionTool.IsConstExpression(parameter.ChildExpression as MemberExpression),
|
||||
MemberName = parameter.CommonTempData
|
||||
};
|
||||
if (methodCallExpressionArgs.MemberName is MapperSql)
|
||||
{
|
||||
methodCallExpressionArgs.MemberName = (methodCallExpressionArgs.MemberName as MapperSql).Sql;
|
||||
}
|
||||
if (methodCallExpressionArgs.IsMember && parameter.ChildExpression != null && parameter.ChildExpression.ToString() == "DateTime.Now")
|
||||
{
|
||||
methodCallExpressionArgs.IsMember = false;
|
||||
}
|
||||
var value = methodCallExpressionArgs.MemberName;
|
||||
if (methodCallExpressionArgs.IsMember)
|
||||
{
|
||||
var childExpression = parameter.ChildExpression as MemberExpression;
|
||||
if (childExpression.Expression != null && childExpression.Expression is ConstantExpression)
|
||||
{
|
||||
methodCallExpressionArgs.IsMember = false;
|
||||
}
|
||||
}
|
||||
if (IsDateDate(item) || IsDateValue(item) || IsDateItemValue(item) || item is ConditionalExpression || IsNot(item))
|
||||
{
|
||||
methodCallExpressionArgs.IsMember = true;
|
||||
}
|
||||
if (methodCallExpressionArgs.IsMember == false && (item is MethodCallExpression && item.ToString() == "GetDate()") || (item is UnaryExpression && ((UnaryExpression)item).Operand.ToString() == "GetDate()"))
|
||||
{
|
||||
var parameterName = this.Context.SqlParameterKeyWord + ExpressionConst.MethodConst + this.Context.ParameterIndex;
|
||||
this.Context.ParameterIndex++;
|
||||
methodCallExpressionArgs.MemberName = value;
|
||||
methodCallExpressionArgs.MemberValue = null;
|
||||
}
|
||||
else if (methodCallExpressionArgs.IsMember == false)
|
||||
{
|
||||
var parameterName = this.Context.SqlParameterKeyWord + ExpressionConst.MethodConst + this.Context.ParameterIndex;
|
||||
this.Context.ParameterIndex++;
|
||||
methodCallExpressionArgs.MemberName = parameterName;
|
||||
methodCallExpressionArgs.MemberValue = value;
|
||||
this.Context.Parameters.Add(new SugarParameter(parameterName, value));
|
||||
}
|
||||
model.Args.Add(methodCallExpressionArgs);
|
||||
parameter.ChildExpression = null;
|
||||
}
|
||||
|
||||
|
||||
private void GetConfigValue(MethodCallExpression express, ExpressionParameter parameter)
|
||||
{
|
||||
var exp = express.Arguments[0];
|
||||
var name = Regex.Match(express.Method.ToString(), @"GetConfigValue\[(.+)\]").Groups[1].Value;
|
||||
string code = null;
|
||||
if (express.Arguments.Count > 1)
|
||||
{
|
||||
code = ExpressionTool.GetExpressionValue(express.Arguments[1]) + "";
|
||||
}
|
||||
var entityDb = SqlFuncExtendsion.TableInfos.FirstOrDefault(y => y.Type.Name == name && y.Code == code);
|
||||
Check.Exception(entityDb == null, string.Format("GetConfigValue no configuration Entity={0} UniqueCode={1}", name, code));
|
||||
var entity = new ConfigTableInfo()
|
||||
{
|
||||
Code = entityDb.Code,
|
||||
TableName = entityDb.TableName,
|
||||
Key = entityDb.Key,
|
||||
Parameter = new List<SugarParameter>(),
|
||||
Type = entityDb.Type,
|
||||
Value = entityDb.Value,
|
||||
Where = entityDb.Where
|
||||
};
|
||||
if (entityDb.Parameter != null && entityDb.Parameter.Any())
|
||||
{
|
||||
foreach (var item in entityDb.Parameter)
|
||||
{
|
||||
entity.Parameter.Add(new SugarParameter("", null)
|
||||
{
|
||||
DbType = item.DbType,
|
||||
Direction = item.Direction,
|
||||
IsArray = item.IsArray,
|
||||
IsJson = item.IsJson,
|
||||
IsNullable = item.IsNullable,
|
||||
IsRefCursor = item.IsRefCursor,
|
||||
ParameterName = item.ParameterName,
|
||||
Size = item.Size,
|
||||
SourceColumn = item.SourceColumn,
|
||||
SourceColumnNullMapping = item.SourceColumnNullMapping,
|
||||
SourceVersion = item.SourceVersion,
|
||||
TempDate = item.TempDate,
|
||||
TypeName = item.TypeName,
|
||||
Value = item.Value,
|
||||
_Size = item._Size
|
||||
|
||||
});
|
||||
}
|
||||
}
|
||||
string sql = " (SELECT {0} FROM {1} WHERE {2}={3}";
|
||||
if (ExpressionTool.IsUnConvertExpress(exp))
|
||||
{
|
||||
exp = (exp as UnaryExpression).Operand;
|
||||
}
|
||||
var member = exp as MemberExpression;
|
||||
var it = member.Expression;
|
||||
var type = it.Type;
|
||||
var properyName = member.Member.Name;
|
||||
var eqName = string.Format("{0}.{1}", this.Context.GetTranslationColumnName(it.ToString()), this.Context.GetDbColumnName(type.Name, properyName));
|
||||
if (this.Context.IsSingle)
|
||||
{
|
||||
this.Context.SingleTableNameSubqueryShortName = it.ToString();
|
||||
}
|
||||
sql = string.Format(sql, entity.Value, this.Context.GetTranslationColumnName(entity.TableName), entity.Key, eqName);
|
||||
if (entity.Parameter != null)
|
||||
{
|
||||
foreach (var item in entity.Parameter)
|
||||
{
|
||||
var oldName = item.ParameterName;
|
||||
item.ParameterName = Regex.Split(oldName, "_con_").First() + "_con_" + this.Context.ParameterIndex;
|
||||
entity.Where = entity.Where.Replace(oldName, item.ParameterName);
|
||||
}
|
||||
this.Context.ParameterIndex++;
|
||||
this.Context.Parameters.AddRange(entity.Parameter);
|
||||
}
|
||||
if (entity.Where.HasValue())
|
||||
{
|
||||
sql += " AND " + entity.Where;
|
||||
}
|
||||
sql += " )";
|
||||
if (this.Context.ResolveType.IsIn(ResolveExpressType.SelectMultiple, ResolveExpressType.SelectSingle, ResolveExpressType.Update))
|
||||
{
|
||||
parameter.BaseParameter.CommonTempData = sql;
|
||||
}
|
||||
else
|
||||
{
|
||||
AppendMember(parameter, parameter.IsLeft, sql);
|
||||
}
|
||||
}
|
||||
private object GetMethodValue(string name, MethodCallExpressionModel model)
|
||||
{
|
||||
if (IsExtMethod(name))
|
||||
{
|
||||
model.Expression = this.Expression;
|
||||
model.BaseExpression = this.BaseParameter.CurrentExpression;
|
||||
DbType type = DbType.SqlServer;
|
||||
if (this.Context is SqlServerExpressionContext)
|
||||
type = DbType.SqlServer;
|
||||
else if (this.Context is MySqlExpressionContext)
|
||||
type = DbType.MySql;
|
||||
else if (this.Context is SqliteExpressionContext)
|
||||
type = DbType.Sqlite;
|
||||
else if (this.Context is OracleExpressionContext)
|
||||
type = DbType.Oracle;
|
||||
else if (this.Context is PostgreSQLExpressionContext)
|
||||
type = DbType.PostgreSQL;
|
||||
else if (this.Context.GetType().Name.StartsWith("MySql"))
|
||||
{
|
||||
type = DbType.MySql;
|
||||
}
|
||||
else
|
||||
{
|
||||
type = GetType(this.Context.GetType().Name);
|
||||
}
|
||||
return this.Context.SqlFuncServices.First(it => it.UniqueMethodName == name).MethodValue(model, type, this.Context);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (name == "Parse" && TempParseType.IsIn(UtilConstants.GuidType) && model.Args != null && model.Args.Count() > 1)
|
||||
{
|
||||
name = "Equals";
|
||||
}
|
||||
else if (name == "Parse")
|
||||
{
|
||||
name = "To" + TempParseType.Name;
|
||||
}
|
||||
else if (name == "IsNullOrWhiteSpace")
|
||||
{
|
||||
name = "IsNullOrEmpty";
|
||||
}
|
||||
switch (name)
|
||||
{
|
||||
case "IIF":
|
||||
return this.Context.DbMehtods.IIF(model);
|
||||
case "HasNumber":
|
||||
return this.Context.DbMehtods.HasNumber(model);
|
||||
case "HasValue":
|
||||
return this.Context.DbMehtods.HasValue(model);
|
||||
case "IsNullOrEmpty":
|
||||
return this.Context.DbMehtods.IsNullOrEmpty(model);
|
||||
case "ToLower":
|
||||
return this.Context.DbMehtods.ToLower(model);
|
||||
case "ToUpper":
|
||||
return this.Context.DbMehtods.ToUpper(model);
|
||||
case "Trim":
|
||||
return this.Context.DbMehtods.Trim(model);
|
||||
case "Contains":
|
||||
return this.Context.DbMehtods.Contains(model);
|
||||
case "ContainsArray":
|
||||
if (model.Args[0].MemberValue == null)
|
||||
{
|
||||
var first = this.Context.Parameters.FirstOrDefault(it => it.ParameterName == model.Args[0].MemberName.ObjToString());
|
||||
if (first.HasValue())
|
||||
{
|
||||
model.Args[0].MemberValue = first.Value;
|
||||
}
|
||||
}
|
||||
if (this.Context.TableEnumIsString == true)
|
||||
{
|
||||
List<string> enumStringList = new List<string>();
|
||||
foreach (var inItem in (model.Args[0].MemberValue as IEnumerable))
|
||||
{
|
||||
if (inItem != null)
|
||||
{
|
||||
if (UtilMethods.GetUnderType(inItem.GetType()).IsEnum())
|
||||
{
|
||||
enumStringList.Add(inItem.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
if (enumStringList.Any())
|
||||
{
|
||||
model.Args[0].MemberValue = enumStringList;
|
||||
}
|
||||
}
|
||||
var caResult = this.Context.DbMehtods.ContainsArray(model);
|
||||
this.Context.Parameters.RemoveAll(it => it.ParameterName == model.Args[0].MemberName.ObjToString());
|
||||
return caResult;
|
||||
case "ContainsArrayUseSqlParameters":
|
||||
if (model.Args[0].MemberValue == null)
|
||||
{
|
||||
var first = this.Context.Parameters.FirstOrDefault(it => it.ParameterName == model.Args[0].MemberName.ObjToString());
|
||||
if (first.HasValue())
|
||||
{
|
||||
model.Args[0].MemberValue = first.Value;
|
||||
}
|
||||
}
|
||||
model.Data = this.Context.SqlParameterKeyWord + "INP_" + this.Context.ParameterIndex;
|
||||
this.Context.ParameterIndex++;
|
||||
if (model.Args[0].MemberValue.HasValue())
|
||||
{
|
||||
var inValueIEnumerable = (IEnumerable)model.Args[0].MemberValue;
|
||||
int i = 0;
|
||||
foreach (var item in inValueIEnumerable)
|
||||
{
|
||||
this.Context.Parameters.Add(new SugarParameter(model.Data + "_" + i, item));
|
||||
i++;
|
||||
}
|
||||
}
|
||||
var caResult2 = this.Context.DbMehtods.ContainsArrayUseSqlParameters(model);
|
||||
this.Context.Parameters.RemoveAll(it => it.ParameterName == model.Args[0].MemberName.ObjToString());
|
||||
return caResult2;
|
||||
case "Equals":
|
||||
return this.Context.DbMehtods.Equals(model);
|
||||
case "EqualsNull":
|
||||
return this.Context.DbMehtods.EqualsNull(model);
|
||||
case "DateIsSame":
|
||||
if (model.Args.Count == 2)
|
||||
return this.Context.DbMehtods.DateIsSameDay(model);
|
||||
else
|
||||
{
|
||||
var dsResult = this.Context.DbMehtods.DateIsSameByType(model);
|
||||
this.Context.Parameters.RemoveAll(it => it.ParameterName == model.Args[2].MemberName.ObjToString());
|
||||
return dsResult;
|
||||
}
|
||||
case "DateAdd":
|
||||
if (model.Args.Count == 2)
|
||||
return this.Context.DbMehtods.DateAddDay(model);
|
||||
else
|
||||
{
|
||||
var daResult = this.Context.DbMehtods.DateAddByType(model);
|
||||
this.Context.Parameters.RemoveAll(it => it.ParameterName == model.Args[2].MemberName.ObjToString());
|
||||
return daResult;
|
||||
}
|
||||
case "DateValue":
|
||||
var dvResult = this.Context.DbMehtods.DateValue(model);
|
||||
this.Context.Parameters.RemoveAll(it => it.ParameterName == model.Args[1].MemberName.ObjToString());
|
||||
return dvResult;
|
||||
case "Between":
|
||||
return this.Context.DbMehtods.Between(model);
|
||||
case "StartsWith":
|
||||
return this.Context.DbMehtods.StartsWith(model);
|
||||
case "EndsWith":
|
||||
return this.Context.DbMehtods.EndsWith(model);
|
||||
case "ToInt32":
|
||||
return this.Context.DbMehtods.ToInt32(model);
|
||||
case "ToInt64":
|
||||
return this.Context.DbMehtods.ToInt64(model);
|
||||
case "ToDate":
|
||||
return this.Context.DbMehtods.ToDate(model);
|
||||
case "ToDateTime":
|
||||
return this.Context.DbMehtods.ToDate(model);
|
||||
case "ToTime":
|
||||
return this.Context.DbMehtods.ToTime(model);
|
||||
case "ToString":
|
||||
if (model.Args.Count > 1)
|
||||
{
|
||||
var dateString2 = this.Context.DbMehtods.GetDateString(model.Args.First().MemberName.ObjToString(), model.Args.Last().MemberValue.ObjToString());
|
||||
if (dateString2 != null) return dateString2;
|
||||
return GeDateFormat(model.Args.Last().MemberValue.ObjToString(), model.Args.First().MemberName.ObjToString());
|
||||
}
|
||||
//Check.Exception(model.Args.Count > 1, "ToString (Format) is not supported, Use ToString().If time formatting can be used it.Date.Year+\"-\"+it.Data.Month+\"-\"+it.Date.Day ");
|
||||
return this.Context.DbMehtods.ToString(model);
|
||||
case "ToVarchar":
|
||||
return this.Context.DbMehtods.ToVarchar(model);
|
||||
case "ToDecimal":
|
||||
return this.Context.DbMehtods.ToDecimal(model);
|
||||
case "ToGuid":
|
||||
return this.Context.DbMehtods.ToGuid(model);
|
||||
case "ToDouble":
|
||||
return this.Context.DbMehtods.ToDouble(model);
|
||||
case "ToBool":
|
||||
return this.Context.DbMehtods.ToBool(model);
|
||||
case "ToBoolean":
|
||||
return this.Context.DbMehtods.ToBool(model);
|
||||
case "Substring":
|
||||
return this.Context.DbMehtods.Substring(model);
|
||||
case "Replace":
|
||||
return this.Context.DbMehtods.Replace(model);
|
||||
case "Length":
|
||||
return this.Context.DbMehtods.Length(model);
|
||||
case "AggregateSum":
|
||||
return this.Context.DbMehtods.AggregateSum(model);
|
||||
case "AggregateAvg":
|
||||
return this.Context.DbMehtods.AggregateAvg(model);
|
||||
case "AggregateMin":
|
||||
return this.Context.DbMehtods.AggregateMin(model);
|
||||
case "AggregateMax":
|
||||
return this.Context.DbMehtods.AggregateMax(model);
|
||||
case "AggregateCount":
|
||||
return this.Context.DbMehtods.AggregateCount(model);
|
||||
case "AggregateDistinctCount":
|
||||
return this.Context.DbMehtods.AggregateDistinctCount(model);
|
||||
case "MappingColumn":
|
||||
var mappingColumnResult = this.Context.DbMehtods.MappingColumn(model);
|
||||
var isValid = model.Args[0].IsMember && model.Args[1].IsMember == false;
|
||||
//Check.Exception(!isValid, "SqlFunc.MappingColumn parameters error, The property name on the left, string value on the right");
|
||||
this.Context.Parameters.RemoveAll(it => it.ParameterName == model.Args[1].MemberName.ObjToString());
|
||||
if (mappingColumnResult == "")
|
||||
{
|
||||
return model.Args[1].MemberName.ObjToString().TrimStart('\'').TrimEnd('\'');
|
||||
}
|
||||
return mappingColumnResult;
|
||||
case "IsNull":
|
||||
return this.Context.DbMehtods.IsNull(model);
|
||||
case "MergeString":
|
||||
return this.Context.DbMehtods.MergeString(model.Args.Select(it => it.MemberName.ObjToString()).ToArray());
|
||||
case "SelectAll":
|
||||
case "GetSelfAndAutoFill":
|
||||
this.Context.Parameters.RemoveAll(it => it.ParameterName == model.Args[0].MemberName.ObjToString());
|
||||
var result1 = this.Context.DbMehtods.GetSelfAndAutoFill(model.Args[0].MemberValue.ObjToString(), this.Context.IsSingle);
|
||||
if ((model.Args[0].MemberValue + "") == "." && this.Context.IsSingle)
|
||||
{
|
||||
result1 = this.Context.GetTranslationTableName(model.Args[0].MemberName + "", false) + ".*/**/" + result1;
|
||||
}
|
||||
return result1;
|
||||
case "GetDate":
|
||||
return this.Context.DbMehtods.GetDate();
|
||||
case "GetRandom":
|
||||
return this.Context.DbMehtods.GetRandom();
|
||||
case "CharIndex":
|
||||
return this.Context.DbMehtods.CharIndex(model);
|
||||
case "BitwiseAnd":
|
||||
return this.Context.DbMehtods.BitwiseAnd(model);
|
||||
case "BitwiseInclusiveOR":
|
||||
return this.Context.DbMehtods.BitwiseInclusiveOR(model);
|
||||
case "ToDateShort":
|
||||
return this.Context.DbMehtods.ToDateShort(model);
|
||||
case "Oracle_ToChar":
|
||||
return this.Context.DbMehtods.Oracle_ToChar(model);
|
||||
case "Oracle_ToDate":
|
||||
return this.Context.DbMehtods.Oracle_ToDate(model);
|
||||
case "SqlServer_DateDiff":
|
||||
return this.Context.DbMehtods.SqlServer_DateDiff(model);
|
||||
case "Format":
|
||||
var xx = base.BaseParameter;
|
||||
var result = this.Context.DbMehtods.Format(model);
|
||||
if (!string.IsNullOrEmpty(this.Context.MethodName))
|
||||
{
|
||||
result = this.Context.DbMehtods.FormatRowNumber(model);
|
||||
}
|
||||
this.Context.Parameters.RemoveAll(it => model.Args.Select(x => x.MemberName.ObjToString()).Contains(it.ParameterName));
|
||||
return result;
|
||||
case "Abs":
|
||||
return this.Context.DbMehtods.Abs(model);
|
||||
case "Round":
|
||||
return this.Context.DbMehtods.Round(model);
|
||||
case "DateDiff":
|
||||
return this.Context.DbMehtods.DateDiff(model);
|
||||
case "GreaterThan":
|
||||
return this.Context.DbMehtods.GreaterThan(model);
|
||||
case "GreaterThanOrEqual":
|
||||
return this.Context.DbMehtods.GreaterThanOrEqual(model);
|
||||
case "LessThan":
|
||||
return this.Context.DbMehtods.LessThan(model);
|
||||
case "LessThanOrEqual":
|
||||
return this.Context.DbMehtods.LessThanOrEqual(model);
|
||||
case "Asc":
|
||||
return this.Context.DbMehtods.Asc(model);
|
||||
case "Desc":
|
||||
return this.Context.DbMehtods.Desc(model);
|
||||
case "Stuff":
|
||||
return this.Context.DbMehtods.Stuff(model);
|
||||
case "RowNumber":
|
||||
return this.Context.DbMehtods.RowNumber(model);
|
||||
case "RowCount":
|
||||
return this.Context.DbMehtods.RowCount(model);
|
||||
case "Exists":
|
||||
if (model.Args.Count > 1)
|
||||
{
|
||||
this.Context.Parameters.RemoveAll(it => model.Args[1].MemberName.ObjToString().Contains(it.ParameterName));
|
||||
List<IConditionalModel> conditionalModels = (List<IConditionalModel>)model.Args[1].MemberValue;
|
||||
var sqlObj = this.Context.SugarContext.Context.Queryable<object>().SqlBuilder.ConditionalModelToSql(conditionalModels, 0);
|
||||
model.Args[1].MemberName = sqlObj.Key;
|
||||
if (sqlObj.Value != null)
|
||||
{
|
||||
this.Context.Parameters.AddRange(sqlObj.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
return " 1=1 ";
|
||||
}
|
||||
}
|
||||
return this.Context.DbMehtods.Exists(model);
|
||||
|
||||
case "JsonField":
|
||||
return this.Context.DbMehtods.JsonField(model);
|
||||
case "JsonArrayLength":
|
||||
return this.Context.DbMehtods.JsonArrayLength(model);
|
||||
case "JsonContainsFieldName":
|
||||
return this.Context.DbMehtods.JsonContainsFieldName(model);
|
||||
case "JsonParse":
|
||||
return this.Context.DbMehtods.JsonParse(model);
|
||||
case "JsonLike":
|
||||
return this.Context.DbMehtods.JsonLike(model);
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
private DbType GetType(string name)
|
||||
{
|
||||
DbType result = DbType.SqlServer;
|
||||
foreach (var item in UtilMethods.EnumToDictionary<DbType>())
|
||||
{
|
||||
if (name.StartsWith(item.Value.ToString()))
|
||||
{
|
||||
result = item.Value;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private bool IsContainsArray(MethodCallExpression express, string methodName, bool isValidNativeMethod)
|
||||
{
|
||||
return !isValidNativeMethod && express.Method.DeclaringType.Namespace.IsIn("System.Linq", "System.Collections.Generic") && methodName == "Contains";
|
||||
}
|
||||
private bool IsSubMethod(MethodCallExpression express, string methodName)
|
||||
{
|
||||
return SubTools.SubItemsConst.Any(it => it.Name == methodName) && express.Object != null && (express.Object.Type.Name.StartsWith("Subqueryable`"));
|
||||
}
|
||||
private bool CheckMethod(MethodCallExpression expression)
|
||||
{
|
||||
if (expression.Method.Name == "SelectAll")
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (expression.Method.Name == "Format" && expression.Method.DeclaringType == UtilConstants.StringType)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (IsExtMethod(expression.Method.Name))
|
||||
return true;
|
||||
if (IsParseMethod(expression))
|
||||
return true;
|
||||
if (expression.Method.Name == "IsNullOrEmpty" && expression.Method.DeclaringType == UtilConstants.StringType)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (expression.Method.Name == "IsNullOrWhiteSpace" && expression.Method.DeclaringType == UtilConstants.StringType)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (expression.Method.ReflectedType().FullName != ExpressionConst.SqlFuncFullName)
|
||||
return false;
|
||||
else
|
||||
return true;
|
||||
}
|
||||
private Type TempParseType;
|
||||
public bool IsParseMethod(MethodCallExpression expression)
|
||||
{
|
||||
if (expression.Method.Name == "Parse" && expression.Method.DeclaringType.IsIn(
|
||||
UtilConstants.DecType,
|
||||
UtilConstants.DateType,
|
||||
UtilConstants.DobType,
|
||||
UtilConstants.GuidType,
|
||||
UtilConstants.FloatType,
|
||||
UtilConstants.ShortType,
|
||||
UtilConstants.LongType,
|
||||
UtilConstants.IntType,
|
||||
UtilConstants.BoolType))
|
||||
{
|
||||
TempParseType = expression.Method.DeclaringType;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
private static bool IsNot(Expression item)
|
||||
{
|
||||
return item is UnaryExpression && (item as UnaryExpression).NodeType == ExpressionType.Not;
|
||||
}
|
||||
private bool IsDateItemValue(Expression item)
|
||||
{
|
||||
var result = false;
|
||||
if (item is MemberExpression)
|
||||
{
|
||||
var memberExp = item as MemberExpression;
|
||||
if (memberExp != null && memberExp.Expression != null && memberExp.Expression.Type == UtilConstants.DateType)
|
||||
{
|
||||
foreach (var dateType in UtilMethods.EnumToDictionary<DateType>())
|
||||
{
|
||||
if (memberExp.Member.Name.EqualCase(dateType.Key))
|
||||
{
|
||||
result = true;
|
||||
break;
|
||||
}
|
||||
else if (memberExp.Member.Name == "DayOfWeek")
|
||||
{
|
||||
result = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
private static bool IsDateDate(Expression item)
|
||||
{
|
||||
return item.Type == UtilConstants.DateType && item is MemberExpression && (item as MemberExpression).Member.Name == "Date" && item.ToString() != "DateTime.Now.Date";
|
||||
}
|
||||
private static bool IsDateValue(Expression item)
|
||||
{
|
||||
return item.Type == UtilConstants.IntType &&
|
||||
item is MemberExpression &&
|
||||
(item as MemberExpression).Expression != null &&
|
||||
(item as MemberExpression).Expression.Type == UtilConstants.DateType &&
|
||||
(item as MemberExpression).Expression is MemberExpression &&
|
||||
((item as MemberExpression).Expression as MemberExpression).Member.Name == "Value";
|
||||
}
|
||||
private bool IsValidNativeMethod(MethodCallExpression express, string methodName)
|
||||
{
|
||||
return MethodMapping.ContainsKey(methodName) && express.Method.DeclaringType.Namespace == ("System");
|
||||
}
|
||||
private bool IsExtMethod(string methodName)
|
||||
{
|
||||
if (this.Context.SqlFuncServices == null) return false;
|
||||
return this.Context.SqlFuncServices.Select(it => it.UniqueMethodName).Contains(methodName);
|
||||
}
|
||||
private bool IsIfElse(MethodCallExpression express, string methodName)
|
||||
{
|
||||
if (methodName == "End" && express.Object.Type == typeof(CaseWhen))
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
@@ -20,9 +20,10 @@ namespace SqlSugar
|
||||
case ResolveExpressType.SelectMultiple:
|
||||
case ResolveExpressType.FieldSingle:
|
||||
case ResolveExpressType.FieldMultiple:
|
||||
#region Filed
|
||||
try
|
||||
{
|
||||
var value = ExpressionTool.DynamicInvoke(expression);
|
||||
var value = ExpressionTool.DynamicInvoke(expression);
|
||||
var isLeft = parameter.IsLeft;
|
||||
var baseParameter = parameter.BaseParameter;
|
||||
var isSetTempData = baseParameter.CommonTempData.HasValue() && baseParameter.CommonTempData.Equals(CommonTempDataType.Result);
|
||||
@@ -64,42 +65,53 @@ namespace SqlSugar
|
||||
catch (Exception)
|
||||
{
|
||||
Check.ThrowNotSupportedException("NewArrayExpression");
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
break;
|
||||
case ResolveExpressType.ArraySingle:
|
||||
foreach (var item in expression.Expressions)
|
||||
{
|
||||
base.Expression = item;
|
||||
base.Start();
|
||||
}
|
||||
ArraySingle(expression);
|
||||
break;
|
||||
case ResolveExpressType.Join:
|
||||
base.Context.ResolveType = ResolveExpressType.WhereMultiple;
|
||||
int i = 0;
|
||||
foreach (var item in expression.Expressions)
|
||||
{
|
||||
if (item is UnaryExpression)
|
||||
{
|
||||
base.Expression = item;
|
||||
base.Start();
|
||||
if (parameter.CommonTempData is JoinType)
|
||||
{
|
||||
if (i > 0)
|
||||
{
|
||||
base.Context.Result.Append("," + parameter.CommonTempData.ObjToString().Replace(",",UtilConstants.ReplaceCommaKey) + ",");
|
||||
}
|
||||
else
|
||||
{
|
||||
base.Context.Result.Append(parameter.CommonTempData.ObjToString().Replace(",", UtilConstants.ReplaceCommaKey) + ",");
|
||||
}
|
||||
++i;
|
||||
}
|
||||
}
|
||||
}
|
||||
Join(parameter, expression);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void Join(ExpressionParameter parameter, NewArrayExpression expression)
|
||||
{
|
||||
base.Context.ResolveType = ResolveExpressType.WhereMultiple;
|
||||
int i = 0;
|
||||
foreach (var item in expression.Expressions)
|
||||
{
|
||||
if (item is UnaryExpression)
|
||||
{
|
||||
base.Expression = item;
|
||||
base.Start();
|
||||
if (parameter.CommonTempData is JoinType)
|
||||
{
|
||||
if (i > 0)
|
||||
{
|
||||
base.Context.Result.Append("," + parameter.CommonTempData.ObjToString().Replace(",", UtilConstants.ReplaceCommaKey) + ",");
|
||||
}
|
||||
else
|
||||
{
|
||||
base.Context.Result.Append(parameter.CommonTempData.ObjToString().Replace(",", UtilConstants.ReplaceCommaKey) + ",");
|
||||
}
|
||||
++i;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ArraySingle(NewArrayExpression expression)
|
||||
{
|
||||
foreach (var item in expression.Expressions)
|
||||
{
|
||||
base.Expression = item;
|
||||
base.Start();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -40,54 +40,64 @@ namespace SqlSugar
|
||||
case ResolveExpressType.FieldMultiple:
|
||||
case ResolveExpressType.ArrayMultiple:
|
||||
case ResolveExpressType.ArraySingle:
|
||||
foreach (var item in expression.Arguments)
|
||||
{
|
||||
if (IsDateValue(item))
|
||||
{
|
||||
var value = GetNewExpressionValue(item);
|
||||
base.Context.Result.Append(value);
|
||||
}
|
||||
else
|
||||
{
|
||||
base.Expression = item;
|
||||
base.Start();
|
||||
}
|
||||
}
|
||||
ArraySingle(expression);
|
||||
break;
|
||||
case ResolveExpressType.Join:
|
||||
base.Context.ResolveType = ResolveExpressType.WhereMultiple;
|
||||
int i = 0;
|
||||
foreach (var item in expression.Arguments)
|
||||
{
|
||||
if (item.Type!=typeof(JoinType))
|
||||
{
|
||||
base.Expression = item;
|
||||
base.Start();
|
||||
}
|
||||
if (item.Type == typeof(JoinType))
|
||||
{
|
||||
var joinValue = item.ObjToString();
|
||||
if (joinValue.Contains("("))
|
||||
{
|
||||
joinValue = ExpressionTool.DynamicInvoke(item).ObjToString();
|
||||
}
|
||||
if (i > 0)
|
||||
{
|
||||
base.Context.Result.Append("," + joinValue + ",");
|
||||
}
|
||||
else
|
||||
{
|
||||
base.Context.Result.Append(joinValue + ",");
|
||||
}
|
||||
++i;
|
||||
}
|
||||
}
|
||||
Join(expression);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void Join(NewExpression expression)
|
||||
{
|
||||
base.Context.ResolveType = ResolveExpressType.WhereMultiple;
|
||||
int i = 0;
|
||||
foreach (var item in expression.Arguments)
|
||||
{
|
||||
if (item.Type != typeof(JoinType))
|
||||
{
|
||||
base.Expression = item;
|
||||
base.Start();
|
||||
}
|
||||
if (item.Type == typeof(JoinType))
|
||||
{
|
||||
var joinValue = item.ObjToString();
|
||||
if (joinValue.Contains("("))
|
||||
{
|
||||
joinValue = ExpressionTool.DynamicInvoke(item).ObjToString();
|
||||
}
|
||||
if (i > 0)
|
||||
{
|
||||
base.Context.Result.Append("," + joinValue + ",");
|
||||
}
|
||||
else
|
||||
{
|
||||
base.Context.Result.Append(joinValue + ",");
|
||||
}
|
||||
++i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ArraySingle(NewExpression expression)
|
||||
{
|
||||
foreach (var item in expression.Arguments)
|
||||
{
|
||||
if (IsDateValue(item))
|
||||
{
|
||||
var value = GetNewExpressionValue(item);
|
||||
base.Context.Result.Append(value);
|
||||
}
|
||||
else
|
||||
{
|
||||
base.Expression = item;
|
||||
base.Start();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsDateValue(Expression item)
|
||||
{
|
||||
var isMember = item is MemberExpression;
|
||||
|
Reference in New Issue
Block a user