Skip to content

Commit

Permalink
Fixed null sql state and 0 error code for sql exceptions (#2018)
Browse files Browse the repository at this point in the history
* Fixed null sql state and 0 error code for sql exceptions

* Additional test

* Fix for case when db error is null
  • Loading branch information
tkyc authored Dec 28, 2022
1 parent ef3ff46 commit 6b571af
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/main/java/com/microsoft/sqlserver/jdbc/SQLServerResultSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.makeFromDriverError(stmt.connection, stmt, form.format(msgArgs), null, 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;
Expand Down Expand Up @@ -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.makeFromDriverError(stmt.connection, stmt, form.format(msgArgs), null, 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);
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -48,13 +53,31 @@
public class ResultSetTest extends AbstractTest {
private static final String tableName = RandomUtil.getIdentifier("StatementParam");

private static final String expectedSqlState = "S0001";

private static final int expectedErrorCode = 8134;

static final String uuid = UUID.randomUUID().toString();

@BeforeAll
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
*
Expand Down Expand Up @@ -595,4 +618,38 @@ public void testMultipleResultSets() throws SQLException {
}
}
}

@Test
public void testResultSetFetchBufferSqlErrorState() 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(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());
}
}
}

0 comments on commit 6b571af

Please sign in to comment.