From d6e4e2bb1470a2ad9b89ca613ec9ad6ba8274046 Mon Sep 17 00:00:00 2001 From: Ananya Garg Date: Tue, 7 Jan 2025 18:13:28 +0530 Subject: [PATCH] updated as per review comments --- .../jdbc/SQLServerCallableStatement.java | 1 + .../statement/BigDecimalPrecisionTest.java | 94 ---------------- .../jdbc/unit/statement/StatementTest.java | 106 ++++++++++++++++++ 3 files changed, 107 insertions(+), 94 deletions(-) delete mode 100644 src/test/java/com/microsoft/sqlserver/jdbc/unit/statement/BigDecimalPrecisionTest.java diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerCallableStatement.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerCallableStatement.java index 9e59c6691..8e40c2fca 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerCallableStatement.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerCallableStatement.java @@ -163,6 +163,7 @@ public void registerOutParameter(int index, int sqlType) throws SQLServerExcepti param.setOutScale(scale); } catch (SQLException e) { loggerExternal.warning("Failed to fetch scale for DECIMAL type parameter at index " + index + ": " + e.getMessage()); + throw new SQLServerException(SQLServerException.getErrString("R_InvalidScale"), null, 0, e); } } break; diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/unit/statement/BigDecimalPrecisionTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/unit/statement/BigDecimalPrecisionTest.java deleted file mode 100644 index 3aac27300..000000000 --- a/src/test/java/com/microsoft/sqlserver/jdbc/unit/statement/BigDecimalPrecisionTest.java +++ /dev/null @@ -1,94 +0,0 @@ -package com.microsoft.sqlserver.jdbc.unit.statement; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import java.math.BigDecimal; -import java.sql.CallableStatement; -import java.sql.Connection; -import java.sql.SQLException; -import java.sql.Statement; -import java.sql.Types; - -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Tag; -import org.junit.jupiter.api.Test; - -import com.microsoft.sqlserver.jdbc.RandomUtil; -import com.microsoft.sqlserver.testframework.AbstractSQLGenerator; -import com.microsoft.sqlserver.testframework.AbstractTest; - -public class BigDecimalPrecisionTest extends AbstractTest { - - String procName1 = AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("test_bigdecimal_3")); - String procName2 = AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("test_bigdecimal_5")); - - @BeforeEach - public void init() throws SQLException { - try (Connection connection = getConnection()) { - String dropProcedureSQL = "DROP PROCEDURE IF EXISTS " + procName1 + ", " + procName2; - try (Statement stmt = connection.createStatement()) { - stmt.execute(dropProcedureSQL); - } - - String createProcedureSQL1 = "CREATE PROCEDURE " + procName1 + "\n" + - " @big_decimal_type decimal(15, 3),\n" + - " @big_decimal_type_o decimal(15, 3) OUTPUT\n" + - "AS\n" + - "BEGIN\n" + - " SET @big_decimal_type_o = @big_decimal_type;\n" + - "END;"; - String createProcedureSQL2 = "CREATE PROCEDURE " + procName2 + "\n" + - " @big_decimal_type decimal(15, 5),\n" + - " @big_decimal_type_o decimal(15, 5) OUTPUT\n" + - "AS\n" + - "BEGIN\n" + - " SET @big_decimal_type_o = @big_decimal_type;\n" + - "END;"; - try (Statement stmt = connection.createStatement()) { - stmt.execute(createProcedureSQL1); - stmt.execute(createProcedureSQL2); - } - } - } - - @AfterEach - public void terminate() throws SQLException { - try (Connection connection = getConnection()) { - try (Statement stmt = connection.createStatement()) { - String dropProcedureSQL = "DROP PROCEDURE IF EXISTS " + procName1 + ", " + procName2; - stmt.execute(dropProcedureSQL); - } - } - } - - @Test - @Tag("BigDecimal") - public void testBigDecimalPrecision() throws SQLException { - try (Connection connection = getConnection()) { - // Test for DECIMAL(15, 3) - String callSQL1 = "{call " + procName1 + "(100.241, ?)}"; - try (CallableStatement call = connection.prepareCall(callSQL1)) { - call.registerOutParameter(1, Types.DECIMAL); - call.execute(); - BigDecimal actual1 = call.getBigDecimal(1); - assertEquals(new BigDecimal("100.241"), actual1); - } - - // Test for DECIMAL(15, 5) - String callSQL2 = "{call " + procName2 + "(100.24112, ?)}"; - try (CallableStatement call = connection.prepareCall(callSQL2)) { - call.registerOutParameter(1, Types.DECIMAL); - call.execute(); - BigDecimal actual2 = call.getBigDecimal(1); - assertEquals(new BigDecimal("100.24112"), actual2); - } - } - } - - @BeforeAll - public static void setupTests() throws Exception { - setConnection(); - } -} diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/unit/statement/StatementTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/unit/statement/StatementTest.java index b7fea527d..0da7566cd 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/unit/statement/StatementTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/unit/statement/StatementTest.java @@ -2691,4 +2691,110 @@ public void terminate() throws Exception { } } } + + @Nested + @Tag(Constants.xAzureSQLDW) + public class BigDecimalPrecisionTest { + + private static String procName1 = AbstractSQLGenerator + .escapeIdentifier(RandomUtil.getIdentifier("test_bigdecimal_3")); + private static String procName2 = AbstractSQLGenerator + .escapeIdentifier(RandomUtil.getIdentifier("test_bigdecimal_5")); + private static String procNameMaxScale = AbstractSQLGenerator + .escapeIdentifier(RandomUtil.getIdentifier("test_bigdecimal_max_scale")); + private static String procNameMaxPrecision = AbstractSQLGenerator + .escapeIdentifier(RandomUtil.getIdentifier("test_bigdecimal_max_precision")); + + @BeforeEach + public void init() throws SQLException { + try (Connection connection = getConnection()) { + String dropProcedureSQL = "DROP PROCEDURE IF EXISTS " + procName1 + ", " + procName2 + ", " + + procNameMaxScale + ", " + procNameMaxPrecision; + try (Statement stmt = connection.createStatement()) { + stmt.execute(dropProcedureSQL); + } + + String createProcedureSQL1 = "CREATE PROCEDURE " + procName1 + "\n" + + " @big_decimal_type decimal(15, 3),\n" + + " @big_decimal_type_o decimal(15, 3) OUTPUT\n" + "AS\n" + "BEGIN\n" + + " SET @big_decimal_type_o = @big_decimal_type;\n" + "END;"; + String createProcedureSQL2 = "CREATE PROCEDURE " + procName2 + "\n" + + " @big_decimal_type decimal(15, 5),\n" + + " @big_decimal_type_o decimal(15, 5) OUTPUT\n" + "AS\n" + "BEGIN\n" + + " SET @big_decimal_type_o = @big_decimal_type;\n" + "END;"; + String createProcedureMaxScale = "CREATE PROCEDURE " + procNameMaxScale + "\n" + + " @big_decimal_type decimal(38, 38),\n" + + " @big_decimal_type_o decimal(38, 38) OUTPUT\n" + "AS\n" + "BEGIN\n" + + " SET @big_decimal_type_o = @big_decimal_type;\n" + "END;"; + String createProcedureMaxPrecision = "CREATE PROCEDURE " + procNameMaxPrecision + "\n" + + " @big_decimal_type decimal(38, 0),\n" + + " @big_decimal_type_o decimal(38, 0) OUTPUT\n" + "AS\n" + "BEGIN\n" + + " SET @big_decimal_type_o = @big_decimal_type;\n" + "END;"; + + try (Statement stmt = connection.createStatement()) { + stmt.execute(createProcedureSQL1); + stmt.execute(createProcedureSQL2); + stmt.execute(createProcedureMaxScale); + stmt.execute(createProcedureMaxPrecision); + } + } + } + + @AfterEach + public void terminate() throws SQLException { + try (Connection connection = getConnection()) { + try (Statement stmt = connection.createStatement()) { + String dropProcedureSQL = "DROP PROCEDURE IF EXISTS " + procName1 + ", " + procName2 + ", " + + procNameMaxScale + ", " + procNameMaxPrecision; + stmt.execute(dropProcedureSQL); + } + } + } + + @Test + @Tag("BigDecimal") + public void testBigDecimalPrecision() throws SQLException { + try (Connection connection = getConnection()) { + // Test for DECIMAL(15, 3) + String callSQL1 = "{call " + procName1 + "(100.241, ?)}"; + try (CallableStatement call = connection.prepareCall(callSQL1)) { + call.registerOutParameter(1, Types.DECIMAL); + call.execute(); + BigDecimal actual1 = call.getBigDecimal(1); + assertEquals(new BigDecimal("100.241"), actual1); + } + + // Test for DECIMAL(15, 5) + String callSQL2 = "{call " + procName2 + "(100.24112, ?)}"; + try (CallableStatement call = connection.prepareCall(callSQL2)) { + call.registerOutParameter(1, Types.DECIMAL); + call.execute(); + BigDecimal actual2 = call.getBigDecimal(1); + assertEquals(new BigDecimal("100.24112"), actual2); + } + + // Test for DECIMAL(38, 38) + String callSQLMaxScale = "{call " + procNameMaxScale + "(?, ?)}"; + try (CallableStatement call = connection.prepareCall(callSQLMaxScale)) { + BigDecimal maxScaleValue = new BigDecimal("0." + "1".repeat(38)); + call.setBigDecimal(1, maxScaleValue); + call.registerOutParameter(2, Types.DECIMAL); + call.execute(); + BigDecimal actualMaxScale = call.getBigDecimal(2); + assertEquals(maxScaleValue, actualMaxScale, "DECIMAL(38, 38) max scale test failed"); + } + + // Test for DECIMAL(38, 0) + String callSQLMaxPrecision = "{call " + procNameMaxPrecision + "(?, ?)}"; + try (CallableStatement call = connection.prepareCall(callSQLMaxPrecision)) { + BigDecimal maxPrecisionValue = new BigDecimal("9".repeat(38)); + call.setBigDecimal(1, maxPrecisionValue); + call.registerOutParameter(2, Types.DECIMAL); + call.execute(); + BigDecimal actualMaxPrecision = call.getBigDecimal(2); + assertEquals(maxPrecisionValue, actualMaxPrecision, "DECIMAL(38, 0) max precision test failed"); + } + } + } + } }