Skip to content

Feature Request: Customize Mapping Handlers for Base types #1765

@speige

Description

@speige

When calling any of the NpgsqlCommand.Execute* methods, I'd like to make the default mapping from system.string point to citext instead of text.

The only way I know how to do this is via:

class CitextParameter : SqlMapper.ICustomQueryParameter
{
    readonly string _value;

    public CitextParameter(string value)
    {
        _value = value;
    }

    public void AddParameter(IDbCommand command, string name)
    {
        command.Parameters.Add(new NpgsqlParameter
        {
            ParameterName = name,
            NpgsqlDbType = NpgsqlDbType.Citext,
            Value = _value
        });
    }
}

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, new DbType[] { DbType.String, DbType.StringFixedLength, DbType.AnsiString, DbType.AnsiStringFixedLength }, new Type[] { typeof(string), typeof(char[]), typeof(char), typeof(ArraySegment<char>) }, DbType.String)]
public class CustomTextHandler : 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?

P.S. This is an awesome library. Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions