From d6b2183c62a943ab5b074e062f3e7d427acdbcb3 Mon Sep 17 00:00:00 2001 From: Michael Simons Date: Thu, 9 Jan 2025 09:42:03 +0100 Subject: [PATCH] feat: Show all catalogs. [metadata] Closes #461. Signed-off-by: Michael Simons --- .../org/neo4j/jdbc/it/cp/DatabaseMetadataIT.java | 12 ++++++++++++ .../java/org/neo4j/jdbc/DatabaseMetadataImpl.java | 12 ++---------- .../resources/queries/DatabaseMetadata.properties | 2 ++ .../org/neo4j/jdbc/DatabaseMetadataImplTests.java | 14 ++++++++++++++ 4 files changed, 30 insertions(+), 10 deletions(-) diff --git a/neo4j-jdbc-it/neo4j-jdbc-it-cp/src/test/java/org/neo4j/jdbc/it/cp/DatabaseMetadataIT.java b/neo4j-jdbc-it/neo4j-jdbc-it-cp/src/test/java/org/neo4j/jdbc/it/cp/DatabaseMetadataIT.java index 4afdd5c4..6f0210d6 100644 --- a/neo4j-jdbc-it/neo4j-jdbc-it-cp/src/test/java/org/neo4j/jdbc/it/cp/DatabaseMetadataIT.java +++ b/neo4j-jdbc-it/neo4j-jdbc-it-cp/src/test/java/org/neo4j/jdbc/it/cp/DatabaseMetadataIT.java @@ -1211,6 +1211,18 @@ void primaryKeysWithMoreThanOneColumn() throws SQLException, IOException { assertThat(primaryKeys.next()).isFalse(); } + @Test + void getCatalogsShouldWork() throws SQLException { + + var catalogs = new ArrayList(); + try (var rs = this.connection.getMetaData().getCatalogs()) { + while (rs.next()) { + catalogs.add(rs.getString("TABLE_CAT")); + } + } + assertThat(catalogs).containsExactly("neo4j", "system"); + } + record IndexInfo(String tableName, boolean nonUnique, String indexName, int type, int ordinalPosition, String columnName, String ascOrDesc) { IndexInfo(ResultSet resultset) throws SQLException { diff --git a/neo4j-jdbc/src/main/java/org/neo4j/jdbc/DatabaseMetadataImpl.java b/neo4j-jdbc/src/main/java/org/neo4j/jdbc/DatabaseMetadataImpl.java index 0b84a8d2..2a44e70d 100644 --- a/neo4j-jdbc/src/main/java/org/neo4j/jdbc/DatabaseMetadataImpl.java +++ b/neo4j-jdbc/src/main/java/org/neo4j/jdbc/DatabaseMetadataImpl.java @@ -492,7 +492,7 @@ public String getProcedureTerm() { @Override public String getCatalogTerm() { - return "catalog"; + return "database"; } @Override @@ -830,18 +830,10 @@ public ResultSet getTables(String catalog, String schemaPattern, String tableNam return doQueryForResultSet(request); } - /*** - * Returns an empty Result set as there cannot be Catalogs in neo4j. - * @return all catalogs - */ @Override public ResultSet getCatalogs() throws SQLException { - var keys = new ArrayList(); - keys.add("TABLE_CAT"); - var response = createRunResponseForStaticKeys(keys); - var pull = staticPullResponseFor(keys, List.of(new Value[] { Values.value(getSingleCatalog()) })); - return new ResultSetImpl(new LocalStatementImpl(), new ThrowingTransactionImpl(), response, pull, -1, -1, -1); + return doQueryForResultSet(getRequest("getCatalogs")); } @Override diff --git a/neo4j-jdbc/src/main/resources/queries/DatabaseMetadata.properties b/neo4j-jdbc/src/main/resources/queries/DatabaseMetadata.properties index 7f290729..62a3568f 100644 --- a/neo4j-jdbc/src/main/resources/queries/DatabaseMetadata.properties +++ b/neo4j-jdbc/src/main/resources/queries/DatabaseMetadata.properties @@ -215,3 +215,5 @@ RETURN toInteger(value) getPrimaryKeys=SHOW CONSTRAINTS YIELD name, labelsOrTypes, properties \ WHERE ANY (v IN labelsOrTypes WHERE v = $name) \ RETURN * + +getCatalogs=SHOW DATABASES YIELD name AS TABLE_CAT ORDER BY TABLE_CAT diff --git a/neo4j-jdbc/src/test/java/org/neo4j/jdbc/DatabaseMetadataImplTests.java b/neo4j-jdbc/src/test/java/org/neo4j/jdbc/DatabaseMetadataImplTests.java index 5ac9e7e2..a593276b 100644 --- a/neo4j-jdbc/src/test/java/org/neo4j/jdbc/DatabaseMetadataImplTests.java +++ b/neo4j-jdbc/src/test/java/org/neo4j/jdbc/DatabaseMetadataImplTests.java @@ -180,6 +180,20 @@ void getAllTablesShouldErrorIfYouPassCatalog() throws SQLException { "Catalog 'NotNull' is not available in this Neo4j instance, please leave blank or specify the current database name"); } + @Test + void getCatalogTermShouldWork() throws SQLException { + + var connection = newConnection(); + assertThat(connection.getMetaData().getCatalogTerm()).isEqualTo("database"); + } + + @Test + void getCatalogSeparatorShouldWork() throws SQLException { + + var connection = newConnection(); + assertThat(connection.getMetaData().getCatalogSeparator()).isEqualTo("."); + } + private Connection newConnection() throws SQLException { var url = "jdbc:neo4j://host";