Open
Description
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
Metadata
Metadata
Assignees
Labels
Type
Projects
Status
Backlog