Skip to content

Commit 14c737f

Browse files
committed
replaced unsupported feature ignore code with feature manager to reduce duplication
1 parent 8983054 commit 14c737f

File tree

4 files changed

+36
-85
lines changed

4 files changed

+36
-85
lines changed

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

Lines changed: 26 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.clickhouse.data.ClickHouseColumn;
1010
import com.clickhouse.data.ClickHouseDataType;
1111
import com.clickhouse.jdbc.internal.ExceptionUtils;
12+
import com.clickhouse.jdbc.internal.FeatureManager;
1213
import com.clickhouse.jdbc.internal.JdbcConfiguration;
1314
import com.clickhouse.jdbc.internal.JdbcUtils;
1415
import com.clickhouse.jdbc.internal.ParsedPreparedStatement;
@@ -36,16 +37,13 @@
3637
import java.sql.Statement;
3738
import java.sql.Struct;
3839
import java.time.Duration;
39-
import java.util.Arrays;
4040
import java.util.Calendar;
4141
import java.util.Collections;
4242
import java.util.HashSet;
43-
import java.util.List;
4443
import java.util.Map;
4544
import java.util.Properties;
4645
import java.util.Set;
4746
import java.util.concurrent.Executor;
48-
import java.util.stream.Collectors;
4947

5048
public class ConnectionImpl implements Connection, JdbcV2Wrapper {
5149
private static final Logger log = LoggerFactory.getLogger(ConnectionImpl.class);
@@ -69,6 +67,8 @@ public class ConnectionImpl implements Connection, JdbcV2Wrapper {
6967

7068
private final SqlParser sqlParser;
7169

70+
private final FeatureManager featureManager;
71+
7272
public ConnectionImpl(String url, Properties info) throws SQLException {
7373
try {
7474
this.url = url;//Raw URL
@@ -117,6 +117,7 @@ public ConnectionImpl(String url, Properties info) throws SQLException {
117117
this.defaultCalendar = Calendar.getInstance();
118118

119119
this.sqlParser = new SqlParser();
120+
this.featureManager = new FeatureManager(this.config);
120121
} catch (SQLException e) {
121122
throw e;
122123
} catch (Exception e) {
@@ -167,10 +168,7 @@ public PreparedStatement prepareStatement(String sql) throws SQLException {
167168
@Override
168169
public CallableStatement prepareCall(String sql) throws SQLException {
169170
ensureOpen();
170-
if (!config.isIgnoreUnsupportedRequests()) {
171-
throw new SQLFeatureNotSupportedException("CallableStatement not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
172-
}
173-
171+
featureManager.unsupportedFeatureThrow("prepareCall(String sql)");
174172
return null;
175173
}
176174

@@ -184,10 +182,7 @@ public String nativeSQL(String sql) throws SQLException {
184182
@Override
185183
public void setAutoCommit(boolean autoCommit) throws SQLException {
186184
ensureOpen();
187-
188-
if (!config.isIgnoreUnsupportedRequests() && !autoCommit) {
189-
throw new SQLFeatureNotSupportedException("setAutoCommit = false not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
190-
}
185+
featureManager.unsupportedFeatureThrow("setAutoCommit(false)", !autoCommit);
191186
}
192187

193188
@Override
@@ -198,16 +193,12 @@ public boolean getAutoCommit() throws SQLException {
198193

199194
@Override
200195
public void commit() throws SQLException {
201-
if (!config.isIgnoreUnsupportedRequests() ) {
202-
throw new SQLFeatureNotSupportedException("Commit/Rollback not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
203-
}
196+
featureManager.unsupportedFeatureThrow("commit()");
204197
}
205198

206199
@Override
207200
public void rollback() throws SQLException {
208-
if (!config.isIgnoreUnsupportedRequests()) {
209-
throw new SQLFeatureNotSupportedException("Commit/Rollback not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
210-
}
201+
featureManager.unsupportedFeatureThrow("rollback()");
211202
}
212203

213204
@Override
@@ -260,9 +251,7 @@ public String getCatalog() throws SQLException {
260251
@Override
261252
public void setTransactionIsolation(int level) throws SQLException {
262253
ensureOpen();
263-
if (!config.isIgnoreUnsupportedRequests() && TRANSACTION_NONE != level) {
264-
throw new SQLFeatureNotSupportedException("setTransactionIsolation not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
265-
}
254+
featureManager.unsupportedFeatureThrow("setTransactionIsolation(TRANSACTION_NONE)", TRANSACTION_NONE != level);
266255
}
267256

268257
@Override
@@ -297,29 +286,21 @@ public PreparedStatement prepareStatement(String sql, int resultSetType, int res
297286
@Override
298287
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
299288
ensureOpen();
300-
if (!config.isIgnoreUnsupportedRequests()) {
301-
throw new SQLFeatureNotSupportedException("CallableStatement not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
302-
}
303-
289+
featureManager.unsupportedFeatureThrow("prepareCall(String sql, int resultSetType, int resultSetConcurrency)");
304290
return null;
305291
}
306292

307293
@Override
308294
public Map<String, Class<?>> getTypeMap() throws SQLException {
309295
ensureOpen();
310-
if (!config.isIgnoreUnsupportedRequests()) {
311-
throw new SQLFeatureNotSupportedException("getTypeMap not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
312-
}
313-
314-
return null;
296+
featureManager.unsupportedFeatureThrow("getTypeMap()");
297+
return Collections.emptyMap();
315298
}
316299

317300
@Override
318301
public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
319302
ensureOpen();
320-
if (!config.isIgnoreUnsupportedRequests()) {
321-
throw new SQLFeatureNotSupportedException("setTypeMap not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
322-
}
303+
featureManager.unsupportedFeatureThrow("setTypeMap(Map<String, Class<?>>)");
323304
}
324305

325306
@Override
@@ -343,37 +324,27 @@ public int getHoldability() throws SQLException {
343324
@Override
344325
public Savepoint setSavepoint() throws SQLException {
345326
ensureOpen();
346-
if (!config.isIgnoreUnsupportedRequests()) {
347-
throw new SQLFeatureNotSupportedException("Savepoint not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
348-
}
349-
327+
featureManager.unsupportedFeatureThrow("setSavepoint()");
350328
return null;
351329
}
352330

353331
@Override
354332
public Savepoint setSavepoint(String name) throws SQLException {
355333
ensureOpen();
356-
if (!config.isIgnoreUnsupportedRequests()) {
357-
throw new SQLFeatureNotSupportedException("Savepoint not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
358-
}
359-
334+
featureManager.unsupportedFeatureThrow("setSavepoint(String name)");
360335
return null;
361336
}
362337

363338
@Override
364339
public void rollback(Savepoint savepoint) throws SQLException {
365340
ensureOpen();
366-
if (!config.isIgnoreUnsupportedRequests()) {
367-
throw new SQLFeatureNotSupportedException("Commit/Rollback not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
368-
}
341+
featureManager.unsupportedFeatureThrow("rollback(Savepoint savepoint)");
369342
}
370343

371344
@Override
372345
public void releaseSavepoint(Savepoint savepoint) throws SQLException {
373346
ensureOpen();
374-
if (!config.isIgnoreUnsupportedRequests()) {
375-
throw new SQLFeatureNotSupportedException("Savepoint not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
376-
}
347+
featureManager.unsupportedFeatureThrow("releaseSavepoint(Savepoint savepoint)");
377348
}
378349

379350
@Override
@@ -429,10 +400,7 @@ public PreparedStatement prepareStatement(String sql, int resultSetType, int res
429400
@Override
430401
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
431402
ensureOpen();
432-
if (!config.isIgnoreUnsupportedRequests()) {
433-
throw new SQLFeatureNotSupportedException("CallableStatement not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
434-
}
435-
403+
featureManager.unsupportedFeatureThrow("prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)");
436404
return null;
437405
}
438406

@@ -472,40 +440,29 @@ public PreparedStatement prepareStatement(String sql, String[] columnNames) thro
472440
@Override
473441
public Clob createClob() throws SQLException {
474442
ensureOpen();
475-
if (!config.isIgnoreUnsupportedRequests()) {
476-
throw new SQLFeatureNotSupportedException("Clob not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
477-
}
443+
featureManager.unsupportedFeatureThrow("createClob()");
478444

479445
return null;
480446
}
481447

482448
@Override
483449
public Blob createBlob() throws SQLException {
484450
ensureOpen();
485-
if (!config.isIgnoreUnsupportedRequests()) {
486-
throw new SQLFeatureNotSupportedException("Blob not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
487-
}
488-
451+
featureManager.unsupportedFeatureThrow("createBlob()");
489452
return null;
490453
}
491454

492455
@Override
493456
public NClob createNClob() throws SQLException {
494457
ensureOpen();
495-
if (!config.isIgnoreUnsupportedRequests()) {
496-
throw new SQLFeatureNotSupportedException("NClob not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
497-
}
498-
458+
featureManager.unsupportedFeatureThrow("createNClob()");
499459
return null;
500460
}
501461

502462
@Override
503463
public SQLXML createSQLXML() throws SQLException {
504464
ensureOpen();
505-
if (!config.isIgnoreUnsupportedRequests()) {
506-
throw new SQLFeatureNotSupportedException("SQLXML not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
507-
}
508-
465+
featureManager.unsupportedFeatureThrow("createSQLXML()");
509466
return null;
510467
}
511468

@@ -586,7 +543,7 @@ public Array createArrayOf(String typeName, Object[] elements) throws SQLExcepti
586543
String clickhouseDataTypeName = (typeName.substring(0, endPos)).trim();
587544
ClickHouseDataType dataType = ClickHouseDataType.valueOf(clickhouseDataTypeName);
588545
if (dataType.equals(ClickHouseDataType.Array)) {
589-
throw new SQLFeatureNotSupportedException("Array cannot be a base type. In case of nested array provide most deep element type name.");
546+
throw new SQLException("Array cannot be a base type. In case of nested array provide most deep element type name.");
590547
}
591548
try {
592549
return new com.clickhouse.jdbc.types.Array(elements, typeName,
@@ -626,26 +583,19 @@ public String getSchema() throws SQLException {
626583

627584
@Override
628585
public void abort(Executor executor) throws SQLException {
629-
if (!config.isIgnoreUnsupportedRequests()) {
630-
throw new SQLFeatureNotSupportedException("abort not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
631-
}
586+
featureManager.unsupportedFeatureThrow("abort()");
632587
}
633588

634589
@Override
635590
public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException {
636591
//TODO: Should this be supported?
637-
if (!config.isIgnoreUnsupportedRequests()) {
638-
throw new SQLFeatureNotSupportedException("setNetworkTimeout not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
639-
}
592+
featureManager.unsupportedFeatureThrow("setNetworkTimeout()");
640593
}
641594

642595
@Override
643596
public int getNetworkTimeout() throws SQLException {
644597
//TODO: Should this be supported?
645-
if (!config.isIgnoreUnsupportedRequests()) {
646-
throw new SQLFeatureNotSupportedException("getNetworkTimeout not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
647-
}
648-
598+
featureManager.unsupportedFeatureThrow("getNetworkTimeout()");
649599
return -1;
650600
}
651601

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@ public FeatureManager(JdbcConfiguration configuration) {
1111
this.configuration = configuration;
1212
}
1313

14-
public void unsupportedFeatureThrow(String methodName) throws SQLException {
15-
if (!configuration.isIgnoreUnsupportedRequests()) {
14+
public void unsupportedFeatureThrow(String featureName) throws SQLException {
15+
unsupportedFeatureThrow(featureName, true);
16+
}
17+
18+
public void unsupportedFeatureThrow(String methodName, boolean doCheck) throws SQLException {
19+
if (doCheck && !configuration.isIgnoreUnsupportedRequests()) {
1620
throw new SQLFeatureNotSupportedException(methodName + " is not supported.",
1721
ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
1822
}
1923
}
20-
}
24+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ public static Object convert(Object value, Class<?> type, ClickHouseColumn colum
292292
return new Tuple(true, value);
293293
}
294294
} catch (Exception e) {
295-
throw new SQLException("Failed to convert " + value + " to " + type.getName(), ExceptionUtils.SQL_STATE_DATA_EXCEPTION, e);
295+
throw new SQLException("Failed to convert from " + value.getClass().getName() + " to " + type.getName(), ExceptionUtils.SQL_STATE_DATA_EXCEPTION, e);
296296
}
297297

298298
throw new SQLException("Unsupported conversion from " + value.getClass().getName() + " to " + type.getName(), ExceptionUtils.SQL_STATE_DATA_EXCEPTION);

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import org.testng.Assert;
1616
import org.testng.annotations.DataProvider;
1717
import org.testng.annotations.Test;
18-
import wiremock.org.eclipse.jetty.util.log.Log;
1918

2019
import java.math.BigDecimal;
2120
import java.net.Inet4Address;
@@ -43,9 +42,6 @@
4342
import java.util.Properties;
4443
import java.util.UUID;
4544
import java.util.function.BiConsumer;
46-
import java.util.function.BiFunction;
47-
import java.util.function.Function;
48-
import java.util.function.Supplier;
4945

5046
import static org.testng.Assert.assertEquals;
5147
import static org.testng.Assert.assertFalse;
@@ -109,7 +105,8 @@ public void testCreateUnsupportedStatements() throws Throwable {
109105
() -> conn.prepareCall("SELECT 1", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT),
110106
conn::setSavepoint,
111107
() -> conn.setSavepoint("save point"),
112-
() -> conn.createSQLXML(),
108+
conn::createSQLXML,
109+
() -> conn.setAutoCommit(false)
113110
};
114111

115112
for (Assert.ThrowingRunnable createStatement : createStatements) {

0 commit comments

Comments
 (0)