Skip to content

Commit d5d1255

Browse files
authored
Merge pull request #2012 from ClickHouse/v2_jdbc_fix_specific_behaviour
[jdbc-v2] Adopt for non-compliant requests
2 parents 16228b2 + ea81d30 commit d5d1255

File tree

7 files changed

+101
-30
lines changed

7 files changed

+101
-30
lines changed

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
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;
89
import com.clickhouse.jdbc.internal.JdbcConfiguration;
910
import com.clickhouse.jdbc.internal.ExceptionUtils;
1011
import org.slf4j.Logger;
@@ -102,6 +103,15 @@ public String getServerVersion() throws SQLException {
102103
return result.getString("server_version");
103104
}
104105

106+
/**
107+
* Returns configuration for current connection. Changes made to the instance of configuration may have side effects.
108+
* Application should avoid making changes to this object until it is documented.
109+
* @return - reference to internal instance of JdbcConfiguration
110+
*/
111+
public JdbcConfiguration getJdbcConfig() {
112+
return this.config;
113+
}
114+
105115
@Override
106116
public Statement createStatement() throws SQLException {
107117
checkOpen();
@@ -130,7 +140,8 @@ public String nativeSQL(String sql) throws SQLException {
130140
@Override
131141
public void setAutoCommit(boolean autoCommit) throws SQLException {
132142
checkOpen();
133-
if (!autoCommit) {
143+
144+
if (!config.isIgnoreUnsupportedRequests() && !autoCommit) {
134145
throw new SQLFeatureNotSupportedException("setAutoCommit = false not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
135146
}
136147
}
@@ -143,12 +154,16 @@ public boolean getAutoCommit() throws SQLException {
143154

144155
@Override
145156
public void commit() throws SQLException {
146-
throw new SQLFeatureNotSupportedException("Commit/Rollback not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
157+
if (!config.isIgnoreUnsupportedRequests() ) {
158+
throw new SQLFeatureNotSupportedException("Commit/Rollback not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
159+
}
147160
}
148161

149162
@Override
150163
public void rollback() throws SQLException {
151-
throw new SQLFeatureNotSupportedException("Commit/Rollback not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
164+
if (!config.isIgnoreUnsupportedRequests()) {
165+
throw new SQLFeatureNotSupportedException("Commit/Rollback not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
166+
}
152167
}
153168

154169
@Override
@@ -280,7 +295,9 @@ public Savepoint setSavepoint(String name) throws SQLException {
280295
@Override
281296
public void rollback(Savepoint savepoint) throws SQLException {
282297
checkOpen();
283-
throw new SQLFeatureNotSupportedException("Commit/Rollback not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
298+
if (!config.isIgnoreUnsupportedRequests()) {
299+
throw new SQLFeatureNotSupportedException("Commit/Rollback not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
300+
}
284301
}
285302

286303
@Override

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
public enum DriverProperties {
77

8+
IGNORE_UNSUPPORTED_VALUES("jdbc_ignore_unsupported_values", ""),
9+
SCHEMA_TERM("jdbc_schema_term", ""),
810
PLACEHOLDER("placeholder", "Placeholder for unknown properties");
911

1012
private final String key;
@@ -16,4 +18,7 @@ public enum DriverProperties {
1618
this.description = description;
1719
}
1820

21+
public String getKey() {
22+
return key;
23+
}
1924
}

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,21 @@ public boolean isDisableFrameworkDetection() {
4949
return disableFrameworkDetection;
5050
}
5151

52+
private boolean isIgnoreUnsupportedRequests;
53+
54+
public boolean isIgnoreUnsupportedRequests() {
55+
return isIgnoreUnsupportedRequests;
56+
}
57+
5258
public JdbcConfiguration(String url, Properties info) {
5359
this.allProperties = new ConcurrentHashMap<>();
5460
info.forEach((k, v) -> allProperties.put(k.toString(), v.toString()));
55-
5661
this.jdbcUrl = url;//Raw URL
5762
this.url = cleanUrl(url);
5863
this.user = info.getProperty("user", "default");
5964
this.password = info.getProperty("password", "");
6065
this.disableFrameworkDetection = Boolean.parseBoolean(info.getProperty("disable_frameworks_detection", "false"));
66+
this.isIgnoreUnsupportedRequests= Boolean.parseBoolean(getDriverProperty(DriverProperties.IGNORE_UNSUPPORTED_VALUES.getKey(), "false"));
6167
}
6268

6369
public static boolean acceptsURL(String url) {
@@ -118,4 +124,8 @@ public static List<DriverPropertyInfo> getDriverPropertyInfo(Properties userProv
118124

119125
return listOfProperties;
120126
}
127+
128+
public String getDriverProperty(String key, String defaultValue) {
129+
return allProperties.getOrDefault(key, defaultValue);
130+
}
121131
}

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

Lines changed: 3 additions & 2 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;
@@ -380,7 +381,7 @@ public boolean supportsLimitedOuterJoins() throws SQLException {
380381
*/
381382
@Override
382383
public String getSchemaTerm() {
383-
return "database";
384+
return connection.getJdbcConfig().getDriverProperty(DriverProperties.SCHEMA_TERM.getKey(), "schema");
384385
}
385386

386387
@Override
@@ -830,7 +831,7 @@ public ResultSet getColumns(String catalog, String schemaPattern, String tableNa
830831
"table AS TABLE_NAME, " +
831832
"name AS COLUMN_NAME, " +
832833
JdbcUtils.generateSqlTypeEnum("system.columns.type") + " AS DATA_TYPE, " +
833-
"replaceRegexpOne(type, '^Nullable\\(([\\\\w ,\\\\)\\\\(]+)\\)$', '\\\\1') AS TYPE_NAME, " +
834+
"type AS TYPE_NAME, " +
834835
JdbcUtils.generateSqlTypeSizes("system.columns.type") + " AS COLUMN_SIZE, " +
835836
"toInt32(0) AS BUFFER_LENGTH, " +
836837
"IF (numeric_scale == 0, NULL, numeric_scale) as DECIMAL_DIGITS, " +

jdbc-v2/src/test/java/com/clickhouse/jdbc/ConnectionTest.java

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.clickhouse.jdbc;
22

33
import java.sql.*;
4+
import java.util.Properties;
5+
46
import com.clickhouse.jdbc.internal.ClientInfoProperties;
7+
import com.clickhouse.jdbc.internal.DriverProperties;
58
import org.testng.Assert;
69
import org.testng.annotations.Test;
710

@@ -50,29 +53,40 @@ public void nativeSQLTest() throws SQLException {
5053

5154
@Test(groups = { "integration" })
5255
public void setAutoCommitTest() throws SQLException {
53-
Connection localConnection = this.getJdbcConnection();
54-
assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.setAutoCommit(false));
55-
localConnection.setAutoCommit(true);
56+
try (Connection localConnection = this.getJdbcConnection()) {
57+
assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.setAutoCommit(false));
58+
Assert.assertTrue(localConnection.getAutoCommit());
59+
localConnection.setAutoCommit(true);
60+
}
61+
62+
Properties prop = new Properties();
63+
prop.setProperty(DriverProperties.IGNORE_UNSUPPORTED_VALUES.getKey(), "true");
64+
try (Connection localConnection = getJdbcConnection(prop)) {
65+
localConnection.setAutoCommit(false);
66+
Assert.assertTrue(localConnection.getAutoCommit());
67+
localConnection.setAutoCommit(true);
68+
Assert.assertTrue(localConnection.getAutoCommit());
69+
localConnection.setAutoCommit(false);
70+
}
71+
}
72+
73+
@Test(groups = { "integration" })
74+
public void testCommitRollback() throws SQLException {
75+
try (Connection localConnection = this.getJdbcConnection()) {
76+
assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.commit());
77+
assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.rollback());
78+
assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.rollback(null));
79+
}
80+
81+
Properties prop = new Properties();
82+
prop.setProperty(DriverProperties.IGNORE_UNSUPPORTED_VALUES.getKey(), "true");
83+
try (Connection localConnection = this.getJdbcConnection(prop)) {
84+
localConnection.commit();
85+
localConnection.rollback();
86+
localConnection.rollback(null);
87+
}
5688
}
5789

58-
@Test(groups = { "integration" })
59-
public void getAutoCommitTest() throws SQLException {
60-
Connection localConnection = this.getJdbcConnection();
61-
Assert.assertTrue(localConnection.getAutoCommit());
62-
}
63-
64-
@Test(groups = { "integration" })
65-
public void commitTest() throws SQLException {
66-
Connection localConnection = this.getJdbcConnection();
67-
assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.commit());
68-
}
69-
70-
@Test(groups = { "integration" })
71-
public void rollbackTest() throws SQLException {
72-
Connection localConnection = this.getJdbcConnection();
73-
assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.rollback());
74-
assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.rollback(null));
75-
}
7690

7791
@Test(groups = { "integration" })
7892
public void closeTest() throws SQLException {

jdbc-v2/src/test/java/com/clickhouse/jdbc/JdbcIntegrationTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,17 @@ public String getEndpointString(boolean includeDbName) {
2525
}
2626

2727
public Connection getJdbcConnection() throws SQLException {
28+
return getJdbcConnection(null);
29+
}
30+
31+
public Connection getJdbcConnection(Properties properties) throws SQLException {
2832
Properties info = new Properties();
2933
info.setProperty("user", "default");
3034
info.setProperty("password", ClickHouseServerForTest.getPassword());
3135
LOGGER.info("Connecting to {}", getEndpointString());
36+
if (properties != null) {
37+
info.putAll(properties);
38+
}
3239

3340
return new ConnectionImpl(getEndpointString(), info);
3441
//return DriverManager.getConnection(getEndpointString(), "default", ClickHouseServerForTest.getPassword());

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.clickhouse.jdbc.metadata;
22

3+
import com.clickhouse.client.api.command.CommandResponse;
34
import com.clickhouse.jdbc.JdbcIntegrationTest;
45
import com.clickhouse.jdbc.internal.ClientInfoProperties;
6+
import com.clickhouse.jdbc.internal.DriverProperties;
57
import org.testng.Assert;
68
import org.testng.annotations.Ignore;
79
import org.testng.annotations.Test;
@@ -13,6 +15,7 @@
1315
import java.util.Arrays;
1416
import java.util.HashSet;
1517
import java.util.List;
18+
import java.util.Properties;
1619
import java.util.Set;
1720

1821
import static org.testng.Assert.assertEquals;
@@ -35,7 +38,7 @@ public void testGetColumns() throws Exception {
3538
List<String> columnTypes = Arrays.asList("UInt64", "String", "Float32", "FixedString(10)", "Decimal(10, 2)", "Nullable(Decimal(5, 4))");
3639
List<Integer> columnSizes = Arrays.asList(8, 0, 4, 10, 10, 5);
3740
List<Integer> columnJDBCDataTypes = Arrays.asList(Types.BIGINT, Types.VARCHAR, Types.FLOAT, Types.CHAR, Types.DECIMAL, Types.DECIMAL);
38-
List<String> columnTypeNames = Arrays.asList("UInt64", "String", "Float32", "FixedString(10)", "Decimal(10, 2)", "Decimal(5, 4)");
41+
List<String> columnTypeNames = Arrays.asList("UInt64", "String", "Float32", "FixedString(10)", "Decimal(10, 2)", "Nullable(Decimal(5, 4))");
3942
List<Boolean> columnNullable = Arrays.asList(false, false, false, false, false, true);
4043
List<Integer> columnDecimalDigits = Arrays.asList(null, null, null, null, 2, 4);
4144
List<Integer> columnRadix = Arrays.asList(2, null, null, null, 10, 10);
@@ -102,7 +105,7 @@ public void testGetTables() throws Exception {
102105
@Test(groups = { "integration" })
103106
public void testGetPrimaryKeys() throws Exception {
104107
runQuery("SELECT 1;");
105-
Thread.sleep(10 * 1000); // wait for query log to be updated
108+
runQuery("SYSTEM FLUSH LOGS");
106109

107110
try (Connection conn = getJdbcConnection()) {
108111
DatabaseMetaData dbmd = conn.getMetaData();
@@ -136,6 +139,20 @@ public void testGetSchemas() throws Exception {
136139
}
137140
}
138141

142+
@Test
143+
public void testSchemaTerm() throws Exception {
144+
145+
try (Connection connection = getJdbcConnection()){
146+
Assert.assertEquals(connection.getMetaData().getSchemaTerm(), "schema");
147+
}
148+
149+
Properties prop = new Properties();
150+
prop.put(DriverProperties.SCHEMA_TERM.getKey(), "database");
151+
try (Connection connection = getJdbcConnection(prop)){
152+
Assert.assertEquals(connection.getMetaData().getSchemaTerm(), "database");
153+
}
154+
}
155+
139156
@Test(groups = { "integration" })
140157
public void testGetCatalogs() throws Exception {
141158
try (Connection conn = getJdbcConnection()) {

0 commit comments

Comments
 (0)