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

Fix | Fix SQL Exception Error State length to respect SQLSTATE Standards #977

Merged
merged 2 commits into from
Mar 11, 2019
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
19 changes: 15 additions & 4 deletions src/main/java/com/microsoft/sqlserver/jdbc/SQLServerException.java
Original file line number Diff line number Diff line change
Expand Up @@ -323,13 +323,13 @@ static String mapFromXopen(String state) {
* the database state
* @return the state code
*/
static String generateStateCode(SQLServerConnection con, int errNum, int databaseState) {
static String generateStateCode(SQLServerConnection con, int errNum, Integer databaseState) {
// Generate a SQL 99 or XOPEN state from a database generated error code
boolean xopenStates = (con != null && con.xopenStates);
if (xopenStates) {
switch (errNum) {
case 4060:
return "08001"; // Database name undefined at loging
return "08001"; // Database name undefined at logging
case 18456:
return "08001"; // username password wrong at login
case 2714:
Expand Down Expand Up @@ -360,8 +360,19 @@ static String generateStateCode(SQLServerConnection con, int errNum, int databas
return "40001"; // deadlock detected
case 2627:
return "23000"; // DPM 4.04. Primary key violation
default:
return "S000" + databaseState;
default: {
String dbState = databaseState.toString();
/*
* Length allowed for SQL State is 5 characters as per SQLSTATE specifications. Append trailing
* zeroes as needed based on length of database error State as length of databaseState is between 1
* to 3 digits.
*/
StringBuilder trailingZeroes = new StringBuilder("S");
for (int i = 0; i < 4 - dbState.length(); i++) {
trailingZeroes.append("0");
}
return trailingZeroes.append(dbState).toString();
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.microsoft.sqlserver.jdbc.exception;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;

import com.microsoft.sqlserver.testframework.AbstractTest;
import com.microsoft.sqlserver.testframework.PrepUtil;


@RunWith(JUnitPlatform.class)
public class ErrorStateTest extends AbstractTest {

@Test
public void testSQLStateNegative() throws Exception {
int state = -1; // Negative error raised converts to positive SQL State (1)
try (Connection con = PrepUtil.getConnection(connectionString); Statement stmt = con.createStatement()) {
stmt.execute("RAISERROR (13002, -1, " + state + ", N'Testing error');");
} catch (SQLException e) {
assert (e.getSQLState().length() == 5);
assert (e.getSQLState().equalsIgnoreCase("S0001"));
}
}

@Test
public void testSQLStateLength1() throws Exception {
try (Connection con = PrepUtil.getConnection(connectionString); Statement stmt = con.createStatement()) {
stmt.execute("SELECT 1/0;");
} catch (SQLException e) {
assert (e.getSQLState().length() == 5);
assert (e.getSQLState().equalsIgnoreCase("S0001"));
}
}

@Test
public void testSQLStateLength2() throws Exception {
int state = 31;
try (Connection con = PrepUtil.getConnection(connectionString); Statement stmt = con.createStatement()) {
stmt.execute("RAISERROR (13002, -1, " + state + ", N'Testing error');");
} catch (SQLException e) {
assert (e.getSQLState().length() == 5);
assert (e.getSQLState().equalsIgnoreCase("S00" + state));
}
}

@Test
public void testSQLStateLength3() throws Exception {
int state = 255; // Max Value of SQL State
try (Connection con = PrepUtil.getConnection(connectionString); Statement stmt = con.createStatement()) {
stmt.execute("RAISERROR (13003, -1, " + state + ", N'Testing error');");
} catch (SQLException e) {
assert (e.getSQLState().length() == 5);
assert (e.getSQLState().equalsIgnoreCase("S0" + state));
}
}
}