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

SQLiteDialect#isUniqueConstraintViolated is incorrect #1210

Open
mintdaniel42 opened this issue Oct 20, 2024 · 1 comment
Open

SQLiteDialect#isUniqueConstraintViolated is incorrect #1210

mintdaniel42 opened this issue Oct 20, 2024 · 1 comment
Assignees

Comments

@mintdaniel42
Copy link

Describe the bug
SQLiteDialect#isUniqueConstraintViolated does not recognize SQLite primary key unique constraint violations

Expected behavior
UniqueConstraintException is thrown

Actual behavior
SQLExecutionException is thrown

To Reproduce
Steps to reproduce the behavior:

  1. Use SQLite
  2. Insert an entry with unique key twice
  3. Exception not catched

Fix
The current way to check is by comparing the message of the exception like below

public boolean isUniqueConstraintViolated(SQLException sqlException) {
    if (sqlException == null) {
      throw new DomaNullPointerException("sqlException");
    }
    SQLException cause = getCauseSQLException(sqlException);
    String message = cause.getMessage();
    return message != null
        && message.startsWith("[SQLITE_CONSTRAINT]")
        && message.contains(" unique)");
}

But this will never work because the exception looks like this

[SQLITE_CONSTRAINT_PRIMARYKEY] A PRIMARY KEY constraint failed (UNIQUE constraint failed: <property>)

I suggest to change the body of the method to something like this

@Override
public boolean isUniqueConstraintViolated(@NonNull final SQLException sqlException) {
	final var cause = getCauseSQLException(sqlException);
	final var message = cause.getMessage();
	return message != null
			&& message.startsWith("[SQLITE_CONSTRAINT_PRIMARYKEY] A PRIMARY KEY constraint failed (UNIQUE constraint failed:");
}
@nakamura-to
Copy link
Member

Thank you for your report.
I will look into it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants