Ejemplo n.º 1
0
        private void BindMethod(ILGenerator generator, EntityColumnInfo columnInfo, int ordinal)
        {
            IDbBind    bind             = Context.Ado.DbBind;
            bool       isNullableType   = false;
            MethodInfo method           = null;
            Type       bindPropertyType = UtilMethods.GetUnderType(columnInfo.PropertyInfo, ref isNullableType);
            string     dbTypeName       = UtilMethods.GetParenthesesValue(DataRecord.GetDataTypeName(ordinal));

            if (dbTypeName.IsNullOrEmpty())
            {
                dbTypeName = bindPropertyType.Name;
            }
            string propertyName      = columnInfo.PropertyName;
            string validPropertyName = bind.GetPropertyTypeName(dbTypeName);

            validPropertyName = validPropertyName == "byte[]" ? "byteArray" : validPropertyName;
            CSharpDataType validPropertyType = (CSharpDataType)Enum.Parse(typeof(CSharpDataType), validPropertyName);

            #region Sqlite Logic
            if (this.Context.CurrentConnectionConfig.DbType == DbType.Sqlite)
            {
                if (bindPropertyType.IsEnum())
                {
                    method = isNullableType ? getConvertEnum_Null.MakeGenericMethod(bindPropertyType) : getEnum.MakeGenericMethod(bindPropertyType);
                }
                else if (bindPropertyType == UtilConstants.IntType)
                {
                    method = isNullableType ? getConvertInt32 : getInt32;
                }
                else if (bindPropertyType == UtilConstants.ByteType)
                {
                    method = isNullableType ? getConvertByte : getByte;
                }
                else if (bindPropertyType == UtilConstants.StringType && dbTypeName?.ToLower() == "timestamp")
                {
                    method = getConvertValueMethod.MakeGenericMethod(columnInfo.PropertyInfo.PropertyType);;
                }
                else if (bindPropertyType == UtilConstants.StringType)
                {
                    method = getString;
                }
                else
                {
                    method = getConvertValueMethod.MakeGenericMethod(columnInfo.PropertyInfo.PropertyType);
                }

                if (method.IsVirtual)
                {
                    generator.Emit(OpCodes.Callvirt, method);
                }
                else
                {
                    generator.Emit(OpCodes.Call, method);
                }
                return;
            }
            ;
            #endregion

            #region Common Database Logic
            string bindProperyTypeName = bindPropertyType.Name.ToLower();
            bool   isEnum = bindPropertyType.IsEnum();
            if (isEnum)
            {
                validPropertyType = CSharpDataType.@enum;
            }
            switch (validPropertyType)
            {
            case CSharpDataType.@int:
                CheckType(bind.IntThrow, bindProperyTypeName, validPropertyName, propertyName);
                if (bindProperyTypeName.IsContainsIn("int", "int32"))
                {
                    method = isNullableType ? getConvertInt32 : getInt32;
                }
                if (bindProperyTypeName.IsContainsIn("int64"))
                {
                    method = isNullableType ? getConvertInt64 : getInt64;
                }
                if (bindProperyTypeName.IsContainsIn("byte"))
                {
                    method = isNullableType ? getConvertByte : getByte;
                }
                if (bindProperyTypeName.IsContainsIn("int16"))
                {
                    method = isNullableType ? getConvertInt16 : getInt16;
                }
                break;

            case CSharpDataType.@bool:
                if (bindProperyTypeName == "bool" || bindProperyTypeName == "boolean")
                {
                    method = isNullableType ? getConvertBoolean : getBoolean;
                }
                break;

            case CSharpDataType.@string:
                if (this.Context.CurrentConnectionConfig.DbType != DbType.Oracle)
                {
                    CheckType(bind.StringThrow, bindProperyTypeName, validPropertyName, propertyName);
                }
                method = getString;
                if (bindProperyTypeName == "guid")
                {
                    method = isNullableType ? getConvertStringGuid : getStringGuid;
                }
                break;

            case CSharpDataType.DateTime:
                CheckType(bind.DateThrow, bindProperyTypeName, validPropertyName, propertyName);
                if (bindProperyTypeName == "datetime")
                {
                    method = isNullableType ? getConvertDateTime : getDateTime;
                }
                if (bindProperyTypeName == "datetime" && dbTypeName.ToLower() == "time")
                {
                    method = isNullableType ? getConvertTime : getTime;
                }
                break;

            case CSharpDataType.@decimal:
                CheckType(bind.DecimalThrow, bindProperyTypeName, validPropertyName, propertyName);
                if (bindProperyTypeName == "decimal")
                {
                    method = isNullableType ? getConvertDecimal : getDecimal;
                }
                break;

            case CSharpDataType.@float:
            case CSharpDataType.@double:
                CheckType(bind.DoubleThrow, bindProperyTypeName, validPropertyName, propertyName);
                if (bindProperyTypeName.IsIn("double", "single") && dbTypeName != "real")
                {
                    method = isNullableType ? getConvertDouble : getDouble;
                }
                else
                {
                    method = isNullableType ? getConvertFloat : getFloat;
                }
                if (dbTypeName.Equals("float", StringComparison.CurrentCultureIgnoreCase) && isNullableType && bindProperyTypeName.Equals("single", StringComparison.CurrentCultureIgnoreCase))
                {
                    method = getConvertDoubleToFloat;
                }
                if (bindPropertyType == UtilConstants.DecType)
                {
                    method = getConvertValueMethod.MakeGenericMethod(bindPropertyType);
                }
                if (bindPropertyType == UtilConstants.IntType)
                {
                    method = getConvertValueMethod.MakeGenericMethod(bindPropertyType);
                }
                break;

            case CSharpDataType.Guid:
                CheckType(bind.GuidThrow, bindProperyTypeName, validPropertyName, propertyName);
                if (bindProperyTypeName == "guid")
                {
                    method = isNullableType ? getConvertGuid : getGuid;
                }
                break;

            case CSharpDataType.@byte:
                if (bindProperyTypeName == "byte")
                {
                    method = isNullableType ? getConvertByte : getByte;
                }
                break;

            case CSharpDataType.@enum:
                method = isNullableType ? getConvertEnum_Null.MakeGenericMethod(bindPropertyType) : getEnum.MakeGenericMethod(bindPropertyType);
                break;

            case CSharpDataType.@short:
                CheckType(bind.ShortThrow, bindProperyTypeName, validPropertyName, propertyName);
                if (bindProperyTypeName == "int16" || bindProperyTypeName == "short")
                {
                    method = isNullableType ? getConvertInt16 : getInt16;
                }
                break;

            case CSharpDataType.@long:
                if (bindProperyTypeName == "int64" || bindProperyTypeName == "long")
                {
                    method = isNullableType ? getConvertInt64 : getInt64;
                }
                break;

            case CSharpDataType.DateTimeOffset:
                method = isNullableType ? getConvertdatetimeoffset : getdatetimeoffset;
                if (bindProperyTypeName == "datetime")
                {
                    method = isNullableType ? getConvertdatetimeoffsetDate : getdatetimeoffsetDate;
                }
                break;

            default:
                method = getConvertValueMethod.MakeGenericMethod(bindPropertyType);
                break;
            }
            if (method == null && bindPropertyType == UtilConstants.StringType)
            {
                method = getConvertString;
            }
            if (bindPropertyType == UtilConstants.ObjType)
            {
                method = getConvertValueMethod.MakeGenericMethod(bindPropertyType);
            }
            if (method == null)
            {
                method = isNullableType ? getOtherNull.MakeGenericMethod(bindPropertyType) : getOther.MakeGenericMethod(bindPropertyType);
            }

            if (method.IsVirtual)
            {
                generator.Emit(OpCodes.Callvirt, method);
            }
            else
            {
                generator.Emit(OpCodes.Call, method);
            }
            #endregion
        }