From a8fe5b4acb970d9e2427371c773dc3a1ee8eb255 Mon Sep 17 00:00:00 2001 From: Terry Chow Date: Wed, 28 Dec 2022 11:29:57 -0800 Subject: [PATCH 1/3] Fixed null sql state and 0 error code for sql exceptions --- .../sqlserver/jdbc/SQLServerResultSet.java | 4 +- .../jdbc/resultset/ResultSetTest.java | 37 +++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerResultSet.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerResultSet.java index 939fef4d8..0bafd1441 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerResultSet.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerResultSet.java @@ -402,7 +402,7 @@ boolean onDone(TDSReader tdsReader) throws SQLServerException { SQLServerError databaseError = this.getDatabaseError(); MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_serverError")); Object[] msgArgs = {status, (databaseError != null) ? databaseError.getErrorMessage() : ""}; - SQLServerException.makeFromDriverError(stmt.connection, stmt, form.format(msgArgs), null, false); + SQLServerException.makeFromDatabaseError(stmt.connection, null, form.format(msgArgs), databaseError, false); } return false; @@ -5404,7 +5404,7 @@ boolean onDone(TDSReader tdsReader) throws SQLServerException { SQLServerError databaseError = getDatabaseError(); MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_serverError")); Object[] msgArgs = {status, (databaseError != null) ? databaseError.getErrorMessage() : ""}; - SQLServerException.makeFromDriverError(stmt.connection, stmt, form.format(msgArgs), null, false); + SQLServerException.makeFromDatabaseError(stmt.connection, null, form.format(msgArgs), databaseError, false); } diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/resultset/ResultSetTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/resultset/ResultSetTest.java index c176fb2cd..522890c5b 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/resultset/ResultSetTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/resultset/ResultSetTest.java @@ -30,7 +30,12 @@ import java.util.TimeZone; import java.util.UUID; +import com.microsoft.sqlserver.jdbc.SQLServerConnection; +import com.microsoft.sqlserver.jdbc.TestResource; +import com.microsoft.sqlserver.testframework.PrepUtil; +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 org.junit.platform.runner.JUnitPlatform; @@ -48,6 +53,10 @@ public class ResultSetTest extends AbstractTest { private static final String tableName = RandomUtil.getIdentifier("StatementParam"); + private static final String expectedSqlState = "S0001"; + + private static final int expectedErrorCodeFetchBuffer = 8134; + static final String uuid = UUID.randomUUID().toString(); @BeforeAll @@ -55,6 +64,20 @@ public static void setupTests() throws Exception { setConnection(); } + @BeforeEach + public void init() throws Exception { + try (Connection conn = getConnection(); Statement stmt = conn.createStatement()) { + TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(tableName), stmt); + } + } + + @AfterEach + public void cleanUp() throws Exception { + try (Connection conn = getConnection(); Statement stmt = conn.createStatement()) { + TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(tableName), stmt); + } + } + /** * Tests proper exception for unsupported operation * @@ -595,4 +618,18 @@ public void testMultipleResultSets() throws SQLException { } } } + + @Test + public void testResultSetSqlErrorState() throws Exception { + try (SQLServerConnection connection = PrepUtil.getConnection(connectionString)) { + try (Statement stmt = connection.createStatement()) { + ResultSet rs = stmt.executeQuery("select 1/0"); + rs.next(); + fail(TestResource.getResource("R_expectedFailPassed")); + } catch (SQLException e) { + assertEquals(expectedSqlState, e.getSQLState()); + assertEquals(expectedErrorCodeFetchBuffer, e.getErrorCode()); + } + } + } } From d8bf7a5b75e94663841b752366d2d5ba6ac1913b Mon Sep 17 00:00:00 2001 From: Terry Chow Date: Wed, 28 Dec 2022 11:46:11 -0800 Subject: [PATCH 2/3] Additional test --- .../jdbc/resultset/ResultSetTest.java | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/resultset/ResultSetTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/resultset/ResultSetTest.java index 522890c5b..4d69693b9 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/resultset/ResultSetTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/resultset/ResultSetTest.java @@ -55,7 +55,7 @@ public class ResultSetTest extends AbstractTest { private static final String expectedSqlState = "S0001"; - private static final int expectedErrorCodeFetchBuffer = 8134; + private static final int expectedErrorCode = 8134; static final String uuid = UUID.randomUUID().toString(); @@ -620,7 +620,7 @@ public void testMultipleResultSets() throws SQLException { } @Test - public void testResultSetSqlErrorState() throws Exception { + public void testResultSetFetchBufferSqlErrorState() throws Exception { try (SQLServerConnection connection = PrepUtil.getConnection(connectionString)) { try (Statement stmt = connection.createStatement()) { ResultSet rs = stmt.executeQuery("select 1/0"); @@ -628,8 +628,28 @@ public void testResultSetSqlErrorState() throws Exception { fail(TestResource.getResource("R_expectedFailPassed")); } catch (SQLException e) { assertEquals(expectedSqlState, e.getSQLState()); - assertEquals(expectedErrorCodeFetchBuffer, e.getErrorCode()); + assertEquals(expectedErrorCode, e.getErrorCode()); } } } + + @Test + public void testResultSetClientCursorInitializerSqlErrorState() throws Exception { + try (Connection con = PrepUtil.getConnection(connectionString); Statement stmt = con.createStatement()) { + stmt.executeUpdate("create table " + AbstractSQLGenerator.escapeIdentifier(tableName) + + " (col1 int)"); + stmt.setQueryTimeout(1); + boolean hasResults = stmt.execute("select * from " + AbstractSQLGenerator.escapeIdentifier(tableName) + + "; select 1/0"); + while(hasResults) { + ResultSet rs = stmt.getResultSet(); + while (rs.next()) {} + hasResults = stmt.getMoreResults(); + } + fail(TestResource.getResource("R_expectedFailPassed")); + } catch (SQLException e) { + assertEquals(expectedSqlState, e.getSQLState()); + assertEquals(expectedErrorCode, e.getErrorCode()); + } + } } From 4c41cbb2e3b5f968cbfa265f11adcf432dc1847a Mon Sep 17 00:00:00 2001 From: Terry Chow Date: Wed, 28 Dec 2022 12:50:38 -0800 Subject: [PATCH 3/3] Fix for case when db error is null --- .../sqlserver/jdbc/SQLServerResultSet.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerResultSet.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerResultSet.java index 0bafd1441..0d1a154f9 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerResultSet.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerResultSet.java @@ -402,7 +402,12 @@ boolean onDone(TDSReader tdsReader) throws SQLServerException { SQLServerError databaseError = this.getDatabaseError(); MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_serverError")); Object[] msgArgs = {status, (databaseError != null) ? databaseError.getErrorMessage() : ""}; - SQLServerException.makeFromDatabaseError(stmt.connection, null, form.format(msgArgs), databaseError, false); + + if (null != databaseError) { + SQLServerException.makeFromDatabaseError(stmt.connection, null, form.format(msgArgs), databaseError, false); + } else { + SQLServerException.makeFromDriverError(stmt.connection, stmt, form.format(msgArgs), null, false); + } } return false; @@ -5404,7 +5409,12 @@ boolean onDone(TDSReader tdsReader) throws SQLServerException { SQLServerError databaseError = getDatabaseError(); MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_serverError")); Object[] msgArgs = {status, (databaseError != null) ? databaseError.getErrorMessage() : ""}; - SQLServerException.makeFromDatabaseError(stmt.connection, null, form.format(msgArgs), databaseError, false); + + if (null != databaseError) { + SQLServerException.makeFromDatabaseError(stmt.connection, null, form.format(msgArgs), databaseError, false); + } else { + SQLServerException.makeFromDriverError(stmt.connection, stmt, form.format(msgArgs), null, false); + } }