Skip to content

Commit b391db9

Browse files
committed
Avoid redundant JSON to string conversion
When creating the param definition, the JSON payload was converted to string. And then converted again when encoding the param. In fact, MSSQL allows to send JSON as NVARCHAR(MAX) regardless of the size. And then we can have a single conversion when encoding the param. Signed-off-by: Thomas Segismont <tsegismont@gmail.com>
1 parent 963e8d3 commit b391db9

1 file changed

Lines changed: 14 additions & 9 deletions

File tree

  • vertx-mssql-client/src/main/java/io/vertx/mssqlclient/impl/codec

vertx-mssql-client/src/main/java/io/vertx/mssqlclient/impl/codec/DataType.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -873,11 +873,6 @@ public void encodeParam(ByteBuf byteBuf, String name, boolean out, Object value)
873873
byteBuf.writeCharSequence(val, StandardCharsets.UTF_16LE);
874874
}
875875
}
876-
877-
private void writeCollation(ByteBuf byteBuf) {
878-
byteBuf.writeInt(0x0904d000);
879-
byteBuf.writeByte(0x34);
880-
}
881876
},
882877
NCHAR(0xEF) {
883878
@Override
@@ -995,14 +990,19 @@ public Object decodeValue(ByteBuf byteBuf, TypeInfo typeInfo) {
995990

996991
@Override
997992
public String paramDefinition(Object value) {
998-
// SQL Server expects text for JSON params
999-
return NVARCHAR.paramDefinition(value);
993+
return "nvarchar(max)";
1000994
}
1001995

1002996
@Override
1003997
public void encodeParam(ByteBuf byteBuf, String name, boolean out, Object value) {
1004-
// SQL Server expects text for JSON params
1005-
NVARCHAR.encodeParam(byteBuf, name, out, value);
998+
writeParamDescription(byteBuf, name, out, NVARCHAR.id);
999+
String val = value.toString();
1000+
byteBuf.writeShortLE(0xFFFF);
1001+
writeCollation(byteBuf);
1002+
byteBuf.writeLongLE(val.length() * 2L);
1003+
byteBuf.writeIntLE(val.length() * 2);
1004+
byteBuf.writeCharSequence(val, StandardCharsets.UTF_16LE);
1005+
byteBuf.writeIntLE(0);
10061006
}
10071007
},
10081008

@@ -1212,4 +1212,9 @@ private static int daysFromStartDate(LocalDate localDate) {
12121212
private static long hundredsOfNanos(LocalTime localTime) {
12131213
return localTime.toNanoOfDay() / 100;
12141214
}
1215+
1216+
private static void writeCollation(ByteBuf byteBuf) {
1217+
byteBuf.writeInt(0x0904d000);
1218+
byteBuf.writeByte(0x34);
1219+
}
12151220
}

0 commit comments

Comments
 (0)