Skip to content

Commit 423e541

Browse files
committed
Merge branch 'main' into v2_process_jdbc_properties
2 parents 9aa0898 + d5d1255 commit 423e541

File tree

11 files changed

+176
-74
lines changed

11 files changed

+176
-74
lines changed

client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/AbstractBinaryFormatReader.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,7 @@
3232
import java.time.ZoneOffset;
3333
import java.time.ZonedDateTime;
3434
import java.time.temporal.ChronoUnit;
35-
import java.util.Collections;
36-
import java.util.HashMap;
37-
import java.util.List;
38-
import java.util.Map;
39-
import java.util.TimeZone;
40-
import java.util.UUID;
35+
import java.util.*;
4136
import java.util.concurrent.ConcurrentHashMap;
4237
import java.util.concurrent.atomic.AtomicBoolean;
4338
import java.util.function.Function;

client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/BinaryReaderBackedRecord.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
11
package com.clickhouse.client.api.data_formats.internal;
22

33
import com.clickhouse.client.api.data_formats.ClickHouseBinaryFormatReader;
4+
import com.clickhouse.client.api.metadata.TableSchema;
45
import com.clickhouse.client.api.query.GenericRecord;
5-
import com.clickhouse.data.value.ClickHouseBitmap;
6-
import com.clickhouse.data.value.ClickHouseGeoMultiPolygonValue;
7-
import com.clickhouse.data.value.ClickHouseGeoPointValue;
8-
import com.clickhouse.data.value.ClickHouseGeoPolygonValue;
9-
import com.clickhouse.data.value.ClickHouseGeoRingValue;
6+
import com.clickhouse.data.ClickHouseColumn;
7+
import com.clickhouse.data.value.*;
108

119
import java.math.BigDecimal;
1210
import java.math.BigInteger;
1311
import java.net.Inet4Address;
1412
import java.net.Inet6Address;
15-
import java.time.Duration;
16-
import java.time.Instant;
17-
import java.time.LocalDate;
18-
import java.time.LocalDateTime;
19-
import java.time.ZonedDateTime;
13+
import java.time.*;
2014
import java.util.List;
15+
import java.util.Map;
2116
import java.util.UUID;
17+
import java.util.stream.Collectors;
2218

