diff --git a/jdbc-v2/src/main/java/com/clickhouse/jdbc/ConnectionImpl.java b/jdbc-v2/src/main/java/com/clickhouse/jdbc/ConnectionImpl.java index 6ab868fe8..5d3cb56b5 100644 --- a/jdbc-v2/src/main/java/com/clickhouse/jdbc/ConnectionImpl.java +++ b/jdbc-v2/src/main/java/com/clickhouse/jdbc/ConnectionImpl.java @@ -6,15 +6,30 @@ import com.clickhouse.client.api.query.QuerySettings; import com.clickhouse.jdbc.internal.ClientInfoProperties; import com.clickhouse.jdbc.internal.JdbcConfiguration; +import com.clickhouse.jdbc.internal.ExceptionUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.sql.*; -import java.util.Collections; -import java.util.HashMap; +import java.sql.Array; +import java.sql.Blob; +import java.sql.CallableStatement; +import java.sql.Clob; +import java.sql.Connection; +import java.sql.DatabaseMetaData; +import java.sql.NClob; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLClientInfoException; +import java.sql.SQLException; +import java.sql.SQLFeatureNotSupportedException; +import java.sql.SQLWarning; +import java.sql.SQLXML; +import java.sql.Savepoint; +import java.sql.ShardingKey; +import java.sql.Statement; +import java.sql.Struct; import java.util.HashSet; import java.util.List; -import java.util.Collection; import java.util.Map; import java.util.Properties; import java.util.Set; @@ -36,7 +51,7 @@ public class ConnectionImpl implements Connection, JdbcV2Wrapper { private final com.clickhouse.jdbc.metadata.DatabaseMetaData metadata; - public ConnectionImpl(String url, Properties info) { + public ConnectionImpl(String url, Properties info) throws SQLException { log.debug("Creating connection to {}", url); this.url = url;//Raw URL this.config = new JdbcConfiguration(url, info); @@ -82,7 +97,7 @@ public void setDefaultQuerySettings(QuerySettings settings) { public String getServerVersion() throws SQLException { GenericRecord result = client.queryAll("SELECT version() as server_version").stream() - .findFirst().orElseThrow(() -> new SQLException("Failed to retrieve server version.")); + .findFirst().orElseThrow(() -> new SQLException("Failed to retrieve server version.", ExceptionUtils.SQL_STATE_CLIENT_ERROR)); return result.getString("server_version"); } @@ -102,21 +117,21 @@ public PreparedStatement prepareStatement(String sql) throws SQLException { @Override public CallableStatement prepareCall(String sql) throws SQLException { checkOpen(); - throw new SQLFeatureNotSupportedException("CallableStatement not supported"); + throw new SQLFeatureNotSupportedException("CallableStatement not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public String nativeSQL(String sql) throws SQLException { checkOpen(); /// TODO: this is not implemented according to JDBC spec and may not be used. - throw new RuntimeException("Not implemented"); + throw new SQLFeatureNotSupportedException("nativeSQL not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void setAutoCommit(boolean autoCommit) throws SQLException { checkOpen(); if (!autoCommit) { - throw new SQLFeatureNotSupportedException("setAutoCommit = false not supported"); + throw new SQLFeatureNotSupportedException("setAutoCommit = false not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } } @@ -128,12 +143,12 @@ public boolean getAutoCommit() throws SQLException { @Override public void commit() throws SQLException { - throw new SQLFeatureNotSupportedException("Commit/Rollback not supported"); + throw new SQLFeatureNotSupportedException("Commit/Rollback not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void rollback() throws SQLException { - throw new SQLFeatureNotSupportedException("Commit/Rollback not supported"); + throw new SQLFeatureNotSupportedException("Commit/Rollback not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override @@ -161,7 +176,7 @@ public DatabaseMetaData getMetaData() throws SQLException { public void setReadOnly(boolean readOnly) throws SQLException { checkOpen(); if (readOnly) { - throw new SQLFeatureNotSupportedException("read-only=true unsupported"); + throw new SQLFeatureNotSupportedException("read-only=true unsupported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } } @@ -186,7 +201,7 @@ public String getCatalog() throws SQLException { public void setTransactionIsolation(int level) throws SQLException { checkOpen(); if (TRANSACTION_NONE != level) { - throw new SQLFeatureNotSupportedException("setTransactionIsolation not supported"); + throw new SQLFeatureNotSupportedException("setTransactionIsolation not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } } @@ -211,31 +226,31 @@ public void clearWarnings() throws SQLException { public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException { checkOpen(); //TODO: Should this be a silent ignore? - throw new SQLFeatureNotSupportedException("Statement with resultSetType and resultSetConcurrency override not supported"); + throw new SQLFeatureNotSupportedException("Statement with resultSetType and resultSetConcurrency override not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { checkOpen(); - throw new SQLFeatureNotSupportedException("PreparedStatement with resultSetType and resultSetConcurrency override not supported"); + throw new SQLFeatureNotSupportedException("PreparedStatement with resultSetType and resultSetConcurrency override not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { checkOpen(); - throw new SQLFeatureNotSupportedException("CallableStatement not supported"); + throw new SQLFeatureNotSupportedException("CallableStatement not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public Map> getTypeMap() throws SQLException { checkOpen(); - throw new SQLFeatureNotSupportedException("getTypeMap not supported"); + throw new SQLFeatureNotSupportedException("getTypeMap not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void setTypeMap(Map> map) throws SQLException { checkOpen(); - throw new SQLFeatureNotSupportedException("setTypeMap not supported"); + throw new SQLFeatureNotSupportedException("setTypeMap not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override @@ -253,97 +268,97 @@ public int getHoldability() throws SQLException { @Override public Savepoint setSavepoint() throws SQLException { checkOpen(); - throw new SQLFeatureNotSupportedException("Savepoint not supported"); + throw new SQLFeatureNotSupportedException("Savepoint not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public Savepoint setSavepoint(String name) throws SQLException { checkOpen(); - throw new SQLFeatureNotSupportedException("Savepoint not supported"); + throw new SQLFeatureNotSupportedException("Savepoint not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void rollback(Savepoint savepoint) throws SQLException { checkOpen(); - throw new SQLFeatureNotSupportedException("Commit/Rollback not supported"); + throw new SQLFeatureNotSupportedException("Commit/Rollback not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void releaseSavepoint(Savepoint savepoint) throws SQLException { checkOpen(); - throw new SQLFeatureNotSupportedException("Savepoint not supported"); + throw new SQLFeatureNotSupportedException("Savepoint not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { checkOpen(); //TODO: Should this be a silent ignore? - throw new SQLFeatureNotSupportedException("Statement with resultSetType, resultSetConcurrency, and resultSetHoldability override not supported"); + throw new SQLFeatureNotSupportedException("Statement with resultSetType, resultSetConcurrency, and resultSetHoldability override not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { checkOpen(); //TODO: Should this be a silent ignore? - throw new SQLFeatureNotSupportedException("PreparedStatement with resultSetType, resultSetConcurrency, and resultSetHoldability override not supported"); + throw new SQLFeatureNotSupportedException("PreparedStatement with resultSetType, resultSetConcurrency, and resultSetHoldability override not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { checkOpen(); - throw new SQLFeatureNotSupportedException("CallableStatement not supported"); + throw new SQLFeatureNotSupportedException("CallableStatement not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException { checkOpen(); //TODO: Should this be supported? - throw new SQLFeatureNotSupportedException("prepareStatement(String sql, int autoGeneratedKeys) not supported"); + throw new SQLFeatureNotSupportedException("prepareStatement(String sql, int autoGeneratedKeys) not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException { checkOpen(); //TODO: Should this be supported? - throw new SQLFeatureNotSupportedException("prepareStatement(String sql, int[] columnIndexes) not supported"); + throw new SQLFeatureNotSupportedException("prepareStatement(String sql, int[] columnIndexes) not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException { checkOpen(); //TODO: Should this be supported? - throw new SQLFeatureNotSupportedException("prepareStatement(String sql, String[] columnNames) not supported"); + throw new SQLFeatureNotSupportedException("prepareStatement(String sql, String[] columnNames) not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public Clob createClob() throws SQLException { checkOpen(); - throw new SQLFeatureNotSupportedException("Clob not supported"); + throw new SQLFeatureNotSupportedException("Clob not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public Blob createBlob() throws SQLException { checkOpen(); - throw new SQLFeatureNotSupportedException("Blob not supported"); + throw new SQLFeatureNotSupportedException("Blob not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public NClob createNClob() throws SQLException { checkOpen(); - throw new SQLFeatureNotSupportedException("NClob not supported"); + throw new SQLFeatureNotSupportedException("NClob not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public SQLXML createSQLXML() throws SQLException { checkOpen(); - throw new SQLFeatureNotSupportedException("SQLXML not supported"); + throw new SQLFeatureNotSupportedException("SQLXML not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public boolean isValid(int timeout) throws SQLException { checkOpen(); if (timeout < 0) { - throw new SQLException("Timeout must be >= 0"); + throw new SQLException("Timeout must be >= 0", ExceptionUtils.SQL_STATE_CLIENT_ERROR); } //TODO: This is a placeholder implementation @@ -405,14 +420,14 @@ public Array createArrayOf(String typeName, Object[] elements) throws SQLExcepti try { return new com.clickhouse.jdbc.types.Array(List.of(elements)); } catch (Exception e) { - throw new SQLException("Failed to create array", e); + throw new SQLException("Failed to create array", ExceptionUtils.SQL_STATE_CLIENT_ERROR, e); } } @Override public Struct createStruct(String typeName, Object[] attributes) throws SQLException { //TODO: Should this be supported? - return null; + throw new SQLFeatureNotSupportedException("createStruct not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override @@ -429,19 +444,19 @@ public String getSchema() throws SQLException { @Override public void abort(Executor executor) throws SQLException { - throw new SQLFeatureNotSupportedException("abort not supported"); + throw new SQLFeatureNotSupportedException("abort not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException { //TODO: Should this be supported? - throw new SQLFeatureNotSupportedException("setNetworkTimeout not supported"); + throw new SQLFeatureNotSupportedException("setNetworkTimeout not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public int getNetworkTimeout() throws SQLException { //TODO: Should this be supported? - throw new SQLFeatureNotSupportedException("getNetworkTimeout not supported"); + throw new SQLFeatureNotSupportedException("getNetworkTimeout not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override @@ -476,7 +491,7 @@ public void setShardingKey(ShardingKey shardingKey) throws SQLException { private void checkOpen() throws SQLException { if (isClosed()) { - throw new SQLException("Connection is closed"); + throw new SQLException("Connection is closed", ExceptionUtils.SQL_STATE_CONNECTION_EXCEPTION); } } } diff --git a/jdbc-v2/src/main/java/com/clickhouse/jdbc/DataSourceImpl.java b/jdbc-v2/src/main/java/com/clickhouse/jdbc/DataSourceImpl.java index 2389909e5..73808ae1c 100644 --- a/jdbc-v2/src/main/java/com/clickhouse/jdbc/DataSourceImpl.java +++ b/jdbc-v2/src/main/java/com/clickhouse/jdbc/DataSourceImpl.java @@ -1,5 +1,7 @@ package com.clickhouse.jdbc; +import com.clickhouse.jdbc.internal.ExceptionUtils; + import javax.sql.DataSource; import java.io.PrintWriter; import java.sql.Connection; @@ -68,12 +70,12 @@ public void setLogWriter(PrintWriter out) throws SQLException { @Override public void setLoginTimeout(int seconds) throws SQLException { - throw new SQLFeatureNotSupportedException("Method not supported"); + throw new SQLFeatureNotSupportedException("Method not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public int getLoginTimeout() throws SQLException { - throw new SQLFeatureNotSupportedException("Method not supported"); + throw new SQLFeatureNotSupportedException("Method not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override @@ -83,7 +85,7 @@ public ConnectionBuilder createConnectionBuilder() throws SQLException { @Override public Logger getParentLogger() throws SQLFeatureNotSupportedException { - throw new SQLFeatureNotSupportedException("Method not supported"); + throw new SQLFeatureNotSupportedException("Method not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override diff --git a/jdbc-v2/src/main/java/com/clickhouse/jdbc/Driver.java b/jdbc-v2/src/main/java/com/clickhouse/jdbc/Driver.java index 6e5f67633..3af4eee2d 100644 --- a/jdbc-v2/src/main/java/com/clickhouse/jdbc/Driver.java +++ b/jdbc-v2/src/main/java/com/clickhouse/jdbc/Driver.java @@ -2,6 +2,7 @@ import com.clickhouse.jdbc.internal.JdbcConfiguration; +import com.clickhouse.jdbc.internal.ExceptionUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -157,6 +158,6 @@ public boolean jdbcCompliant() { @Override public java.util.logging.Logger getParentLogger() throws SQLFeatureNotSupportedException { - throw new SQLFeatureNotSupportedException("Method not supported"); + throw new SQLFeatureNotSupportedException("Method not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } } diff --git a/jdbc-v2/src/main/java/com/clickhouse/jdbc/PreparedStatementImpl.java b/jdbc-v2/src/main/java/com/clickhouse/jdbc/PreparedStatementImpl.java index 29540cadd..36f89640d 100644 --- a/jdbc-v2/src/main/java/com/clickhouse/jdbc/PreparedStatementImpl.java +++ b/jdbc-v2/src/main/java/com/clickhouse/jdbc/PreparedStatementImpl.java @@ -1,5 +1,6 @@ package com.clickhouse.jdbc; +import com.clickhouse.jdbc.internal.ExceptionUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -234,7 +235,7 @@ public void setCharacterStream(int parameterIndex, Reader x, int length) throws @Override public void setRef(int parameterIndex, Ref x) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Ref is not supported."); + throw new SQLFeatureNotSupportedException("Ref is not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override @@ -531,7 +532,7 @@ private static String encodeObject(Object x) throws SQLException { return escapeString(x.toString());//Escape single quotes } catch (Exception e) { LOG.error("Error encoding object", e); - throw new SQLException("Error encoding object", e); + throw new SQLException("Error encoding object", ExceptionUtils.SQL_STATE_SQL_ERROR, e); } } diff --git a/jdbc-v2/src/main/java/com/clickhouse/jdbc/ResultSetImpl.java b/jdbc-v2/src/main/java/com/clickhouse/jdbc/ResultSetImpl.java index 3294cc544..e5ca8da62 100644 --- a/jdbc-v2/src/main/java/com/clickhouse/jdbc/ResultSetImpl.java +++ b/jdbc-v2/src/main/java/com/clickhouse/jdbc/ResultSetImpl.java @@ -17,6 +17,7 @@ import com.clickhouse.client.api.data_formats.ClickHouseBinaryFormatReader; import com.clickhouse.client.api.metadata.TableSchema; import com.clickhouse.client.api.query.QueryResponse; +import com.clickhouse.jdbc.internal.ExceptionUtils; import com.clickhouse.jdbc.types.Array; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -41,7 +42,7 @@ public ResultSetImpl(Statement parentStatement, QueryResponse response, ClickHou private void checkClosed() throws SQLException { if (closed) { - throw new SQLException("ResultSet is closed."); + throw new SQLException("ResultSet is closed.", ExceptionUtils.SQL_STATE_CONNECTION_EXCEPTION); } } @@ -65,7 +66,7 @@ public void close() throws SQLException { try { reader.close(); } catch (Exception e) { - throw new SQLException(e); + throw ExceptionUtils.toSqlState(e); } reader = null; @@ -75,7 +76,7 @@ public void close() throws SQLException { try { response.close(); } catch (Exception e) { - throw new SQLException(e); + throw ExceptionUtils.toSqlState(e); } response = null; } @@ -99,7 +100,7 @@ public String getString(int columnIndex) throws SQLException { return null; } } catch (Exception e) { - throw new SQLException(e); + throw ExceptionUtils.toSqlState(e); } } @@ -115,7 +116,7 @@ public boolean getBoolean(int columnIndex) throws SQLException { return false; } } catch (Exception e) { - throw new SQLException(e); + throw ExceptionUtils.toSqlState(e); } } @@ -131,7 +132,7 @@ public byte getByte(int columnIndex) throws SQLException { return 0; } } catch (Exception e) { - throw new SQLException(e); + throw ExceptionUtils.toSqlState(e); } } @@ -147,7 +148,7 @@ public short getShort(int columnIndex) throws SQLException { return 0; } } catch (Exception e) { - throw new SQLException(e); + throw ExceptionUtils.toSqlState(e); } } @@ -163,7 +164,7 @@ public int getInt(int columnIndex) throws SQLException { return 0; } } catch (Exception e) { - throw new SQLException(e); + throw ExceptionUtils.toSqlState(e); } } @@ -179,7 +180,7 @@ public long getLong(int columnIndex) throws SQLException { return 0; } } catch (Exception e) { - throw new SQLException(e); + throw ExceptionUtils.toSqlState(e); } } @@ -195,7 +196,7 @@ public float getFloat(int columnIndex) throws SQLException { return 0; } } catch (Exception e) { - throw new SQLException(e); + throw ExceptionUtils.toSqlState(e); } } @@ -211,7 +212,7 @@ public double getDouble(int columnIndex) throws SQLException { return 0; } } catch (Exception e) { - throw new SQLException(e); + throw ExceptionUtils.toSqlState(e); } } @@ -227,7 +228,7 @@ public BigDecimal getBigDecimal(int columnIndex, int scale) throws SQLException return null; } } catch (Exception e) { - throw new SQLException(e); + throw ExceptionUtils.toSqlState(e); } } @@ -243,7 +244,7 @@ public byte[] getBytes(int columnIndex) throws SQLException { return null; } } catch (Exception e) { - throw new SQLException(e); + throw ExceptionUtils.toSqlState(e); } } @@ -261,7 +262,7 @@ public Date getDate(int columnIndex) throws SQLException { wasNull = false; return Date.valueOf(localDate); } catch (Exception e) { - throw new SQLException(e); + throw ExceptionUtils.toSqlState(e); } } @@ -278,7 +279,7 @@ public Time getTime(int columnIndex) throws SQLException { wasNull = false; return Time.valueOf(localDateTime.toLocalTime()); } catch (Exception e) { - throw new SQLException(e); + throw ExceptionUtils.toSqlState(e); } } @@ -295,7 +296,7 @@ public Timestamp getTimestamp(int columnIndex) throws SQLException { wasNull = false; return Timestamp.valueOf(localDateTime); } catch (Exception e) { - throw new SQLException(e); + throw ExceptionUtils.toSqlState(e); } } @@ -303,7 +304,7 @@ public Timestamp getTimestamp(int columnIndex) throws SQLException { public InputStream getAsciiStream(int columnIndex) throws SQLException { checkClosed(); //TODO: Add this to ClickHouseBinaryFormatReader - throw new SQLFeatureNotSupportedException("AsciiStream is not yet supported."); + throw new SQLFeatureNotSupportedException("AsciiStream is not yet supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override @@ -316,7 +317,7 @@ public InputStream getUnicodeStream(int columnIndex) throws SQLException { public InputStream getBinaryStream(int columnIndex) throws SQLException { checkClosed(); //TODO: implement - throw new SQLFeatureNotSupportedException("BinaryStream is not yet supported."); + throw new SQLFeatureNotSupportedException("BinaryStream is not yet supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override @@ -331,7 +332,7 @@ public String getString(String columnLabel) throws SQLException { return null; } } catch (Exception e) { - throw new SQLException(e); + throw ExceptionUtils.toSqlState(e); } } @@ -347,7 +348,7 @@ public boolean getBoolean(String columnLabel) throws SQLException { return false; } } catch (Exception e) { - throw new SQLException(e); + throw ExceptionUtils.toSqlState(e); } } @@ -363,7 +364,7 @@ public byte getByte(String columnLabel) throws SQLException { return 0; } } catch (Exception e) { - throw new SQLException(e); + throw ExceptionUtils.toSqlState(e); } } @@ -379,7 +380,7 @@ public short getShort(String columnLabel) throws SQLException { return 0; } } catch (Exception e) { - throw new SQLException(e); + throw ExceptionUtils.toSqlState(e); } } @@ -395,7 +396,7 @@ public int getInt(String columnLabel) throws SQLException { return 0; } } catch (Exception e) { - throw new SQLException(e); + throw ExceptionUtils.toSqlState(e); } } @@ -411,7 +412,7 @@ public long getLong(String columnLabel) throws SQLException { return 0; } } catch (Exception e) { - throw new SQLException(e); + throw ExceptionUtils.toSqlState(e); } } @@ -427,7 +428,7 @@ public float getFloat(String columnLabel) throws SQLException { return 0; } } catch (Exception e) { - throw new SQLException(e); + throw ExceptionUtils.toSqlState(e); } } @@ -443,7 +444,7 @@ public double getDouble(String columnLabel) throws SQLException { return 0; } } catch (Exception e) { - throw new SQLException(e); + throw ExceptionUtils.toSqlState(e); } } @@ -459,7 +460,7 @@ public BigDecimal getBigDecimal(String columnLabel, int scale) throws SQLExcepti return null; } } catch (Exception e) { - throw new SQLException(e); + throw ExceptionUtils.toSqlState(e); } } @@ -475,7 +476,7 @@ public byte[] getBytes(String columnLabel) throws SQLException { return null; } } catch (Exception e) { - throw new SQLException(e); + throw ExceptionUtils.toSqlState(e); } } @@ -493,7 +494,7 @@ public Date getDate(String columnLabel) throws SQLException { wasNull = false; return Date.valueOf(localDate); } catch (Exception e) { - throw new SQLException(e); + throw ExceptionUtils.toSqlState(e); } } @@ -510,7 +511,7 @@ public Time getTime(String columnLabel) throws SQLException { wasNull = false; return Time.valueOf(localDateTime.toLocalTime()); } catch (Exception e) { - throw new SQLException(e); + throw ExceptionUtils.toSqlState(e); } } @@ -527,7 +528,7 @@ public Timestamp getTimestamp(String columnLabel) throws SQLException { wasNull = false; return Timestamp.valueOf(localDateTime); } catch (Exception e) { - throw new SQLException(e); + throw ExceptionUtils.toSqlState(e); } } @@ -535,7 +536,7 @@ public Timestamp getTimestamp(String columnLabel) throws SQLException { public InputStream getAsciiStream(String columnLabel) throws SQLException { checkClosed(); //TODO: Add this to ClickHouseBinaryFormatReader - throw new SQLFeatureNotSupportedException("AsciiStream is not yet supported."); + throw new SQLFeatureNotSupportedException("AsciiStream is not yet supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override @@ -548,7 +549,7 @@ public InputStream getUnicodeStream(String columnLabel) throws SQLException { public InputStream getBinaryStream(String columnLabel) throws SQLException { checkClosed(); //TODO: implement - throw new SQLFeatureNotSupportedException("BinaryStream is not yet supported."); + throw new SQLFeatureNotSupportedException("BinaryStream is not yet supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override @@ -586,7 +587,7 @@ public Object getObject(int columnIndex) throws SQLException { return null; } } catch (Exception e) { - throw new SQLException(e); + throw ExceptionUtils.toSqlState(e); } } @@ -602,7 +603,7 @@ public Object getObject(String columnLabel) throws SQLException { return null; } } catch (Exception e) { - throw new SQLException(e); + throw ExceptionUtils.toSqlState(e); } } @@ -612,20 +613,20 @@ public int findColumn(String columnLabel) throws SQLException { try { return reader.getSchema().getColumnByName(columnLabel).getColumnIndex(); } catch (Exception e) { - throw new SQLException(e); + throw ExceptionUtils.toSqlState(e); } } @Override public Reader getCharacterStream(int columnIndex) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("CharacterStream is not yet supported."); + throw new SQLFeatureNotSupportedException("CharacterStream is not yet supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public Reader getCharacterStream(String columnLabel) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("CharacterStream is not yet supported."); + throw new SQLFeatureNotSupportedException("CharacterStream is not yet supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override @@ -640,7 +641,7 @@ public BigDecimal getBigDecimal(int columnIndex) throws SQLException { return null; } } catch (Exception e) { - throw new SQLException(e); + throw ExceptionUtils.toSqlState(e); } } @@ -656,32 +657,32 @@ public BigDecimal getBigDecimal(String columnLabel) throws SQLException { return null; } } catch (Exception e) { - throw new SQLException(e); + throw ExceptionUtils.toSqlState(e); } } @Override public boolean isBeforeFirst() throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("isBeforeFirst is not supported."); + throw new SQLFeatureNotSupportedException("isBeforeFirst is not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public boolean isAfterLast() throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("isAfterLast is not supported."); + throw new SQLFeatureNotSupportedException("isAfterLast is not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public boolean isFirst() throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("isFirst is not supported."); + throw new SQLFeatureNotSupportedException("isFirst is not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public boolean isLast() throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("isLast is not supported."); + throw new SQLFeatureNotSupportedException("isLast is not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override @@ -697,37 +698,37 @@ public void afterLast() throws SQLException { @Override public boolean first() throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("first is not supported."); + throw new SQLFeatureNotSupportedException("first is not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public boolean last() throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("last is not supported."); + throw new SQLFeatureNotSupportedException("last is not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public int getRow() throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("getRow is not supported."); + throw new SQLFeatureNotSupportedException("getRow is not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public boolean absolute(int row) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("absolute is not supported."); + throw new SQLFeatureNotSupportedException("absolute is not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public boolean relative(int rows) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("relative is not supported."); + throw new SQLFeatureNotSupportedException("relative is not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public boolean previous() throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("previous is not supported."); + throw new SQLFeatureNotSupportedException("previous is not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override @@ -739,7 +740,7 @@ public int getFetchDirection() throws SQLException { @Override public void setFetchDirection(int direction) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("setFetchDirection is not supported."); + throw new SQLFeatureNotSupportedException("setFetchDirection is not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override @@ -768,247 +769,247 @@ public int getConcurrency() throws SQLException { @Override public boolean rowUpdated() throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public boolean rowInserted() throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public boolean rowDeleted() throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateNull(int columnIndex) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateBoolean(int columnIndex, boolean x) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateByte(int columnIndex, byte x) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateShort(int columnIndex, short x) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateInt(int columnIndex, int x) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateLong(int columnIndex, long x) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateFloat(int columnIndex, float x) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateDouble(int columnIndex, double x) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateBigDecimal(int columnIndex, BigDecimal x) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateString(int columnIndex, String x) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateBytes(int columnIndex, byte[] x) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateDate(int columnIndex, Date x) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateTime(int columnIndex, Time x) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateTimestamp(int columnIndex, Timestamp x) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateAsciiStream(int columnIndex, InputStream x, int length) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateBinaryStream(int columnIndex, InputStream x, int length) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateCharacterStream(int columnIndex, Reader x, int length) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateObject(int columnIndex, Object x, int scaleOrLength) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateObject(int columnIndex, Object x) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateNull(String columnLabel) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateBoolean(String columnLabel, boolean x) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateByte(String columnLabel, byte x) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateShort(String columnLabel, short x) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateInt(String columnLabel, int x) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateLong(String columnLabel, long x) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateFloat(String columnLabel, float x) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateDouble(String columnLabel, double x) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateBigDecimal(String columnLabel, BigDecimal x) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateString(String columnLabel, String x) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateBytes(String columnLabel, byte[] x) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateDate(String columnLabel, Date x) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateTime(String columnLabel, Time x) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateTimestamp(String columnLabel, Timestamp x) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateAsciiStream(String columnLabel, InputStream x, int length) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateBinaryStream(String columnLabel, InputStream x, int length) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateCharacterStream(String columnLabel, Reader reader, int length) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateObject(String columnLabel, Object x, int scaleOrLength) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateObject(String columnLabel, Object x) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override @@ -1061,19 +1062,19 @@ public Object getObject(int columnIndex, Map> map) throws SQLEx @Override public Ref getRef(int columnIndex) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Ref is not supported."); + throw new SQLFeatureNotSupportedException("Ref is not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public Blob getBlob(int columnIndex) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Blob is not supported."); + throw new SQLFeatureNotSupportedException("Blob is not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public java.sql.Clob getClob(int columnIndex) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Clob is not supported."); + throw new SQLFeatureNotSupportedException("Clob is not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override @@ -1082,7 +1083,7 @@ public java.sql.Array getArray(int columnIndex) throws SQLException { try { return new Array(reader.getList(columnIndex)); } catch (Exception e) { - throw new SQLException(e); + throw ExceptionUtils.toSqlState(e); } } @@ -1095,19 +1096,19 @@ public Object getObject(String columnLabel, Map> map) throws SQ @Override public Ref getRef(String columnLabel) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Ref is not supported."); + throw new SQLFeatureNotSupportedException("Ref is not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public Blob getBlob(String columnLabel) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Blob is not supported."); + throw new SQLFeatureNotSupportedException("Blob is not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public Clob getClob(String columnLabel) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Clob is not supported."); + throw new SQLFeatureNotSupportedException("Clob is not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override @@ -1116,7 +1117,7 @@ public java.sql.Array getArray(String columnLabel) throws SQLException { try { return new Array(reader.getList(columnLabel)); } catch (Exception e) { - throw new SQLException(e); + throw ExceptionUtils.toSqlState(e); } } @@ -1172,56 +1173,56 @@ public URL getURL(String columnLabel) throws SQLException { try { return new URL(reader.getString(columnLabel)); } catch (MalformedURLException e) { - throw new SQLDataException(e); + throw new SQLDataException(e.getMessage(), ExceptionUtils.SQL_STATE_DATA_EXCEPTION, e); } } @Override public void updateRef(int columnIndex, Ref x) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateRef(String columnLabel, Ref x) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateBlob(int columnIndex, Blob x) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateBlob(String columnLabel, Blob x) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateClob(int columnIndex, Clob x) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateClob(String columnLabel, Clob x) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateArray(int columnIndex, java.sql.Array x) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateArray(String columnLabel, java.sql.Array x) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override @@ -1239,13 +1240,13 @@ public RowId getRowId(String columnLabel) throws SQLException { @Override public void updateRowId(int columnIndex, RowId x) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateRowId(String columnLabel, RowId x) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override @@ -1262,61 +1263,61 @@ public boolean isClosed() throws SQLException { @Override public void updateNString(int columnIndex, String nString) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateNString(String columnLabel, String nString) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateNClob(int columnIndex, NClob nClob) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateNClob(String columnLabel, NClob nClob) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public NClob getNClob(int columnIndex) throws SQLException { checkClosed(); - return null; + throw new SQLFeatureNotSupportedException("NClob is not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public NClob getNClob(String columnLabel) throws SQLException { checkClosed(); - return null; + throw new SQLFeatureNotSupportedException("NClob is not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public SQLXML getSQLXML(int columnIndex) throws SQLException { checkClosed(); - return null; + throw new SQLFeatureNotSupportedException("SQLXML is not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public SQLXML getSQLXML(String columnLabel) throws SQLException { checkClosed(); - return null; + throw new SQLFeatureNotSupportedException("SQLXML is not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateSQLXML(int columnIndex, SQLXML xmlObject) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateSQLXML(String columnLabel, SQLXML xmlObject) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override @@ -1346,181 +1347,189 @@ public Reader getNCharacterStream(String columnLabel) throws SQLException { @Override public void updateNCharacterStream(int columnIndex, Reader x, long length) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateNCharacterStream(String columnLabel, Reader reader, long length) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateAsciiStream(int columnIndex, InputStream x, long length) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateBinaryStream(int columnIndex, InputStream x, long length) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateCharacterStream(int columnIndex, Reader x, long length) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateAsciiStream(String columnLabel, InputStream x, long length) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateBinaryStream(String columnLabel, InputStream x, long length) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateCharacterStream(String columnLabel, Reader reader, long length) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateBlob(int columnIndex, InputStream inputStream, long length) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateBlob(String columnLabel, InputStream inputStream, long length) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateClob(int columnIndex, Reader reader, long length) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateClob(String columnLabel, Reader reader, long length) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateNClob(int columnIndex, Reader reader, long length) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateNClob(String columnLabel, Reader reader, long length) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateNCharacterStream(int columnIndex, Reader x) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateNCharacterStream(String columnLabel, Reader reader) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateAsciiStream(int columnIndex, InputStream x) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateBinaryStream(int columnIndex, InputStream x) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateCharacterStream(int columnIndex, Reader x) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateAsciiStream(String columnLabel, InputStream x) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateBinaryStream(String columnLabel, InputStream x) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateCharacterStream(String columnLabel, Reader reader) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateBlob(int columnIndex, InputStream inputStream) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateBlob(String columnLabel, InputStream inputStream) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateClob(int columnIndex, Reader reader) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateClob(String columnLabel, Reader reader) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateNClob(int columnIndex, Reader reader) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public void updateNClob(String columnLabel, Reader reader) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Writes are not supported."); + throw new SQLFeatureNotSupportedException("Writes are not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public T getObject(int columnIndex, Class type) throws SQLException { checkClosed(); - return null; + try { + return (T) reader.readValue(columnIndex); + } catch (Exception e) { + throw ExceptionUtils.toSqlState(e); + } } @Override public T getObject(String columnLabel, Class type) throws SQLException { checkClosed(); - return null; + try { + return (T) reader.readValue(columnLabel); + } catch (Exception e) { + throw ExceptionUtils.toSqlState(e); + } } @Override diff --git a/jdbc-v2/src/main/java/com/clickhouse/jdbc/StatementImpl.java b/jdbc-v2/src/main/java/com/clickhouse/jdbc/StatementImpl.java index faa385f9f..e0aa3c212 100644 --- a/jdbc-v2/src/main/java/com/clickhouse/jdbc/StatementImpl.java +++ b/jdbc-v2/src/main/java/com/clickhouse/jdbc/StatementImpl.java @@ -6,6 +6,7 @@ import com.clickhouse.client.api.query.QueryResponse; import com.clickhouse.client.api.query.QuerySettings; import com.clickhouse.jdbc.internal.JdbcUtils; +import com.clickhouse.jdbc.internal.ExceptionUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -18,7 +19,6 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; -import java.util.UUID; import java.util.concurrent.TimeUnit; public class StatementImpl implements Statement, JdbcV2Wrapper { @@ -46,7 +46,7 @@ public StatementImpl(ConnectionImpl connection) throws SQLException { protected void checkClosed() throws SQLException { if (closed) { - throw new SQLException("Statement is closed"); + throw new SQLException("Statement is closed", ExceptionUtils.SQL_STATE_CONNECTION_EXCEPTION); } } @@ -130,7 +130,7 @@ public ResultSetImpl executeQuery(String sql, QuerySettings settings) throws SQL metrics = response.getMetrics(); lastQueryId = response.getQueryId(); } catch (Exception e) { - throw new SQLException(e); + throw ExceptionUtils.toSqlState(e); } return currentResultSet; @@ -147,7 +147,7 @@ public int executeUpdate(String sql, QuerySettings settings) throws SQLException checkClosed(); if (parseStatementType(sql) == StatementType.SELECT) { - throw new SQLException("executeUpdate() cannot be called with a SELECT statement"); + throw new SQLException("executeUpdate() cannot be called with a SELECT statement", ExceptionUtils.SQL_STATE_SQL_ERROR); } QuerySettings mergedSettings = QuerySettings.merge(connection.getDefaultQuerySettings(), settings); @@ -184,7 +184,7 @@ public int getMaxFieldSize() throws SQLException { @Override public void setMaxFieldSize(int max) throws SQLException { checkClosed(); - throw new SQLFeatureNotSupportedException("Set max field size is not supported."); + throw new SQLFeatureNotSupportedException("Set max field size is not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override @@ -311,7 +311,7 @@ public boolean getMoreResults() throws SQLException { @Override public void setFetchDirection(int direction) throws SQLException { checkClosed(); - throw new UnsupportedOperationException("Fetch direction is not supported."); + throw new SQLFeatureNotSupportedException("Set fetch direction is not supported.", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override diff --git a/jdbc-v2/src/main/java/com/clickhouse/jdbc/internal/ExceptionUtils.java b/jdbc-v2/src/main/java/com/clickhouse/jdbc/internal/ExceptionUtils.java new file mode 100644 index 000000000..87ec5aeb8 --- /dev/null +++ b/jdbc-v2/src/main/java/com/clickhouse/jdbc/internal/ExceptionUtils.java @@ -0,0 +1,50 @@ +package com.clickhouse.jdbc.internal; + +import com.clickhouse.client.api.ClientException; +import com.clickhouse.client.api.ClientMisconfigurationException; +import com.clickhouse.client.api.ConnectionInitiationException; +import com.clickhouse.client.api.ServerException; + +import java.sql.SQLException; + +/** + * Helper class for building {@link SQLException}. + */ +public final class ExceptionUtils { + public static final String SQL_STATE_CLIENT_ERROR = "HY000"; + public static final String SQL_STATE_OPERATION_CANCELLED = "HY008"; + public static final String SQL_STATE_CONNECTION_EXCEPTION = "08000"; + public static final String SQL_STATE_SQL_ERROR = "07000"; + public static final String SQL_STATE_NO_DATA = "02000"; + public static final String SQL_STATE_INVALID_SCHEMA = "3F000"; + public static final String SQL_STATE_INVALID_TX_STATE = "25000"; + public static final String SQL_STATE_DATA_EXCEPTION = "22000"; + public static final String SQL_STATE_FEATURE_NOT_SUPPORTED = "0A000"; + + private ExceptionUtils() {}//Private constructor + + // https://en.wikipedia.org/wiki/SQLSTATE + + /** + * Convert a {@link Exception} to a {@link SQLException}. + * @param e {@link Exception} to convert + * @return Converted {@link SQLException} + */ + public static SQLException toSqlState(Exception e) { + if (e == null) { + return new SQLException("Unknown client error", SQL_STATE_CLIENT_ERROR); + } else if (e instanceof SQLException) { + return (SQLException) e; + } else if (e instanceof ClientMisconfigurationException) { + return new SQLException(e.getMessage(), SQL_STATE_CLIENT_ERROR, e); + } else if (e instanceof ConnectionInitiationException) { + return new SQLException(e.getMessage(), SQL_STATE_CONNECTION_EXCEPTION, e); + } else if (e instanceof ServerException) { + return new SQLException(e.getMessage(), SQL_STATE_DATA_EXCEPTION, e); + } else if (e instanceof ClientException) { + return new SQLException(e.getMessage(), SQL_STATE_CLIENT_ERROR, e); + } + + return new SQLException(e.getMessage(), SQL_STATE_CLIENT_ERROR, e);//Default + } +} diff --git a/jdbc-v2/src/main/java/com/clickhouse/jdbc/metadata/DatabaseMetaData.java b/jdbc-v2/src/main/java/com/clickhouse/jdbc/metadata/DatabaseMetaData.java index a89f2450b..f99ae17ae 100644 --- a/jdbc-v2/src/main/java/com/clickhouse/jdbc/metadata/DatabaseMetaData.java +++ b/jdbc-v2/src/main/java/com/clickhouse/jdbc/metadata/DatabaseMetaData.java @@ -6,6 +6,7 @@ import com.clickhouse.jdbc.JdbcV2Wrapper; import com.clickhouse.jdbc.internal.ClientInfoProperties; import com.clickhouse.jdbc.internal.JdbcUtils; +import com.clickhouse.jdbc.internal.ExceptionUtils; import com.clickhouse.logging.Logger; import com.clickhouse.logging.LoggerFactory; @@ -13,9 +14,8 @@ import java.sql.ResultSet; import java.sql.RowIdLifetime; import java.sql.SQLException; +import java.sql.SQLFeatureNotSupportedException; import java.util.Arrays; -import java.util.List; -import java.util.stream.Collectors; public class DatabaseMetaData implements java.sql.DatabaseMetaData, JdbcV2Wrapper { private static final Logger log = LoggerFactory.getLogger(DatabaseMetaData.class); @@ -31,9 +31,9 @@ public class DatabaseMetaData implements java.sql.DatabaseMetaData, JdbcV2Wrappe * @param connection - connection for which metadata is created * @param useCatalogs - if true then getCatalogs() will return non-empty list (not implemented yet) */ - public DatabaseMetaData(ConnectionImpl connection, boolean useCatalogs) { + public DatabaseMetaData(ConnectionImpl connection, boolean useCatalogs) throws SQLFeatureNotSupportedException { if (useCatalogs) { - throw new UnsupportedOperationException("Catalogs are not supported yet"); + throw new SQLFeatureNotSupportedException("Catalogs are not supported yet", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } this.connection = connection; this.useCatalogs = useCatalogs; @@ -52,17 +52,29 @@ public boolean allTablesAreSelectable() throws SQLException { @Override public String getURL() throws SQLException { - return connection.getURL(); + try { + return connection.getURL(); + } catch (Exception e) { + throw ExceptionUtils.toSqlState(e); + } } @Override public String getUserName() throws SQLException { - return connection.getUser(); + try { + return connection.getUser(); + } catch (Exception e) { + throw ExceptionUtils.toSqlState(e); + } } @Override public boolean isReadOnly() throws SQLException { - return connection.isReadOnly(); + try { + return connection.isReadOnly(); + } catch (Exception e) { + throw ExceptionUtils.toSqlState(e); + } } @@ -95,7 +107,11 @@ public String getDatabaseProductName() throws SQLException { @Override public String getDatabaseProductVersion() throws SQLException { - return connection.getServerVersion(); + try { + return connection.getServerVersion(); + } catch (Exception e) { + throw ExceptionUtils.toSqlState(e); + } } @Override @@ -624,7 +640,11 @@ public int getMaxUserNameLength() throws SQLException { @Override public int getDefaultTransactionIsolation() throws SQLException { - return connection.getTransactionIsolation(); + try { + return connection.getTransactionIsolation(); + } catch (Exception e) { + throw ExceptionUtils.toSqlState(e); + } } @Override @@ -634,7 +654,11 @@ public boolean supportsTransactions() throws SQLException { @Override public boolean supportsTransactionIsolationLevel(int level) throws SQLException { - return level == connection.getTransactionIsolation(); + try { + return level == connection.getTransactionIsolation(); + } catch (Exception e) { + throw ExceptionUtils.toSqlState(e); + } } @Override @@ -668,7 +692,11 @@ public ResultSet getProcedures(String catalog, String schemaPattern, String proc "'' AS SPECIFIC_NAME " + "LIMIT 0"; log.info("getProcedures: {}", sql); - return connection.createStatement().executeQuery(sql); + try { + return connection.createStatement().executeQuery(sql); + } catch (Exception e) { + throw ExceptionUtils.toSqlState(e); + } } @Override @@ -695,7 +723,11 @@ public ResultSet getProcedureColumns(String catalog, String schemaPattern, Strin "'' AS IS_NULLABLE, " + "'' AS SPECIFIC_NAME " + "LIMIT 0"; - return connection.createStatement().executeQuery(sql); + try { + return connection.createStatement().executeQuery(sql); + } catch (Exception e) { + throw ExceptionUtils.toSqlState(e); + } } /** @@ -733,7 +765,12 @@ public ResultSet getTables(String catalog, String schemaPattern, String tableNam } sql = sql.substring(0, sql.length() - 1) + ") "; } - return connection.createStatement().executeQuery(sql); + + try { + return connection.createStatement().executeQuery(sql); + } catch (Exception e) { + throw ExceptionUtils.toSqlState(e); + } } /** @@ -747,7 +784,11 @@ public ResultSet getTables(String catalog, String schemaPattern, String tableNam @Override public ResultSet getSchemas() throws SQLException { // TODO: handle useCatalogs == true and return schema catalog name - return connection.createStatement().executeQuery("SELECT name AS TABLE_SCHEM, " + catalogPlaceholder + " AS TABLE_CATALOG FROM system.databases ORDER BY name"); + try { + return connection.createStatement().executeQuery("SELECT name AS TABLE_SCHEM, " + catalogPlaceholder + " AS TABLE_CATALOG FROM system.databases ORDER BY name"); + } catch (Exception e) { + throw ExceptionUtils.toSqlState(e); + } } /** @@ -758,7 +799,11 @@ public ResultSet getSchemas() throws SQLException { */ @Override public ResultSet getCatalogs() throws SQLException { - return connection.createStatement().executeQuery("SELECT 'local' AS TABLE_CAT " + (useCatalogs ? "" : " WHERE 1 = 0")); + try { + return connection.createStatement().executeQuery("SELECT 'local' AS TABLE_CAT " + (useCatalogs ? "" : " WHERE 1 = 0")); + } catch (Exception e) { + throw ExceptionUtils.toSqlState(e); + } } /** @@ -768,7 +813,11 @@ public ResultSet getCatalogs() throws SQLException { */ @Override public ResultSet getTableTypes() throws SQLException { - return connection.createStatement().executeQuery("SELECT name AS TABLE_TYPE FROM system.table_engines"); + try { + return connection.createStatement().executeQuery("SELECT name AS TABLE_TYPE FROM system.table_engines"); + } catch (Exception e) { + throw ExceptionUtils.toSqlState(e); + } } @Override @@ -808,67 +857,107 @@ public ResultSet getColumns(String catalog, String schemaPattern, String tableNa " ORDER BY TABLE_SCHEM, TABLE_NAME, ORDINAL_POSITION"; log.info("getColumns: {}", sql); - return connection.createStatement().executeQuery(sql); + try { + return connection.createStatement().executeQuery(sql); + } catch (Exception e) { + throw ExceptionUtils.toSqlState(e); + } } @Override public ResultSet getColumnPrivileges(String catalog, String schema, String table, String columnNamePattern) throws SQLException { //Return an empty result set with the required columns log.warn("getColumnPrivileges is not supported and may return invalid results"); - return connection.createStatement().executeQuery("SELECT NULL AS TABLE_CAT, NULL AS TABLE_SCHEM, NULL AS TABLE_NAME, NULL AS COLUMN_NAME, NULL AS GRANTOR, NULL AS GRANTEE, NULL AS PRIVILEGE, NULL AS IS_GRANTABLE"); + try { + return connection.createStatement().executeQuery("SELECT NULL AS TABLE_CAT, NULL AS TABLE_SCHEM, NULL AS TABLE_NAME, NULL AS COLUMN_NAME, NULL AS GRANTOR, NULL AS GRANTEE, NULL AS PRIVILEGE, NULL AS IS_GRANTABLE"); + } catch (Exception e) { + throw ExceptionUtils.toSqlState(e); + } } @Override public ResultSet getTablePrivileges(String catalog, String schemaPattern, String tableNamePattern) throws SQLException { //Return an empty result set with the required columns log.warn("getTablePrivileges is not supported and may return invalid results"); - return connection.createStatement().executeQuery("SELECT NULL AS TABLE_CAT, NULL AS TABLE_SCHEM, NULL AS TABLE_NAME, NULL AS GRANTOR, NULL AS GRANTEE, NULL AS PRIVILEGE, NULL AS IS_GRANTABLE"); + try { + return connection.createStatement().executeQuery("SELECT NULL AS TABLE_CAT, NULL AS TABLE_SCHEM, NULL AS TABLE_NAME, NULL AS GRANTOR, NULL AS GRANTEE, NULL AS PRIVILEGE, NULL AS IS_GRANTABLE"); + } catch (Exception e) { + throw ExceptionUtils.toSqlState(e); + } } @Override public ResultSet getBestRowIdentifier(String catalog, String schema, String table, int scope, boolean nullable) throws SQLException { //Return an empty result set with the required columns log.warn("getBestRowIdentifier is not supported and may return invalid results"); - return connection.createStatement().executeQuery("SELECT NULL AS SCOPE, NULL AS COLUMN_NAME, NULL AS DATA_TYPE, NULL AS TYPE_NAME, NULL AS COLUMN_SIZE, NULL AS BUFFER_LENGTH, NULL AS DECIMAL_DIGITS, NULL AS PSEUDO_COLUMN"); + try { + return connection.createStatement().executeQuery("SELECT NULL AS SCOPE, NULL AS COLUMN_NAME, NULL AS DATA_TYPE, NULL AS TYPE_NAME, NULL AS COLUMN_SIZE, NULL AS BUFFER_LENGTH, NULL AS DECIMAL_DIGITS, NULL AS PSEUDO_COLUMN"); + } catch (Exception e) { + throw ExceptionUtils.toSqlState(e); + } } @Override public ResultSet getVersionColumns(String catalog, String schema, String table) throws SQLException { //Return an empty result set with the required columns log.warn("getVersionColumns is not supported and may return invalid results"); - return connection.createStatement().executeQuery("SELECT NULL AS SCOPE, NULL AS COLUMN_NAME, NULL AS DATA_TYPE, NULL AS TYPE_NAME, NULL AS COLUMN_SIZE, NULL AS BUFFER_LENGTH, NULL AS DECIMAL_DIGITS, NULL AS PSEUDO_COLUMN"); + try { + return connection.createStatement().executeQuery("SELECT NULL AS SCOPE, NULL AS COLUMN_NAME, NULL AS DATA_TYPE, NULL AS TYPE_NAME, NULL AS COLUMN_SIZE, NULL AS BUFFER_LENGTH, NULL AS DECIMAL_DIGITS, NULL AS PSEUDO_COLUMN"); + } catch (Exception e) { + throw ExceptionUtils.toSqlState(e); + } } @Override public ResultSet getPrimaryKeys(String catalog, String schema, String table) throws SQLException { //Return an empty result set with the required columns log.warn("getPrimaryKeys is not supported and may return invalid results"); - 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"); + try { + 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"); + } catch (Exception e) { + throw ExceptionUtils.toSqlState(e); + } } @Override public ResultSet getImportedKeys(String catalog, String schema, String table) throws SQLException { //Return an empty result set with the required columns log.warn("getImportedKeys is not supported and may return invalid results"); - 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"); + try { + 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"); + } catch (Exception e) { + throw ExceptionUtils.toSqlState(e); + } } @Override public ResultSet getExportedKeys(String catalog, String schema, String table) throws SQLException { //Return an empty result set with the required columns log.warn("getExportedKeys is not supported and may return invalid results"); - 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"); + try { + 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"); + } catch (Exception e) { + throw ExceptionUtils.toSqlState(e); + } } @Override public ResultSet getCrossReference(String parentCatalog, String parentSchema, String parentTable, String foreignCatalog, String foreignSchema, String foreignTable) throws SQLException { //Return an empty result set with the required columns log.warn("getCrossReference is not supported and may return invalid results"); - 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"); + try { + 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"); + } catch (Exception e) { + throw ExceptionUtils.toSqlState(e); + } } @Override public ResultSet getTypeInfo() throws SQLException { - return connection.createStatement().executeQuery(DATA_TYPE_INFO_SQL); + try { + return connection.createStatement().executeQuery(DATA_TYPE_INFO_SQL); + } catch (Exception e) { + throw ExceptionUtils.toSqlState(e); + } } private static final String DATA_TYPE_INFO_SQL = getDataTypeInfoSql(); @@ -991,7 +1080,11 @@ public boolean supportsBatchUpdates() throws SQLException { public ResultSet getUDTs(String catalog, String schemaPattern, String typeNamePattern, int[] types) throws SQLException { //Return an empty result set with the required columns log.warn("getUDTs is not supported and may return invalid results"); - return connection.createStatement().executeQuery("SELECT NULL AS TYPE_CAT, NULL AS TYPE_SCHEM, NULL AS TYPE_NAME, NULL AS CLASS_NAME, NULL AS DATA_TYPE, NULL AS REMARKS, NULL AS BASE_TYPE"); + try { + return connection.createStatement().executeQuery("SELECT NULL AS TYPE_CAT, NULL AS TYPE_SCHEM, NULL AS TYPE_NAME, NULL AS CLASS_NAME, NULL AS DATA_TYPE, NULL AS REMARKS, NULL AS BASE_TYPE"); + } catch (Exception e) { + throw ExceptionUtils.toSqlState(e); + } } @Override @@ -1026,21 +1119,33 @@ public boolean supportsGetGeneratedKeys() throws SQLException { public ResultSet getSuperTypes(String catalog, String schemaPattern, String typeNamePattern) throws SQLException { //Return an empty result set with the required columns log.warn("getSuperTypes is not supported and may return invalid results"); - return connection.createStatement().executeQuery("SELECT NULL AS TYPE_CAT, NULL AS TYPE_SCHEM, NULL AS TYPE_NAME, NULL AS SUPERTYPE_CAT, NULL AS SUPERTYPE_SCHEM, NULL AS SUPERTYPE_NAME"); + try { + return connection.createStatement().executeQuery("SELECT NULL AS TYPE_CAT, NULL AS TYPE_SCHEM, NULL AS TYPE_NAME, NULL AS SUPERTYPE_CAT, NULL AS SUPERTYPE_SCHEM, NULL AS SUPERTYPE_NAME"); + } catch (Exception e) { + throw ExceptionUtils.toSqlState(e); + } } @Override public ResultSet getSuperTables(String catalog, String schemaPattern, String tableNamePattern) throws SQLException { //Return an empty result set with the required columns log.warn("getSuperTables is not supported and may return invalid results"); - return connection.createStatement().executeQuery("SELECT NULL AS TABLE_CAT, NULL AS TABLE_SCHEM, NULL AS TABLE_NAME, NULL AS SUPERTABLE_NAME"); + try { + return connection.createStatement().executeQuery("SELECT NULL AS TABLE_CAT, NULL AS TABLE_SCHEM, NULL AS TABLE_NAME, NULL AS SUPERTABLE_NAME"); + } catch (Exception e) { + throw ExceptionUtils.toSqlState(e); + } } @Override public ResultSet getAttributes(String catalog, String schemaPattern, String typeNamePattern, String attributeNamePattern) throws SQLException { //Return an empty result set with the required columns log.warn("getAttributes is not supported and may return invalid results"); - return connection.createStatement().executeQuery("SELECT NULL AS TYPE_CAT, NULL AS TYPE_SCHEM, NULL AS TYPE_NAME, NULL AS ATTR_NAME, NULL AS DATA_TYPE, NULL AS ATTR_TYPE_NAME, NULL AS ATTR_SIZE, NULL AS DECIMAL_DIGITS, NULL AS NUM_PREC_RADIX, NULL AS NULLABLE, NULL AS REMARKS, NULL AS ATTR_DEF, NULL AS SQL_DATA_TYPE, NULL AS SQL_DATETIME_SUB, NULL AS CHAR_OCTET_LENGTH, NULL AS ORDINAL_POSITION, NULL AS IS_NULLABLE, NULL AS SCOPE_CATALOG, NULL AS SCOPE_SCHEMA, NULL AS SCOPE_TABLE, NULL AS SOURCE_DATA_TYPE"); + try { + return connection.createStatement().executeQuery("SELECT NULL AS TYPE_CAT, NULL AS TYPE_SCHEM, NULL AS TYPE_NAME, NULL AS ATTR_NAME, NULL AS DATA_TYPE, NULL AS ATTR_TYPE_NAME, NULL AS ATTR_SIZE, NULL AS DECIMAL_DIGITS, NULL AS NUM_PREC_RADIX, NULL AS NULLABLE, NULL AS REMARKS, NULL AS ATTR_DEF, NULL AS SQL_DATA_TYPE, NULL AS SQL_DATETIME_SUB, NULL AS CHAR_OCTET_LENGTH, NULL AS ORDINAL_POSITION, NULL AS IS_NULLABLE, NULL AS SCOPE_CATALOG, NULL AS SCOPE_SCHEMA, NULL AS SCOPE_TABLE, NULL AS SOURCE_DATA_TYPE"); + } catch (Exception e) { + throw ExceptionUtils.toSqlState(e); + } } @Override @@ -1060,7 +1165,7 @@ public int getDatabaseMajorVersion() throws SQLException { return Integer.parseInt(version.split("\\.")[0]); } catch (NumberFormatException e) { log.error("Failed to parse major version from server version: " + version, e); - throw new SQLException("Failed to parse major version from server version: " + version); + throw new SQLException("Failed to parse major version from server version: " + version, ExceptionUtils.SQL_STATE_CLIENT_ERROR, e); } } @@ -1071,7 +1176,7 @@ public int getDatabaseMinorVersion() throws SQLException { return Integer.parseInt(version.split("\\.")[1]); } catch (NumberFormatException e) { log.error("Failed to parse minor version from server version: " + version, e); - throw new SQLException("Failed to parse minor version from server version: " + version); + throw new SQLException("Failed to parse minor version from server version: " + version, ExceptionUtils.SQL_STATE_CLIENT_ERROR, e); } } @@ -1108,8 +1213,12 @@ public RowIdLifetime getRowIdLifetime() throws SQLException { @Override public ResultSet getSchemas(String catalog, String schemaPattern) throws SQLException { // TODO: handle useCatalogs == true and return schema catalog name - return connection.createStatement().executeQuery("SELECT name AS TABLE_SCHEM, " + catalogPlaceholder + " AS TABLE_CATALOG FROM system.databases " + - "WHERE name LIKE '" + (schemaPattern == null ? "%" : schemaPattern) + "'"); + try { + return connection.createStatement().executeQuery("SELECT name AS TABLE_SCHEM, " + catalogPlaceholder + " AS TABLE_CATALOG FROM system.databases " + + "WHERE name LIKE '" + (schemaPattern == null ? "%" : schemaPattern) + "'"); + } catch (Exception e) { + throw ExceptionUtils.toSqlState(e); + } } @Override @@ -1140,8 +1249,11 @@ private static String getClientInfoPropertiesSql() { @Override public ResultSet getClientInfoProperties() throws SQLException { - - return connection.createStatement().executeQuery(CLIENT_INFO_PROPERTIES_SQL); + try { + return connection.createStatement().executeQuery(CLIENT_INFO_PROPERTIES_SQL); + } catch (Exception e) { + throw ExceptionUtils.toSqlState(e); + } } @Override @@ -1156,7 +1268,12 @@ public ResultSet getFunctions(String catalog, String schemaPattern, String funct "FROM system.functions " + "WHERE name LIKE '" + (functionNamePattern == null ? "%" : functionNamePattern) + "'"; log.info("getFunctions: " + sql); - return connection.createStatement().executeQuery(sql); + + try { + return connection.createStatement().executeQuery(sql); + } catch (Exception e) { + throw ExceptionUtils.toSqlState(e); + } } @Override @@ -1181,7 +1298,11 @@ public ResultSet getFunctionColumns(String catalog, String schemaPattern, String "'' AS SPECIFIC_NAME " + "LIMIT 0"; - return connection.createStatement().executeQuery(sql); + try { + return connection.createStatement().executeQuery(sql); + } catch (Exception e) { + throw ExceptionUtils.toSqlState(e); + } } @Override @@ -1201,7 +1322,11 @@ public ResultSet getPseudoColumns(String catalog, String schemaPattern, String t "'' AS IS_NULLABLE " + " LIMIT 0"; - return connection.createStatement().executeQuery(sql); + try { + return connection.createStatement().executeQuery(sql); + } catch (Exception e) { + throw ExceptionUtils.toSqlState(e); + } } @Override diff --git a/jdbc-v2/src/main/java/com/clickhouse/jdbc/metadata/ParameterMetaData.java b/jdbc-v2/src/main/java/com/clickhouse/jdbc/metadata/ParameterMetaData.java index 347dd4824..c91a17d42 100644 --- a/jdbc-v2/src/main/java/com/clickhouse/jdbc/metadata/ParameterMetaData.java +++ b/jdbc-v2/src/main/java/com/clickhouse/jdbc/metadata/ParameterMetaData.java @@ -3,6 +3,7 @@ import com.clickhouse.data.ClickHouseColumn; import com.clickhouse.jdbc.JdbcV2Wrapper; import com.clickhouse.jdbc.internal.JdbcUtils; +import com.clickhouse.jdbc.internal.ExceptionUtils; import java.sql.SQLException; import java.util.List; @@ -10,9 +11,9 @@ public class ParameterMetaData implements java.sql.ParameterMetaData, JdbcV2Wrapper { private final List params; - protected ParameterMetaData(List params) { + protected ParameterMetaData(List params) throws SQLException { if (params == null) { - throw new IllegalArgumentException("Parameters array cannot be null."); + throw ExceptionUtils.toSqlState(new IllegalArgumentException("Parameters array cannot be null.")); } this.params = params; @@ -20,7 +21,7 @@ protected ParameterMetaData(List params) { protected ClickHouseColumn getParam(int param) throws SQLException { if (param < 1 || param > params.size()) { - throw new SQLException("Parameter index out of range: " + param); + throw new SQLException("Parameter index out of range: " + param, ExceptionUtils.SQL_STATE_CLIENT_ERROR); } return params.get(param - 1); @@ -28,44 +29,76 @@ protected ClickHouseColumn getParam(int param) throws SQLException { @Override public int getParameterCount() throws SQLException { - return params.size(); + try { + return params.size(); + } catch (Exception e) { + throw ExceptionUtils.toSqlState(e); + } } @Override public int isNullable(int param) throws SQLException { - return getParam(param).isNullable() ? parameterNullable : parameterNoNulls; + try { + return getParam(param).isNullable() ? parameterNullable : parameterNoNulls; + } catch (Exception e) { + throw ExceptionUtils.toSqlState(e); + } } @Override public boolean isSigned(int param) throws SQLException { - return getParam(param).getDataType().isSigned(); + try{ + return getParam(param).getDataType().isSigned(); + } catch (Exception e) { + throw ExceptionUtils.toSqlState(e); + } } @Override public int getPrecision(int param) throws SQLException { - return getParam(param).getPrecision(); + try { + return getParam(param).getPrecision(); + } catch (Exception e) { + throw ExceptionUtils.toSqlState(e); + } } @Override public int getScale(int param) throws SQLException { - return getParam(param).getScale(); + try { + return getParam(param).getScale(); + } catch (Exception e) { + throw ExceptionUtils.toSqlState(e); + } } @Override public int getParameterType(int param) throws SQLException { //TODO: Should we implement .getSQLType()? - return JdbcUtils.convertToSqlType(getParam(param).getDataType()); + try { + return JdbcUtils.convertToSqlType(getParam(param).getDataType()); + } catch (Exception e) { + throw ExceptionUtils.toSqlState(e); + } } @Override public String getParameterTypeName(int param) throws SQLException { - return getParam(param).getDataType().name(); + try { + return getParam(param).getDataType().name(); + } catch (Exception e) { + throw ExceptionUtils.toSqlState(e); + } } @Override public String getParameterClassName(int param) throws SQLException { //TODO: Should we implement .getClassName()? - return getParam(param).getDataType().getObjectClass().getName(); + try { + return getParam(param).getDataType().getObjectClass().getName(); + } catch (Exception e) { + throw ExceptionUtils.toSqlState(e); + } } @Override diff --git a/jdbc-v2/src/main/java/com/clickhouse/jdbc/metadata/ResultSetMetaData.java b/jdbc-v2/src/main/java/com/clickhouse/jdbc/metadata/ResultSetMetaData.java index acbca2cf4..d0be7d12f 100644 --- a/jdbc-v2/src/main/java/com/clickhouse/jdbc/metadata/ResultSetMetaData.java +++ b/jdbc-v2/src/main/java/com/clickhouse/jdbc/metadata/ResultSetMetaData.java @@ -7,6 +7,7 @@ import com.clickhouse.jdbc.JdbcV2Wrapper; import com.clickhouse.jdbc.ResultSetImpl; import com.clickhouse.jdbc.internal.JdbcUtils; +import com.clickhouse.jdbc.internal.ExceptionUtils; public class ResultSetMetaData implements java.sql.ResultSetMetaData, JdbcV2Wrapper { private final ResultSetImpl resultSet; @@ -16,7 +17,7 @@ public ResultSetMetaData(ResultSetImpl resultSet) { private ClickHouseColumn getColumn(int column) throws SQLException { if (column < 1 || column > getColumnCount()) { - throw new SQLException("Column index out of range: " + column); + throw new SQLException("Column index out of range: " + column, ExceptionUtils.SQL_STATE_CLIENT_ERROR); } return resultSet.getSchema().getColumns().get(column - 1); } @@ -27,7 +28,7 @@ public int getColumnCount() throws SQLException { TableSchema schema = resultSet.getSchema(); return schema.getColumns().size(); } catch (Exception e) { - throw new SQLException(e); + throw ExceptionUtils.toSqlState(e); } } @@ -53,12 +54,20 @@ public boolean isCurrency(int column) throws SQLException { @Override public int isNullable(int column) throws SQLException { - return getColumn(column).isNullable() ? columnNullable : columnNoNulls; + try { + return getColumn(column).isNullable() ? columnNullable : columnNoNulls; + } catch (Exception e) { + throw ExceptionUtils.toSqlState(e); + } } @Override public boolean isSigned(int column) throws SQLException { - return getColumn(column).getDataType().isSigned(); + try { + return getColumn(column).getDataType().isSigned(); + } catch (Exception e) { + throw ExceptionUtils.toSqlState(e); + } } @Override @@ -68,12 +77,20 @@ public int getColumnDisplaySize(int column) throws SQLException { @Override public String getColumnLabel(int column) throws SQLException { - return getColumn(column).getColumnName(); + try { + return getColumn(column).getColumnName(); + } catch (Exception e) { + throw ExceptionUtils.toSqlState(e); + } } @Override public String getColumnName(int column) throws SQLException { - return getColumn(column).getColumnName(); + try { + return getColumn(column).getColumnName(); + } catch (Exception e) { + throw ExceptionUtils.toSqlState(e); + } } @Override @@ -83,17 +100,29 @@ public String getSchemaName(int column) throws SQLException { @Override public int getPrecision(int column) throws SQLException { - return getColumn(column).getPrecision(); + try { + return getColumn(column).getPrecision(); + } catch (Exception e) { + throw ExceptionUtils.toSqlState(e); + } } @Override public int getScale(int column) throws SQLException { - return getColumn(column).getScale(); + try { + return getColumn(column).getScale(); + } catch (Exception e) { + throw ExceptionUtils.toSqlState(e); + } } @Override public String getTableName(int column) throws SQLException { - return resultSet.getSchema().getTableName(); + try { + return resultSet.getSchema().getTableName(); + } catch (Exception e) { + throw ExceptionUtils.toSqlState(e); + } } @Override @@ -103,12 +132,20 @@ public String getCatalogName(int column) throws SQLException { @Override public int getColumnType(int column) throws SQLException { - return JdbcUtils.convertToSqlType(getColumn(column).getDataType()); + try { + return JdbcUtils.convertToSqlType(getColumn(column).getDataType()); + } catch (Exception e) { + throw ExceptionUtils.toSqlState(e); + } } @Override public String getColumnTypeName(int column) throws SQLException { - return getColumn(column).getDataType().name(); + try { + return getColumn(column).getDataType().name(); + } catch (Exception e) { + throw ExceptionUtils.toSqlState(e); + } } @Override @@ -128,6 +165,6 @@ public boolean isDefinitelyWritable(int column) throws SQLException { @Override public String getColumnClassName(int column) throws SQLException { - throw new UnsupportedOperationException("Not implemented yet."); + throw new SQLException("Not implemented", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } } diff --git a/jdbc-v2/src/main/java/com/clickhouse/jdbc/types/Array.java b/jdbc-v2/src/main/java/com/clickhouse/jdbc/types/Array.java index 89a13ceec..bdd710884 100644 --- a/jdbc-v2/src/main/java/com/clickhouse/jdbc/types/Array.java +++ b/jdbc-v2/src/main/java/com/clickhouse/jdbc/types/Array.java @@ -1,5 +1,6 @@ package com.clickhouse.jdbc.types; +import com.clickhouse.jdbc.internal.ExceptionUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -15,9 +16,9 @@ public class Array implements java.sql.Array { Object[] array; int type; //java.sql.Types - public Array(List list) { + public Array(List list) throws SQLException { if (list == null) { - throw new IllegalArgumentException("List cannot be null"); + throw ExceptionUtils.toSqlState(new IllegalArgumentException("List cannot be null")); } this.array = list.toArray(); @@ -41,7 +42,7 @@ public Object getArray() throws SQLException { @Override public Object getArray(Map> map) throws SQLException { - throw new SQLFeatureNotSupportedException("getArray(Map>) is not supported"); + throw new SQLFeatureNotSupportedException("getArray(Map>) is not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override @@ -52,33 +53,33 @@ public Object getArray(long index, int count) throws SQLException { return smallerArray; } catch (Exception e) { log.error("Failed to get array", e); - throw new SQLException(e); + throw new SQLException(e.getMessage(), ExceptionUtils.SQL_STATE_CLIENT_ERROR, e); } } @Override public Object getArray(long index, int count, Map> map) throws SQLException { - throw new SQLFeatureNotSupportedException("getArray(long, int, Map>) is not supported"); + throw new SQLFeatureNotSupportedException("getArray(long, int, Map>) is not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public ResultSet getResultSet() throws SQLException { - throw new SQLFeatureNotSupportedException("getResultSet() is not supported"); + throw new SQLFeatureNotSupportedException("getResultSet() is not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public ResultSet getResultSet(Map> map) throws SQLException { - throw new SQLFeatureNotSupportedException("getResultSet(Map>) is not supported"); + throw new SQLFeatureNotSupportedException("getResultSet(Map>) is not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public ResultSet getResultSet(long index, int count) throws SQLException { - throw new SQLFeatureNotSupportedException("getResultSet(long, int) is not supported"); + throw new SQLFeatureNotSupportedException("getResultSet(long, int) is not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override public ResultSet getResultSet(long index, int count, Map> map) throws SQLException { - throw new SQLFeatureNotSupportedException("getResultSet(long, int, Map>) is not supported"); + throw new SQLFeatureNotSupportedException("getResultSet(long, int, Map>) is not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); } @Override diff --git a/jdbc-v2/src/test/java/com/clickhouse/jdbc/ConnectionTest.java b/jdbc-v2/src/test/java/com/clickhouse/jdbc/ConnectionTest.java index 4cadb4c2f..9d704a623 100644 --- a/jdbc-v2/src/test/java/com/clickhouse/jdbc/ConnectionTest.java +++ b/jdbc-v2/src/test/java/com/clickhouse/jdbc/ConnectionTest.java @@ -1,13 +1,12 @@ package com.clickhouse.jdbc; import java.sql.*; -import java.util.Properties; - -import com.clickhouse.client.api.ClientConfigProperties; import com.clickhouse.jdbc.internal.ClientInfoProperties; import org.testng.Assert; import org.testng.annotations.Test; +import static org.testng.Assert.assertThrows; + public class ConnectionTest extends JdbcIntegrationTest { @@ -17,8 +16,8 @@ public void createAndCloseStatementTest() throws SQLException { Statement statement = localConnection.createStatement(); Assert.assertNotNull(statement); - Assert.assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)); - Assert.assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.CLOSE_CURSORS_AT_COMMIT)); + assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)); + assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.CLOSE_CURSORS_AT_COMMIT)); } @Test(groups = { "integration" }) @@ -26,19 +25,19 @@ public void prepareStatementTest() throws SQLException { Connection localConnection = this.getJdbcConnection(); PreparedStatement statement = localConnection.prepareStatement("SELECT 1"); Assert.assertNotNull(statement); - Assert.assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.prepareStatement("SELECT 1", Statement.RETURN_GENERATED_KEYS)); - Assert.assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.prepareStatement("SELECT 1", new int[] { 1 })); - Assert.assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.prepareStatement("SELECT 1", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)); - Assert.assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.prepareStatement("SELECT 1", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.CLOSE_CURSORS_AT_COMMIT)); - Assert.assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.prepareStatement("SELECT 1", new String[] { "1" })); + assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.prepareStatement("SELECT 1", Statement.RETURN_GENERATED_KEYS)); + assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.prepareStatement("SELECT 1", new int[] { 1 })); + assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.prepareStatement("SELECT 1", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)); + assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.prepareStatement("SELECT 1", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.CLOSE_CURSORS_AT_COMMIT)); + assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.prepareStatement("SELECT 1", new String[] { "1" })); } @Test(groups = { "integration" }) public void prepareCallTest() throws SQLException { Connection localConnection = this.getJdbcConnection(); - Assert.assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.prepareCall("SELECT 1")); - Assert.assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.prepareCall("SELECT 1", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)); - Assert.assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.prepareCall("SELECT 1", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.CLOSE_CURSORS_AT_COMMIT)); + assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.prepareCall("SELECT 1")); + assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.prepareCall("SELECT 1", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)); + assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.prepareCall("SELECT 1", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.CLOSE_CURSORS_AT_COMMIT)); } @Test(groups = { "integration" }, enabled = false) @@ -52,7 +51,7 @@ public void nativeSQLTest() throws SQLException { @Test(groups = { "integration" }) public void setAutoCommitTest() throws SQLException { Connection localConnection = this.getJdbcConnection(); - Assert.assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.setAutoCommit(false)); + assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.setAutoCommit(false)); localConnection.setAutoCommit(true); } @@ -65,14 +64,14 @@ public void getAutoCommitTest() throws SQLException { @Test(groups = { "integration" }) public void commitTest() throws SQLException { Connection localConnection = this.getJdbcConnection(); - Assert.assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.commit()); + assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.commit()); } @Test(groups = { "integration" }) public void rollbackTest() throws SQLException { Connection localConnection = this.getJdbcConnection(); - Assert.assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.rollback()); - Assert.assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.rollback(null)); + assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.rollback()); + assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.rollback(null)); } @Test(groups = { "integration" }) @@ -81,8 +80,8 @@ public void closeTest() throws SQLException { Assert.assertFalse(localConnection.isClosed()); localConnection.close(); Assert.assertTrue(localConnection.isClosed()); - Assert.assertThrows(SQLException.class, localConnection::createStatement); - Assert.assertThrows(SQLException.class, () -> localConnection.prepareStatement("SELECT 1")); + assertThrows(SQLException.class, localConnection::createStatement); + assertThrows(SQLException.class, () -> localConnection.prepareStatement("SELECT 1")); } @Test(groups = { "integration" }) @@ -97,7 +96,7 @@ public void getMetaDataTest() throws SQLException { public void setReadOnlyTest() throws SQLException { Connection localConnection = this.getJdbcConnection(); localConnection.setReadOnly(false); - Assert.assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.setReadOnly(true)); + assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.setReadOnly(true)); } @Test(groups = { "integration" }) @@ -118,10 +117,10 @@ public void setTransactionIsolationTest() throws SQLException { Connection localConnection = this.getJdbcConnection(); localConnection.setTransactionIsolation(Connection.TRANSACTION_NONE); Assert.assertEquals(localConnection.getTransactionIsolation(), Connection.TRANSACTION_NONE); - Assert.assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED)); - Assert.assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED)); - Assert.assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ)); - Assert.assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE)); + assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED)); + assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED)); + assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ)); + assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE)); } @Test(groups = { "integration" }) @@ -146,13 +145,13 @@ public void clearWarningsTest() throws SQLException { @Test(groups = { "integration" }) public void getTypeMapTest() throws SQLException { Connection localConnection = this.getJdbcConnection(); - Assert.assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.getTypeMap()); + assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.getTypeMap()); } @Test(groups = { "integration" }) public void setTypeMapTest() throws SQLException { Connection localConnection = this.getJdbcConnection(); - Assert.assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.setTypeMap(null)); + assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.setTypeMap(null)); } @Test(groups = { "integration" }) @@ -170,44 +169,44 @@ public void getHoldabilityTest() throws SQLException { @Test(groups = { "integration" }) public void setSavepointTest() throws SQLException { Connection localConnection = this.getJdbcConnection(); - Assert.assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.setSavepoint()); - Assert.assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.setSavepoint("savepoint-name")); + assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.setSavepoint()); + assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.setSavepoint("savepoint-name")); } @Test(groups = { "integration" }) public void releaseSavepointTest() throws SQLException { Connection localConnection = this.getJdbcConnection(); - Assert.assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.releaseSavepoint(null)); + assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.releaseSavepoint(null)); } @Test(groups = { "integration" }) public void createClobTest() throws SQLException { Connection localConnection = this.getJdbcConnection(); - Assert.assertThrows(SQLFeatureNotSupportedException.class, localConnection::createClob); + assertThrows(SQLFeatureNotSupportedException.class, localConnection::createClob); } @Test(groups = { "integration" }) public void createBlobTest() throws SQLException { Connection localConnection = this.getJdbcConnection(); - Assert.assertThrows(SQLFeatureNotSupportedException.class, localConnection::createBlob); + assertThrows(SQLFeatureNotSupportedException.class, localConnection::createBlob); } @Test(groups = { "integration" }) public void createNClobTest() throws SQLException { Connection localConnection = this.getJdbcConnection(); - Assert.assertThrows(SQLFeatureNotSupportedException.class, localConnection::createNClob); + assertThrows(SQLFeatureNotSupportedException.class, localConnection::createNClob); } @Test(groups = { "integration" }) public void createSQLXMLTest() throws SQLException { Connection localConnection = this.getJdbcConnection(); - Assert.assertThrows(SQLFeatureNotSupportedException.class, localConnection::createSQLXML); + assertThrows(SQLFeatureNotSupportedException.class, localConnection::createSQLXML); } @Test(groups = { "integration" }) public void isValidTest() throws SQLException { Connection localConnection = this.getJdbcConnection(); - Assert.assertThrows(SQLException.class, () -> localConnection.isValid(-1)); + assertThrows(SQLException.class, () -> localConnection.isValid(-1)); Assert.assertTrue(localConnection.isValid(0)); } @@ -232,7 +231,7 @@ public void createArrayOfTest() throws SQLException { @Test(groups = { "integration" }) public void createStructTest() throws SQLException { Connection localConnection = this.getJdbcConnection(); - Assert.assertNull(localConnection.createStruct("type-name", new Object[] { 1, 2, 3 })); + assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.createStruct("type-name", new Object[] { 1, 2, 3 })); } @Test(groups = { "integration" }) @@ -245,19 +244,19 @@ public void setSchemaTest() throws SQLException { @Test(groups = { "integration" }) public void abortTest() throws SQLException { Connection localConnection = this.getJdbcConnection(); - Assert.assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.abort(null)); + assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.abort(null)); } @Test(groups = { "integration" }) public void setNetworkTimeoutTest() throws SQLException { Connection localConnection = this.getJdbcConnection(); - Assert.assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.setNetworkTimeout(null, 0)); + assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.setNetworkTimeout(null, 0)); } @Test(groups = { "integration" }) public void getNetworkTimeoutTest() throws SQLException { Connection localConnection = this.getJdbcConnection(); - Assert.assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.getNetworkTimeout()); + assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.getNetworkTimeout()); } @Test(groups = { "integration" }) @@ -275,14 +274,14 @@ public void endRequestTest() throws SQLException { @Test(groups = { "integration" }) public void setShardingKeyIfValidTest() throws SQLException { Connection localConnection = this.getJdbcConnection(); - Assert.assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.setShardingKeyIfValid(null, 0)); - Assert.assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.setShardingKeyIfValid(null, null, 0)); + assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.setShardingKeyIfValid(null, 0)); + assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.setShardingKeyIfValid(null, null, 0)); } @Test(groups = { "integration" }) public void setShardingKeyTest() throws SQLException { Connection localConnection = this.getJdbcConnection(); - Assert.assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.setShardingKey(null)); - Assert.assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.setShardingKey(null, null)); + assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.setShardingKey(null)); + assertThrows(SQLFeatureNotSupportedException.class, () -> localConnection.setShardingKey(null, null)); } }