Skip to content

Support for non-UTF8 encoding #124

Closed

Description

@MrJul

Hello,

I have to deal with a PostgreSQL database I've no control over, where the encoding is set to SQL_ASCII. Every client of this database is expected to encode text values in Latin-1. This sucks, I know, Unicode exists for a thing...

Well, that's my unfortunate requirements, and I can't meet them with Npgsql. All strings are always UTF-8 encoded (believe me, I understand this is normally a good thing) and there seems to be no way to change that.

There is a now obsolete connection parameter Encoding, so I suppose support for this has been willingly dropped. I tried to use byte arrays parameters instead of strings or changing the DbType, but the bytes get escaped and the resulting strings aren't correct.

Before I fork my own Npgsql with Latin-1 encoding just for this terrible database, is there any way I can currently make this work? Did I miss something obvious? Is some support planned?

Thanks.

Activity

  1. roji commented on Dec 12, 2013

    @roji
    Member

    Hmm... I'm pretty sure the Postgresql backend supports ASCII so Npgsql should probably support this too... Let us look into this.

  2. franciscojunior commented on Dec 12, 2013

    @franciscojunior
    Member

    In fact, ASCII encode was only supported on Npgsql1. We dropped support for ASCII after we came to the conclusion that by using UTF-8, Postgresql server would make the proper conversion between different server encodings BUT ASCII :(

    As it isn't recommended to use ASCII encoding anymore we decided to drop it when we start to work on Npgsql 2.x version.

    Sorry for that, @MrJul .

  3. MrJul commented on Dec 12, 2013

    @MrJul
    Author

    Thanks for the answer. I think I would have done the same thing as you regarding support for obsolete ASCII.

    I saw this page, if only the database encoding had been properly configured the conversion from and to UTF-8 would be automatic. Alas, that's not the case.

    Well, a fork it is :( Is the current trunk stable?

  4. MrJul commented on Dec 12, 2013

    @MrJul
    Author

    I managed to work around it.

    For possible future readers, use convert_from(@param, 'LATIN1'), and pass all your string parameters as byte arrays using Encoding.GetBytes() in the encoding of your choice, here Latin-1. Better than a fork, and works like a charm. I don't know if it might kill some indexing if used in a WHERE clause, but that wasn't a concern in my case.

  5. franciscojunior commented on Dec 12, 2013

    @franciscojunior
    Member

    I managed to work around it.

    Excellent!

    For possible future readers, use convert_from(@PARAM, 'LATIN1'), and pass all your string parameters as byte arrays using Encoding.GetBytes() in the encoding of your choice, here Latin-1. Better than a fork, and works like a charm. I don't know if it might kill some indexing if used in a WHERE clause, but that wasn't a concern in my case.

    I didn't try that yet, but I was thinking that maybe you could use Npgsql Encoding.GetBytes internal calls in your favor. You could try pass UTF-8 as parameter for convert_from and let Npgsql do the dirty work for you.

  6. MrJul commented on Dec 12, 2013

    @MrJul
    Author

    I tried something along these lines, using convert(@p, 'UTF8', 'LATIN1'), with p being a normal string, which should work.

    However Npsql insists on adding a cast. The SQL then looks like convert('foo'::text, 'UTF8', 'LATIN1'), resulting in ERROR: function convert(text, unknown, unknown) does not exist. The same query ran manually without ::text works though.

  7. franciscojunior commented on Dec 12, 2013

    @franciscojunior
    Member

    I see....

    Well, in order to remove this cast you would need to set the parameter type
    to DbType.Object. But this would be more like a hack than a good solution.
    :)

    This topic made me remind that we should make something about this type
    casting handling inside Npgsql. Sometimes it gives more trouble to users
    than solutions.

    I'll check that.

    On Thu, Dec 12, 2013 at 12:53 PM, Julien Lebosquain <
    [email protected]> wrote:

    I tried something along these lines, using convert(@p, 'UTF8', 'LATIN1'),
    with p being a normal string, which should work.

    However Npsql insists on adding a cast. The SQL then looks like convert('foo'::text,
    'UTF8', 'LATIN1'), resulting in ERROR: function convert(text, unknown,
    unknown) does not exist
    . The same query ran manually without ::textworks though.

    —
    Reply to this email directly or view it on GitHubhttps://github.com//issues/124#issuecomment-30428446
    .

    Regards,

    Francisco Figueiredo Jr.
    Npgsql Lead Developer
    http://www.npgsql.org
    http://gplus.to/franciscojunior
    http://fxjr.blogspot.com
    http://twitter.com/franciscojunior

  8. franciscojunior commented on Dec 12, 2013

    @franciscojunior
    Member

    Just another thing... Although the insertion of the explicit cast is giving you problem, the error message is correct because the function convert has the first parameter of type bytea and not text. I think that if you specify the NpgsqlDbType of the parameter to NpgsqlDbType.Bytea, it will work.

  9. MrJul commented on Dec 12, 2013

    @MrJul
    Author

    There's an InvalidCastException thrown, since it's a string and not a byte array. Then I have to provide the byte array myself, and might as well go back to convert_from.

    I see what you meant though, in pure pgsql, any string is convertible to text or bytea. With an explicit cast to text the function won't be resolved anymore, and with an explicit cast to bytea it will work. But not through the Npgsql client. Not a big deal though, I'm fine with encoding the string myself.

  10. franciscojunior commented on Dec 13, 2013

    @franciscojunior
    Member

    There's an InvalidCastException thrown, since it's a string and not a byte array. Then I have to provide the byte array myself, and might as well go back to convert_from.

    I see what you meant though, in pure pgsql, any string is convertible to text or bytea. With an explicit cast to text the function won't be resolved anymore, and with an explicit cast to bytea it will work. But not through the Npgsql client. Not a big deal though, I'm fine with encoding the string myself.

    Yes, that was the point. But I forgot that although internally Npgsql would convert the data to byte array with encoding.GetBytes, Npgsql won't allow you to send a string parameter as a bytea data type. You would need to send a byte[] which is what you are already doing with Encoding.GetBytes. So this time Npgsql won't be able to be used for the dirty work... :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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