r2dbc / r2dbc-mssql

R2DBC Driver for Microsoft SQL Server using TDS (Tabular Data Stream) Protocol
Apache License 2.0
178 stars 32 forks source link

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

Closed JINWANDALAOHU250 closed 1 year ago

JINWANDALAOHU250 commented 1 year ago

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 ?

mp911de commented 1 year ago

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.

JINWANDALAOHU250 commented 1 year ago

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.

mp911de commented 1 year ago

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.

JINWANDALAOHU250 commented 1 year ago

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!!!!
mp911de commented 1 year ago

Basically, the following:

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