2319
public class BinaryReaderBackedRecord implements GenericRecord {
2420

@@ -377,4 +373,16 @@ public ClickHouseBitmap getClickHouseBitmap(String colName) {
377373
public ClickHouseBitmap getClickHouseBitmap(int index) {
378374
return reader.readValue(index);
379375
}
376+
377+
@Override
378+
public TableSchema getSchema() {
379+
return reader.getSchema();
380+
}
381+
382+
@Override
383+
public Map<String, Object> getValues() {
384+
return this.getSchema().getColumns().stream().collect(Collectors.toMap(
385+
ClickHouseColumn::getColumnName,
386+
column -> this.getObject(column.getColumnName())));
387+
}
380388
}

client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/MapBackedRecord.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,13 @@
55
import com.clickhouse.client.api.query.GenericRecord;
66
import com.clickhouse.client.api.query.NullValueException;
77
import com.clickhouse.data.ClickHouseColumn;
8-
import com.clickhouse.data.value.ClickHouseArrayValue;
9-
import com.clickhouse.data.value.ClickHouseBitmap;
10-
import com.clickhouse.data.value.ClickHouseGeoMultiPolygonValue;
11-
import com.clickhouse.data.value.ClickHouseGeoPointValue;
12-
import com.clickhouse.data.value.ClickHouseGeoPolygonValue;
13-
import com.clickhouse.data.value.ClickHouseGeoRingValue;
8+
import com.clickhouse.data.value.*;
149

1510
import java.math.BigDecimal;
1611
import java.math.BigInteger;
1712
import java.net.Inet4Address;
1813
import java.net.Inet6Address;
19-
import java.time.Duration;
20-
import java.time.Instant;
21-
import java.time.LocalDate;
22-
import java.time.LocalDateTime;
23-
import java.time.ZoneOffset;
24-
import java.time.ZonedDateTime;
14+
import java.time.*;
2515
import java.time.temporal.ChronoUnit;
2616
import java.util.HashMap;
2717
import java.util.List;
@@ -33,7 +23,7 @@ public class MapBackedRecord implements GenericRecord {
3323

3424
private final Map<String, Object> record;
3525

36-
private TableSchema schema;
26+
private final TableSchema schema;
3727

3828
private Map[] columnConverters;
3929

@@ -513,6 +503,11 @@ public ClickHouseBitmap getClickHouseBitmap(int index) {
513503
return readValue(index);
514504
}
515505

506+
@Override
507+
public TableSchema getSchema() {
508+
return this.schema;
509+
}
510+
516511
@Override
517512
public Object getObject(String colName) {
518513
return readValue(colName);
@@ -522,4 +517,9 @@ public Object getObject(String colName) {
522517
public Object getObject(int index) {
523518
return readValue(index);
524519
}
520+
521+
@Override
522+
public Map<String, Object> getValues() {
523+
return this.record;
524+
}
525525
}

client-v2/src/main/java/com/clickhouse/client/api/query/GenericRecord.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
11
package com.clickhouse.client.api.query;
22

3-
import com.clickhouse.data.value.ClickHouseBitmap;
4-
import com.clickhouse.data.value.ClickHouseGeoMultiPolygonValue;
5-
import com.clickhouse.data.value.ClickHouseGeoPointValue;
6-
import com.clickhouse.data.value.ClickHouseGeoPolygonValue;
7-
import com.clickhouse.data.value.ClickHouseGeoRingValue;
3+
import com.clickhouse.client.api.metadata.TableSchema;
4+
import com.clickhouse.data.value.*;
85

96
import java.math.BigDecimal;
107
import java.math.BigInteger;
118
import java.net.Inet4Address;
129
import java.net.Inet6Address;
13-
import java.time.Duration;
14-
import java.time.Instant;
15-
import java.time.LocalDate;
16-
import java.time.LocalDateTime;
17-
import java.time.ZonedDateTime;
10+
import java.time.*;
1811
import java.util.List;
12+
import java.util.Map;
1913
import java.util.UUID;
2014

2115
public interface GenericRecord {
@@ -500,4 +494,13 @@ public interface GenericRecord {
500494
ClickHouseBitmap getClickHouseBitmap(String colName);
501495

502496
ClickHouseBitmap getClickHouseBitmap(int index);
497+
498+
TableSchema getSchema();
499+
500+
/**
501+
* Returns all values of this record in Map.
502+
*
503+
* @return a Map of column names and values.
504+
*/
505+
Map<String, Object> getValues();
503506
}

jdbc-v2/src/main/java/com/clickhouse/jdbc/ConnectionImpl.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import com.clickhouse.client.api.query.GenericRecord;
66
import com.clickhouse.client.api.query.QuerySettings;
77
import com.clickhouse.jdbc.internal.ClientInfoProperties;
8+
import com.clickhouse.jdbc.internal.DriverProperties;
9+
import com.clickhouse.jdbc.internal.JdbcConfiguration;
810
import com.clickhouse.jdbc.internal.ExceptionUtils;
911
import com.clickhouse.jdbc.internal.JdbcConfiguration;
1012
import org.slf4j.Logger;
@@ -91,6 +93,15 @@ public String getServerVersion() throws SQLException {
9193
return result.getString("server_version");
9294
}
9395

96+
/**
97+
* Returns configuration for current connection. Changes made to the instance of configuration may have side effects.
98+
* Application should avoid making changes to this object until it is documented.
99+
* @return - reference to internal instance of JdbcConfiguration
100+
*/
101+
public JdbcConfiguration getJdbcConfig() {
102+
return this.config;
103+
}
104+
94105
@Override
95106
public Statement createStatement() throws SQLException {
96107
checkOpen();
@@ -119,7 +130,8 @@ public String nativeSQL(String sql) throws SQLException {
119130
@Override
120131
public void setAutoCommit(boolean autoCommit) throws SQLException {
121132
checkOpen();
122-
if (!autoCommit) {
133+
134+
if (!config.isIgnoreUnsupportedRequests() && !autoCommit) {
123135
throw new SQLFeatureNotSupportedException("setAutoCommit = false not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
124136
}
125137
}
@@ -132,12 +144,16 @@ public boolean getAutoCommit() throws SQLException {
132144

133145
@Override
134146
public void commit() throws SQLException {
135-
throw new SQLFeatureNotSupportedException("Commit/Rollback not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
147+
if (!config.isIgnoreUnsupportedRequests() ) {
148+
throw new SQLFeatureNotSupportedException("Commit/Rollback not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
149+
}
136150
}
137151

138152
@Override
139153
public void rollback() throws SQLException {
140-
throw new SQLFeatureNotSupportedException("Commit/Rollback not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
154+
if (!config.isIgnoreUnsupportedRequests()) {
155+
throw new SQLFeatureNotSupportedException("Commit/Rollback not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
156+
}
141157
}
142158

143159
@Override
@@ -269,7 +285,9 @@ public Savepoint setSavepoint(String name) throws SQLException {
269285
@Override
270286
public void rollback(Savepoint savepoint) throws SQLException {
271287
checkOpen();
272-
throw new SQLFeatureNotSupportedException("Commit/Rollback not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
288+
if (!config.isIgnoreUnsupportedRequests()) {
289+
throw new SQLFeatureNotSupportedException("Commit/Rollback not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
290+
}
273291
}
274292

275293
@Override

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
*/
1212
public enum DriverProperties {
1313

14+
IGNORE_UNSUPPORTED_VALUES("jdbc_ignore_unsupported_values", ""),
15+
SCHEMA_TERM("jdbc_schema_term", ""),
1416
/**
1517
* Indicates if driver should create a secure connection over SSL/TLS
1618
*/

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ public boolean isDisableFrameworkDetection() {
3333
return disableFrameworkDetection;
3434
}
3535

36+
private boolean isIgnoreUnsupportedRequests;
37+
38+
public boolean isIgnoreUnsupportedRequests() {
39+
return isIgnoreUnsupportedRequests;
40+
}
41+
3642
/**
3743
* Parses URL to get property and target host.
3844
* Properties that are passed in the {@code info} parameter will override that are set in the {@code url}.
@@ -51,6 +57,7 @@ public JdbcConfiguration(String url, Properties info) throws SQLException {
5157
// after initializing all properties - set final connection URL
5258
boolean useSSL = Boolean.parseBoolean(info.getProperty("ssl", "false"));
5359
this.connectionUrl = createConnectionURL(tmpConnectionUrl, useSSL);
60+
this.isIgnoreUnsupportedRequests= Boolean.parseBoolean(getDriverProperty(DriverProperties.IGNORE_UNSUPPORTED_VALUES.getKey(), "false"));
5461
}
5562

5663
public static boolean acceptsURL(String url) {
@@ -224,6 +231,10 @@ public List<DriverPropertyInfo> getDriverPropertyInfo() {
224231
return listOfProperties;
225232
}
226233

234+
public String getDriverProperty(String key, String defaultValue) {
235+
return driverProperties.getOrDefault(key, defaultValue);
236+
}
237+
227238
public Client.Builder applyClientProperties(Client.Builder builder) {
228239
builder.addEndpoint(connectionUrl)
229240
.setOptions(clientProperties);

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

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.clickhouse.jdbc.Driver;
66
import com.clickhouse.jdbc.JdbcV2Wrapper;
77
import com.clickhouse.jdbc.internal.ClientInfoProperties;
8+
import com.clickhouse.jdbc.internal.DriverProperties;
89
import com.clickhouse.jdbc.internal.JdbcUtils;
910
import com.clickhouse.jdbc.internal.ExceptionUtils;
1011
import com.clickhouse.logging.Logger;
@@ -374,7 +375,7 @@ public boolean supportsLimitedOuterJoins() throws SQLException {
374375
*/
375376
@Override
376377
public String getSchemaTerm() {
377-
return "database";
378+
return connection.getJdbcConfig().getDriverProperty(DriverProperties.SCHEMA_TERM.getKey(), "schema");
378379
}
379380

380381
@Override
@@ -824,7 +825,7 @@ public ResultSet getColumns(String catalog, String schemaPattern, String tableNa
824825
"table AS TABLE_NAME, " +
825826
"name AS COLUMN_NAME, " +
826827
JdbcUtils.generateSqlTypeEnum("system.columns.type") + " AS DATA_TYPE, " +
827-
"replaceRegexpOne(type, '^Nullable\\(([\\\\w ,\\\\)\\\\(]+)\\)$', '\\\\1') AS TYPE_NAME, " +
828+
"type AS TYPE_NAME, " +
828829
JdbcUtils.generateSqlTypeSizes("system.columns.type") + " AS COLUMN_SIZE, " +
829830
"toInt32(0) AS BUFFER_LENGTH, " +
830831
"IF (numeric_scale == 0, NULL, numeric_scale) as DECIMAL_DIGITS, " +
@@ -903,10 +904,21 @@ public ResultSet getVersionColumns(String catalog, String schema, String table)
903904

904905
@Override
905906
public ResultSet getPrimaryKeys(String catalog, String schema, String table) throws SQLException {
906-
//Return an empty result set with the required columns
907-
log.warn("getPrimaryKeys is not supported and may return invalid results");
908907
try {
909-
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");
908+
String sql = "SELECT NULL AS TABLE_CAT, " +
909+
"system.tables.database AS TABLE_SCHEM, " +
910+
"system.tables.name AS TABLE_NAME, " +
911+
"trim(c.1) AS COLUMN_NAME, " +
912+
"c.2 AS KEY_SEQ, " +
913+
"'PRIMARY' AS PK_NAME " +
914+
"FROM system.tables " +
915+
"ARRAY JOIN arrayZip(splitByChar(',', primary_key), arrayEnumerate(splitByChar(',', primary_key))) as c " +
916+
"WHERE system.tables.primary_key <> '' " +
917+
"AND system.tables.database ILIKE '" + (schema == null ? "%" : schema) + "' " +
918+
"AND system.tables.name ILIKE '" + (table == null ? "%" : table) + "' " +
919+
"ORDER BY COLUMN_NAME";
920+
log.debug("getPrimaryKeys: %s", sql);
921+
return connection.createStatement().executeQuery(sql);
910922
} catch (Exception e) {
911923
throw ExceptionUtils.toSqlState(e);
912924
}
@@ -917,7 +929,21 @@ public ResultSet getImportedKeys(String catalog, String schema, String table) th
917929
//Return an empty result set with the required columns
918930
log.warn("getImportedKeys is not supported and may return invalid results");
919931
try {
920-
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");
932+
String sql = "SELECT NULL AS PKTABLE_CAT, " +
933+
"NULL AS PKTABLE_SCHEM, " +
934+
"NULL AS PKTABLE_NAME, " +
935+
"NULL AS PKCOLUMN_NAME, " +
936+
"NULL AS FKTABLE_CAT, " +
937+
"NULL AS FKTABLE_SCHEM, " +
938+
"NULL AS FKTABLE_NAME, " +
939+
"NULL AS FKCOLUMN_NAME, " +
940+
"NULL AS KEY_SEQ, " +
941+
"NULL AS UPDATE_RULE, " +
942+
"NULL AS DELETE_RULE, " +
943+
"NULL AS FK_NAME, " +
944+
"NULL AS PK_NAME, " +
945+
"NULL AS DEFERRABILITY LIMIT 0";
946+
return connection.createStatement().executeQuery(sql);
921947
} catch (Exception e) {
922948
throw ExceptionUtils.toSqlState(e);
923949
}

0 commit comments

Comments
 (0)