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
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?
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.
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?
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.
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.
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.
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
.
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.
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.
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... :)
Activity
roji commentedon Dec 12, 2013
Hmm... I'm pretty sure the Postgresql backend supports ASCII so Npgsql should probably support this too... Let us look into this.
franciscojunior commentedon Dec 12, 2013
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 .
franciscojunior commentedon Dec 12, 2013
More info about postgresql encoding can be found here: http://www.postgresql.org/docs/9.2/static/multibyte.html
MrJul commentedon Dec 12, 2013
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?
MrJul commentedon Dec 12, 2013
I managed to work around it.
For possible future readers, use
convert_from(@param, 'LATIN1'), and pass all your string parameters as byte arrays usingEncoding.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 aWHEREclause, but that wasn't a concern in my case.franciscojunior commentedon Dec 12, 2013
Excellent!
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.
MrJul commentedon Dec 12, 2013
I tried something along these lines, using
convert(@p, 'UTF8', 'LATIN1'), withpbeing 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.franciscojunior commentedon Dec 12, 2013
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:
Regards,
Francisco Figueiredo Jr.
Npgsql Lead Developer
http://www.npgsql.org
http://gplus.to/franciscojunior
http://fxjr.blogspot.com
http://twitter.com/franciscojunior
franciscojunior commentedon Dec 12, 2013
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.
MrJul commentedon Dec 12, 2013
There's an
InvalidCastExceptionthrown, 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 toconvert_from.I see what you meant though, in pure pgsql, any string is convertible to
textorbytea. With an explicit cast totextthe function won't be resolved anymore, and with an explicit cast tobyteait will work. But not through the Npgsql client. Not a big deal though, I'm fine with encoding the string myself.franciscojunior commentedon Dec 13, 2013
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... :)