Skip to content

Commit 80e99a1

Browse files
committed
fixed getTables, getColumns, turned on tests in maven (some fail, but it is ok)
1 parent 1a65a14 commit 80e99a1

File tree

12 files changed

+253
-206
lines changed

12 files changed

+253
-206
lines changed

client-v2/src/main/java/com/clickhouse/client/api/ClientConfigProperties.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,9 @@ public static String commaSeparated(Collection<?> values) {
139139
for (Object value : values) {
140140
sb.append(value.toString().replaceAll(",", "\\\\,")).append(",");
141141
}
142-
sb.setLength(sb.length() - 1);
142+
if (sb.length() > 0) {
143+
sb.setLength(sb.length() - 1);
144+
}
143145
return sb.toString();
144146
}
145147

jdbc-v2/src/main/java/com/clickhouse/jdbc/internal/JdbcUtils.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
import java.sql.Types;
66
import java.util.HashMap;
77
import java.util.Map;
8+
import java.util.TreeMap;
89

910
public class JdbcUtils {
1011
//Define a map to store the mapping between ClickHouse data types and SQL data types
1112
private static final Map<ClickHouseDataType, Integer> CLICKHOUSE_TO_SQL_TYPE_MAP = generateTypeMap();
1213
private static Map<ClickHouseDataType, Integer> generateTypeMap() {
13-
Map<ClickHouseDataType, Integer> map = new HashMap<>();
14+
Map<ClickHouseDataType, Integer> map = new TreeMap<>(); // TreeMap is used to sort the keys in natural order so FixedString will be before String :-) (type match should be more accurate)
1415
map.put(ClickHouseDataType.Int8, Types.TINYINT);
1516
map.put(ClickHouseDataType.UInt8, Types.TINYINT);
1617
map.put(ClickHouseDataType.Int16, Types.SMALLINT);

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ public ResultSet getTables(String catalog, String schemaPattern, String tableNam
674674
// TODO: when switch between catalog and schema is implemented, then TABLE_SCHEMA and TABLE_CAT should be populated accordingly
675675
// String commentColumn = connection.getServerVersion().check("[21.6,)") ? "t.comment" : "''";
676676
// TODO: handle useCatalogs == true and return schema catalog name
677-
String catalogPlaceholder = useCatalogs ? "'local' " : "";
677+
String catalogPlaceholder = useCatalogs ? "'local' " : "''";
678678

679679
String sql = "SELECT " +
680680
catalogPlaceholder + " AS TABLE_CAT, " +
@@ -712,7 +712,7 @@ public ResultSet getTables(String catalog, String schemaPattern, String tableNam
712712
@Override
713713
public ResultSet getSchemas() throws SQLException {
714714
// TODO: handle useCatalogs == true and return schema catalog name
715-
String catalogPlaceholder = useCatalogs ? "'local' " : "";
715+
String catalogPlaceholder = useCatalogs ? "'local' " : "''";
716716
return connection.createStatement().executeQuery("SELECT name AS TABLE_SCHEM, " + catalogPlaceholder + " AS TABLE_CATALOG FROM system.databases ORDER BY name");
717717
}
718718

@@ -741,28 +741,27 @@ public ResultSet getTableTypes() throws SQLException {
741741
public ResultSet getColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern) throws SQLException {
742742
//TODO: Best way to convert type to JDBC data type
743743
// TODO: handle useCatalogs == true and return schema catalog name
744-
String catalogPlaceholder = useCatalogs ? "'local' " : "";
744+
String catalogPlaceholder = useCatalogs ? "'local' " : "''";
745745

746746
String sql = "SELECT " +
747747
catalogPlaceholder + " AS TABLE_CAT, " +
748748
"database AS TABLE_SCHEM, " +
749749
"table AS TABLE_NAME, " +
750750
"name AS COLUMN_NAME, " +
751751
JdbcUtils.generateSqlTypeEnum("system.columns.type") + " AS DATA_TYPE, " +
752-
"type AS TYPE_NAME, " +
753-
JdbcUtils.generateSqlTypeSizes("system.columns.type") + " AS COLUMN_SIZE, " +
754-
752+
"replaceRegexpOne(type, '^Nullable\\(([\\\\w ,\\\\)\\\\(]+)\\)$', '\\\\1') AS TYPE_NAME, " +
753+
JdbcUtils.generateSqlTypeSizes("system.columns.type") + " AS COLUMN_SIZE, " +
755754
"toInt32(0) AS BUFFER_LENGTH, " +
756-
"if(numeric_scale IS NOT NULL, numeric_scale, 0) as DECIMAL_DIGITS, " +
757-
"toInt32(system.columns.numeric_precision_radix) AS NUM_PREC_RADIX, " +
755+
"IF (numeric_scale == 0, NULL, numeric_scale) as DECIMAL_DIGITS, " +
756+
"toInt32(numeric_precision_radix) AS NUM_PREC_RADIX, " +
758757
"toInt32(position(type, 'Nullable(') >= 1 ?" + java.sql.DatabaseMetaData.typeNullable + " : " + java.sql.DatabaseMetaData.typeNoNulls + ") as NULLABLE, " +
759758
"system.columns.comment AS REMARKS, " +
760759
"system.columns.default_expression AS COLUMN_DEF, " +
761760
"toInt32(0) AS SQL_DATA_TYPE, " +
762761
"toInt32(0) AS SQL_DATETIME_SUB, " +
763762
"character_octet_length AS CHAR_OCTET_LENGTH, " +
764763
"toInt32(system.columns.position) AS ORDINAL_POSITION, " +
765-
"position(type, 'Nullable(') >= 1 ? 'YES' : 'NO' AS IS_NULLABLE," +
764+
"position(upper(type), 'NULLABLE') >= 1 ? 'YES' : 'NO' AS IS_NULLABLE," +
766765
"NULL AS SCOPE_CATALOG, " +
767766
"NULL AS SCOPE_SCHEMA, " +
768767
"NULL AS SCOPE_TABLE, " +
@@ -773,7 +772,8 @@ public ResultSet getColumns(String catalog, String schemaPattern, String tableNa
773772
" WHERE database LIKE '" + (schemaPattern == null ? "%" : schemaPattern) + "'" +
774773
" AND database LIKE '" + (catalog == null ? "%" : catalog) + "'" +
775774
" AND table LIKE '" + (tableNamePattern == null ? "%" : tableNamePattern) + "'" +
776-
" AND name LIKE '" + (columnNamePattern == null ? "%" : columnNamePattern) + "'";
775+
" AND name LIKE '" + (columnNamePattern == null ? "%" : columnNamePattern) + "'" +
776+
" ORDER BY TABLE_SCHEM, TABLE_NAME, ORDINAL_POSITION";
777777
log.info("getColumns: {}", sql);
778778

779779
return connection.createStatement().executeQuery(sql);
@@ -1035,7 +1035,7 @@ public RowIdLifetime getRowIdLifetime() throws SQLException {
10351035
@Override
10361036
public ResultSet getSchemas(String catalog, String schemaPattern) throws SQLException {
10371037
// TODO: handle useCatalogs == true and return schema catalog name
1038-
String catalogPlaceholder = useCatalogs ? "'local' " : "";
1038+
String catalogPlaceholder = useCatalogs ? "'local' " : "''";
10391039
return connection.createStatement().executeQuery("SELECT name AS TABLE_SCHEM, " + catalogPlaceholder + " AS TABLE_CATALOG FROM system.databases " +
10401040
"WHERE name LIKE '" + (schemaPattern == null ? "%" : schemaPattern) + "'");
10411041
}

0 commit comments

Comments
 (0)