You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Feature Request: Customize Mapping Handlers for Base types #1765
This would be fine if I was using NpgsqlCommand, because I would be manually creating each param anyway. However, I'm using Dapper, which allows me to pass an object & have it auto-mapped to parameters.
Dapper uses the built-in SqlDbType & npgsql translates them to NpgsqlDbType during execution. So, I don't think this can be fixed by a change in Dapper.
I could modify the SqlDbType =>NpgsqlDbType mappping in npgsql by creating a custom Handler with TypeMapping attributes, however, these classes are all internal.
Example:
//text & citext reversed compared to Npgsql.TextHandler[TypeMapping("text",NpgsqlDbType.Text,DbType.String)][TypeMapping("citext",NpgsqlDbType.Citext,newDbType[]{DbType.String,DbType.StringFixedLength,DbType.AnsiString,DbType.AnsiStringFixedLength},newType[]{typeof(string),typeof(char[]),typeof(char),typeof(ArraySegment<char>)},DbType.String)]publicclassCustomTextHandler:Npgsql.TextHandler{}
Without this, I need to type-cast every text variable in every SQL statement or manually map every parameter to CitextParameter.
Is there a global way to do this that I'm overlooking?
I have found a workaround via reflection, but it's not pretty. It essentially lets the static constructor of TypeHandlerRegistry run (which sets up a bunch of cache dictionaries for all the mappings) & then it swaps citext & text entries.
It seems to work, but I haven't done extensive testing. Obviously reflection is not a good approach & it's likely to break with each npgsql release, but I prefer it over forking the repo.
publicstaticvoidChangeParamMapperToCITextInsteadOfText(){//note: this must be called when the app first launches before anything in the Npgsql namespace has been run, otherwise it's too latevartypeHandlerRegistryType=typeof(NpgsqlConnection).Assembly.GetType("Npgsql.TypeHandlerRegistry");objecthandlerTypes=typeHandlerRegistryType.GetField("HandlerTypes",BindingFlags.Static|BindingFlags.NonPublic).GetValue(null);//Dictionary<string, TypeAndMapping>objecthandlerTypesByNpsgqlDbType=typeHandlerRegistryType.GetField("HandlerTypesByNpsgqlDbType",BindingFlags.Static|BindingFlags.NonPublic).GetValue(null);//Dictionary<NpgsqlDbType, TypeAndMapping>Dictionary<DbType,NpgsqlDbType>dbTypeToNpgsqlDbType=(Dictionary<DbType,NpgsqlDbType>)typeHandlerRegistryType.GetField("DbTypeToNpgsqlDbType",BindingFlags.Static|BindingFlags.NonPublic).GetValue(null);Dictionary<Type,NpgsqlDbType>typeToNpgsqlDbType=(Dictionary<Type,NpgsqlDbType>)typeHandlerRegistryType.GetField("TypeToNpgsqlDbType",BindingFlags.Static|BindingFlags.NonPublic).GetValue(null);varhandlerTypesGetItemMethod=handlerTypes.GetType().GetMethod("get_Item");varhandlerTypesSetItemMethod=handlerTypes.GetType().GetMethod("set_Item");varhandlerTypesByNpsgqlDbTypeSetItemMethod=handlerTypesByNpsgqlDbType.GetType().GetMethod("set_Item");varoldCIText=handlerTypesGetItemMethod.Invoke(handlerTypes,new[]{"citext"});varoldText=handlerTypesGetItemMethod.Invoke(handlerTypes,new[]{"text"});varmappingField=oldText.GetType().GetField("Mapping",BindingFlags.Instance|BindingFlags.NonPublic);vartypeMappingAttributeType=typeof(NpgsqlConnection).Assembly.GetType("Npgsql.TypeMappingAttribute");varpgNameProperty=typeMappingAttributeType.GetProperty("PgName",BindingFlags.Instance|BindingFlags.NonPublic);varnpgsqlDbTypeProperty=typeMappingAttributeType.GetProperty("NpgsqlDbType",BindingFlags.Instance|BindingFlags.NonPublic);varoldTextMappingField=mappingField.GetValue(oldText);varoldCITextMappingField=mappingField.GetValue(oldCIText);pgNameProperty.SetValue(oldTextMappingField,"citext");npgsqlDbTypeProperty.SetValue(oldTextMappingField,NpgsqlDbType.Citext);pgNameProperty.SetValue(oldCITextMappingField,"text");npgsqlDbTypeProperty.SetValue(oldCITextMappingField,NpgsqlDbType.Text);handlerTypesSetItemMethod.Invoke(handlerTypes,new[]{"citext",oldText});handlerTypesSetItemMethod.Invoke(handlerTypes,new[]{"text",oldCIText});handlerTypesByNpsgqlDbTypeSetItemMethod.Invoke(handlerTypesByNpsgqlDbType,new[]{NpgsqlDbType.Citext,oldText});handlerTypesByNpsgqlDbTypeSetItemMethod.Invoke(handlerTypesByNpsgqlDbType,new[]{NpgsqlDbType.Text,oldCIText});List<DbType>dbTypesToChange=newList<DbType>(){DbType.String,DbType.StringFixedLength,DbType.AnsiString,DbType.AnsiStringFixedLength};foreach(DbTypedbTypeToChangeindbTypesToChange){dbTypeToNpgsqlDbType[dbTypeToChange]=NpgsqlDbType.Citext;}List<Type>clrTypesToChange=newList<Type>(){typeof(string),typeof(char[]),typeof(char),typeof(ArraySegment<char>)};foreach(TypeclrTypeToChangeinclrTypesToChange){typeToNpgsqlDbType[clrTypeToChange]=NpgsqlDbType.Citext;}}
What you're asking is already supported in the next version of Npgsql (3.3), which opens up the type handling/mapping system (#1475). I've added a test to demonstrate how this would look like.
You can continue using the reflection hack until 3.3 is released (probably in a couple months).
Activity
speige commentedon Jan 2, 2018
I have found a workaround via reflection, but it's not pretty. It essentially lets the static constructor of TypeHandlerRegistry run (which sets up a bunch of cache dictionaries for all the mappings) & then it swaps citext & text entries.
It seems to work, but I haven't done extensive testing. Obviously reflection is not a good approach & it's likely to break with each npgsql release, but I prefer it over forking the repo.
roji commentedon Jan 2, 2018
What you're asking is already supported in the next version of Npgsql (3.3), which opens up the type handling/mapping system (#1475). I've added a test to demonstrate how this would look like.
You can continue using the reflection hack until 3.3 is released (probably in a couple months).
speige commentedon Jan 2, 2018
Awesome. Thank you!