Skip to content

Commit

Permalink
Fixed null sql state and error code 0 (#2022)
Browse files Browse the repository at this point in the history
  • Loading branch information
tkyc authored Dec 29, 2022
1 parent 81477ee commit d6792f3
Show file tree
Hide file tree
Showing 2 changed files with 68 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 @@ -393,7 +393,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 @@ -5393,7 +5398,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);
}
}

// Done with all the rows in this fetch buffer and done with parsing
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,37 @@ 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)");
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 d6792f3

Please sign in to comment.