mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-02-17 18:35:25 +00:00
SQL: Wrap coercion in JdbcResultSet to throw SQLException (elastic/x-pack-elasticsearch#3559)
* SQL: Wrap coercion in JdbcResultSet to throw SQLException This catches the `ClassCastException` that could be thrown when retrieving data from a result set, instead converting it into a `SQLException`. Resolves elastic/x-pack-elasticsearch#3207 * Add simple test for incorrect coercion Original commit: elastic/x-pack-elasticsearch@5480a48d95
This commit is contained in:
parent
ab14aa7059
commit
f53a19374b
@ -7,6 +7,7 @@ package org.elasticsearch.xpack.qa.sql.jdbc;
|
|||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
import java.sql.Statement;
|
import java.sql.Statement;
|
||||||
|
|
||||||
public class SimpleExampleTestCase extends JdbcIntegrationTestCase {
|
public class SimpleExampleTestCase extends JdbcIntegrationTestCase {
|
||||||
@ -23,6 +24,8 @@ public class SimpleExampleTestCase extends JdbcIntegrationTestCase {
|
|||||||
assertTrue(results.next());
|
assertTrue(results.next());
|
||||||
assertEquals("Don Quixote", results.getString(1));
|
assertEquals("Don Quixote", results.getString(1));
|
||||||
assertEquals(1072, results.getInt(2));
|
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());
|
assertFalse(results.next());
|
||||||
}
|
}
|
||||||
// end::simple_example
|
// end::simple_example
|
||||||
|
@ -134,48 +134,80 @@ class JdbcResultSet implements ResultSet, JdbcWrapper {
|
|||||||
@Override
|
@Override
|
||||||
public boolean getBoolean(int columnIndex) throws SQLException {
|
public boolean getBoolean(int columnIndex) throws SQLException {
|
||||||
Object val = column(columnIndex);
|
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
|
@Override
|
||||||
public byte getByte(int columnIndex) throws SQLException {
|
public byte getByte(int columnIndex) throws SQLException {
|
||||||
Object val = column(columnIndex);
|
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
|
@Override
|
||||||
public short getShort(int columnIndex) throws SQLException {
|
public short getShort(int columnIndex) throws SQLException {
|
||||||
Object val = column(columnIndex);
|
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
|
@Override
|
||||||
public int getInt(int columnIndex) throws SQLException {
|
public int getInt(int columnIndex) throws SQLException {
|
||||||
Object val = column(columnIndex);
|
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
|
@Override
|
||||||
public long getLong(int columnIndex) throws SQLException {
|
public long getLong(int columnIndex) throws SQLException {
|
||||||
Object val = column(columnIndex);
|
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
|
@Override
|
||||||
public float getFloat(int columnIndex) throws SQLException {
|
public float getFloat(int columnIndex) throws SQLException {
|
||||||
Object val = column(columnIndex);
|
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
|
@Override
|
||||||
public double getDouble(int columnIndex) throws SQLException {
|
public double getDouble(int columnIndex) throws SQLException {
|
||||||
Object val = column(columnIndex);
|
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
|
@Override
|
||||||
public byte[] getBytes(int columnIndex) throws SQLException {
|
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
|
@Override
|
||||||
@ -245,7 +277,11 @@ class JdbcResultSet implements ResultSet, JdbcWrapper {
|
|||||||
|
|
||||||
private Long dateTime(int columnIndex) throws SQLException {
|
private Long dateTime(int columnIndex) throws SQLException {
|
||||||
Object val = column(columnIndex);
|
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) {
|
private Calendar safeCalendar(Calendar calendar) {
|
||||||
@ -324,7 +360,11 @@ class JdbcResultSet implements ResultSet, JdbcWrapper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (type != null && type.isInstance(val)) {
|
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;
|
JDBCType columnType = cursor.columns().get(columnIndex - 1).type;
|
||||||
@ -1137,4 +1177,4 @@ class JdbcResultSet implements ResultSet, JdbcWrapper {
|
|||||||
public String toString() {
|
public String toString() {
|
||||||
return format(Locale.ROOT, "%s:row %d", getClass().getSimpleName(), rowNumber);
|
return format(Locale.ROOT, "%s:row %d", getClass().getSimpleName(), rowNumber);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user