Skip to content

Commit 8bd8b2f

Browse files
committed
implemented type info
1 parent e61519f commit 8bd8b2f

File tree

2 files changed

+58
-6
lines changed

2 files changed

+58
-6
lines changed

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

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.clickhouse.jdbc.metadata;
22

3+
import com.clickhouse.data.ClickHouseDataType;
34
import com.clickhouse.jdbc.ConnectionImpl;
45
import com.clickhouse.jdbc.Driver;
56
import com.clickhouse.jdbc.JdbcV2Wrapper;
@@ -867,7 +868,49 @@ public ResultSet getCrossReference(String parentCatalog, String parentSchema, St
867868

868869
@Override
869870
public ResultSet getTypeInfo() throws SQLException {
870-
return null;
871+
return connection.createStatement().executeQuery(DATA_TYPE_INFO_SQL);
872+
}
873+
874+
private static final String DATA_TYPE_INFO_SQL = getDataTypeInfoSql();
875+
876+
private static String getDataTypeInfoSql() {
877+
StringBuilder sql = new StringBuilder("SELECT " +
878+
"name AS TYPE_NAME, " +
879+
JdbcUtils.generateSqlTypeEnum("name") +" AS DATA_TYPE, " +
880+
"attrs.c2 AS PRECISION, " +
881+
"NULL AS LITERAL_PREFIX, " +
882+
"NULL AS LITERAL_SUFFIX, " +
883+
"NULL AS CREATE_PARAMS, " +
884+
java.sql.DatabaseMetaData.typeNullable + " AS NULLABLE, " +
885+
"not(dt.case_insensitive) AS CASE_SENSITIVE, " +
886+
java.sql.DatabaseMetaData.typeSearchable + " AS SEARCHABLE, " +
887+
"attrs.c3 AS UNSIGNED_ATTRIBUTE, " +
888+
"false AS FIXED_PREC_SCALE, " +
889+
"false AS AUTO_INCREMENT, " +
890+
"name AS LOCAL_TYPE_NAME, " +
891+
"attrs.c4 AS MINIMUM_SCALE, " +
892+
"attrs.c5 AS MAXIMUM_SCALE, " +
893+
"0 AS SQL_DATA_TYPE, " +
894+
"0 AS SQL_DATETIME_SUB, " +
895+
"0 AS NUM_PREC_RADIX " +
896+
"FROM system.data_type_families dt " +
897+
" LEFT JOIN (SELECT * FROM VALUES ( ");
898+
for (ClickHouseDataType type : ClickHouseDataType.values()) {
899+
sql.append("(")
900+
.append('\'').append(type.name()).append("',") // c1
901+
.append(type.getMaxPrecision()).append(',') // c2
902+
.append(type.isSigned()).append(',') // c3
903+
.append(type.getMinScale()).append(',') // c4
904+
.append(type.getMaxScale()) // c5
905+
.append("),");
906+
}
907+
sql.setLength(sql.length() - 1);
908+
sql.append(") ");
909+
910+
sql.append(") as attrs ON (dt.name = attrs.c1)")
911+
.append(" WHERE alias_to == ''");
912+
System.out.println(sql.toString());
913+
return sql.toString();
871914
}
872915

873916
@Override

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,22 @@ public void testGetServerVersions() throws Exception {
185185
}
186186
}
187187

188-
@Test(groups = { "integration" }, enabled = false)
188+
@Test(groups = { "integration" })
189189
public void testGetTypeInfo() throws Exception {
190-
Assert.fail("Not implemented");
191-
}
192-
static {
193-
System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "DEBUG");
190+
try (Connection conn = getJdbcConnection()) {
191+
DatabaseMetaData dbmd = conn.getMetaData();
192+
try (ResultSet rs = dbmd.getTypeInfo()) {
193+
int count = 0;
194+
while (rs.next()) {
195+
count++;
196+
Assert.assertTrue(rs.getString("TYPE_NAME").length() > 0);
197+
}
198+
199+
assertTrue(count > 10, "At least 10 types should be returned but was " + count);
200+
}
201+
}
194202
}
203+
195204
@Test(groups = { "integration" })
196205
public void testGetFunctions() throws Exception {
197206
try (Connection conn = getJdbcConnection()) {

0 commit comments

Comments
 (0)