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 = isNullableType ? getOtherNull.MakeGenericMethod(bindPropertyType) : getOther.MakeGenericMethod(bindPropertyType); } else if (bindPropertyType == UtilConstants.StringType) { method = getString; } else { method = isNullableType ? getOtherNull.MakeGenericMethod(bindPropertyType) : getOther.MakeGenericMethod(bindPropertyType); } 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; } if (bindProperyTypeName == "datetimeoffset") { method = isNullableType ? getConvertdatetimeoffset : getdatetimeoffset; } 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 = isNullableType ? getOtherNull.MakeGenericMethod(bindPropertyType) : getOther.MakeGenericMethod(bindPropertyType); } if (bindPropertyType == UtilConstants.IntType) { method = isNullableType ? getOtherNull.MakeGenericMethod(bindPropertyType) : getOther.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; case CSharpDataType.Single: break; default: method = isNullableType ? getOtherNull.MakeGenericMethod(bindPropertyType) : getOther.MakeGenericMethod(bindPropertyType); break; } if (method == null && bindPropertyType == UtilConstants.StringType) { method = getConvertString; } if (bindPropertyType == UtilConstants.ObjType) { method = isNullableType ? getOtherNull.MakeGenericMethod(bindPropertyType) : getOther.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 }
private void BindMethod(ILGenerator generator, PropertyInfo bindProperty, int ordinal) { IDbBind bind = Context.Ado.DbBind; bool isNullableType = false; MethodInfo method = null; Type bindPropertyType = UtilMethods.GetUnderType(bindProperty, ref isNullableType); string dbTypeName = DataRecord.GetDataTypeName(ordinal); if (Regex.IsMatch(dbTypeName, @"\(.+\)")) { dbTypeName = Regex.Replace(dbTypeName, @"\(.+\)", ""); } string propertyName = bindProperty.Name; 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.StringType) { method = getString; } else if (bindPropertyType == UtilConstants.ByteArrayType) { method = getValueMethod; generator.Emit(OpCodes.Call, method); generator.Emit(OpCodes.Unbox_Any, bindProperty.PropertyType); return; } else { method = isNullableType ? getSqliteTypeNull.MakeGenericMethod(bindPropertyType) : getSqliteType.MakeGenericMethod(bindPropertyType); } 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: 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; } 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 == "double") { method = isNullableType ? getConvertDouble : getDouble; } if (bindProperyTypeName == "single") { method = isNullableType ? getConvertFloat : getFloat; } 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 = getValueMethod; break; } if (method == null && bindPropertyType == UtilConstants.StringType) { method = getConvertString; } if (method == null) { method = isNullableType ? getOtherNull.MakeGenericMethod(bindPropertyType) : getOther.MakeGenericMethod(bindPropertyType); } generator.Emit(OpCodes.Call, method); if (method == getValueMethod) { generator.Emit(OpCodes.Unbox_Any, bindProperty.PropertyType); } #endregion }