Skip to content

Commit 3397f43

Browse files
committed
Extract the numeric parameter scale
Compute the scale once for the parameter definition and the encoded value, and cover a negative value with a negative scale. Signed-off-by: Sharang Gupta <sharang@sharanggupta.dev> Assisted-by: Anthropic Claude (claude-fable-5-2)
1 parent 50f1641 commit 3397f43

2 files changed

Lines changed: 18 additions & 6 deletions

File tree

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -407,20 +407,18 @@ public Object decodeValue(ByteBuf byteBuf, TypeInfo typeInfo) {
407407

408408
@Override
409409
public String paramDefinition(Object value) {
410-
return "numeric(38," + (value == null ? 0 : Math.max(0, ((BigDecimal) value).scale())) + ")";
410+
return "numeric(38," + (value == null ? 0 : paramScale((BigDecimal) value)) + ")";
411411
}
412412

413413
@Override
414414
public void encodeParam(ByteBuf byteBuf, String name, boolean out, Object value) {
415-
BigDecimal bigDecimal = (BigDecimal) value;
416-
if (bigDecimal.scale() < 0) {
417-
bigDecimal = bigDecimal.setScale(0);
418-
}
415+
int scale = paramScale((BigDecimal) value);
416+
BigDecimal bigDecimal = ((BigDecimal) value).setScale(scale);
419417
writeParamDescription(byteBuf, name, out, id);
420418
writeParamSize(byteBuf, 17, 38);
421419
int sign = bigDecimal.signum() < 0 ? 0 : 1;
422420
byte[] bytes = (sign == 0 ? bigDecimal.negate() : bigDecimal).unscaledValue().toByteArray();
423-
byteBuf.writeByte(bigDecimal.scale());
421+
byteBuf.writeByte(scale);
424422
byteBuf.writeByte(1 + bytes.length);
425423
byteBuf.writeByte(sign);
426424
for (int i = bytes.length - 1; i >= 0; i--) byteBuf.writeByte(bytes[i]);
@@ -1053,6 +1051,13 @@ public Object decodeValue(ByteBuf byteBuf, TypeInfo typeInfo) {
10531051
this.id = id;
10541052
}
10551053

1054+
/**
1055+
* @return the scale of a numeric parameter: the server does not accept a negative scale
1056+
*/
1057+
private static int paramScale(BigDecimal value) {
1058+
return Math.max(0, value.scale());
1059+
}
1060+
10561061
private static void skipMultipartTableName(ByteBuf byteBuf) {
10571062
int numParts = byteBuf.readUnsignedByte();
10581063
for (int i = 0; i < numParts; i++) {

‎vertx-mssql-client/src/test/java/io/vertx/tests/mssqlclient/data/MSSQLPreparedQueryNotNullableDataTypeTest.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ public void testEncodeDecimalWithNegativeScale(TestContext ctx) {
8181
});
8282
}
8383

84+
@Test
85+
public void testEncodeNegativeDecimalWithNegativeScale(TestContext ctx) {
86+
testPreparedQueryEncodeGeneric(ctx, "not_nullable_datatype", "test_decimal", new BigDecimal("-1500").stripTrailingZeros(), row -> {
87+
checkNumber(row, "test_decimal", new BigDecimal("-1500"));
88+
});
89+
}
90+
8491
@Test
8592
public void testEncodeBit(TestContext ctx) {
8693
testPreparedQueryEncodeGeneric(ctx, "not_nullable_datatype", "test_boolean", false, row -> {

0 commit comments

Comments
 (0)