Skip to content

Commit b022645

Browse files
authored
Merge pull request #2005 from ClickHouse/support-get-primary-keys
Update DatabaseMetaData.java to handle getPrimaryKeys
2 parents 7be8d90 + 05a6c31 commit b022645

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

jdbc-v2/src/main/java/com/clickhouse/jdbc/metadata/DatabaseMetaData.java

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -909,10 +909,21 @@ public ResultSet getVersionColumns(String catalog, String schema, String table)
909909

910910
@Override
911911
public ResultSet getPrimaryKeys(String catalog, String schema, String table) throws SQLException {
912-
//Return an empty result set with the required columns
913-
log.warn("getPrimaryKeys is not supported and may return invalid results");
914912
try {
915-
return connection.createStatement().executeQuery("SELECT NULL AS TABLE_CAT, NULL AS TABLE_SCHEM, NULL AS TABLE_NAME, NULL AS COLUMN_NAME, NULL AS KEY_SEQ, NULL AS PK_NAME");
913+
String sql = "SELECT NULL AS TABLE_CAT, " +
914+
"system.tables.database AS TABLE_SCHEM, " +
915+
"system.tables.name AS TABLE_NAME, " +
916+
"trim(c.1) AS COLUMN_NAME, " +
917+
"c.2 AS KEY_SEQ, " +
918+
"'PRIMARY' AS PK_NAME " +
919+
"FROM system.tables " +
920+
"ARRAY JOIN arrayZip(splitByChar(',', primary_key), arrayEnumerate(splitByChar(',', primary_key))) as c " +
921+
"WHERE system.tables.primary_key <> '' " +
922+
"AND system.tables.database ILIKE '" + (schema == null ? "%" : schema) + "' " +
923+
"AND system.tables.name ILIKE '" + (table == null ? "%" : table) + "' " +
924+
"ORDER BY COLUMN_NAME";
925+
log.debug("getPrimaryKeys: %s", sql);
926+
return connection.createStatement().executeQuery(sql);
916927
} catch (Exception e) {
917928
throw ExceptionUtils.toSqlState(e);
918929
}
@@ -923,7 +934,21 @@ public ResultSet getImportedKeys(String catalog, String schema, String table) th
923934
//Return an empty result set with the required columns
924935
log.warn("getImportedKeys is not supported and may return invalid results");
925936
try {
926-
return connection.createStatement().executeQuery("SELECT NULL AS PKTABLE_CAT, NULL AS PKTABLE_SCHEM, NULL AS PKTABLE_NAME, NULL AS PKCOLUMN_NAME, NULL AS FKTABLE_CAT, NULL AS FKTABLE_SCHEM, NULL AS FKTABLE_NAME, NULL AS FKCOLUMN_NAME, NULL AS KEY_SEQ, NULL AS UPDATE_RULE, NULL AS DELETE_RULE, NULL AS FK_NAME, NULL AS PK_NAME, NULL AS DEFERRABILITY");
937+
String sql = "SELECT NULL AS PKTABLE_CAT, " +
938+
"NULL AS PKTABLE_SCHEM, " +
939+
"NULL AS PKTABLE_NAME, " +
940+
"NULL AS PKCOLUMN_NAME, " +
941+
"NULL AS FKTABLE_CAT, " +
942+
"NULL AS FKTABLE_SCHEM, " +
943+
"NULL AS FKTABLE_NAME, " +
944+
"NULL AS FKCOLUMN_NAME, " +
945+
"NULL AS KEY_SEQ, " +
946+
"NULL AS UPDATE_RULE, " +
947+
"NULL AS DELETE_RULE, " +
948+
"NULL AS FK_NAME, " +
949+
"NULL AS PK_NAME, " +
950+
"NULL AS DEFERRABILITY LIMIT 0";
951+
return connection.createStatement().executeQuery(sql);
927952
} catch (Exception e) {
928953
throw ExceptionUtils.toSqlState(e);
929954
}

jdbc-v2/src/test/java/com/clickhouse/jdbc/metadata/DatabaseMetaDataTest.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,23 @@ public void testGetTables() throws Exception {
9898
}
9999
}
100100

101-
@Ignore("ClickHouse does not support primary keys")
101+
102102
@Test(groups = { "integration" })
103103
public void testGetPrimaryKeys() throws Exception {
104+
runQuery("SELECT 1;");
105+
Thread.sleep(10 * 1000); // wait for query log to be updated
106+
104107
try (Connection conn = getJdbcConnection()) {
105108
DatabaseMetaData dbmd = conn.getMetaData();
106-
ResultSet rs = dbmd.getPrimaryKeys("system", null, "numbers");
109+
ResultSet rs = dbmd.getPrimaryKeys(null, "system", "query_log");
107110
assertTrue(rs.next());
108-
assertEquals(rs.getString("TABLE_NAME"), "numbers");
109-
assertEquals(rs.getString("COLUMN_NAME"), "number");
111+
assertEquals(rs.getString("TABLE_NAME"), "query_log");
112+
assertEquals(rs.getString("COLUMN_NAME"), "event_date");
110113
assertEquals(rs.getShort("KEY_SEQ"), 1);
114+
assertTrue(rs.next());
115+
assertEquals(rs.getString("TABLE_NAME"), "query_log");
116+
assertEquals(rs.getString("COLUMN_NAME"), "event_time");
117+
assertEquals(rs.getShort("KEY_SEQ"), 2);
111118
assertFalse(rs.next());
112119
}
113120
}

0 commit comments

Comments
 (0)