提交 c3f92764 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Add storage format for INTERVAL data type

上级 513b5621
......@@ -13,6 +13,7 @@ import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Arrays;
import org.h2.api.ErrorCode;
import org.h2.api.IntervalQualifier;
import org.h2.engine.Database;
import org.h2.engine.Mode;
import org.h2.message.DbException;
......@@ -38,6 +39,7 @@ import org.h2.value.ValueDouble;
import org.h2.value.ValueFloat;
import org.h2.value.ValueGeometry;
import org.h2.value.ValueInt;
import org.h2.value.ValueInterval;
import org.h2.value.ValueJavaObject;
import org.h2.value.ValueLobDb;
import org.h2.value.ValueLong;
......@@ -440,6 +442,32 @@ public class ValueDataType implements DataType {
put(b);
break;
}
case Value.INTERVAL_YEAR:
case Value.INTERVAL_MONTH:
case Value.INTERVAL_DAY:
case Value.INTERVAL_HOUR:
case Value.INTERVAL_MINUTE: {
ValueInterval interval = (ValueInterval) v;
buff.put((byte) Value.INTERVAL_YEAR).
put((byte) (type - Value.INTERVAL_YEAR)).
putVarLong(interval.getLeading());
break;
}
case Value.INTERVAL_SECOND:
case Value.INTERVAL_YEAR_TO_MONTH:
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: {
ValueInterval interval = (ValueInterval) v;
buff.put((byte) Value.INTERVAL_YEAR).
put((byte) (type - Value.INTERVAL_YEAR)).
putVarLong(interval.getLeading()).
putVarLong(interval.getRemaining());
break;
}
default:
if (JdbcUtils.customDataTypesHandler != null) {
byte[] b = v.getBytesNoCopy();
......@@ -543,6 +571,11 @@ public class ValueDataType implements DataType {
return ValueStringIgnoreCase.get(readString(buff));
case Value.STRING_FIXED:
return ValueStringFixed.get(readString(buff));
case Value.INTERVAL_YEAR: {
int ordinal = buff.get() & 0xff;
return ValueInterval.from(IntervalQualifier.valueOf(ordinal), readVarLong(buff),
ordinal < 5 ? 0 : readVarLong(buff));
}
case FLOAT_0_1:
return ValueFloat.get(0);
case FLOAT_0_1 + 1:
......
......@@ -19,6 +19,7 @@ import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Arrays;
import org.h2.api.ErrorCode;
import org.h2.api.IntervalQualifier;
import org.h2.engine.Constants;
import org.h2.message.DbException;
import org.h2.tools.SimpleResultSet;
......@@ -39,6 +40,7 @@ import org.h2.value.ValueDouble;
import org.h2.value.ValueFloat;
import org.h2.value.ValueGeometry;
import org.h2.value.ValueInt;
import org.h2.value.ValueInterval;
import org.h2.value.ValueJavaObject;
import org.h2.value.ValueLob;
import org.h2.value.ValueLobDb;
......@@ -655,6 +657,32 @@ public class Data {
}
break;
}
case Value.INTERVAL_YEAR:
case Value.INTERVAL_MONTH:
case Value.INTERVAL_DAY:
case Value.INTERVAL_HOUR:
case Value.INTERVAL_MINUTE: {
ValueInterval interval = (ValueInterval) v;
writeByte((byte) Value.INTERVAL_YEAR);
writeByte((byte) (type - Value.INTERVAL_YEAR));
writeVarLong(interval.getLeading());
break;
}
case Value.INTERVAL_SECOND:
case Value.INTERVAL_YEAR_TO_MONTH:
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: {
ValueInterval interval = (ValueInterval) v;
writeByte((byte) Value.INTERVAL_YEAR);
writeByte((byte) (type - Value.INTERVAL_YEAR));
writeVarLong(interval.getLeading());
writeVarLong(interval.getRemaining());
break;
}
default:
if (JdbcUtils.customDataTypesHandler != null) {
byte[] b = v.getBytesNoCopy();
......@@ -843,6 +871,11 @@ public class Data {
}
return ValueResultSet.get(rs);
}
case Value.INTERVAL_YEAR: {
int ordinal = readByte() & 0xff;
return ValueInterval.from(IntervalQualifier.valueOf(ordinal), readVarLong(),
ordinal < 5 ? 0 : readVarLong());
}
case CUSTOM_DATA_TYPE: {
if (JdbcUtils.customDataTypesHandler != null) {
int customType = readVarInt();
......@@ -1082,6 +1115,25 @@ public class Data {
}
return len;
}
case Value.INTERVAL_YEAR:
case Value.INTERVAL_MONTH:
case Value.INTERVAL_DAY:
case Value.INTERVAL_HOUR:
case Value.INTERVAL_MINUTE: {
ValueInterval interval = (ValueInterval) v;
return 2 + getVarLongLen(interval.getLeading());
}
case Value.INTERVAL_SECOND:
case Value.INTERVAL_YEAR_TO_MONTH:
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: {
ValueInterval interval = (ValueInterval) v;
return 2 + getVarLongLen(interval.getLeading()) + getVarLongLen(interval.getRemaining());
}
default:
if (JdbcUtils.customDataTypesHandler != null) {
byte[] b = v.getBytesNoCopy();
......
......@@ -157,6 +157,26 @@ public class ValueInterval extends Value {
return IntervalQualifier.valueOf(type - INTERVAL_YEAR);
}
/**
* Returns value of leading field of this interval. For {@code SECOND}
* intervals returns integer part of seconds.
*
* @return value of leading field
*/
public long getLeading() {
return leading;
}
/**
* Returns combined value of remaining fields of this interval. For
* {@code SECOND} intervals returns nanoseconds.
*
* @return combined value of remaining fields
*/
public long getRemaining() {
return remaining;
}
@Override
public void set(PreparedStatement prep, int parameterIndex) throws SQLException {
prep.setString(parameterIndex, getString());
......
......@@ -71,6 +71,8 @@ INSERT INTO TEST VALUES (
INTERVAL -'1:2:3.123456789' HOUR TO SECOND, INTERVAL -'1:2.123456789' MINUTE TO SECOND);
> update count: 2
@reconnect
SELECT I01, I02, I03, I04, I05, I06 FROM TEST ORDER BY ID;
> I01 I02 I03 I04 I05 I06
> ------------------ ------------------- ----------------- ------------------ -------------------- --------------------------
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论