Skip to content

Commit 5e304cb

Browse files
authored
Merge pull request #1998 from ClickHouse/add-sqlstate-exception-codes
Adding exception codes
2 parents 252aa6c + ebbf097 commit 5e304cb

File tree

12 files changed

+588
-315
lines changed

12 files changed

+588
-315
lines changed

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

Lines changed: 54 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,30 @@
66
import com.clickhouse.client.api.query.QuerySettings;
77
import com.clickhouse.jdbc.internal.ClientInfoProperties;
88
import com.clickhouse.jdbc.internal.JdbcConfiguration;
9+
import com.clickhouse.jdbc.internal.ExceptionUtils;
910
import org.slf4j.Logger;
1011
import org.slf4j.LoggerFactory;
1112

12-
import java.sql.*;
13-
import java.util.Collections;
14-
import java.util.HashMap;
13+
import java.sql.Array;
14+
import java.sql.Blob;
15+
import java.sql.CallableStatement;
16+
import java.sql.Clob;
17+
import java.sql.Connection;
18+
import java.sql.DatabaseMetaData;
19+
import java.sql.NClob;
20+
import java.sql.PreparedStatement;
21+
import java.sql.ResultSet;
22+
import java.sql.SQLClientInfoException;
23+
import java.sql.SQLException;
24+
import java.sql.SQLFeatureNotSupportedException;
25+
import java.sql.SQLWarning;
26+
import java.sql.SQLXML;
27+
import java.sql.Savepoint;
28+
import java.sql.ShardingKey;
29+
import java.sql.Statement;
30+
import java.sql.Struct;
1531
import java.util.HashSet;
1632
import java.util.List;
17-
import java.util.Collection;
1833
import java.util.Map;
1934
import java.util.Properties;
2035
import java.util.Set;
@@ -36,7 +51,7 @@ public class ConnectionImpl implements Connection, JdbcV2Wrapper {
3651

3752
private final com.clickhouse.jdbc.metadata.DatabaseMetaData metadata;
3853

39-
public ConnectionImpl(String url, Properties info) {
54+
public ConnectionImpl(String url, Properties info) throws SQLException {
4055
log.debug("Creating connection to {}", url);
4156
this.url = url;//Raw URL
4257
this.config = new JdbcConfiguration(url, info);
@@ -82,7 +97,7 @@ public void setDefaultQuerySettings(QuerySettings settings) {
8297

8398
public String getServerVersion() throws SQLException {
8499
GenericRecord result = client.queryAll("SELECT version() as server_version").stream()
85-
.findFirst().orElseThrow(() -> new SQLException("Failed to retrieve server version."));
100+
.findFirst().orElseThrow(() -> new SQLException("Failed to retrieve server version.", ExceptionUtils.SQL_STATE_CLIENT_ERROR));
86101

87102
return result.getString("server_version");
88103
}
@@ -102,21 +117,21 @@ public PreparedStatement prepareStatement(String sql) throws SQLException {
102117
@Override
103118
public CallableStatement prepareCall(String sql) throws SQLException {
104119
checkOpen();
105-
throw new SQLFeatureNotSupportedException("CallableStatement not supported");
120+
throw new SQLFeatureNotSupportedException("CallableStatement not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
106121
}
107122

108123
@Override
109124
public String nativeSQL(String sql) throws SQLException {
110125
checkOpen();
111126
/// TODO: this is not implemented according to JDBC spec and may not be used.
112-
throw new RuntimeException("Not implemented");
127+
throw new SQLFeatureNotSupportedException("nativeSQL not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
113128
}
114129

115130
@Override
116131
public void setAutoCommit(boolean autoCommit) throws SQLException {
117132
checkOpen();
118133
if (!autoCommit) {
119-
throw new SQLFeatureNotSupportedException("setAutoCommit = false not supported");
134+
throw new SQLFeatureNotSupportedException("setAutoCommit = false not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
120135
}
121136
}
122137

@@ -128,12 +143,12 @@ public boolean getAutoCommit() throws SQLException {
128143

129144
@Override
130145
public void commit() throws SQLException {
131-
throw new SQLFeatureNotSupportedException("Commit/Rollback not supported");
146+
throw new SQLFeatureNotSupportedException("Commit/Rollback not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
132147
}
133148

134149
@Override
135150
public void rollback() throws SQLException {
136-
throw new SQLFeatureNotSupportedException("Commit/Rollback not supported");
151+
throw new SQLFeatureNotSupportedException("Commit/Rollback not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
137152
}
138153

139154
@Override
@@ -161,7 +176,7 @@ public DatabaseMetaData getMetaData() throws SQLException {
161176
public void setReadOnly(boolean readOnly) throws SQLException {
162177
checkOpen();
163178
if (readOnly) {
164-
throw new SQLFeatureNotSupportedException("read-only=true unsupported");
179+
throw new SQLFeatureNotSupportedException("read-only=true unsupported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
165180
}
166181
}
167182

@@ -186,7 +201,7 @@ public String getCatalog() throws SQLException {
186201
public void setTransactionIsolation(int level) throws SQLException {
187202
checkOpen();
188203
if (TRANSACTION_NONE != level) {
189-
throw new SQLFeatureNotSupportedException("setTransactionIsolation not supported");
204+
throw new SQLFeatureNotSupportedException("setTransactionIsolation not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
190205
}
191206
}
192207

@@ -211,31 +226,31 @@ public void clearWarnings() throws SQLException {
211226
public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
212227
checkOpen();
213228
//TODO: Should this be a silent ignore?
214-
throw new SQLFeatureNotSupportedException("Statement with resultSetType and resultSetConcurrency override not supported");
229+
throw new SQLFeatureNotSupportedException("Statement with resultSetType and resultSetConcurrency override not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
215230
}
216231

217232
@Override
218233
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
219234
checkOpen();
220-
throw new SQLFeatureNotSupportedException("PreparedStatement with resultSetType and resultSetConcurrency override not supported");
235+
throw new SQLFeatureNotSupportedException("PreparedStatement with resultSetType and resultSetConcurrency override not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
221236
}
222237

223238
@Override
224239
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
225240
checkOpen();
226-
throw new SQLFeatureNotSupportedException("CallableStatement not supported");
241+
throw new SQLFeatureNotSupportedException("CallableStatement not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
227242
}
228243

229244
@Override
230245
public Map<String, Class<?>> getTypeMap() throws SQLException {
231246
checkOpen();
232-
throw new SQLFeatureNotSupportedException("getTypeMap not supported");
247+
throw new SQLFeatureNotSupportedException("getTypeMap not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
233248
}
234249

235250
@Override
236251
public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
237252
checkOpen();
238-
throw new SQLFeatureNotSupportedException("setTypeMap not supported");
253+
throw new SQLFeatureNotSupportedException("setTypeMap not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
239254
}
240255

241256
@Override
@@ -253,97 +268,97 @@ public int getHoldability() throws SQLException {
253268
@Override
254269
public Savepoint setSavepoint() throws SQLException {
255270
checkOpen();
256-
throw new SQLFeatureNotSupportedException("Savepoint not supported");
271+
throw new SQLFeatureNotSupportedException("Savepoint not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
257272
}
258273

259274
@Override
260275
public Savepoint setSavepoint(String name) throws SQLException {
261276
checkOpen();
262-
throw new SQLFeatureNotSupportedException("Savepoint not supported");
277+
throw new SQLFeatureNotSupportedException("Savepoint not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
263278
}
264279

265280
@Override
266281
public void rollback(Savepoint savepoint) throws SQLException {
267282
checkOpen();
268-
throw new SQLFeatureNotSupportedException("Commit/Rollback not supported");
283+
throw new SQLFeatureNotSupportedException("Commit/Rollback not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
269284
}
270285

271286
@Override
272287
public void releaseSavepoint(Savepoint savepoint) throws SQLException {
273288
checkOpen();
274-
throw new SQLFeatureNotSupportedException("Savepoint not supported");
289+
throw new SQLFeatureNotSupportedException("Savepoint not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
275290
}
276291

277292
@Override
278293
public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
279294
checkOpen();
280295
//TODO: Should this be a silent ignore?
281-
throw new SQLFeatureNotSupportedException("Statement with resultSetType, resultSetConcurrency, and resultSetHoldability override not supported");
296+
throw new SQLFeatureNotSupportedException("Statement with resultSetType, resultSetConcurrency, and resultSetHoldability override not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
282297
}
283298

284299
@Override
285300
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
286301
checkOpen();
287302
//TODO: Should this be a silent ignore?
288-
throw new SQLFeatureNotSupportedException("PreparedStatement with resultSetType, resultSetConcurrency, and resultSetHoldability override not supported");
303+
throw new SQLFeatureNotSupportedException("PreparedStatement with resultSetType, resultSetConcurrency, and resultSetHoldability override not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
289304
}
290305

291306
@Override
292307
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
293308
checkOpen();
294-
throw new SQLFeatureNotSupportedException("CallableStatement not supported");
309+
throw new SQLFeatureNotSupportedException("CallableStatement not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
295310
}
296311

297312
@Override
298313
public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
299314
checkOpen();
300315
//TODO: Should this be supported?
301-
throw new SQLFeatureNotSupportedException("prepareStatement(String sql, int autoGeneratedKeys) not supported");
316+
throw new SQLFeatureNotSupportedException("prepareStatement(String sql, int autoGeneratedKeys) not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
302317
}
303318

304319
@Override
305320
public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {
306321
checkOpen();
307322
//TODO: Should this be supported?
308-
throw new SQLFeatureNotSupportedException("prepareStatement(String sql, int[] columnIndexes) not supported");
323+
throw new SQLFeatureNotSupportedException("prepareStatement(String sql, int[] columnIndexes) not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
309324
}
310325

311326
@Override
312327
public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {
313328
checkOpen();
314329
//TODO: Should this be supported?
315-
throw new SQLFeatureNotSupportedException("prepareStatement(String sql, String[] columnNames) not supported");
330+
throw new SQLFeatureNotSupportedException("prepareStatement(String sql, String[] columnNames) not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
316331
}
317332

318333
@Override
319334
public Clob createClob() throws SQLException {
320335
checkOpen();
321-
throw new SQLFeatureNotSupportedException("Clob not supported");
336+
throw new SQLFeatureNotSupportedException("Clob not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
322337
}
323338

324339
@Override
325340
public Blob createBlob() throws SQLException {
326341
checkOpen();
327-
throw new SQLFeatureNotSupportedException("Blob not supported");
342+
throw new SQLFeatureNotSupportedException("Blob not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
328343
}
329344

330345
@Override
331346
public NClob createNClob() throws SQLException {
332347
checkOpen();
333-
throw new SQLFeatureNotSupportedException("NClob not supported");
348+
throw new SQLFeatureNotSupportedException("NClob not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
334349
}
335350

336351
@Override
337352
public SQLXML createSQLXML() throws SQLException {
338353
checkOpen();
339-
throw new SQLFeatureNotSupportedException("SQLXML not supported");
354+
throw new SQLFeatureNotSupportedException("SQLXML not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
340355
}
341356

342357
@Override
343358
public boolean isValid(int timeout) throws SQLException {
344359
checkOpen();
345360
if (timeout < 0) {
346-
throw new SQLException("Timeout must be >= 0");
361+
throw new SQLException("Timeout must be >= 0", ExceptionUtils.SQL_STATE_CLIENT_ERROR);
347362
}
348363

349364
//TODO: This is a placeholder implementation
@@ -405,14 +420,14 @@ public Array createArrayOf(String typeName, Object[] elements) throws SQLExcepti
405420
try {
406421
return new com.clickhouse.jdbc.types.Array(List.of(elements));
407422
} catch (Exception e) {
408-
throw new SQLException("Failed to create array", e);
423+
throw new SQLException("Failed to create array", ExceptionUtils.SQL_STATE_CLIENT_ERROR, e);
409424
}
410425
}
411426

412427
@Override
413428
public Struct createStruct(String typeName, Object[] attributes) throws SQLException {
414429
//TODO: Should this be supported?
415-
return null;
430+
throw new SQLFeatureNotSupportedException("createStruct not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
416431
}
417432

418433
@Override
@@ -429,19 +444,19 @@ public String getSchema() throws SQLException {
429444

430445
@Override
431446
public void abort(Executor executor) throws SQLException {
432-
throw new SQLFeatureNotSupportedException("abort not supported");
447+
throw new SQLFeatureNotSupportedException("abort not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
433448
}
434449

435450
@Override
436451
public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException {
437452
//TODO: Should this be supported?
438-
throw new SQLFeatureNotSupportedException("setNetworkTimeout not supported");
453+
throw new SQLFeatureNotSupportedException("setNetworkTimeout not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
439454
}
440455

441456
@Override
442457
public int getNetworkTimeout() throws SQLException {
443458
//TODO: Should this be supported?
444-
throw new SQLFeatureNotSupportedException("getNetworkTimeout not supported");
459+
throw new SQLFeatureNotSupportedException("getNetworkTimeout not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
445460
}
446461

447462
@Override
@@ -476,7 +491,7 @@ public void setShardingKey(ShardingKey shardingKey) throws SQLException {
476491

477492
private void checkOpen() throws SQLException {
478493
if (isClosed()) {
479-
throw new SQLException("Connection is closed");
494+
throw new SQLException("Connection is closed", ExceptionUtils.SQL_STATE_CONNECTION_EXCEPTION);
480495
}
481496
}
482497
}

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

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

3+
import com.clickhouse.jdbc.internal.ExceptionUtils;
4+
35
import javax.sql.DataSource;
46
import java.io.PrintWriter;
57
import java.sql.Connection;
@@ -68,12 +70,12 @@ public void setLogWriter(PrintWriter out) throws SQLException {
6870

6971
@Override
7072
public void setLoginTimeout(int seconds) throws SQLException {
71-
throw new SQLFeatureNotSupportedException("Method not supported");
73+
throw new SQLFeatureNotSupportedException("Method not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
7274
}
7375

7476
@Override
7577
public int getLoginTimeout() throws SQLException {
76-
throw new SQLFeatureNotSupportedException("Method not supported");
78+
throw new SQLFeatureNotSupportedException("Method not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
7779
}
7880

7981
@Override
@@ -83,7 +85,7 @@ public ConnectionBuilder createConnectionBuilder() throws SQLException {
8385

8486
@Override
8587
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
86-
throw new SQLFeatureNotSupportedException("Method not supported");
88+
throw new SQLFeatureNotSupportedException("Method not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
8789
}
8890

8991
@Override

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33

44
import com.clickhouse.jdbc.internal.JdbcConfiguration;
5+
import com.clickhouse.jdbc.internal.ExceptionUtils;
56
import org.slf4j.Logger;
67
import org.slf4j.LoggerFactory;
78

@@ -157,6 +158,6 @@ public boolean jdbcCompliant() {
157158

158159
@Override
159160
public java.util.logging.Logger getParentLogger() throws SQLFeatureNotSupportedException {
160-
throw new SQLFeatureNotSupportedException("Method not supported");
161+
throw new SQLFeatureNotSupportedException("Method not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
161162
}
162163
}

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

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

3+
import com.clickhouse.jdbc.internal.ExceptionUtils;
34
import org.slf4j.Logger;
45
import org.slf4j.LoggerFactory;
56

@@ -234,7 +235,7 @@ public void setCharacterStream(int parameterIndex, Reader x, int length) throws
234235
@Override
235236
public void setRef(int parameterIndex, Ref x) throws SQLException {
236237
checkClosed();
237-
throw new SQLFeatureNotSupportedException("Ref is not supported.");
238+
throw new SQLFeatureNotSupportedException("Ref is not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
238239
}
239240

240241
@Override
@@ -531,7 +532,7 @@ private static String encodeObject(Object x) throws SQLException {
531532
return escapeString(x.toString());//Escape single quotes
532533
} catch (Exception e) {
533534
LOG.error("Error encoding object", e);
534-
throw new SQLException("Error encoding object", e);
535+
throw new SQLException("Error encoding object", ExceptionUtils.SQL_STATE_SQL_ERROR, e);
535536
}
536537
}
537538

0 commit comments

Comments
 (0)