public static DbExpression StripInvalidConvert(DbExpression exp) { DbConvertExpression convertExpression = exp as DbConvertExpression; if (convertExpression == null) return exp; if (convertExpression.Type.IsEnum) { //(enumType)123 if (typeof(int) == convertExpression.Operand.Type) return StripInvalidConvert(convertExpression.Operand); DbConvertExpression newExp = new DbConvertExpression(typeof(int), convertExpression.Operand); return StripInvalidConvert(newExp); } Type unType; //(int?)123 if (Utils.IsNullable(convertExpression.Type, out unType))//可空类型转换 { if (unType == convertExpression.Operand.Type) return StripInvalidConvert(convertExpression.Operand); DbConvertExpression newExp = new DbConvertExpression(unType, convertExpression.Operand); return StripInvalidConvert(newExp); } //(int)enumTypeValue if (exp.Type == typeof(int)) { //(int)enumTypeValue if (convertExpression.Operand.Type.IsEnum) return StripInvalidConvert(convertExpression.Operand); //(int)NullableEnumTypeValue if (Utils.IsNullable(convertExpression.Operand.Type, out unType) && unType.IsEnum) return StripInvalidConvert(convertExpression.Operand); } //float long double and so on if (exp.Type.IsValueType) { //(long)NullableValue if (Utils.IsNullable(convertExpression.Operand.Type, out unType) && unType == exp.Type) return StripInvalidConvert(convertExpression.Operand); } if (convertExpression.Type == convertExpression.Operand.Type) { return StripInvalidConvert(convertExpression.Operand); } //如果是子类向父类转换 if (exp.Type.IsAssignableFrom(convertExpression.Operand.Type)) return StripInvalidConvert(convertExpression.Operand); return convertExpression; }
static void Method_ToString(DbMethodCallExpression exp, SqlGenerator generator) { if (exp.Method.Name != "ToString" && exp.Arguments.Count != 0) { throw UtilExceptions.NotSupportedMethod(exp.Method); } if (exp.Object.Type == UtilConstants.TypeOfString) { exp.Object.Accept(generator); return; } if (!NumericTypes.ContainsKey(exp.Object.Type.GetUnderlyingType())) { throw UtilExceptions.NotSupportedMethod(exp.Method); } DbConvertExpression c = DbExpression.Convert(exp.Object, UtilConstants.TypeOfString); c.Accept(generator); }
public override DbExpression Visit(DbConvertExpression exp) { DbExpression stripedExp = DbExpressionExtension.StripInvalidConvert(exp); if (stripedExp.NodeType != DbExpressionType.Convert) { EnsureDbExpressionReturnCSharpBoolean(stripedExp).Accept(this); return exp; } exp = (DbConvertExpression)stripedExp; if (exp.Type == UtilConstants.TypeOfString) { this._sqlBuilder.Append("TO_CHAR("); exp.Operand.Accept(this); this._sqlBuilder.Append(")"); return exp; } if (exp.Type == UtilConstants.TypeOfDateTime) { this._sqlBuilder.Append("TO_TIMESTAMP("); exp.Operand.Accept(this); this._sqlBuilder.Append(",'yyyy-mm-dd hh24:mi:ssxff')"); return exp; } string dbTypeString; if (TryGetCastTargetDbTypeString(exp.Operand.Type, exp.Type, out dbTypeString, false)) { this.BuildCastState(EnsureDbExpressionReturnCSharpBoolean(exp.Operand), dbTypeString); } else EnsureDbExpressionReturnCSharpBoolean(exp.Operand).Accept(this); return exp; }
public override DbExpression Visit(DbConvertExpression exp) { DbExpression stripedExp = DbExpressionExtension.StripInvalidConvert(exp); if (stripedExp.NodeType != DbExpressionType.Convert) { stripedExp.Accept(this); return(exp); } exp = (DbConvertExpression)stripedExp; string dbTypeString; if (TryGetCastTargetDbTypeString(exp.Operand.Type, exp.Type, out dbTypeString, false)) { this.BuildCastState(exp.Operand, dbTypeString); } else { Type targetType = ReflectionExtension.GetUnderlyingType(exp.Type); if (targetType == PublicConstants.TypeOfDateTime) { /* DATETIME('2016-08-06 09:01:24') */ this.SqlBuilder.Append("DATETIME("); exp.Operand.Accept(this); this.SqlBuilder.Append(")"); } else { exp.Operand.Accept(this); } } return(exp); }
public override object Visit(DbConvertExpression exp) { object operandValue = exp.Operand.Accept(this); //(int)null if (operandValue == null) { //(int)null if (exp.Type.IsValueType && !exp.Type.IsNullable()) { throw new NullReferenceException(); } return(null); } Type operandValueType = operandValue.GetType(); if (exp.Type == operandValueType || exp.Type.IsAssignableFrom(operandValueType)) { return(operandValue); } Type underlyingType; if (exp.Type.IsNullable(out underlyingType)) { //(int?)int if (underlyingType == operandValueType) { var constructor = exp.Type.GetConstructor(new Type[] { operandValueType }); var val = constructor.Invoke(new object[] { operandValue }); return(val); } else { //如果不等,则诸如:(long?)int / (long?)int? --> (long?)((long)int) / (long?)((long)int?) var c = DbExpression.Convert(DbExpression.Constant(operandValue), underlyingType); var cc = DbExpression.Convert(c, exp.Type); return(this.Visit(cc)); } } //(int)int? if (operandValueType.IsNullable(out underlyingType)) { if (underlyingType == exp.Type) { var pro = operandValueType.GetProperty("Value"); var val = pro.GetValue(operandValue, null); return(val); } else { //如果不等,则诸如:(long)int? --> (long)((long)int) var c = DbExpression.Convert(DbExpression.Constant(operandValue), underlyingType); var cc = DbExpression.Convert(c, exp.Type); return(this.Visit(cc)); } } if (exp.Type.IsEnum) { return(Enum.ToObject(exp.Type, operandValue)); } //(long)int if (operandValue is IConvertible) { return(Convert.ChangeType(operandValue, exp.Type)); } throw new NotSupportedException(string.Format("Does not support the type '{0}' converted to type '{1}'.", operandValueType.FullName, exp.Type.FullName)); }
public override DbExpression Visit(DbConvertExpression exp) { return(exp); }
/// <summary> /// 尝试将 exp 转换成 DbParameterExpression。 /// </summary> /// <param name="exp"></param> /// <returns></returns> public static DbExpression ParseDbExpression(this DbExpression exp) { DbExpression stripedExp = DbExpressionHelper.StripInvalidConvert(exp); DbExpression tempExp = stripedExp; List<DbConvertExpression> cList = null; while (tempExp.NodeType == DbExpressionType.Convert) { if (cList == null) cList = new List<DbConvertExpression>(); DbConvertExpression c = (DbConvertExpression)tempExp; cList.Add(c); tempExp = c.Operand; } if (tempExp.NodeType == DbExpressionType.Constant || tempExp.NodeType == DbExpressionType.Parameter) return stripedExp; if (tempExp.NodeType == DbExpressionType.MemberAccess) { DbParameterExpression val; if (DbExpressionExtensions.TryParseToParameterExpression((DbMemberExpression)tempExp, out val)) { if (cList != null) { if (val.Value == DBNull.Value)//如果是 null,则不需要 Convert 了,在数据库里没意义 return val; DbConvertExpression c = null; for (int i = cList.Count - 1; i > -1; i--) { DbConvertExpression item = cList[i]; c = new DbConvertExpression(item.Type, val); } return c; } return val; } } return stripedExp; }
public override DbExpression Visit(DbConvertExpression exp) { return(exp.Accept(this._generator)); }
public abstract T Visit(DbConvertExpression exp);
public static DbExpression StripInvalidConvert(this DbExpression exp) { if (exp.NodeType != DbExpressionType.Convert) { return(exp); } DbConvertExpression convertExpression = (DbConvertExpression)exp; if (convertExpression.Type.IsEnum()) { //(enumType)123 if (typeof(int) == convertExpression.Operand.Type) { return(StripInvalidConvert(convertExpression.Operand)); } DbConvertExpression newExp = new DbConvertExpression(typeof(int), convertExpression.Operand); return(StripInvalidConvert(newExp)); } Type underlyingType; //(int?)123 if (ReflectionExtension.IsNullable(convertExpression.Type, out underlyingType))//可空类型转换 { if (underlyingType == convertExpression.Operand.Type) { return(StripInvalidConvert(convertExpression.Operand)); } DbConvertExpression newExp = new DbConvertExpression(underlyingType, convertExpression.Operand); return(StripInvalidConvert(newExp)); } //(int)enumTypeValue if (exp.Type == typeof(int)) { //(int)enumTypeValue if (convertExpression.Operand.Type.IsEnum()) { return(StripInvalidConvert(convertExpression.Operand)); } //(int)NullableEnumTypeValue if (ReflectionExtension.IsNullable(convertExpression.Operand.Type, out underlyingType) && underlyingType.IsEnum()) { return(StripInvalidConvert(convertExpression.Operand)); } } //float long double and so on if (exp.Type.IsValueType()) { //(long)NullableValue if (ReflectionExtension.IsNullable(convertExpression.Operand.Type, out underlyingType) && underlyingType == exp.Type) { return(StripInvalidConvert(convertExpression.Operand)); } } if (convertExpression.Type == convertExpression.Operand.Type) { return(StripInvalidConvert(convertExpression.Operand)); } //如果是子类向父类转换 if (exp.Type.IsAssignableFrom(convertExpression.Operand.Type)) { return(StripInvalidConvert(convertExpression.Operand)); } return(convertExpression); }
public static DbExpression StripInvalidConvert(DbExpression exp) { DbConvertExpression convertExpression = exp as DbConvertExpression; if (convertExpression == null) { return(exp); } if (convertExpression.Type.IsEnum) { //(enumType)123 if (typeof(int) == convertExpression.Operand.Type) { return(StripInvalidConvert(convertExpression.Operand)); } DbConvertExpression newExp = new DbConvertExpression(typeof(int), convertExpression.Operand); return(StripInvalidConvert(newExp)); } Type unType; //(int?)123 if (Utils.IsNullable(convertExpression.Type, out unType))//可空类型转换 { if (unType == convertExpression.Operand.Type) { return(StripInvalidConvert(convertExpression.Operand)); } DbConvertExpression newExp = new DbConvertExpression(unType, convertExpression.Operand); return(StripInvalidConvert(newExp)); } //(int)enumTypeValue if (exp.Type == typeof(int)) { //(int)enumTypeValue if (convertExpression.Operand.Type.IsEnum) { return(StripInvalidConvert(convertExpression.Operand)); } //(int)NullableEnumTypeValue if (Utils.IsNullable(convertExpression.Operand.Type, out unType) && unType.IsEnum) { return(StripInvalidConvert(convertExpression.Operand)); } } //float long double and so on if (exp.Type.IsValueType) { //(long)NullableValue if (Utils.IsNullable(convertExpression.Operand.Type, out unType) && unType == exp.Type) { return(StripInvalidConvert(convertExpression.Operand)); } } if (convertExpression.Type == convertExpression.Operand.Type) { return(StripInvalidConvert(convertExpression.Operand)); } //如果是子类向父类转换 if (exp.Type.IsAssignableFrom(convertExpression.Operand.Type)) { return(StripInvalidConvert(convertExpression.Operand)); } return(convertExpression); }
public static DbExpression StripInvalidConvert(this DbExpression exp) { if (exp.NodeType != DbExpressionType.Convert) { return(exp); } DbConvertExpression convertExpression = (DbConvertExpression)exp; if (convertExpression.Type == convertExpression.Operand.Type) { return(StripInvalidConvert(convertExpression.Operand)); } //(enumType)1 if (convertExpression.Type.IsEnum) { Type enumUnderlyingType = Enum.GetUnderlyingType(convertExpression.Type); if (enumUnderlyingType == convertExpression.Operand.Type) { //(enumType)1 --> 1 return(StripInvalidConvert(convertExpression.Operand)); } //(enumType)1 --> (Int16/Int32/Int64)1 DbConvertExpression newExp = new DbConvertExpression(enumUnderlyingType, convertExpression.Operand); return(StripInvalidConvert(newExp)); } Type underlyingType; //(Nullable<T>)1 if (convertExpression.Type.IsNullable(out underlyingType))//可空类型转换 { if (underlyingType == convertExpression.Operand.Type) { //T == convertExpression.Operand.Type //(Nullable<T>)1 --> 1 return(StripInvalidConvert(convertExpression.Operand)); } //(Nullable<T>)1 --> (T)1 DbConvertExpression newExp = new DbConvertExpression(underlyingType, convertExpression.Operand); return(StripInvalidConvert(newExp)); } if (!exp.Type.IsEnum) { //(Int16/Int32/Int64)TEnum if (convertExpression.Operand.Type.IsEnum) { //(Int16/Int32/Int64)TEnum --> TEnum return(StripInvalidConvert(convertExpression.Operand)); } //(Int16/Int32/Int64)Nullable<TEnum> if (convertExpression.Operand.Type.IsNullable(out underlyingType) && underlyingType.IsEnum) { //(Int16/Int32/Int64)Nullable<TEnum> --> TEnum return(StripInvalidConvert(convertExpression.Operand)); } } //float long double and so on if (exp.Type.IsValueType) { if (convertExpression.Operand.Type.IsNullable(out underlyingType) && underlyingType == exp.Type) { //(T)Nullable<T> --> T return(StripInvalidConvert(convertExpression.Operand)); } } //如果是子类向父类转换 if (exp.Type.IsAssignableFrom(convertExpression.Operand.Type)) { return(StripInvalidConvert(convertExpression.Operand)); } return(convertExpression); }
public override bool Visit(DbConvertExpression exp) { return(this.VisitCore(exp.Operand)); }
public static bool ExpressionEquals(DbConvertExpression exp1, DbConvertExpression exp2) { if (exp1.Type != exp2.Type) return false; return EqualsCompare(exp1.Operand, exp2.Operand); }
public override DbExpression Visit(DbConvertExpression exp) { exp = DbExpression.Convert(exp.Operand.Accept(this), exp.Type); return(exp); }