Skip to content

Commit 510373e

Browse files
author
Paultagoras
committed
Update DatabaseMetaData.java
1 parent 5e304cb commit 510373e

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

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

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -909,10 +909,19 @@ 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 LIKE '" + (schema == null ? "%" : schema) + "' " +
923+
"AND system.tables.name LIKE '" + (table == null ? "%" : table) + "'";
924+
return connection.createStatement().executeQuery(sql);
916925
} catch (Exception e) {
917926
throw ExceptionUtils.toSqlState(e);
918927
}
@@ -923,7 +932,21 @@ public ResultSet getImportedKeys(String catalog, String schema, String table) th
923932
//Return an empty result set with the required columns
924933
log.warn("getImportedKeys is not supported and may return invalid results");
925934
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");
935+
String sql = "SELECT NULL AS PKTABLE_CAT, " +
936+
"NULL AS PKTABLE_SCHEM, " +
937+
"NULL AS PKTABLE_NAME, " +
938+
"NULL AS PKCOLUMN_NAME, " +
939+
"NULL AS FKTABLE_CAT, " +
940+
"NULL AS FKTABLE_SCHEM, " +
941+
"NULL AS FKTABLE_NAME, " +
942+
"NULL AS FKCOLUMN_NAME, " +
943+
"NULL AS KEY_SEQ, " +
944+
"NULL AS UPDATE_RULE, " +
945+
"NULL AS DELETE_RULE, " +
946+
"NULL AS FK_NAME, " +
947+
"NULL AS PK_NAME, " +
948+
"NULL AS DEFERRABILITY LIMIT 0";
949+
return connection.createStatement().executeQuery(sql);
927950
} catch (Exception e) {
928951
throw ExceptionUtils.toSqlState(e);
929952
}

0 commit comments

Comments
 (0)