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

To provide machine-readeble SQLIntegrityConstraintViolationException. Required schema name, table name, constraint name. #2533

Open
DmitriyGod opened this issue Nov 7, 2024 · 2 comments
Labels
Backlog The topic in question has been recognized and added to development backlog Enhancement An enhancement to the driver. Lower priority than bugs.

Comments

@DmitriyGod
Copy link

Driver version

latest

SQL Server version

mcr.microsoft.com/mssql/server:2022-latest

Client Operating System

any

JAVA/JVM version

any

Table schema

Some constraint, i.e. unique index

Problem description

MSSQL jdbc driver does not provide machine-readeble SQLIntegrityConstraintViolationException.

Expected behavior

Convinient ConstraintViolationException instance with fields: schema name, table name, constraint name like in PG:

if (
    ex instanceof PSQLException pex &&
    ex.getSQLState().startsWith("23")
) {
    return new ConstraintViolationException(
        null,
        **pex.getServerErrorMessage().getSchema(),
        pex.getServerErrorMessage().getTable(),
        pex.getServerErrorMessage().getConstraint()**
    );
}

Actual behavior

The hard way (and fragile solution):

var mssqlUniqueConstraintCode = "23000";
if (
    ex instanceof SQLServerException sex &&
    sex.getSQLState().equals(mssqlUniqueConstraintCode)
) {
    var message = sex.getMessage();
    var databaseName = message.replaceAll(".*database \"(\\S+)\".*", "$1");
    var tableAndSchema = message.replaceAll(".*table \"(\\S+)\".*", "$1").split("\\.");
    var constraintName = message.replaceAll(".*constraint \"(\\S+)\".*", "$1");

    return new ConstraintViolationException(
        databaseName, tableAndSchema[0], tableAndSchema[1], constraintName
    );
}

Error message/stack trace

No

Any other details that can be helpful

Obviously in PG not a perfect solution, but at least this way:
https://jdbc.postgresql.org/documentation/publicapi/org/postgresql/util/PSQLException.html#getServerErrorMessage--

JDBC trace logs

No

@machavan
Copy link
Contributor

machavan commented Nov 8, 2024

Current behavior:

try {

...
} catch (SQLException e) {
			System.out.println("SQL State : " + e.getSQLState());
			System.out.println("SQL Error Code : " + e.getErrorCode());
			System.out.println("Error Message : " + e.getMessage());
			e.printStackTrace();
}

SQL Server :

Exception type : com.microsoft.sqlserver.jdbc.SQLServerException

SQL State : 23000
SQL Error Code : 2627
Error Message : Violation of PRIMARY KEY constraint 'PK__tcons__3BD0198ED76C99CC'. Cannot insert duplicate key in object 'dbo.tcons'. The duplicate key value is (1).

PostgreSQL :

Exception type : org.postgresql.util.PSQLException

SQL State : 23505
SQL Error Code : 0
Error Message : ERROR: duplicate key value violates unique constraint "tcons_pkey"
Detail: Key (a)=(1) already exists.
org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "tcons_pkey"

(Note : PG does not have a specialized exception type to represent constraint violation exception)

The ask here is an enhancement to create a new exception type in mssql-jdbc e.g. SQLIntegrityConstraintViloationException with various fields such as database, schema, table, constraint name set in it. Is this a correct interpretation?

@DmitriyGod
Copy link
Author

Yeah

@github-project-automation github-project-automation bot moved this to To be triaged in MSSQL JDBC Nov 12, 2024
@Jeffery-Wasty Jeffery-Wasty added the Enhancement An enhancement to the driver. Lower priority than bugs. label Nov 12, 2024
@Jeffery-Wasty Jeffery-Wasty moved this from To be triaged to Backlog in MSSQL JDBC Nov 12, 2024
@Jeffery-Wasty Jeffery-Wasty added the Backlog The topic in question has been recognized and added to development backlog label Nov 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backlog The topic in question has been recognized and added to development backlog Enhancement An enhancement to the driver. Lower priority than bugs.
Projects
Status: Backlog
Development

No branches or pull requests

3 participants