diff --git a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcDatabaseMetaData.java b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcDatabaseMetaData.java index 4bc4f51d874c8..c06a96c988167 100644 --- a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcDatabaseMetaData.java +++ b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcDatabaseMetaData.java @@ -19,6 +19,7 @@ import java.sql.RowIdLifetime; import java.sql.SQLException; import java.sql.SQLFeatureNotSupportedException; +import java.sql.SQLType; import java.util.ArrayList; import java.util.List; @@ -1124,11 +1125,11 @@ private static List columnInfo(String tableName, Object... cols) thr Object obj = cols[i]; if (obj instanceof String) { String name = obj.toString(); - JDBCType type = JDBCType.VARCHAR; + SQLType type = JDBCType.VARCHAR; if (i + 1 < cols.length) { // check if the next item it's a type - if (cols[i + 1] instanceof JDBCType) { - type = (JDBCType) cols[i + 1]; + if (cols[i + 1] instanceof SQLType) { + type = (SQLType) cols[i + 1]; i++; } // it's not, use the default and move on diff --git a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcParameterMetaData.java b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcParameterMetaData.java index ca464813dc2b5..988fa6da047fd 100644 --- a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcParameterMetaData.java +++ b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcParameterMetaData.java @@ -54,7 +54,7 @@ public int getParameterType(int param) throws SQLException { @Override public String getParameterTypeName(int param) throws SQLException { - return paramInfo(param).type.name(); + return paramInfo(param).type.getName(); } @Override diff --git a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcPreparedStatement.java b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcPreparedStatement.java index bae4260ac2b69..dc3dac978a728 100644 --- a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcPreparedStatement.java +++ b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcPreparedStatement.java @@ -26,6 +26,7 @@ import java.sql.SQLDataException; import java.sql.SQLException; import java.sql.SQLFeatureNotSupportedException; +import java.sql.SQLType; import java.sql.SQLXML; import java.sql.Struct; import java.sql.Time; @@ -69,7 +70,7 @@ public int executeUpdate() throws SQLException { throw new SQLFeatureNotSupportedException("Writes not supported"); } - private void setParam(int parameterIndex, Object value, int type) throws SQLException { + private void setParam(int parameterIndex, Object value, SQLType type) throws SQLException { checkOpen(); if (parameterIndex < 0 || parameterIndex > query.paramCount()) { @@ -77,12 +78,12 @@ private void setParam(int parameterIndex, Object value, int type) throws SQLExce "]"); } - query.setParam(parameterIndex, value, JDBCType.valueOf(type)); + query.setParam(parameterIndex, value, type); } @Override public void setNull(int parameterIndex, int sqlType) throws SQLException { - setParam(parameterIndex, null, sqlType); + setParam(parameterIndex, null, JDBCType.valueOf(sqlType)); } @Override @@ -181,7 +182,7 @@ public void setObject(int parameterIndex, Object x, int targetSqlType) throws SQ @Override public void setObject(int parameterIndex, Object x) throws SQLException { if (x == null) { - setParam(parameterIndex, null, Types.NULL); + setParam(parameterIndex, null, JDBCType.NULL); return; } @@ -338,7 +339,7 @@ public void setObject(int parameterIndex, Object x, int targetSqlType, int scale // set the null value on the type and exit if (x == null) { - setParam(parameterIndex, null, targetSqlType); + setParam(parameterIndex, null, JDBCType.valueOf(targetSqlType)); return; } @@ -348,7 +349,7 @@ public void setObject(int parameterIndex, Object x, int targetSqlType, int scale throw new SQLFeatureNotSupportedException( "Conversion from type byte[] to " + targetJDBCType + " not supported"); } - setParam(parameterIndex, x, Types.VARBINARY); + setParam(parameterIndex, x, JDBCType.VARBINARY); return; } @@ -357,7 +358,7 @@ public void setObject(int parameterIndex, Object x, int targetSqlType, int scale || x instanceof Date || x instanceof LocalDateTime || x instanceof Time - || x instanceof java.util.Date) + || x instanceof java.util.Date) { if (targetJDBCType == JDBCType.TIMESTAMP) { // converting to {@code java.util.Date} because this is the type supported by {@code XContentBuilder} for serialization @@ -380,10 +381,10 @@ public void setObject(int parameterIndex, Object x, int targetSqlType, int scale dateToSet = (java.util.Date) x; } - setParam(parameterIndex, dateToSet, Types.TIMESTAMP); + setParam(parameterIndex, dateToSet, JDBCType.TIMESTAMP); return; } else if (targetJDBCType == JDBCType.VARCHAR) { - setParam(parameterIndex, String.valueOf(x), Types.VARCHAR); + setParam(parameterIndex, String.valueOf(x), JDBCType.VARCHAR); return; } // anything else other than VARCHAR and TIMESTAMP is not supported in this JDBC driver @@ -399,9 +400,9 @@ public void setObject(int parameterIndex, Object x, int targetSqlType, int scale || x instanceof Float || x instanceof Double || x instanceof String) { - setParam(parameterIndex, - TypeConverter.convert(x, TypeConverter.fromJavaToJDBC(x.getClass()), DataType.fromJdbcTypeToJava(targetJDBCType)), - targetSqlType); + setParam(parameterIndex, + TypeConverter.convert(x, TypeConverter.fromJavaToJDBC(x.getClass()), DataType.fromJdbcTypeToJava(targetJDBCType)), + JDBCType.valueOf(targetSqlType)); return; } @@ -410,8 +411,8 @@ public void setObject(int parameterIndex, Object x, int targetSqlType, int scale } private void checkKnownUnsupportedTypes(Object x) throws SQLFeatureNotSupportedException { - List> unsupportedTypes = new ArrayList>(Arrays.asList(Struct.class, Array.class, SQLXML.class, - RowId.class, Ref.class, Blob.class, NClob.class, Clob.class, LocalDate.class, LocalTime.class, + List> unsupportedTypes = new ArrayList<>(Arrays.asList(Struct.class, Array.class, SQLXML.class, + RowId.class, Ref.class, Blob.class, NClob.class, Clob.class, LocalDate.class, LocalTime.class, OffsetTime.class, OffsetDateTime.class, URL.class, BigDecimal.class)); for (Class clazz:unsupportedTypes) { diff --git a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcResultSet.java b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcResultSet.java index ebdeaef15cae4..a289991853fde 100644 --- a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcResultSet.java +++ b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcResultSet.java @@ -25,6 +25,7 @@ import java.sql.RowId; import java.sql.SQLException; import java.sql.SQLFeatureNotSupportedException; +import java.sql.SQLType; import java.sql.SQLWarning; import java.sql.SQLXML; import java.sql.Statement; @@ -133,7 +134,7 @@ public String getString(int columnIndex) throws SQLException { @Override public boolean getBoolean(int columnIndex) throws SQLException { - return column(columnIndex) != null ? getObject(columnIndex, Boolean.class) : false; + return column(columnIndex) != null ? getObject(columnIndex, Boolean.class) : false; } @Override @@ -245,7 +246,7 @@ public Date getDate(String columnLabel) throws SQLException { private Long dateTime(int columnIndex) throws SQLException { Object val = column(columnIndex); - JDBCType type = cursor.columns().get(columnIndex - 1).type; + SQLType type = cursor.columns().get(columnIndex - 1).type; try { // TODO: the B6 appendix of the jdbc spec does mention CHAR, VARCHAR, LONGVARCHAR, DATE, TIMESTAMP as supported // jdbc types that should be handled by getDate and getTime methods. From all of those we support VARCHAR and @@ -338,7 +339,7 @@ private T convert(int columnIndex, Class type) throws SQLException { return null; } - JDBCType columnType = cursor.columns().get(columnIndex - 1).type; + SQLType columnType = cursor.columns().get(columnIndex - 1).type; return TypeConverter.convert(val, columnType, type); } diff --git a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcResultSetMetaData.java b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcResultSetMetaData.java index 574cdeb62b4b5..ed2b899e22a52 100644 --- a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcResultSetMetaData.java +++ b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcResultSetMetaData.java @@ -114,7 +114,7 @@ public int getColumnType(int column) throws SQLException { @Override public String getColumnTypeName(int column) throws SQLException { - return column(column).type.name(); + return column(column).type.getName(); } @Override diff --git a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/PreparedQuery.java b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/PreparedQuery.java index 06825ee6e3f96..ab459e90d969f 100644 --- a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/PreparedQuery.java +++ b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/PreparedQuery.java @@ -11,6 +11,7 @@ import java.sql.JDBCType; import java.sql.SQLException; +import java.sql.SQLType; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; @@ -18,10 +19,10 @@ class PreparedQuery { static class ParamInfo { - JDBCType type; + SQLType type; Object value; - ParamInfo(Object value, JDBCType type) { + ParamInfo(Object value, SQLType type) { this.value = value; this.type = type; } @@ -43,7 +44,7 @@ ParamInfo getParam(int param) throws JdbcSQLException { return params[param - 1]; } - void setParam(int param, Object value, JDBCType type) throws JdbcSQLException { + void setParam(int param, Object value, SQLType type) throws JdbcSQLException { if (param < 1 || param > params.length) { throw new JdbcSQLException("Invalid parameter index [" + param + "]"); } diff --git a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/TypeConverter.java b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/TypeConverter.java index 7b638d8bd094f..2decfe5d3c5c5 100644 --- a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/TypeConverter.java +++ b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/TypeConverter.java @@ -12,6 +12,7 @@ import java.sql.JDBCType; import java.sql.SQLException; import java.sql.SQLFeatureNotSupportedException; +import java.sql.SQLType; import java.sql.Time; import java.sql.Timestamp; import java.time.LocalDate; @@ -56,11 +57,11 @@ private TypeConverter() { } private static final long DAY_IN_MILLIS = 60 * 60 * 24 * 1000; - private static final Map, JDBCType> javaToJDBC; + private static final Map, SQLType> javaToJDBC; static { - Map, JDBCType> aMap = Arrays.stream(DataType.values()) + Map, SQLType> aMap = Arrays.stream(DataType.values()) .filter(dataType -> dataType.javaClass() != null && dataType != DataType.HALF_FLOAT && dataType != DataType.SCALED_FLOAT @@ -139,7 +140,7 @@ static long convertFromCalendarToUTC(long value, Calendar cal) { * Converts object val from columnType to type */ @SuppressWarnings("unchecked") - static T convert(Object val, JDBCType columnType, Class type) throws SQLException { + static T convert(Object val, SQLType columnType, Class type) throws SQLException { if (type == null) { return (T) convert(val, columnType); } @@ -151,7 +152,7 @@ static T convert(Object val, JDBCType columnType, Class type) throws SQLE try { return type.cast(val); } catch (ClassCastException cce) { - throw new SQLException(format(Locale.ROOT, "Unable to convert value [%.128s] of type [%s] to a %s", val, + throw new SQLException(format(Locale.ROOT, "Unable to convert value [%.128s] of type [%s] to a %s", val, columnType.getName(), type.getName()), cce); } } @@ -210,7 +211,7 @@ static T convert(Object val, JDBCType columnType, Class type) throws SQLE if (type == OffsetDateTime.class) { return (T) asOffsetDateTime(val, columnType); } - throw new SQLException(format(Locale.ROOT, "Unable to convert value [%.128s] of type [%s] to a %s", val, + throw new SQLException(format(Locale.ROOT, "Unable to convert value [%.128s] of type [%s] to a %s", val, columnType.getName(), type.getName())); } @@ -220,7 +221,7 @@ static T convert(Object val, JDBCType columnType, Class type) throws SQLE * See {@link javax.sql.rowset.RowSetMetaDataImpl#getColumnClassName} and * https://db.apache.org/derby/docs/10.5/ref/rrefjdbc20377.html */ - public static String classNameOf(JDBCType jdbcType) throws JdbcSQLException { + public static String classNameOf(SQLType jdbcType) throws JdbcSQLException { final DataType dataType; try { dataType = DataType.fromJdbcType(jdbcType); @@ -239,31 +240,35 @@ public static String classNameOf(JDBCType jdbcType) throws JdbcSQLException { *

* The returned types needs to correspond to ES-portion of classes returned by {@link TypeConverter#classNameOf} */ - static Object convert(Object v, JDBCType columnType) throws SQLException { - switch (columnType) { - case NULL: - return null; - case BOOLEAN: - case VARCHAR: - return v; // These types are already represented correctly in JSON - case TINYINT: - return ((Number) v).byteValue(); // Parser might return it as integer or long - need to update to the correct type - case SMALLINT: - return ((Number) v).shortValue(); // Parser might return it as integer or long - need to update to the correct type - case INTEGER: - return ((Number) v).intValue(); - case BIGINT: - return ((Number) v).longValue(); - case FLOAT: - case DOUBLE: - return doubleValue(v); // Double might be represented as string for infinity and NaN values - case REAL: - return floatValue(v); // Float might be represented as string for infinity and NaN values - case TIMESTAMP: - return new Timestamp(((Number) v).longValue()); - default: - throw new SQLException("Unexpected column type [" + columnType.getName() + "]"); - + static Object convert(Object v, SQLType columnType) throws SQLException { + if (columnType instanceof JDBCType) { + switch ((JDBCType) columnType) { + case NULL: + return null; + case BOOLEAN: + case VARCHAR: + return v; // These types are already represented correctly in JSON + case TINYINT: + return ((Number) v).byteValue(); // Parser might return it as integer or long - need to update to the correct type + case SMALLINT: + return ((Number) v).shortValue(); // Parser might return it as integer or long - need to update to the correct type + case INTEGER: + return ((Number) v).intValue(); + case BIGINT: + return ((Number) v).longValue(); + case FLOAT: + case DOUBLE: + return doubleValue(v); // Double might be represented as string for infinity and NaN values + case REAL: + return floatValue(v); // Float might be represented as string for infinity and NaN values + case TIMESTAMP: + return new Timestamp(((Number) v).longValue()); + default: + throw new SQLException("Unexpected column type [" + columnType.getName() + "]"); + + } + } else { + throw new SQLException("Unexpected column type [" + columnType.getName() + "]"); } } @@ -272,7 +277,7 @@ static Object convert(Object v, JDBCType columnType) throws SQLException { *

* It needs to support both params and column types */ - static boolean isSigned(JDBCType jdbcType) throws SQLException { + static boolean isSigned(SQLType jdbcType) throws SQLException { final DataType dataType; try { dataType = DataType.fromJdbcType(jdbcType); @@ -284,8 +289,8 @@ static boolean isSigned(JDBCType jdbcType) throws SQLException { } - static JDBCType fromJavaToJDBC(Class clazz) throws SQLException { - for (Entry, JDBCType> e : javaToJDBC.entrySet()) { + static SQLType fromJavaToJDBC(Class clazz) throws SQLException { + for (Entry, SQLType> e : javaToJDBC.entrySet()) { // java.util.Calendar from {@code javaToJDBC} is an abstract class and this method can be used with concrete classes as well if (e.getKey().isAssignableFrom(clazz)) { return e.getValue(); @@ -331,200 +336,215 @@ private static String asString(Object nativeValue) { return nativeValue == null ? null : String.valueOf(nativeValue); } - private static Boolean asBoolean(Object val, JDBCType columnType) throws SQLException { - switch (columnType) { - case BOOLEAN: - case TINYINT: - case SMALLINT: - case INTEGER: - case BIGINT: - case REAL: - case FLOAT: - case DOUBLE: - return Boolean.valueOf(Integer.signum(((Number) val).intValue()) != 0); - case VARCHAR: - return Boolean.valueOf((String) val); - default: - throw new SQLException( - format(Locale.ROOT, "Unable to convert value [%.128s] of type [%s] to a Boolean", val, columnType.getName())); - - } - } - - private static Byte asByte(Object val, JDBCType columnType) throws SQLException { - switch (columnType) { - case BOOLEAN: - return Byte.valueOf(((Boolean) val).booleanValue() ? (byte) 1 : (byte) 0); - case TINYINT: - case SMALLINT: - case INTEGER: - case BIGINT: - return safeToByte(((Number) val).longValue()); - case REAL: - case FLOAT: - case DOUBLE: - return safeToByte(safeToLong(((Number) val).doubleValue())); - case VARCHAR: - try { - return Byte.valueOf((String) val); - } catch (NumberFormatException e) { - throw new SQLException(format(Locale.ROOT, "Unable to convert value [%.128s] of type [VARCHAR] to a Byte", val), e); - } - default: + private static Boolean asBoolean(Object val, SQLType columnType) throws SQLException { + if (columnType instanceof JDBCType) { + switch ((JDBCType) columnType) { + case BOOLEAN: + case TINYINT: + case SMALLINT: + case INTEGER: + case BIGINT: + case REAL: + case FLOAT: + case DOUBLE: + return Boolean.valueOf(Integer.signum(((Number) val).intValue()) != 0); + case VARCHAR: + return Boolean.valueOf((String) val); + default: + throw new SQLException( + format(Locale.ROOT, "Unable to convert value [%.128s] of type [%s] to a Boolean", val, columnType.getName())); + + } + } else { + throw new SQLException("Unexpected column type [" + columnType.getName() + "]"); + } + } + + private static Byte asByte(Object val, SQLType columnType) throws SQLException { + if (columnType instanceof JDBCType) { + switch ((JDBCType) columnType) { + case BOOLEAN: + return Byte.valueOf(((Boolean) val).booleanValue() ? (byte) 1 : (byte) 0); + case TINYINT: + case SMALLINT: + case INTEGER: + case BIGINT: + return safeToByte(((Number) val).longValue()); + case REAL: + case FLOAT: + case DOUBLE: + return safeToByte(safeToLong(((Number) val).doubleValue())); + case VARCHAR: + try { + return Byte.valueOf((String) val); + } catch (NumberFormatException e) { + throw new SQLException(format(Locale.ROOT, "Unable to convert value [%.128s] of type [VARCHAR] to a Byte", val), e); + } + default: + } } throw new SQLException(format(Locale.ROOT, "Unable to convert value [%.128s] of type [%s] to a Byte", val, columnType.getName())); } - private static Short asShort(Object val, JDBCType columnType) throws SQLException { - switch (columnType) { - case BOOLEAN: - return Short.valueOf(((Boolean) val).booleanValue() ? (short) 1 : (short) 0); - case TINYINT: - case SMALLINT: - case INTEGER: - case BIGINT: - return safeToShort(((Number) val).longValue()); - case REAL: - case FLOAT: - case DOUBLE: - return safeToShort(safeToLong(((Number) val).doubleValue())); - case VARCHAR: - try { - return Short.valueOf((String) val); - } catch (NumberFormatException e) { - throw new SQLException(format(Locale.ROOT, "Unable to convert value [%.128s] of type [VARCHAR] to a Short", val), e); - } - default: + private static Short asShort(Object val, SQLType columnType) throws SQLException { + if (columnType instanceof JDBCType) { + switch ((JDBCType) columnType) { + case BOOLEAN: + return Short.valueOf(((Boolean) val).booleanValue() ? (short) 1 : (short) 0); + case TINYINT: + case SMALLINT: + case INTEGER: + case BIGINT: + return safeToShort(((Number) val).longValue()); + case REAL: + case FLOAT: + case DOUBLE: + return safeToShort(safeToLong(((Number) val).doubleValue())); + case VARCHAR: + try { + return Short.valueOf((String) val); + } catch (NumberFormatException e) { + throw new SQLException(format(Locale.ROOT, "Unable to convert value [%.128s] of type [VARCHAR] to a Short", val), + e); + } + default: + } } - throw new SQLException(format(Locale.ROOT, "Unable to convert value [%.128s] of type [%s] to a Short", val, columnType.getName())); } - private static Integer asInteger(Object val, JDBCType columnType) throws SQLException { - switch (columnType) { - case BOOLEAN: - return Integer.valueOf(((Boolean) val).booleanValue() ? 1 : 0); - case TINYINT: - case SMALLINT: - case INTEGER: - case BIGINT: - return safeToInt(((Number) val).longValue()); - case REAL: - case FLOAT: - case DOUBLE: - return safeToInt(safeToLong(((Number) val).doubleValue())); - case VARCHAR: - try { - return Integer.valueOf((String) val); - } catch (NumberFormatException e) { - throw new SQLException( - format(Locale.ROOT, "Unable to convert value [%.128s] of type [VARCHAR] to an Integer", val), e); - } - default: - } + private static Integer asInteger(Object val, SQLType columnType) throws SQLException { + if (columnType instanceof JDBCType) { + switch ((JDBCType) columnType) { + case BOOLEAN: + return Integer.valueOf(((Boolean) val).booleanValue() ? 1 : 0); + case TINYINT: + case SMALLINT: + case INTEGER: + case BIGINT: + return safeToInt(((Number) val).longValue()); + case REAL: + case FLOAT: + case DOUBLE: + return safeToInt(safeToLong(((Number) val).doubleValue())); + case VARCHAR: + try { + return Integer.valueOf((String) val); + } catch (NumberFormatException e) { + throw new SQLException( + format(Locale.ROOT, "Unable to convert value [%.128s] of type [VARCHAR] to an Integer", val), e); + } + default: + } + } throw new SQLException( format(Locale.ROOT, "Unable to convert value [%.128s] of type [%s] to an Integer", val, columnType.getName())); } - private static Long asLong(Object val, JDBCType columnType) throws SQLException { - switch (columnType) { - case BOOLEAN: - return Long.valueOf(((Boolean) val).booleanValue() ? 1 : 0); - case TINYINT: - case SMALLINT: - case INTEGER: - case BIGINT: - return Long.valueOf(((Number) val).longValue()); - case REAL: - case FLOAT: - case DOUBLE: - return safeToLong(((Number) val).doubleValue()); - //TODO: should we support conversion to TIMESTAMP? - //The spec says that getLong() should support the following types conversions: - //TINYINT, SMALLINT, INTEGER, BIGINT, REAL, FLOAT, DOUBLE, DECIMAL, NUMERIC, BIT, BOOLEAN, CHAR, VARCHAR, LONGVARCHAR - //case TIMESTAMP: - // return ((Number) val).longValue(); - case VARCHAR: - try { - return Long.valueOf((String) val); - } catch (NumberFormatException e) { - throw new SQLException(format(Locale.ROOT, "Unable to convert value [%.128s] of type [VARCHAR] to a Long", val), e); - } - default: + private static Long asLong(Object val, SQLType columnType) throws SQLException { + if (columnType instanceof JDBCType) { + switch ((JDBCType) columnType) { + case BOOLEAN: + return Long.valueOf(((Boolean) val).booleanValue() ? 1 : 0); + case TINYINT: + case SMALLINT: + case INTEGER: + case BIGINT: + return Long.valueOf(((Number) val).longValue()); + case REAL: + case FLOAT: + case DOUBLE: + return safeToLong(((Number) val).doubleValue()); + //TODO: should we support conversion to TIMESTAMP? + //The spec says that getLong() should support the following types conversions: + //TINYINT, SMALLINT, INTEGER, BIGINT, REAL, FLOAT, DOUBLE, DECIMAL, NUMERIC, BIT, BOOLEAN, CHAR, VARCHAR, LONGVARCHAR + //case TIMESTAMP: + // return ((Number) val).longValue(); + case VARCHAR: + try { + return Long.valueOf((String) val); + } catch (NumberFormatException e) { + throw new SQLException(format(Locale.ROOT, "Unable to convert value [%.128s] of type [VARCHAR] to a Long", val), e); + } + default: + } } - throw new SQLException(format(Locale.ROOT, "Unable to convert value [%.128s] of type [%s] to a Long", val, columnType.getName())); } - private static Float asFloat(Object val, JDBCType columnType) throws SQLException { - switch (columnType) { - case BOOLEAN: - return Float.valueOf(((Boolean) val).booleanValue() ? 1 : 0); - case TINYINT: - case SMALLINT: - case INTEGER: - case BIGINT: - return Float.valueOf(((Number) val).longValue()); - case REAL: - case FLOAT: - case DOUBLE: - return Float.valueOf((((float) ((Number) val).doubleValue()))); - case VARCHAR: - try { - return Float.valueOf((String) val); - } catch (NumberFormatException e) { - throw new SQLException(format(Locale.ROOT, "Unable to convert value [%.128s] of type [VARCHAR] to a Float", val), e); - } - default: + private static Float asFloat(Object val, SQLType columnType) throws SQLException { + if (columnType instanceof JDBCType) { + switch ((JDBCType) columnType) { + case BOOLEAN: + return Float.valueOf(((Boolean) val).booleanValue() ? 1 : 0); + case TINYINT: + case SMALLINT: + case INTEGER: + case BIGINT: + return Float.valueOf(((Number) val).longValue()); + case REAL: + case FLOAT: + case DOUBLE: + return Float.valueOf((((float) ((Number) val).doubleValue()))); + case VARCHAR: + try { + return Float.valueOf((String) val); + } catch (NumberFormatException e) { + throw new SQLException(format(Locale.ROOT, "Unable to convert value [%.128s] of type [VARCHAR] to a Float", val), + e); + } + default: + } } - throw new SQLException(format(Locale.ROOT, "Unable to convert value [%.128s] of type [%s] to a Float", val, columnType.getName())); } - private static Double asDouble(Object val, JDBCType columnType) throws SQLException { - switch (columnType) { - case BOOLEAN: - return Double.valueOf(((Boolean) val).booleanValue() ? 1 : 0); - case TINYINT: - case SMALLINT: - case INTEGER: - case BIGINT: - return Double.valueOf(((Number) val).longValue()); - case REAL: - case FLOAT: - case DOUBLE: - - return Double.valueOf(((Number) val).doubleValue()); - case VARCHAR: - try { - return Double.valueOf((String) val); - } catch (NumberFormatException e) { - throw new SQLException(format(Locale.ROOT, "Unable to convert value [%.128s] of type [VARCHAR] to a Double", val), e); - } - default: + private static Double asDouble(Object val, SQLType columnType) throws SQLException { + if (columnType instanceof JDBCType) { + switch ((JDBCType) columnType) { + case BOOLEAN: + return Double.valueOf(((Boolean) val).booleanValue() ? 1 : 0); + case TINYINT: + case SMALLINT: + case INTEGER: + case BIGINT: + return Double.valueOf(((Number) val).longValue()); + case REAL: + case FLOAT: + case DOUBLE: + + return Double.valueOf(((Number) val).doubleValue()); + case VARCHAR: + try { + return Double.valueOf((String) val); + } catch (NumberFormatException e) { + throw new SQLException(format(Locale.ROOT, "Unable to convert value [%.128s] of type [VARCHAR] to a Double", val), + e); + } + default: + } } - throw new SQLException( format(Locale.ROOT, "Unable to convert value [%.128s] of type [%s] to a Double", val, columnType.getName())); } - private static Date asDate(Object val, JDBCType columnType) throws SQLException { + private static Date asDate(Object val, SQLType columnType) throws SQLException { if (columnType == JDBCType.TIMESTAMP) { return new Date(utcMillisRemoveTime(((Number) val).longValue())); } throw new SQLException(format(Locale.ROOT, "Unable to convert value [%.128s] of type [%s] to a Date", val, columnType.getName())); } - private static Time asTime(Object val, JDBCType columnType) throws SQLException { + private static Time asTime(Object val, SQLType columnType) throws SQLException { if (columnType == JDBCType.TIMESTAMP) { return new Time(utcMillisRemoveDate(((Number) val).longValue())); } throw new SQLException(format(Locale.ROOT, "Unable to convert value [%.128s] of type [%s] to a Time", val, columnType.getName())); } - private static Timestamp asTimestamp(Object val, JDBCType columnType) throws SQLException { + private static Timestamp asTimestamp(Object val, SQLType columnType) throws SQLException { if (columnType == JDBCType.TIMESTAMP) { return new Timestamp(((Number) val).longValue()); } @@ -532,27 +552,27 @@ private static Timestamp asTimestamp(Object val, JDBCType columnType) throws SQL format(Locale.ROOT, "Unable to convert value [%.128s] of type [%s] to a Timestamp", val, columnType.getName())); } - private static byte[] asByteArray(Object val, JDBCType columnType) { + private static byte[] asByteArray(Object val, SQLType columnType) { throw new UnsupportedOperationException(); } - private static LocalDate asLocalDate(Object val, JDBCType columnType) { + private static LocalDate asLocalDate(Object val, SQLType columnType) { throw new UnsupportedOperationException(); } - private static LocalTime asLocalTime(Object val, JDBCType columnType) { + private static LocalTime asLocalTime(Object val, SQLType columnType) { throw new UnsupportedOperationException(); } - private static LocalDateTime asLocalDateTime(Object val, JDBCType columnType) { + private static LocalDateTime asLocalDateTime(Object val, SQLType columnType) { throw new UnsupportedOperationException(); } - private static OffsetTime asOffsetTime(Object val, JDBCType columnType) { + private static OffsetTime asOffsetTime(Object val, SQLType columnType) { throw new UnsupportedOperationException(); } - private static OffsetDateTime asOffsetDateTime(Object val, JDBCType columnType) { + private static OffsetDateTime asOffsetDateTime(Object val, SQLType columnType) { throw new UnsupportedOperationException(); } @@ -592,4 +612,4 @@ private static long safeToLong(double x) throws SQLException { } return Math.round(x); } -} +} \ No newline at end of file diff --git a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/net/protocol/ColumnInfo.java b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/net/protocol/ColumnInfo.java index 6e61d65ff532b..b8582d8e9b386 100644 --- a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/net/protocol/ColumnInfo.java +++ b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/net/protocol/ColumnInfo.java @@ -5,7 +5,7 @@ */ package org.elasticsearch.xpack.sql.jdbc.net.protocol; -import java.sql.JDBCType; +import java.sql.SQLType; import java.util.Objects; public class ColumnInfo { @@ -15,9 +15,9 @@ public class ColumnInfo { public final String label; public final String name; public final int displaySize; - public final JDBCType type; + public final SQLType type; - public ColumnInfo(String name, JDBCType type, String table, String catalog, String schema, String label, int displaySize) { + public ColumnInfo(String name, SQLType type, String table, String catalog, String schema, String label, int displaySize) { if (name == null) { throw new IllegalArgumentException("[name] must not be null"); } @@ -88,4 +88,4 @@ public boolean equals(Object obj) { public int hashCode() { return Objects.hash(name, type, table, catalog, schema, label, displaySize); } -} +} \ No newline at end of file diff --git a/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcPreparedStatementTests.java b/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcPreparedStatementTests.java index 35a3ec5748748..229c7e8182ca3 100644 --- a/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcPreparedStatementTests.java +++ b/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcPreparedStatementTests.java @@ -9,9 +9,9 @@ import java.net.URL; import java.nio.charset.StandardCharsets; -import java.sql.JDBCType; import java.sql.SQLException; import java.sql.SQLFeatureNotSupportedException; +import java.sql.SQLType; import java.sql.Struct; import java.sql.Time; import java.sql.Timestamp; @@ -450,7 +450,7 @@ public void testSettingCalendarValues() throws SQLException { someCalendar.setTimeInMillis(randomLong()); jps.setObject(1, someCalendar); - assertEquals(someCalendar.getTime(), (Date) value(jps)); + assertEquals(someCalendar.getTime(), value(jps)); assertEquals(TIMESTAMP, jdbcType(jps)); assertTrue(value(jps) instanceof java.util.Date); @@ -460,7 +460,7 @@ public void testSettingCalendarValues() throws SQLException { Calendar nonDefaultCal = randomCalendar(); jps.setObject(1, nonDefaultCal); - assertEquals(nonDefaultCal.getTime(), (Date) value(jps)); + assertEquals(nonDefaultCal.getTime(), value(jps)); assertEquals(TIMESTAMP, jdbcType(jps)); } @@ -477,7 +477,7 @@ public void testSettingDateValues() throws SQLException { Date someDate = new Date(randomLong()); jps.setObject(1, someDate); - assertEquals(someDate, (Date) value(jps)); + assertEquals(someDate, value(jps)); assertEquals(TIMESTAMP, jdbcType(jps)); assertTrue(value(jps) instanceof java.util.Date); @@ -530,7 +530,7 @@ public void testSettingByteArrayValues() throws SQLException { assertTrue(value(jps) instanceof byte[]); jps.setObject(1, buffer, Types.VARBINARY); - assertEquals((byte[]) value(jps), buffer); + assertEquals(value(jps), buffer); assertEquals(VARBINARY, jdbcType(jps)); SQLException sqle = expectThrows(SQLFeatureNotSupportedException.class, () -> jps.setObject(1, buffer, Types.VARCHAR)); @@ -555,7 +555,7 @@ private JdbcPreparedStatement createJdbcPreparedStatement() throws SQLException return new JdbcPreparedStatement(null, JdbcConfiguration.create("jdbc:es://l:1", null, 0), "?"); } - private JDBCType jdbcType(JdbcPreparedStatement jps) throws SQLException { + private SQLType jdbcType(JdbcPreparedStatement jps) throws SQLException { return jps.query.getParam(1).type; } diff --git a/x-pack/plugin/sql/sql-action/src/main/java/org/elasticsearch/xpack/sql/action/SqlQueryResponse.java b/x-pack/plugin/sql/sql-action/src/main/java/org/elasticsearch/xpack/sql/action/SqlQueryResponse.java index 1dc356f9fba5f..970be02e38572 100644 --- a/x-pack/plugin/sql/sql-action/src/main/java/org/elasticsearch/xpack/sql/action/SqlQueryResponse.java +++ b/x-pack/plugin/sql/sql-action/src/main/java/org/elasticsearch/xpack/sql/action/SqlQueryResponse.java @@ -183,6 +183,7 @@ public static ColumnInfo readColumnInfo(StreamInput in) throws IOException { JDBCType jdbcType; int displaySize; if (in.readBoolean()) { + // FIXME: this needs changing to allow custom types jdbcType = JDBCType.valueOf(in.readVInt()); displaySize = in.readVInt(); } else { @@ -207,8 +208,12 @@ public static void writeColumnInfo(StreamOutput out, ColumnInfo columnInfo) thro @Override public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } SqlQueryResponse that = (SqlQueryResponse) o; return Objects.equals(cursor, that.cursor) && Objects.equals(columns, that.columns) && diff --git a/x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/proto/ColumnInfo.java b/x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/proto/ColumnInfo.java index dcd4f31400513..28eb1b37fdcbc 100644 --- a/x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/proto/ColumnInfo.java +++ b/x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/proto/ColumnInfo.java @@ -14,6 +14,7 @@ import java.io.IOException; import java.sql.JDBCType; +import java.sql.SQLType; import java.util.Objects; import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; @@ -51,10 +52,10 @@ public class ColumnInfo implements ToXContentObject { private final String name; private final String esType; @Nullable - private final JDBCType jdbcType; + private final SQLType jdbcType; private final int displaySize; - public ColumnInfo(String table, String name, String esType, JDBCType jdbcType, int displaySize) { + public ColumnInfo(String table, String name, String esType, SQLType jdbcType, int displaySize) { this.table = table; this.name = name; this.esType = esType; @@ -79,6 +80,10 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.field("name", name); builder.field("type", esType); if (jdbcType != null) { + // FIXME: make this pluggable by saving the SQLType.getVendorName + if (!(jdbcType instanceof JDBCType)) { + throw new IOException("Unknown jdbc type " + jdbcType); + } builder.field("jdbc_type", jdbcType.getVendorTypeNumber()); builder.field("display_size", displaySize); } @@ -114,7 +119,7 @@ public String esType() { /** * The type of the column as it would be returned by a JDBC driver. */ - public JDBCType jdbcType() { + public SQLType jdbcType() { return jdbcType; } @@ -127,8 +132,12 @@ public int displaySize() { @Override public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } ColumnInfo that = (ColumnInfo) o; return displaySize == that.displaySize && Objects.equals(table, that.table) && diff --git a/x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/type/DataType.java b/x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/type/DataType.java index 3f77bc2fc2ed7..05fb192f8d1da 100644 --- a/x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/type/DataType.java +++ b/x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/type/DataType.java @@ -6,6 +6,7 @@ package org.elasticsearch.xpack.sql.type; import java.sql.JDBCType; +import java.sql.SQLType; import java.sql.Timestamp; import java.util.Arrays; import java.util.Locale; @@ -43,7 +44,7 @@ public enum DataType { DATE( JDBCType.TIMESTAMP, Timestamp.class, Long.BYTES, 24, 24); // @formatter:on - private static final Map jdbcToEs; + private static final Map jdbcToEs; static { jdbcToEs = Arrays.stream(DataType.values()) @@ -59,7 +60,7 @@ public enum DataType { /** * Compatible JDBC type */ - public final JDBCType jdbcType; + public final SQLType jdbcType; /** * Size of the type in bytes @@ -102,7 +103,7 @@ public enum DataType { private final Class javaClass; - DataType(JDBCType jdbcType, Class javaClass, int size, int defaultPrecision, int displaySize, boolean isInteger, boolean isRational, + DataType(SQLType jdbcType, Class javaClass, int size, int defaultPrecision, int displaySize, boolean isInteger, boolean isRational, boolean defaultDocValues) { this.esType = name().toLowerCase(Locale.ROOT); this.javaClass = javaClass; @@ -115,7 +116,7 @@ public enum DataType { this.defaultDocValues = defaultDocValues; } - DataType(JDBCType jdbcType, Class javaClass, int size, int defaultPrecision, int displaySize) { + DataType(SQLType jdbcType, Class javaClass, int size, int defaultPrecision, int displaySize) { this(jdbcType, javaClass, size, defaultPrecision, displaySize, false, false, true); } @@ -147,14 +148,14 @@ public boolean isPrimitive() { return this != OBJECT && this != NESTED; } - public static DataType fromJdbcType(JDBCType jdbcType) { + public static DataType fromJdbcType(SQLType jdbcType) { if (jdbcToEs.containsKey(jdbcType) == false) { throw new IllegalArgumentException("Unsupported JDBC type [" + jdbcType + "]"); } return jdbcToEs.get(jdbcType); } - public static Class fromJdbcTypeToJava(JDBCType jdbcType) { + public static Class fromJdbcTypeToJava(SQLType jdbcType) { if (jdbcToEs.containsKey(jdbcType) == false) { throw new IllegalArgumentException("Unsupported JDBC type [" + jdbcType + "]"); } diff --git a/x-pack/qa/sql/src/main/java/org/elasticsearch/xpack/qa/sql/jdbc/ResultSetTestCase.java b/x-pack/qa/sql/src/main/java/org/elasticsearch/xpack/qa/sql/jdbc/ResultSetTestCase.java index 447fc4f17e182..80580f3461ac3 100644 --- a/x-pack/qa/sql/src/main/java/org/elasticsearch/xpack/qa/sql/jdbc/ResultSetTestCase.java +++ b/x-pack/qa/sql/src/main/java/org/elasticsearch/xpack/qa/sql/jdbc/ResultSetTestCase.java @@ -24,13 +24,13 @@ import java.sql.Clob; import java.sql.Connection; import java.sql.DriverManager; -import java.sql.JDBCType; import java.sql.NClob; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.SQLFeatureNotSupportedException; +import java.sql.SQLType; import java.sql.Timestamp; import java.sql.Types; import java.util.Arrays; @@ -64,7 +64,7 @@ public class ResultSetTestCase extends JdbcIntegrationTestCase { static final Set fieldsNames = Stream.of("test_byte", "test_integer", "test_long", "test_short", "test_double", "test_float", "test_keyword") .collect(Collectors.toCollection(HashSet::new)); - static final Map,JDBCType> dateTimeTestingFields = new HashMap,JDBCType>(); + static final Map, SQLType> dateTimeTestingFields = new HashMap<>(); static final String SELECT_ALL_FIELDS = "SELECT test_boolean, test_byte, test_integer," + "test_long, test_short, test_double, test_float, test_keyword, test_date FROM test"; static final String SELECT_WILDCARD = "SELECT * FROM test"; @@ -193,17 +193,17 @@ public void testGettingInvalidByte() throws Exception { assertEquals(format(Locale.ROOT, "Numeric %s out of range", Double.toString(floatNotByte)), sqle.getMessage()); sqle = expectThrows(SQLException.class, () -> results.getByte("test_keyword")); - assertEquals(format(Locale.ROOT, "Unable to convert value [%.128s] of type [VARCHAR] to a Byte", randomString), + assertEquals(format(Locale.ROOT, "Unable to convert value [%.128s] of type [VARCHAR] to a Byte", randomString), sqle.getMessage()); sqle = expectThrows(SQLException.class, () -> results.getObject("test_keyword", Byte.class)); - assertEquals(format(Locale.ROOT, "Unable to convert value [%.128s] of type [VARCHAR] to a Byte", randomString), + assertEquals(format(Locale.ROOT, "Unable to convert value [%.128s] of type [VARCHAR] to a Byte", randomString), sqle.getMessage()); sqle = expectThrows(SQLException.class, () -> results.getByte("test_date")); - assertEquals(format(Locale.ROOT, "Unable to convert value [%.128s] of type [TIMESTAMP] to a Byte", randomDate), + assertEquals(format(Locale.ROOT, "Unable to convert value [%.128s] of type [TIMESTAMP] to a Byte", randomDate), sqle.getMessage()); sqle = expectThrows(SQLException.class, () -> results.getObject("test_date", Byte.class)); - assertEquals(format(Locale.ROOT, "Unable to convert value [%.128s] of type [TIMESTAMP] to a Byte", randomDate), + assertEquals(format(Locale.ROOT, "Unable to convert value [%.128s] of type [TIMESTAMP] to a Byte", randomDate), sqle.getMessage()); }); } @@ -249,7 +249,7 @@ public void testGettingValidShortWithCasting() throws Exception { doWithQuery(SELECT_WILDCARD, (results) -> { results.next(); for(Entry e : map.entrySet()) { - short actual = (short) results.getObject(e.getKey(), Short.class); + short actual = results.getObject(e.getKey(), Short.class); if (e.getValue() instanceof Double) { assertEquals("For field " + e.getKey(), Math.round(e.getValue().doubleValue()), results.getShort(e.getKey())); assertEquals("For field " + e.getKey(), Math.round(e.getValue().doubleValue()), actual); @@ -590,7 +590,7 @@ public void testGettingValidDoubleWithCasting() throws Exception { results.next(); for(Entry e : map.entrySet()) { assertEquals("For field " + e.getKey(), e.getValue().doubleValue(), results.getDouble(e.getKey()), 0.0d); - assertEquals("For field " + e.getKey(), + assertEquals("For field " + e.getKey(), e.getValue().doubleValue(), results.getObject(e.getKey(), Double.class), 0.0d); } }); @@ -673,7 +673,7 @@ public void testGettingValidFloatWithCasting() throws Exception { results.next(); for(Entry e : map.entrySet()) { assertEquals("For field " + e.getKey(), e.getValue().floatValue(), results.getFloat(e.getKey()), 0.0f); - assertEquals("For field " + e.getKey(), + assertEquals("For field " + e.getKey(), e.getValue().floatValue(), results.getObject(e.getKey(), Float.class), 0.0f); } }); @@ -746,7 +746,7 @@ public void testGettingBooleanValues() throws Exception { builder.field("test_integer", randomValueOtherThan(0, () -> randomInt())); builder.field("test_long", randomValueOtherThan(0L, () -> randomLong())); builder.field("test_short", randomValueOtherThan((short) 0, () -> randomShort())); - builder.field("test_double", randomValueOtherThanMany(i -> i < 1.0d && i > -1.0d && i < Double.MAX_VALUE + builder.field("test_double", randomValueOtherThanMany(i -> i < 1.0d && i > -1.0d && i < Double.MAX_VALUE && i > Double.MIN_VALUE, () -> randomDouble() * randomInt())); builder.field("test_float", randomValueOtherThanMany(i -> i < 1.0f && i > -1.0f && i < Float.MAX_VALUE && i > Float.MIN_VALUE, @@ -820,9 +820,9 @@ public void testGettingDateWithoutCalendar() throws Exception { assertEquals(results.getDate("test_date"), new java.sql.Date(connCalendar.getTimeInMillis())); assertEquals(results.getDate(9), new java.sql.Date(connCalendar.getTimeInMillis())); - assertEquals(results.getObject("test_date", java.sql.Date.class), + assertEquals(results.getObject("test_date", java.sql.Date.class), new java.sql.Date(randomLongDate - (randomLongDate % 86400000L))); - assertEquals(results.getObject(9, java.sql.Date.class), + assertEquals(results.getObject(9, java.sql.Date.class), new java.sql.Date(randomLongDate - (randomLongDate % 86400000L))); // bulk validation for all fields which are not of type date @@ -889,9 +889,9 @@ public void testGettingTimeWithoutCalendar() throws Exception { assertEquals(results.getTime("test_date"), new java.sql.Time(c.getTimeInMillis())); assertEquals(results.getTime(9), new java.sql.Time(c.getTimeInMillis())); - assertEquals(results.getObject("test_date", java.sql.Time.class), + assertEquals(results.getObject("test_date", java.sql.Time.class), new java.sql.Time(randomLongDate % 86400000L)); - assertEquals(results.getObject(9, java.sql.Time.class), + assertEquals(results.getObject(9, java.sql.Time.class), new java.sql.Time(randomLongDate % 86400000L)); validateErrorsForDateTimeTestsWithoutCalendar(results::getTime); @@ -1126,7 +1126,7 @@ public void testUnsupportedGetMethods() throws IOException, SQLException { assertThrowsUnsupportedAndExpectErrorMessage(() -> r.getRowId("test"), "RowId not supported"); assertThrowsUnsupportedAndExpectErrorMessage(() -> r.getRowId(1), "RowId not supported"); assertThrowsUnsupportedAndExpectErrorMessage(() -> r.getSQLXML("test"), "SQLXML not supported"); - assertThrowsUnsupportedAndExpectErrorMessage(() -> r.getSQLXML(1), "SQLXML not supported"); + assertThrowsUnsupportedAndExpectErrorMessage(() -> r.getSQLXML(1), "SQLXML not supported"); assertThrowsUnsupportedAndExpectErrorMessage(() -> r.getURL("test"), "URL not supported"); assertThrowsUnsupportedAndExpectErrorMessage(() -> r.getURL(1), "URL not supported"); } @@ -1425,7 +1425,7 @@ private void indexSimpleDocumentWithTrueValues(Long randomLongDate) throws IOExc * It returns a map containing the field name and its randomly generated value to be later used in checking the returned values. */ private Map createTestDataForNumericValueTypes(Supplier randomGenerator) throws Exception, IOException { - Map map = new HashMap(); + Map map = new HashMap<>(); createIndex("test"); updateMappingForNumericValuesTests("test"); @@ -1482,20 +1482,20 @@ private void assertThrowsWritesUnsupportedForUpdate(ThrowingRunnable r) { private void validateErrorsForDateTimeTestsWithoutCalendar(CheckedFunction method) { SQLException sqle; - for(Entry,JDBCType> field : dateTimeTestingFields.entrySet()) { + for (Entry, SQLType> field : dateTimeTestingFields.entrySet()) { sqle = expectThrows(SQLException.class, () -> method.apply(field.getKey().v1())); assertEquals( - format(Locale.ROOT, "Unable to convert value [%.128s] of type [%s] to a Long", + format(Locale.ROOT, "Unable to convert value [%.128s] of type [%s] to a Long", field.getKey().v2(), field.getValue()), sqle.getMessage()); } } private void validateErrorsForDateTimeTestsWithCalendar(Calendar c, CheckedBiFunction method) { SQLException sqle; - for(Entry,JDBCType> field : dateTimeTestingFields.entrySet()) { + for (Entry, SQLType> field : dateTimeTestingFields.entrySet()) { sqle = expectThrows(SQLException.class, () -> method.apply(field.getKey().v1(), c)); assertEquals( - format(Locale.ROOT, "Unable to convert value [%.128s] of type [%s] to a Long", + format(Locale.ROOT, "Unable to convert value [%.128s] of type [%s] to a Long", field.getKey().v2(), field.getValue()), sqle.getMessage()); } }