66import com .clickhouse .client .api .query .QuerySettings ;
77import com .clickhouse .jdbc .internal .ClientInfoProperties ;
88import com .clickhouse .jdbc .internal .JdbcConfiguration ;
9+ import com .clickhouse .jdbc .internal .ExceptionUtils ;
910import org .slf4j .Logger ;
1011import 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 ;
1531import java .util .HashSet ;
1632import java .util .List ;
17- import java .util .Collection ;
1833import java .util .Map ;
1934import java .util .Properties ;
2035import 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}
0 commit comments