提交 40e1f3ce authored 作者: Noel Grandin's avatar Noel Grandin

split up the rather large convertTo method

and unify the convert-from-string code with the corresponding target
type, so the logic is easy to see on a per-target-type basis.
上级 9ba3f6ea
...@@ -716,9 +716,84 @@ public abstract class Value { ...@@ -716,9 +716,84 @@ public abstract class Value {
return this; return this;
} }
try { try {
// decimal conversion
switch (targetType) { switch (targetType) {
case BOOLEAN: { case NULL:
return ValueNull.INSTANCE;
case BOOLEAN:
return convertToBoolean();
case BYTE:
return convertToByte(column);
case SHORT:
return convertToShort(column);
case INT:
return convertToInt(column);
case LONG:
return convertToLong(column);
case DECIMAL:
return convertToDecimal();
case DOUBLE:
return convertToDouble();
case FLOAT:
return convertToFloat();
case DATE:
return convertToDate();
case TIME:
return convertToTime();
case TIMESTAMP:
return convertToTimestamp(mode);
case TIMESTAMP_TZ:
return convertToTimestampTimeZone();
case BYTES:
return convertToBytes(mode);
case STRING:
return convertToString(mode);
case STRING_IGNORECASE:
return convertToStringIgnoreCase(mode);
case STRING_FIXED:
return convertToStringFixed(precision, mode);
case JAVA_OBJECT:
return convertToJavaObject();
case ENUM:
return convertToEnumInternal(enumerators);
case BLOB:
return convertToBlob();
case CLOB:
return convertToClob();
case UUID:
return convertToUuid();
case GEOMETRY:
return convertToGeometry();
case Value.INTERVAL_YEAR:
case Value.INTERVAL_MONTH:
case Value.INTERVAL_YEAR_TO_MONTH:
return convertToInterval1(targetType);
case Value.INTERVAL_DAY:
case Value.INTERVAL_HOUR:
case Value.INTERVAL_MINUTE:
case Value.INTERVAL_SECOND:
case Value.INTERVAL_DAY_TO_HOUR:
case Value.INTERVAL_DAY_TO_MINUTE:
case Value.INTERVAL_DAY_TO_SECOND:
case Value.INTERVAL_HOUR_TO_MINUTE:
case Value.INTERVAL_HOUR_TO_SECOND:
case Value.INTERVAL_MINUTE_TO_SECOND:
return convertToInterval2(targetType);
case ARRAY:
return convertToArray();
case RESULT_SET:
return convertToResultSet();
default:
if (JdbcUtils.customDataTypesHandler != null) {
return JdbcUtils.customDataTypesHandler.convert(this, targetType);
}
throw getDataConversionError(targetType);
}
} catch (NumberFormatException e) {
throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, e, getString());
}
}
private ValueBoolean convertToBoolean() {
switch (getType()) { switch (getType()) {
case BYTE: case BYTE:
case SHORT: case SHORT:
...@@ -736,12 +811,22 @@ public abstract class Value { ...@@ -736,12 +811,22 @@ public abstract class Value {
case JAVA_OBJECT: case JAVA_OBJECT:
case UUID: case UUID:
case ENUM: case ENUM:
throw DbException.get( throw getDataConversionError(BOOLEAN);
ErrorCode.DATA_CONVERSION_ERROR_1, getString()); }
String s = getString();
if (s.equalsIgnoreCase("true") || s.equalsIgnoreCase("t") || s.equalsIgnoreCase("yes")
|| s.equalsIgnoreCase("y")) {
return ValueBoolean.TRUE;
} else if (s.equalsIgnoreCase("false") || s.equalsIgnoreCase("f") || s.equalsIgnoreCase("no")
|| s.equalsIgnoreCase("n")) {
return ValueBoolean.FALSE;
} else {
// convert to a number, and if it is not 0 then it is true
return ValueBoolean.get(new BigDecimal(s).signum() != 0);
} }
break;
} }
case BYTE: {
private ValueByte convertToByte(Object column) {
switch (getType()) { switch (getType()) {
case BOOLEAN: case BOOLEAN:
return ValueByte.get(getBoolean() ? (byte) 1 : (byte) 0); return ValueByte.get(getBoolean() ? (byte) 1 : (byte) 0);
...@@ -760,12 +845,12 @@ public abstract class Value { ...@@ -760,12 +845,12 @@ public abstract class Value {
case BYTES: case BYTES:
return ValueByte.get((byte) Integer.parseInt(getString(), 16)); return ValueByte.get((byte) Integer.parseInt(getString(), 16));
case TIMESTAMP_TZ: case TIMESTAMP_TZ:
throw DbException.get( throw getDataConversionError(BYTE);
ErrorCode.DATA_CONVERSION_ERROR_1, getString());
} }
break; return ValueByte.get(Byte.parseByte(getString().trim()));
} }
case SHORT: {
private ValueShort convertToShort(Object column) {
switch (getType()) { switch (getType()) {
case BOOLEAN: case BOOLEAN:
return ValueShort.get(getBoolean() ? (short) 1 : (short) 0); return ValueShort.get(getBoolean() ? (short) 1 : (short) 0);
...@@ -785,12 +870,12 @@ public abstract class Value { ...@@ -785,12 +870,12 @@ public abstract class Value {
case BYTES: case BYTES:
return ValueShort.get((short) Integer.parseInt(getString(), 16)); return ValueShort.get((short) Integer.parseInt(getString(), 16));
case TIMESTAMP_TZ: case TIMESTAMP_TZ:
throw DbException.get( throw getDataConversionError(SHORT);
ErrorCode.DATA_CONVERSION_ERROR_1, getString());
} }
break; return ValueShort.get(Short.parseShort(getString().trim()));
} }
case INT: {
private ValueInt convertToInt(Object column) {
switch (getType()) { switch (getType()) {
case BOOLEAN: case BOOLEAN:
return ValueInt.get(getBoolean() ? 1 : 0); return ValueInt.get(getBoolean() ? 1 : 0);
...@@ -809,12 +894,12 @@ public abstract class Value { ...@@ -809,12 +894,12 @@ public abstract class Value {
case BYTES: case BYTES:
return ValueInt.get((int) Long.parseLong(getString(), 16)); return ValueInt.get((int) Long.parseLong(getString(), 16));
case TIMESTAMP_TZ: case TIMESTAMP_TZ:
throw DbException.get( throw getDataConversionError(INT);
ErrorCode.DATA_CONVERSION_ERROR_1, getString());
} }
break; return ValueInt.get(Integer.parseInt(getString().trim()));
} }
case LONG: {
private ValueLong convertToLong(Object column) {
switch (getType()) { switch (getType()) {
case BOOLEAN: case BOOLEAN:
return ValueLong.get(getBoolean() ? 1 : 0); return ValueLong.get(getBoolean() ? 1 : 0);
...@@ -838,12 +923,12 @@ public abstract class Value { ...@@ -838,12 +923,12 @@ public abstract class Value {
return ValueLong.get(Long.parseLong(getString(), 16)); return ValueLong.get(Long.parseLong(getString(), 16));
} }
case TIMESTAMP_TZ: case TIMESTAMP_TZ:
throw DbException.get( throw getDataConversionError(LONG);
ErrorCode.DATA_CONVERSION_ERROR_1, getString());
} }
break; return ValueLong.get(Long.parseLong(getString().trim()));
} }
case DECIMAL: {
private ValueDecimal convertToDecimal() {
switch (getType()) { switch (getType()) {
case BOOLEAN: case BOOLEAN:
return (ValueDecimal) (getBoolean() ? ValueDecimal.ONE : ValueDecimal.ZERO); return (ValueDecimal) (getBoolean() ? ValueDecimal.ONE : ValueDecimal.ZERO);
...@@ -857,27 +942,25 @@ public abstract class Value { ...@@ -857,27 +942,25 @@ public abstract class Value {
case DOUBLE: { case DOUBLE: {
double d = getDouble(); double d = getDouble();
if (Double.isInfinite(d) || Double.isNaN(d)) { if (Double.isInfinite(d) || Double.isNaN(d)) {
throw DbException.get( throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, Double.toString(d));
ErrorCode.DATA_CONVERSION_ERROR_1, Double.toString(d));
} }
return ValueDecimal.get(BigDecimal.valueOf(d)); return ValueDecimal.get(BigDecimal.valueOf(d));
} }
case FLOAT: { case FLOAT: {
float f = getFloat(); float f = getFloat();
if (Float.isInfinite(f) || Float.isNaN(f)) { if (Float.isInfinite(f) || Float.isNaN(f)) {
throw DbException.get( throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, Float.toString(f));
ErrorCode.DATA_CONVERSION_ERROR_1, Float.toString(f));
} }
// better rounding behavior than BigDecimal.valueOf(f) // better rounding behavior than BigDecimal.valueOf(f)
return ValueDecimal.get(new BigDecimal(Float.toString(f))); return ValueDecimal.get(new BigDecimal(Float.toString(f)));
} }
case TIMESTAMP_TZ: case TIMESTAMP_TZ:
throw DbException.get( throw getDataConversionError(DECIMAL);
ErrorCode.DATA_CONVERSION_ERROR_1, getString());
} }
break; return ValueDecimal.get(new BigDecimal(getString().trim()));
} }
case DOUBLE: {
private ValueDouble convertToDouble() {
switch (getType()) { switch (getType()) {
case BOOLEAN: case BOOLEAN:
return ValueDouble.get(getBoolean() ? 1 : 0); return ValueDouble.get(getBoolean() ? 1 : 0);
...@@ -893,12 +976,12 @@ public abstract class Value { ...@@ -893,12 +976,12 @@ public abstract class Value {
return ValueDouble.get(getFloat()); return ValueDouble.get(getFloat());
case ENUM: case ENUM:
case TIMESTAMP_TZ: case TIMESTAMP_TZ:
throw DbException.get( throw getDataConversionError(DOUBLE);
ErrorCode.DATA_CONVERSION_ERROR_1, getString());
} }
break; return ValueDouble.get(Double.parseDouble(getString().trim()));
} }
case FLOAT: {
private ValueFloat convertToFloat() {
switch (getType()) { switch (getType()) {
case BOOLEAN: case BOOLEAN:
return ValueFloat.get(getBoolean() ? 1 : 0); return ValueFloat.get(getBoolean() ? 1 : 0);
...@@ -914,20 +997,19 @@ public abstract class Value { ...@@ -914,20 +997,19 @@ public abstract class Value {
return ValueFloat.get((float) getDouble()); return ValueFloat.get((float) getDouble());
case ENUM: case ENUM:
case TIMESTAMP_TZ: case TIMESTAMP_TZ:
throw DbException.get( throw getDataConversionError(FLOAT);
ErrorCode.DATA_CONVERSION_ERROR_1, getString());
} }
break; return ValueFloat.get(Float.parseFloat(getString().trim()));
} }
case DATE: {
private ValueDate convertToDate() {
switch (getType()) { switch (getType()) {
case TIME: case TIME:
// because the time has set the date to 1970-01-01, // because the time has set the date to 1970-01-01,
// this will be the result // this will be the result
return ValueDate.fromDateValue(DateTimeUtils.EPOCH_DATE_VALUE); return ValueDate.fromDateValue(DateTimeUtils.EPOCH_DATE_VALUE);
case TIMESTAMP: case TIMESTAMP:
return ValueDate.fromDateValue( return ValueDate.fromDateValue(((ValueTimestamp) this).getDateValue());
((ValueTimestamp) this).getDateValue());
case TIMESTAMP_TZ: { case TIMESTAMP_TZ: {
ValueTimestampTimeZone ts = (ValueTimestampTimeZone) this; ValueTimestampTimeZone ts = (ValueTimestampTimeZone) this;
long dateValue = ts.getDateValue(), timeNanos = ts.getTimeNanos(); long dateValue = ts.getDateValue(), timeNanos = ts.getTimeNanos();
...@@ -935,20 +1017,19 @@ public abstract class Value { ...@@ -935,20 +1017,19 @@ public abstract class Value {
return ValueDate.fromMillis(millis); return ValueDate.fromMillis(millis);
} }
case ENUM: case ENUM:
throw DbException.get( throw getDataConversionError(DATE);
ErrorCode.DATA_CONVERSION_ERROR_1, getString());
} }
break; return ValueDate.parse(getString().trim());
} }
case TIME: {
private ValueTime convertToTime() {
switch (getType()) { switch (getType()) {
case DATE: case DATE:
// need to normalize the year, month and day because a date // need to normalize the year, month and day because a date
// has the time set to 0, the result will be 0 // has the time set to 0, the result will be 0
return ValueTime.fromNanos(0); return ValueTime.fromNanos(0);
case TIMESTAMP: case TIMESTAMP:
return ValueTime.fromNanos( return ValueTime.fromNanos(((ValueTimestamp) this).getTimeNanos());
((ValueTimestamp) this).getTimeNanos());
case TIMESTAMP_TZ: { case TIMESTAMP_TZ: {
ValueTimestampTimeZone ts = (ValueTimestampTimeZone) this; ValueTimestampTimeZone ts = (ValueTimestampTimeZone) this;
long dateValue = ts.getDateValue(), timeNanos = ts.getTimeNanos(); long dateValue = ts.getDateValue(), timeNanos = ts.getTimeNanos();
...@@ -956,19 +1037,17 @@ public abstract class Value { ...@@ -956,19 +1037,17 @@ public abstract class Value {
return ValueTime.fromNanos(DateTimeUtils.nanosFromDate(millis) + timeNanos % 1_000_000); return ValueTime.fromNanos(DateTimeUtils.nanosFromDate(millis) + timeNanos % 1_000_000);
} }
case ENUM: case ENUM:
throw DbException.get( throw getDataConversionError(TIME);
ErrorCode.DATA_CONVERSION_ERROR_1, getString());
} }
break; return ValueTime.parse(getString().trim());
} }
case TIMESTAMP: {
private ValueTimestamp convertToTimestamp(Mode mode) {
switch (getType()) { switch (getType()) {
case TIME: case TIME:
return DateTimeUtils.normalizeTimestamp( return DateTimeUtils.normalizeTimestamp(0, ((ValueTime) this).getNanos());
0, ((ValueTime) this).getNanos());
case DATE: case DATE:
return ValueTimestamp.fromDateValueAndNanos( return ValueTimestamp.fromDateValueAndNanos(((ValueDate) this).getDateValue(), 0);
((ValueDate) this).getDateValue(), 0);
case TIMESTAMP_TZ: { case TIMESTAMP_TZ: {
ValueTimestampTimeZone ts = (ValueTimestampTimeZone) this; ValueTimestampTimeZone ts = (ValueTimestampTimeZone) this;
long dateValue = ts.getDateValue(), timeNanos = ts.getTimeNanos(); long dateValue = ts.getDateValue(), timeNanos = ts.getTimeNanos();
...@@ -976,33 +1055,30 @@ public abstract class Value { ...@@ -976,33 +1055,30 @@ public abstract class Value {
return ValueTimestamp.fromMillisNanos(millis, (int) (timeNanos % 1_000_000)); return ValueTimestamp.fromMillisNanos(millis, (int) (timeNanos % 1_000_000));
} }
case ENUM: case ENUM:
throw DbException.get( throw getDataConversionError(TIMESTAMP);
ErrorCode.DATA_CONVERSION_ERROR_1, getString());
} }
break; return ValueTimestamp.parse(getString().trim(), mode);
} }
case TIMESTAMP_TZ: {
private ValueTimestampTimeZone convertToTimestampTimeZone() {
switch (getType()) { switch (getType()) {
case TIME: { case TIME: {
ValueTimestamp ts = DateTimeUtils.normalizeTimestamp(0, ((ValueTime) this).getNanos()); ValueTimestamp ts = DateTimeUtils.normalizeTimestamp(0, ((ValueTime) this).getNanos());
return DateTimeUtils.timestampTimeZoneFromLocalDateValueAndNanos( return DateTimeUtils.timestampTimeZoneFromLocalDateValueAndNanos(ts.getDateValue(), ts.getTimeNanos());
ts.getDateValue(), ts.getTimeNanos());
} }
case DATE: case DATE:
return DateTimeUtils.timestampTimeZoneFromLocalDateValueAndNanos( return DateTimeUtils.timestampTimeZoneFromLocalDateValueAndNanos(((ValueDate) this).getDateValue(), 0);
((ValueDate) this).getDateValue(), 0);
case TIMESTAMP: { case TIMESTAMP: {
ValueTimestamp ts = (ValueTimestamp) this; ValueTimestamp ts = (ValueTimestamp) this;
return DateTimeUtils.timestampTimeZoneFromLocalDateValueAndNanos( return DateTimeUtils.timestampTimeZoneFromLocalDateValueAndNanos(ts.getDateValue(), ts.getTimeNanos());
ts.getDateValue(), ts.getTimeNanos());
} }
case ENUM: case ENUM:
throw DbException.get( throw getDataConversionError(TIMESTAMP_TZ);
ErrorCode.DATA_CONVERSION_ERROR_1, getString());
} }
break; return ValueTimestampTimeZone.parse(getString().trim());
} }
case BYTES: {
private ValueBytes convertToBytes(Mode mode) {
switch (getType()) { switch (getType()) {
case JAVA_OBJECT: case JAVA_OBJECT:
case BLOB: case BLOB:
...@@ -1011,13 +1087,10 @@ public abstract class Value { ...@@ -1011,13 +1087,10 @@ public abstract class Value {
case GEOMETRY: case GEOMETRY:
return ValueBytes.getNoCopy(getBytes()); return ValueBytes.getNoCopy(getBytes());
case BYTE: case BYTE:
return ValueBytes.getNoCopy(new byte[]{getByte()}); return ValueBytes.getNoCopy(new byte[] { getByte() });
case SHORT: { case SHORT: {
int x = getShort(); int x = getShort();
return ValueBytes.getNoCopy(new byte[]{ return ValueBytes.getNoCopy(new byte[] { (byte) (x >> 8), (byte) x });
(byte) (x >> 8),
(byte) x
});
} }
case INT: { case INT: {
byte[] b = new byte[4]; byte[] b = new byte[4];
...@@ -1031,12 +1104,14 @@ public abstract class Value { ...@@ -1031,12 +1104,14 @@ public abstract class Value {
} }
case ENUM: case ENUM:
case TIMESTAMP_TZ: case TIMESTAMP_TZ:
throw DbException.get( throw getDataConversionError(BYTES);
ErrorCode.DATA_CONVERSION_ERROR_1, getString());
} }
break; String s = getString();
return ValueBytes.getNoCopy(mode != null && mode.charToBinaryInUtf8 ? s.getBytes(StandardCharsets.UTF_8)
: StringUtils.convertHexToBytes(s.trim()));
} }
case STRING: {
private ValueString convertToString(Mode mode) {
String s; String s;
if (getType() == BYTES && mode != null && mode.charToBinaryInUtf8) { if (getType() == BYTES && mode != null && mode.charToBinaryInUtf8) {
// Bugfix - Can't use the locale encoding when enabling // Bugfix - Can't use the locale encoding when enabling
...@@ -1047,9 +1122,10 @@ public abstract class Value { ...@@ -1047,9 +1122,10 @@ public abstract class Value {
} else { } else {
s = getString(); s = getString();
} }
return ValueString.get(s); return (ValueString) ValueString.get(s);
} }
case STRING_IGNORECASE: {
private ValueString convertToStringIgnoreCase(Mode mode) {
String s; String s;
if (getType() == BYTES && mode != null && mode.charToBinaryInUtf8) { if (getType() == BYTES && mode != null && mode.charToBinaryInUtf8) {
s = new String(getBytesNoCopy(), StandardCharsets.UTF_8); s = new String(getBytesNoCopy(), StandardCharsets.UTF_8);
...@@ -1058,7 +1134,8 @@ public abstract class Value { ...@@ -1058,7 +1134,8 @@ public abstract class Value {
} }
return ValueStringIgnoreCase.get(s); return ValueStringIgnoreCase.get(s);
} }
case STRING_FIXED: {
private ValueString convertToStringFixed(int precision, Mode mode) {
String s; String s;
if (getType() == BYTES && mode != null && mode.charToBinaryInUtf8) { if (getType() == BYTES && mode != null && mode.charToBinaryInUtf8) {
s = new String(getBytesNoCopy(), StandardCharsets.UTF_8); s = new String(getBytesNoCopy(), StandardCharsets.UTF_8);
...@@ -1067,20 +1144,20 @@ public abstract class Value { ...@@ -1067,20 +1144,20 @@ public abstract class Value {
} }
return ValueStringFixed.get(s, precision, mode); return ValueStringFixed.get(s, precision, mode);
} }
case JAVA_OBJECT: {
private ValueJavaObject convertToJavaObject() {
switch (getType()) { switch (getType()) {
case BYTES: case BYTES:
case BLOB: case BLOB:
return ValueJavaObject.getNoCopy( return ValueJavaObject.getNoCopy(null, getBytesNoCopy(), getDataHandler());
null, getBytesNoCopy(), getDataHandler());
case ENUM: case ENUM:
case TIMESTAMP_TZ: case TIMESTAMP_TZ:
throw DbException.get( throw getDataConversionError(JAVA_OBJECT);
ErrorCode.DATA_CONVERSION_ERROR_1, getString());
} }
break; return ValueJavaObject.getNoCopy(null, StringUtils.convertHexToBytes(getString().trim()), getDataHandler());
} }
case ENUM: {
private ValueEnum convertToEnumInternal(String[] enumerators) {
switch (getType()) { switch (getType()) {
case BYTE: case BYTE:
case SHORT: case SHORT:
...@@ -1093,48 +1170,48 @@ public abstract class Value { ...@@ -1093,48 +1170,48 @@ public abstract class Value {
case STRING_FIXED: case STRING_FIXED:
return ValueEnum.get(enumerators, getString()); return ValueEnum.get(enumerators, getString());
case JAVA_OBJECT: case JAVA_OBJECT:
Object object = JdbcUtils.deserialize(getBytesNoCopy(), Object object = JdbcUtils.deserialize(getBytesNoCopy(), getDataHandler());
getDataHandler());
if (object instanceof String) { if (object instanceof String) {
return ValueEnum.get(enumerators, (String) object); return ValueEnum.get(enumerators, (String) object);
} else if (object instanceof Integer) { } else if (object instanceof Integer) {
return ValueEnum.get(enumerators, (int) object); return ValueEnum.get(enumerators, (int) object);
} }
//$FALL-THROUGH$ //$FALL-THROUGH$
default:
throw DbException.get(
ErrorCode.DATA_CONVERSION_ERROR_1, getString());
} }
throw getDataConversionError(ENUM);
} }
case BLOB: {
private ValueLobDb convertToBlob() {
switch (getType()) { switch (getType()) {
case BYTES: case BYTES:
return ValueLobDb.createSmallLob( return ValueLobDb.createSmallLob(Value.BLOB, getBytesNoCopy());
Value.BLOB, getBytesNoCopy());
case TIMESTAMP_TZ: case TIMESTAMP_TZ:
throw DbException.get( throw getDataConversionError(BLOB);
ErrorCode.DATA_CONVERSION_ERROR_1, getString());
} }
break; return ValueLobDb.createSmallLob(BLOB, StringUtils.convertHexToBytes(getString().trim()));
}
private ValueLobDb convertToClob() {
return ValueLobDb.createSmallLob(CLOB, getString().getBytes(StandardCharsets.UTF_8));
} }
case UUID: {
private ValueUuid convertToUuid() {
switch (getType()) { switch (getType()) {
case BYTES: case BYTES:
return ValueUuid.get(getBytesNoCopy()); return ValueUuid.get(getBytesNoCopy());
case JAVA_OBJECT: case JAVA_OBJECT:
Object object = JdbcUtils.deserialize(getBytesNoCopy(), Object object = JdbcUtils.deserialize(getBytesNoCopy(), getDataHandler());
getDataHandler());
if (object instanceof java.util.UUID) { if (object instanceof java.util.UUID) {
return ValueUuid.get((java.util.UUID) object); return ValueUuid.get((java.util.UUID) object);
} }
throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, getString()); //$FALL-THROUGH$
case TIMESTAMP_TZ: case TIMESTAMP_TZ:
throw DbException.get( throw getDataConversionError(UUID);
ErrorCode.DATA_CONVERSION_ERROR_1, getString());
} }
break; return ValueUuid.get(getString());
} }
case GEOMETRY: {
private ValueGeometry convertToGeometry() {
switch (getType()) { switch (getType()) {
case BYTES: case BYTES:
return ValueGeometry.get(getBytesNoCopy()); return ValueGeometry.get(getBytesNoCopy());
...@@ -1145,22 +1222,20 @@ public abstract class Value { ...@@ -1145,22 +1222,20 @@ public abstract class Value {
} }
//$FALL-THROUGH$ //$FALL-THROUGH$
case TIMESTAMP_TZ: case TIMESTAMP_TZ:
throw DbException.get( throw getDataConversionError(GEOMETRY);
ErrorCode.DATA_CONVERSION_ERROR_1, getString());
} }
break; return ValueGeometry.get(getString());
} }
case Value.INTERVAL_YEAR:
case Value.INTERVAL_MONTH: private ValueInterval convertToInterval1(int targetType) {
case Value.INTERVAL_YEAR_TO_MONTH:
switch (getType()) { switch (getType()) {
case Value.STRING: case Value.STRING:
case Value.STRING_IGNORECASE: case Value.STRING_IGNORECASE:
case Value.STRING_FIXED: { case Value.STRING_FIXED: {
String s = getString(); String s = getString();
try { try {
return DateTimeUtils.parseFormattedInterval( return (ValueInterval) DateTimeUtils
IntervalQualifier.valueOf(targetType - Value.INTERVAL_YEAR), s) .parseFormattedInterval(IntervalQualifier.valueOf(targetType - Value.INTERVAL_YEAR), s)
.convertTo(targetType); .convertTo(targetType);
} catch (Exception e) { } catch (Exception e) {
throw DbException.get(ErrorCode.INVALID_DATETIME_CONSTANT_2, e, "INTERVAL", s); throw DbException.get(ErrorCode.INVALID_DATETIME_CONSTANT_2, e, "INTERVAL", s);
...@@ -1169,29 +1244,21 @@ public abstract class Value { ...@@ -1169,29 +1244,21 @@ public abstract class Value {
case Value.INTERVAL_YEAR: case Value.INTERVAL_YEAR:
case Value.INTERVAL_MONTH: case Value.INTERVAL_MONTH:
case Value.INTERVAL_YEAR_TO_MONTH: case Value.INTERVAL_YEAR_TO_MONTH:
return DateTimeUtils.intervalFromAbsolute( return DateTimeUtils.intervalFromAbsolute(IntervalQualifier.valueOf(targetType - Value.INTERVAL_YEAR),
IntervalQualifier.valueOf(targetType - Value.INTERVAL_YEAR),
DateTimeUtils.intervalToAbsolute((ValueInterval) this)); DateTimeUtils.intervalToAbsolute((ValueInterval) this));
} }
break; throw getDataConversionError(targetType);
case Value.INTERVAL_DAY: }
case Value.INTERVAL_HOUR:
case Value.INTERVAL_MINUTE: private ValueInterval convertToInterval2(int targetType) {
case Value.INTERVAL_SECOND:
case Value.INTERVAL_DAY_TO_HOUR:
case Value.INTERVAL_DAY_TO_MINUTE:
case Value.INTERVAL_DAY_TO_SECOND:
case Value.INTERVAL_HOUR_TO_MINUTE:
case Value.INTERVAL_HOUR_TO_SECOND:
case Value.INTERVAL_MINUTE_TO_SECOND:
switch (getType()) { switch (getType()) {
case Value.STRING: case Value.STRING:
case Value.STRING_IGNORECASE: case Value.STRING_IGNORECASE:
case Value.STRING_FIXED: { case Value.STRING_FIXED: {
String s = getString(); String s = getString();
try { try {
return DateTimeUtils.parseFormattedInterval( return (ValueInterval) DateTimeUtils
IntervalQualifier.valueOf(targetType - Value.INTERVAL_YEAR), s) .parseFormattedInterval(IntervalQualifier.valueOf(targetType - Value.INTERVAL_YEAR), s)
.convertTo(targetType); .convertTo(targetType);
} catch (Exception e) { } catch (Exception e) {
throw DbException.get(ErrorCode.INVALID_DATETIME_CONSTANT_2, e, "INTERVAL", s); throw DbException.get(ErrorCode.INVALID_DATETIME_CONSTANT_2, e, "INTERVAL", s);
...@@ -1207,94 +1274,30 @@ public abstract class Value { ...@@ -1207,94 +1274,30 @@ public abstract class Value {
case Value.INTERVAL_HOUR_TO_MINUTE: case Value.INTERVAL_HOUR_TO_MINUTE:
case Value.INTERVAL_HOUR_TO_SECOND: case Value.INTERVAL_HOUR_TO_SECOND:
case Value.INTERVAL_MINUTE_TO_SECOND: case Value.INTERVAL_MINUTE_TO_SECOND:
return DateTimeUtils.intervalFromAbsolute( return DateTimeUtils.intervalFromAbsolute(IntervalQualifier.valueOf(targetType - Value.INTERVAL_YEAR),
IntervalQualifier.valueOf(targetType - Value.INTERVAL_YEAR),
DateTimeUtils.intervalToAbsolute((ValueInterval) this)); DateTimeUtils.intervalToAbsolute((ValueInterval) this));
} }
break; throw getDataConversionError(targetType);
}
// conversion by parsing the string value
String s = getString();
switch (targetType) {
case NULL:
return ValueNull.INSTANCE;
case BOOLEAN: {
if (s.equalsIgnoreCase("true") ||
s.equalsIgnoreCase("t") ||
s.equalsIgnoreCase("yes") ||
s.equalsIgnoreCase("y")) {
return ValueBoolean.TRUE;
} else if (s.equalsIgnoreCase("false") ||
s.equalsIgnoreCase("f") ||
s.equalsIgnoreCase("no") ||
s.equalsIgnoreCase("n")) {
return ValueBoolean.FALSE;
} else {
// convert to a number, and if it is not 0 then it is true
return ValueBoolean.get(new BigDecimal(s).signum() != 0);
} }
private ValueArray convertToArray() {
return ValueArray.get(new Value[] { ValueString.get(getString()) });
} }
case BYTE:
return ValueByte.get(Byte.parseByte(s.trim())); private ValueResultSet convertToResultSet() {
case SHORT: String s = getString();
return ValueShort.get(Short.parseShort(s.trim()));
case INT:
return ValueInt.get(Integer.parseInt(s.trim()));
case LONG:
return ValueLong.get(Long.parseLong(s.trim()));
case DECIMAL:
return ValueDecimal.get(new BigDecimal(s.trim()));
case TIME:
return ValueTime.parse(s.trim());
case DATE:
return ValueDate.parse(s.trim());
case TIMESTAMP:
return ValueTimestamp.parse(s.trim(), mode);
case TIMESTAMP_TZ:
return ValueTimestampTimeZone.parse(s.trim());
case BYTES:
return ValueBytes.getNoCopy(mode != null && mode.charToBinaryInUtf8 ?
s.getBytes(StandardCharsets.UTF_8): StringUtils.convertHexToBytes(s.trim()));
case JAVA_OBJECT:
return ValueJavaObject.getNoCopy(null,
StringUtils.convertHexToBytes(s.trim()), getDataHandler());
case DOUBLE:
return ValueDouble.get(Double.parseDouble(s.trim()));
case FLOAT:
return ValueFloat.get(Float.parseFloat(s.trim()));
case CLOB:
return ValueLobDb.createSmallLob(
CLOB, s.getBytes(StandardCharsets.UTF_8));
case BLOB:
return ValueLobDb.createSmallLob(
BLOB, StringUtils.convertHexToBytes(s.trim()));
case ARRAY:
return ValueArray.get(new Value[]{ValueString.get(s)});
case RESULT_SET: {
SimpleResultSet rs = new SimpleResultSet(); SimpleResultSet rs = new SimpleResultSet();
rs.setAutoClose(false); rs.setAutoClose(false);
rs.addColumn("X", Types.VARCHAR, s.length(), 0); rs.addColumn("X", Types.VARCHAR, s.length(), 0);
rs.addRow(s); rs.addRow(s);
return ValueResultSet.get(rs); return ValueResultSet.get(rs);
} }
case UUID:
return ValueUuid.get(s); private DbException getDataConversionError(int targetType) {
case GEOMETRY:
return ValueGeometry.get(s);
default:
if (JdbcUtils.customDataTypesHandler != null) {
return JdbcUtils.customDataTypesHandler.convert(this, targetType);
}
DataType from = DataType.getDataType(getType()); DataType from = DataType.getDataType(getType());
DataType to = DataType.getDataType(targetType); DataType to = DataType.getDataType(targetType);
throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, (from != null ? from.name : "type=" + getType())
(from != null ? from.name : "type=" + getType()) + " to " + " to " + (to != null ? to.name : "type=" + targetType));
+ (to != null ? to.name : "type=" + targetType));
}
} catch (NumberFormatException e) {
throw DbException.get(
ErrorCode.DATA_CONVERSION_ERROR_1, e, getString());
}
} }
/** /**
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论