|
@@ -3,9 +3,7 @@
|
|
|
* or more contributor license agreements. Licensed under the Elastic License;
|
|
|
* you may not use this file except in compliance with the Elastic License.
|
|
|
*/
|
|
|
-package org.elasticsearch.xpack.sql.jdbc.jdbc;
|
|
|
-
|
|
|
-import org.elasticsearch.xpack.sql.jdbc.type.DataType;
|
|
|
+package org.elasticsearch.xpack.sql.jdbc;
|
|
|
|
|
|
import java.sql.Date;
|
|
|
import java.sql.SQLException;
|
|
@@ -115,7 +113,7 @@ final class TypeConverter {
|
|
|
* Converts object val from columnType to type
|
|
|
*/
|
|
|
@SuppressWarnings("unchecked")
|
|
|
- static <T> T convert(Object val, DataType columnType, Class<T> type, String typeString) throws SQLException {
|
|
|
+ static <T> T convert(Object val, EsType columnType, Class<T> type, String typeString) throws SQLException {
|
|
|
if (type == null) {
|
|
|
return (T) convert(val, columnType, typeString);
|
|
|
}
|
|
@@ -123,7 +121,7 @@ final class TypeConverter {
|
|
|
// converting a Long to a Timestamp shouldn't be possible according to the spec,
|
|
|
// it feels a little brittle to check this scenario here and I don't particularly like it
|
|
|
// TODO: can we do any better or should we go over the spec and allow getLong(date) to be valid?
|
|
|
- if (!(type == Long.class && columnType == DataType.DATE) && type.isInstance(val)) {
|
|
|
+ if (!(type == Long.class && columnType == EsType.DATE) && type.isInstance(val)) {
|
|
|
try {
|
|
|
return type.cast(val);
|
|
|
} catch (ClassCastException cce) {
|
|
@@ -192,7 +190,7 @@ final class TypeConverter {
|
|
|
/**
|
|
|
* Converts the object from JSON representation to the specified JDBCType
|
|
|
*/
|
|
|
- static Object convert(Object v, DataType columnType, String typeString) throws SQLException {
|
|
|
+ static Object convert(Object v, EsType columnType, String typeString) throws SQLException {
|
|
|
switch (columnType) {
|
|
|
case NULL:
|
|
|
return null;
|
|
@@ -273,18 +271,18 @@ final class TypeConverter {
|
|
|
return nativeValue == null ? null : String.valueOf(nativeValue);
|
|
|
}
|
|
|
|
|
|
- private static <T> T failConversion(Object value, DataType columnType, String typeString, Class<T> target) throws SQLException {
|
|
|
+ private static <T> T failConversion(Object value, EsType columnType, String typeString, Class<T> target) throws SQLException {
|
|
|
return failConversion(value, columnType, typeString, target, null);
|
|
|
}
|
|
|
|
|
|
- private static <T> T failConversion(Object value, DataType columnType, String typeString, Class<T> target, Exception e)
|
|
|
+ private static <T> T failConversion(Object value, EsType columnType, String typeString, Class<T> target, Exception e)
|
|
|
throws SQLException {
|
|
|
String message = format(Locale.ROOT, "Unable to convert value [%.128s] of type [%s] to [%s]", value, columnType,
|
|
|
typeString);
|
|
|
throw e != null ? new SQLException(message, e) : new SQLException(message);
|
|
|
}
|
|
|
|
|
|
- private static Boolean asBoolean(Object val, DataType columnType, String typeString) throws SQLException {
|
|
|
+ private static Boolean asBoolean(Object val, EsType columnType, String typeString) throws SQLException {
|
|
|
switch (columnType) {
|
|
|
case BOOLEAN:
|
|
|
case BYTE:
|
|
@@ -304,7 +302,7 @@ final class TypeConverter {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private static Byte asByte(Object val, DataType columnType, String typeString) throws SQLException {
|
|
|
+ private static Byte asByte(Object val, EsType columnType, String typeString) throws SQLException {
|
|
|
switch (columnType) {
|
|
|
case BOOLEAN:
|
|
|
return Byte.valueOf(((Boolean) val).booleanValue() ? (byte) 1 : (byte) 0);
|
|
@@ -331,7 +329,7 @@ final class TypeConverter {
|
|
|
return failConversion(val, columnType, typeString, Byte.class);
|
|
|
}
|
|
|
|
|
|
- private static Short asShort(Object val, DataType columnType, String typeString) throws SQLException {
|
|
|
+ private static Short asShort(Object val, EsType columnType, String typeString) throws SQLException {
|
|
|
switch (columnType) {
|
|
|
case BOOLEAN:
|
|
|
return Short.valueOf(((Boolean) val).booleanValue() ? (short) 1 : (short) 0);
|
|
@@ -357,7 +355,7 @@ final class TypeConverter {
|
|
|
return failConversion(val, columnType, typeString, Short.class);
|
|
|
}
|
|
|
|
|
|
- private static Integer asInteger(Object val, DataType columnType, String typeString) throws SQLException {
|
|
|
+ private static Integer asInteger(Object val, EsType columnType, String typeString) throws SQLException {
|
|
|
switch (columnType) {
|
|
|
case BOOLEAN:
|
|
|
return Integer.valueOf(((Boolean) val).booleanValue() ? 1 : 0);
|
|
@@ -383,7 +381,7 @@ final class TypeConverter {
|
|
|
return failConversion(val, columnType, typeString, Integer.class);
|
|
|
}
|
|
|
|
|
|
- private static Long asLong(Object val, DataType columnType, String typeString) throws SQLException {
|
|
|
+ private static Long asLong(Object val, EsType columnType, String typeString) throws SQLException {
|
|
|
switch (columnType) {
|
|
|
case BOOLEAN:
|
|
|
return Long.valueOf(((Boolean) val).booleanValue() ? 1 : 0);
|
|
@@ -415,7 +413,7 @@ final class TypeConverter {
|
|
|
return failConversion(val, columnType, typeString, Long.class);
|
|
|
}
|
|
|
|
|
|
- private static Float asFloat(Object val, DataType columnType, String typeString) throws SQLException {
|
|
|
+ private static Float asFloat(Object val, EsType columnType, String typeString) throws SQLException {
|
|
|
switch (columnType) {
|
|
|
case BOOLEAN:
|
|
|
return Float.valueOf(((Boolean) val).booleanValue() ? 1 : 0);
|
|
@@ -441,7 +439,7 @@ final class TypeConverter {
|
|
|
return failConversion(val, columnType, typeString, Float.class);
|
|
|
}
|
|
|
|
|
|
- private static Double asDouble(Object val, DataType columnType, String typeString) throws SQLException {
|
|
|
+ private static Double asDouble(Object val, EsType columnType, String typeString) throws SQLException {
|
|
|
switch (columnType) {
|
|
|
case BOOLEAN:
|
|
|
return Double.valueOf(((Boolean) val).booleanValue() ? 1 : 0);
|
|
@@ -467,48 +465,48 @@ final class TypeConverter {
|
|
|
return failConversion(val, columnType, typeString, Double.class);
|
|
|
}
|
|
|
|
|
|
- private static Date asDate(Object val, DataType columnType, String typeString) throws SQLException {
|
|
|
- if (columnType == DataType.DATE) {
|
|
|
+ private static Date asDate(Object val, EsType columnType, String typeString) throws SQLException {
|
|
|
+ if (columnType == EsType.DATE) {
|
|
|
return new Date(utcMillisRemoveTime(((Number) val).longValue()));
|
|
|
}
|
|
|
return failConversion(val, columnType, typeString, Date.class);
|
|
|
}
|
|
|
|
|
|
- private static Time asTime(Object val, DataType columnType, String typeString) throws SQLException {
|
|
|
- if (columnType == DataType.DATE) {
|
|
|
+ private static Time asTime(Object val, EsType columnType, String typeString) throws SQLException {
|
|
|
+ if (columnType == EsType.DATE) {
|
|
|
return new Time(utcMillisRemoveDate(((Number) val).longValue()));
|
|
|
}
|
|
|
return failConversion(val, columnType, typeString, Time.class);
|
|
|
}
|
|
|
|
|
|
- private static Timestamp asTimestamp(Object val, DataType columnType, String typeString) throws SQLException {
|
|
|
- if (columnType == DataType.DATE) {
|
|
|
+ private static Timestamp asTimestamp(Object val, EsType columnType, String typeString) throws SQLException {
|
|
|
+ if (columnType == EsType.DATE) {
|
|
|
return new Timestamp(((Number) val).longValue());
|
|
|
}
|
|
|
return failConversion(val, columnType, typeString, Timestamp.class);
|
|
|
}
|
|
|
|
|
|
- private static byte[] asByteArray(Object val, DataType columnType, String typeString) throws SQLException {
|
|
|
+ private static byte[] asByteArray(Object val, EsType columnType, String typeString) throws SQLException {
|
|
|
throw new SQLFeatureNotSupportedException();
|
|
|
}
|
|
|
|
|
|
- private static LocalDate asLocalDate(Object val, DataType columnType, String typeString) throws SQLException {
|
|
|
+ private static LocalDate asLocalDate(Object val, EsType columnType, String typeString) throws SQLException {
|
|
|
throw new SQLFeatureNotSupportedException();
|
|
|
}
|
|
|
|
|
|
- private static LocalTime asLocalTime(Object val, DataType columnType, String typeString) throws SQLException {
|
|
|
+ private static LocalTime asLocalTime(Object val, EsType columnType, String typeString) throws SQLException {
|
|
|
throw new SQLFeatureNotSupportedException();
|
|
|
}
|
|
|
|
|
|
- private static LocalDateTime asLocalDateTime(Object val, DataType columnType, String typeString) throws SQLException {
|
|
|
+ private static LocalDateTime asLocalDateTime(Object val, EsType columnType, String typeString) throws SQLException {
|
|
|
throw new SQLFeatureNotSupportedException();
|
|
|
}
|
|
|
|
|
|
- private static OffsetTime asOffsetTime(Object val, DataType columnType, String typeString) throws SQLException {
|
|
|
+ private static OffsetTime asOffsetTime(Object val, EsType columnType, String typeString) throws SQLException {
|
|
|
throw new SQLFeatureNotSupportedException();
|
|
|
}
|
|
|
|
|
|
- private static OffsetDateTime asOffsetDateTime(Object val, DataType columnType, String typeString) throws SQLException {
|
|
|
+ private static OffsetDateTime asOffsetDateTime(Object val, EsType columnType, String typeString) throws SQLException {
|
|
|
throw new SQLFeatureNotSupportedException();
|
|
|
}
|
|
|
|