r2dbc/r2dbc-mssql

Choosing whether to use `nvarchar` vs `nvarchar` encoding for indivual IN values

JINWANDALAOHU250 opened this issue · 5 comments

Feature Request

In java entity , the column username is chinese string ,but when I use ReactiveCrudRepository.save() method insert into sqlServer is messy code like ‘???’ How can I map Java Type String to nvarchar ?

The driver lets you configure the default encoding for string. By default, sendStringParametersAsUnicode is configured to true using NVARCHAR-encoded data when sending character data to your server.

THANKS!

Thank mp911de for help, sendStringParametersAsUnicode = trueis worked!

NEW QUESTION

Turns out to be another colleague to modify the default configuration.
But I have another question, when sendStringParametersAsUnicode=true String type parameter will transform to unicode, that make sql be slow, Is there any other solution to solve this problem ? (Like preparedStatement.setNString(1, "你好世界"); just for some field ,not global )

ANOTHER

My English is not good, if I can not express clearly, please forgive me.

If you want to send individual values ar NVARCHAR, then you can specify this through the R2DBC API via Parameters.in(R2dbcType.NVARCHAR, "…"). Alternatively, all other values, specified Parameters.in(R2dbcType.VARCHAR, "…") would default to VARCHAR if sendStringParametersAsUnicode is set to false.

Spring Data R2DBC doesn't provide means to encode the target Type (VARCHAR vs. NVARCHAR) into insert or update operations.

I'm closing this one as the question is answered.

Thanks For Help, your excellency,

I want you to show me the highest respect !!!.

Follow your instructions I found Parameter.in(Type type, @Nullable Object value) in package io.r2dbc.spi .
But I still don't understand, how am I supposed to use it when I insert SQL. Can you give an example if you have time?

May the force be with you !!! Thanks a lot!!!!

Basically, the following:

connection.createStatement("INSERT INTO my_table (some_column) VALUES(:some_column_value)")
   .bind("some_column_value", Parameters.in(R2dbcType.NVARCHAR, "…"))
   .execute()