diff --git a/qa/sql/src/main/java/org/elasticsearch/xpack/qa/sql/jdbc/SimpleExampleTestCase.java b/qa/sql/src/main/java/org/elasticsearch/xpack/qa/sql/jdbc/SimpleExampleTestCase.java index adabf47809b..7621743481a 100644 --- a/qa/sql/src/main/java/org/elasticsearch/xpack/qa/sql/jdbc/SimpleExampleTestCase.java +++ b/qa/sql/src/main/java/org/elasticsearch/xpack/qa/sql/jdbc/SimpleExampleTestCase.java @@ -7,6 +7,7 @@ package org.elasticsearch.xpack.qa.sql.jdbc; import java.sql.Connection; import java.sql.ResultSet; +import java.sql.SQLException; import java.sql.Statement; public class SimpleExampleTestCase extends JdbcIntegrationTestCase { @@ -23,6 +24,8 @@ public class SimpleExampleTestCase extends JdbcIntegrationTestCase { assertTrue(results.next()); assertEquals("Don Quixote", results.getString(1)); assertEquals(1072, results.getInt(2)); + SQLException e = expectThrows(SQLException.class, () -> results.getInt(1)); + assertTrue(e.getMessage(), e.getMessage().contains("unable to convert column 1 to an int")); assertFalse(results.next()); } // end::simple_example diff --git a/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcResultSet.java b/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcResultSet.java index 04f81c72ace..317ff80f79c 100644 --- a/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcResultSet.java +++ b/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcResultSet.java @@ -134,48 +134,80 @@ class JdbcResultSet implements ResultSet, JdbcWrapper { @Override public boolean getBoolean(int columnIndex) throws SQLException { Object val = column(columnIndex); - return val != null ? (Boolean) val : false; + try { + return val != null ? (Boolean) val : false; + } catch (ClassCastException cce) { + throw new SQLException("unable to convert column " + columnIndex + " to a boolean", cce); + } } @Override public byte getByte(int columnIndex) throws SQLException { Object val = column(columnIndex); - return val != null ? ((Number) val).byteValue() : 0; + try { + return val != null ? ((Number) val).byteValue() : 0; + } catch (ClassCastException cce) { + throw new SQLException("unable to convert column " + columnIndex + " to a byte", cce); + } } @Override public short getShort(int columnIndex) throws SQLException { Object val = column(columnIndex); - return val != null ? ((Number) val).shortValue() : 0; + try { + return val != null ? ((Number) val).shortValue() : 0; + } catch (ClassCastException cce) { + throw new SQLException("unable to convert column " + columnIndex + " to a short", cce); + } } @Override public int getInt(int columnIndex) throws SQLException { Object val = column(columnIndex); - return val != null ? ((Number) val).intValue() : 0; + try { + return val != null ? ((Number) val).intValue() : 0; + } catch (ClassCastException cce) { + throw new SQLException("unable to convert column " + columnIndex + " to an int", cce); + } } @Override public long getLong(int columnIndex) throws SQLException { Object val = column(columnIndex); - return val != null ? ((Number) val).longValue() : 0; + try { + return val != null ? ((Number) val).longValue() : 0; + } catch (ClassCastException cce) { + throw new SQLException("unable to convert column " + columnIndex + " to a long", cce); + } } @Override public float getFloat(int columnIndex) throws SQLException { Object val = column(columnIndex); - return val != null ? ((Number) val).floatValue() : 0; + try { + return val != null ? ((Number) val).floatValue() : 0; + } catch (ClassCastException cce) { + throw new SQLException("unable to convert column " + columnIndex + " to a float", cce); + } } @Override public double getDouble(int columnIndex) throws SQLException { Object val = column(columnIndex); - return val != null ? ((Number) val).doubleValue() : 0; + try { + return val != null ? ((Number) val).doubleValue() : 0; + } catch (ClassCastException cce) { + throw new SQLException("unable to convert column " + columnIndex + " to a double", cce); + } } @Override public byte[] getBytes(int columnIndex) throws SQLException { - return (byte[]) column(columnIndex); + try { + return (byte[]) column(columnIndex); + } catch (ClassCastException cce) { + throw new SQLException("unable to convert column " + columnIndex + " to a byte array", cce); + } } @Override @@ -245,7 +277,11 @@ class JdbcResultSet implements ResultSet, JdbcWrapper { private Long dateTime(int columnIndex) throws SQLException { Object val = column(columnIndex); - return val == null ? null : (Long) val; + try { + return val == null ? null : (Long) val; + } catch (ClassCastException cce) { + throw new SQLException("unable to convert column " + columnIndex + " to a long", cce); + } } private Calendar safeCalendar(Calendar calendar) { @@ -324,7 +360,11 @@ class JdbcResultSet implements ResultSet, JdbcWrapper { } if (type != null && type.isInstance(val)) { - return type.cast(val); + try { + return type.cast(val); + } catch (ClassCastException cce) { + throw new SQLException("unable to convert column " + columnIndex + " to " + type, cce); + } } JDBCType columnType = cursor.columns().get(columnIndex - 1).type; @@ -1137,4 +1177,4 @@ class JdbcResultSet implements ResultSet, JdbcWrapper { public String toString() { return format(Locale.ROOT, "%s:row %d", getClass().getSimpleName(), rowNumber); } -} \ No newline at end of file +}