Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed null sql state and 0 error code for sql exceptions #2018

Merged
merged 3 commits into from
Dec 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
}
}