Skip to content

Commit

Permalink
fix(jdbc): DatabaseMetaData#getPrimaryKeys and getExportedKeys should…
Browse files Browse the repository at this point in the history
… return an empty ResultSet for sqlite_schema

those 2 functions rely on querying sqlite_schema for information, which doesn't contain information about itself.
sqlite_schema doesn't have any imported/exported/primary key, so returning an empty ResultSet is better than throwing an exception

Closes: #831
  • Loading branch information
gotson committed Feb 2, 2023
1 parent e437b3f commit 0dc6ad9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/main/java/org/sqlite/jdbc3/JDBC3DatabaseMetaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -1979,6 +1979,11 @@ class PrimaryKeyFinder {
public PrimaryKeyFinder(String table) throws SQLException {
this.table = table;

// specific handling for sqlite_schema and synonyms, so that
// getExportedKeys/getPrimaryKeys return an empty ResultSet instead of throwing an
// exception
if ("sqlite_schema".equals(table) || "sqlite_master".equals(table)) return;

if (table == null || table.trim().length() == 0) {
throw new SQLException("Invalid table name: '" + this.table + "'");
}
Expand Down
30 changes: 30 additions & 0 deletions src/test/java/org/sqlite/DBMetaDataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
import java.util.Properties;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledInNativeImage;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

/** These tests are designed to stress Statements on memory databases. */
public class DBMetaDataTest {
Expand Down Expand Up @@ -1596,4 +1599,31 @@ public void version() throws Exception {
assertThat(meta.getDatabaseMinorVersion()).as("db minor version").isEqualTo(minorVersion);
assertThat(meta.getUserName()).as("user name").isNull();
}

@Nested
class SqliteSchema {
@ParameterizedTest
@ValueSource(strings = {"sqlite_schema", "sqlite_master"})
public void getImportedKeys(String table) throws SQLException {
ResultSet importedKeys = meta.getImportedKeys(null, null, table);

assertThat(importedKeys.next()).isFalse();
}

@ParameterizedTest
@ValueSource(strings = {"sqlite_schema", "sqlite_master"})
public void getExportedKeys(String table) throws SQLException {
ResultSet exportedKeys = meta.getExportedKeys(null, null, table);

assertThat(exportedKeys.next()).isFalse();
}

@ParameterizedTest
@ValueSource(strings = {"sqlite_schema", "sqlite_master"})
public void getPrimaryKeys(String table) throws SQLException {
ResultSet primaryKeys = meta.getPrimaryKeys(null, null, table);

assertThat(primaryKeys.next()).isFalse();
}
}
}

0 comments on commit 0dc6ad9

Please sign in to comment.