public string GetValue(Expression expression) { var exp = expression as MethodCallExpression; object value = null; try { value = ExpressionTool.DynamicInvoke(exp.Arguments[0]); } catch { Check.Exception(true, ErrorMessage.WhereIFCheck, exp.Arguments[0].ToString()); } var isWhere = Convert.ToBoolean(value); if (!Convert.ToBoolean(isWhere)) { return("WHERE 1=1 "); } var argExp = exp.Arguments[1]; var result = "WHERE " + SubTools.GetMethodValue(Context, argExp, ResolveExpressType.WhereMultiple);; var selfParameterName = Context.GetTranslationColumnName((argExp as LambdaExpression).Parameters.First().Name) + UtilConstants.Dot; result = result.Replace(selfParameterName, SubTools.GetSubReplace(this.Context)); return(result); }
public MemberNewExpressionResolve(ExpressionParameter parameter) : base(parameter) { var expression = base.Expression as MemberExpression; var isLeft = parameter.IsLeft; object value = null; value = ExpressionTool.DynamicInvoke(expression); }
private void MemberLogic(ExpressionParameter parameter, ExpressionParameter baseParameter, ExpressionType nodeType) { var memberExpression = (base.Expression as MemberExpression); var isLogicOperator = ExpressionTool.IsLogicOperator(baseParameter.OperatorValue) || baseParameter.OperatorValue.IsNullOrEmpty(); var isHasValue = isLogicOperator && memberExpression.Member.Name == "HasValue" && memberExpression.Expression != null && memberExpression.NodeType == ExpressionType.MemberAccess; if (isHasValue) { var member = memberExpression.Expression as MemberExpression; parameter.CommonTempData = CommonTempDataType.Result; var isConst = member.Expression != null && member.Expression is ConstantExpression; if (isConst) { var paramterValue = ExpressionTool.DynamicInvoke(member); var paramterName = base.AppendParameter(paramterValue); var result = this.Context.DbMehtods.HasValue(new MethodCallExpressionModel() { Args = new List <MethodCallExpressionArgs>() { new MethodCallExpressionArgs() { IsMember = false, MemberName = paramterName, MemberValue = paramterValue } } }); this.Context.Result.Append(result); } else { this.Expression = isConst ? member.Expression : member; this.Start(); var methodParamter = isConst ? new MethodCallExpressionArgs() { IsMember = false } : new MethodCallExpressionArgs() { IsMember = true, MemberName = parameter.CommonTempData, MemberValue = null }; var result = this.Context.DbMehtods.HasValue(new MethodCallExpressionModel() { Args = new List <MethodCallExpressionArgs>() { methodParamter } }); this.Context.Result.Append(result); parameter.CommonTempData = null; } } else if (memberExpression.Type == UtilConstants.BoolType && isLogicOperator) { Append(parameter, nodeType); } else { Result(parameter, nodeType); } }
private void NewValueType(ExpressionParameter parameter, NewExpression expression) { try { var value = ExpressionTool.DynamicInvoke(expression); var isSetTempData = parameter.CommonTempData.HasValue() && parameter.CommonTempData.Equals(CommonTempDataType.Result); if (isSetTempData) { parameter.CommonTempData = value; } else { AppendValue(parameter, parameter.IsLeft, value); } } catch (Exception ex) { Check.Exception(expression.Type == UtilConstants.DateType, "ThrowNotSupportedException {0} ", ex.ToString()); } }
private void CusMethod(ExpressionParameter parameter, MethodCallExpression express, bool?isLeft) { try { var constValue = ExpressionTool.DynamicInvoke(express); 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 { Check.Exception(true, string.Format(ErrorMessage.MethodError, express.Method.Name)); } }
private void ResolveCallValue(ExpressionParameter parameter, ExpressionParameter baseParameter, MemberExpression expression, bool?isLeft, bool isSetTempData, bool isSingle) { try { baseParameter.ChildExpression = expression; string fieldName = string.Empty; if (isSetTempData) { var value = ExpressionTool.DynamicInvoke(expression); baseParameter.CommonTempData = value; } else { var value = ExpressionTool.DynamicInvoke(expression); base.AppendValue(parameter, isLeft, value); } } catch { Check.Exception(true, "Not Support {0}", expression.ToString()); } }
public MethodCallExpressionResolve(ExpressionParameter parameter) : base(parameter) { contextIndex = this.Context.Index; var express = base.Expression as MethodCallExpression; if (express == null) { return; } var isLeft = parameter.IsLeft; string methodName = express.Method.Name; var isValidNativeMethod = IsValidNativeMethod(express, methodName); List <MethodCallExpressionArgs> appendArgs = null; if (MethodTimeMapping.ContainsKey(methodName)) { appendArgs = new List <MethodCallExpressionArgs>(); var dateType = MethodTimeMapping[methodName]; string paramterName = this.Context.SqlParameterKeyWord + ExpressionConst.Const + this.Context.ParameterIndex; appendArgs.Add(new MethodCallExpressionArgs() { IsMember = false, MemberName = paramterName, MemberValue = dateType }); this.Context.Parameters.Add(new DbLiteParameter(paramterName, dateType.ToString())); this.Context.ParameterIndex++; methodName = "DateAdd"; isValidNativeMethod = true; } else if (methodName == "get_Item") { string paramterName = this.Context.SqlParameterKeyWord + ExpressionConst.Const + this.Context.ParameterIndex; this.Context.Parameters.Add(new DbLiteParameter(paramterName, ExpressionTool.DynamicInvoke(express))); this.Context.Result.Append(string.Format(" {0} ", paramterName)); this.Context.ParameterIndex++; return; } else if (methodName == "NewGuid") { this.Context.Result.Append(this.Context.DbMehtods.GuidNew()); return; } else if (IsSubMethod(express, methodName)) { //Check.Exception(!(parameter.BaseExpression is BinaryExpression), "Current expressions are not supported"); SubResolve subResolve = new SubResolve(express, this.Context, parameter.OppsiteExpression); var appendSql = subResolve.GetSql(); if (this.Context.ResolveType.IsIn(ResolveExpressType.SelectMultiple, ResolveExpressType.SelectSingle) || (parameter.BaseParameter != null && parameter.BaseParameter.CommonTempData != null && parameter.BaseParameter.CommonTempData.Equals(CommonTempDataType.Result))) { parameter.BaseParameter.CommonTempData = appendSql; } else { base.AppendValue(parameter, isLeft, appendSql); } return; } else if (IsIfElse(express, methodName)) { CaseWhenResolve caseResole = new CaseWhenResolve(express, this.Context, parameter.OppsiteExpression); var appendSql = caseResole.GetSql(); var isRoot = contextIndex == 2 && parameter.BaseExpression == null; if (isRoot || (parameter.BaseExpression != null && ExpressionTool.IsLogicOperator(parameter.BaseExpression))) { appendSql = appendSql + "=1 "; } if (this.Context.ResolveType.IsIn(ResolveExpressType.SelectMultiple, ResolveExpressType.SelectSingle, ResolveExpressType.Update)) { parameter.BaseParameter.CommonTempData = appendSql; } else { base.AppendValue(parameter, isLeft, appendSql); } return; } if (IsContainsArray(express, methodName, isValidNativeMethod)) { methodName = "ContainsArray"; isValidNativeMethod = true; } if (isValidNativeMethod) { NativeExtensionMethod(parameter, express, isLeft, MethodMapping[methodName], appendArgs); } else { SqlFuncMethod(parameter, express, isLeft); } }
public NewArrayExpessionResolve(ExpressionParameter parameter) : base(parameter) { var expression = (NewArrayExpression)base.Expression; switch (base.Context.ResolveType) { case ResolveExpressType.WhereSingle: case ResolveExpressType.WhereMultiple: case ResolveExpressType.SelectSingle: case ResolveExpressType.SelectMultiple: case ResolveExpressType.FieldSingle: case ResolveExpressType.FieldMultiple: try { var value = ExpressionTool.DynamicInvoke(expression); var isLeft = parameter.IsLeft; var baseParameter = parameter.BaseParameter; var isSetTempData = baseParameter.CommonTempData.HasValue() && baseParameter.CommonTempData.Equals(CommonTempDataType.Result); if (isSetTempData) { baseParameter.CommonTempData = value; } else { var parentIsBinary = parameter.BaseParameter.CurrentExpression is BinaryExpression; var parentIsRoot = parameter.BaseParameter.CurrentExpression is LambdaExpression; var isBool = value != null && value.GetType() == UtilConstants.BoolType; if (parentIsRoot && isBool) { this.Context.Result.Append(value.ObjToBool() ? this.Context.DbMehtods.True() : this.Context.DbMehtods.False()); break; } if (parentIsBinary && isBool) { var isLogicOperator = parameter.BaseExpression.NodeType == ExpressionType.And || parameter.BaseExpression.NodeType == ExpressionType.AndAlso || parameter.BaseExpression.NodeType == ExpressionType.Or || parameter.BaseExpression.NodeType == ExpressionType.OrElse; if (isLogicOperator) { AppendMember(parameter, isLeft, (value.ObjToBool() ? this.Context.DbMehtods.True() : this.Context.DbMehtods.False())); break; } } if (value == null && parentIsBinary) { parameter.BaseParameter.ValueIsNull = true; value = this.Context.DbMehtods.Null(); } AppendValue(parameter, isLeft, value); } } catch (Exception) { Check.ThrowNotSupportedException("NewArrayExpression"); } break; case ResolveExpressType.ArraySingle: foreach (var item in expression.Expressions) { base.Expression = item; base.Start(); } 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; } } } break; default: break; } }
private void Update(MemberInitExpression expression, ExpressionParameter parameter) { int i = 0; foreach (MemberBinding binding in expression.Bindings) { ++i; if (binding.BindingType != MemberBindingType.Assignment) { throw new NotSupportedException(); } MemberAssignment memberAssignment = (MemberAssignment)binding; var type = expression.Type; var memberName = this.Context.GetDbColumnName(type.Name, memberAssignment.Member.Name); var item = memberAssignment.Expression; if ((item is MemberExpression) && ((MemberExpression)item).Expression == null) { var paramterValue = ExpressionTool.DynamicInvoke(item); string parameterName = AppendParameter(paramterValue); this.Context.Result.Append(base.Context.GetEqString(memberName, parameterName)); } else if (IsNotMember(item)) { if (base.Context.Result.IsLockCurrentParameter == false) { base.Context.Result.CurrentParameter = parameter; base.Context.Result.IsLockCurrentParameter = true; parameter.IsAppendTempDate(); base.Expression = item; base.Expression = (item as UnaryExpression).Operand; base.Start(); parameter.IsAppendResult(); var result = this.Context.DbMehtods.IIF(new MethodCallExpressionModel() { Args = new List <MethodCallExpressionArgs>() { new MethodCallExpressionArgs() { IsMember = true, MemberName = parameter.CommonTempData.ObjToString() + "=1" }, new MethodCallExpressionArgs() { IsMember = true, MemberName = AppendParameter(0) }, new MethodCallExpressionArgs() { IsMember = true, MemberName = AppendParameter(1) } } }); parameter.Context.Result.Append(base.Context.GetEqString(memberName, result)); base.Context.Result.CurrentParameter = null; } } else if (IsNotParameter(item)) { try { parameter.Context.Result.Append(base.Context.GetEqString(memberName, AppendParameter(ExpressionTool.DynamicInvoke(item).ObjToBool()))); } catch { throw new NotSupportedException(item.ToString()); } } else if (IsMethod(item)) { if (item is UnaryExpression) { item = (item as UnaryExpression).Operand; } var callMethod = item as MethodCallExpression; if (MethodTimeMapping.Any(it => it.Key == callMethod.Method.Name) || MethodMapping.Any(it => it.Key == callMethod.Method.Name) || IsExtMethod(callMethod.Method.Name) || IsSubMethod(callMethod) || callMethod.Method.DeclaringType.FullName.StartsWith(UtilConstants.AssemblyName + UtilConstants.Dot)) { MethodCall(parameter, memberName, item); } else { var paramterValue = ExpressionTool.DynamicInvoke(item); string parameterName = AppendParameter(paramterValue); this.Context.Result.Append(base.Context.GetEqString(memberName, parameterName)); } } else if (IsConst(item) && IsConvert(item) && UtilMethods.IsNullable(item.Type) && UtilMethods.GetUnderType(item.Type) == UtilConstants.BoolType) { item = (item as UnaryExpression).Operand; parameter.Context.Result.Append(base.Context.GetEqString(memberName, GetNewExpressionValue(item))); } else if (IsConst(item)) { base.Expression = item; base.Start(); string parameterName = this.Context.SqlParameterKeyWord + ExpressionConst.Const + this.Context.ParameterIndex; parameter.Context.Result.Append(base.Context.GetEqString(memberName, parameterName)); this.Context.Parameters.Add(new DbLiteParameter(parameterName, parameter.CommonTempData)); this.Context.ParameterIndex++; } else if (item is MemberExpression) { if (base.Context.Result.IsLockCurrentParameter == false) { base.Context.Result.CurrentParameter = parameter; base.Context.Result.IsLockCurrentParameter = true; parameter.IsAppendTempDate(); base.Expression = item; base.Start(); parameter.IsAppendResult(); parameter.Context.Result.Append(base.Context.GetEqString(memberName, parameter.CommonTempData.ObjToString())); base.Context.Result.CurrentParameter = null; } } else if (item is BinaryExpression) { var result = GetNewExpressionValue(item); this.Context.Result.Append(base.Context.GetEqString(memberName, result)); } else if (item is MemberInitExpression) { try { var value = ExpressionTool.DynamicInvoke(item); var parameterName = AppendParameter(value); parameter.Context.Result.Append(base.Context.GetEqString(memberName, parameterName)); } catch (Exception ex) { throw new NotSupportedException("Not Supported " + item.ToString() + " " + ex.Message); } } else if (item is NewExpression) { try { var value = ExpressionTool.DynamicInvoke(item); var parameterName = AppendParameter(value); parameter.Context.Result.Append(base.Context.GetEqString(memberName, parameterName)); } catch (Exception ex) { throw new NotSupportedException("Not Supported " + item.ToString() + " " + ex.Message); } } } }