use of org.h2.value.ValueDate in project h2database by h2database.
the class DateTimeUtils method dateAndTimeFromValue.
/**
* Extracts date value and nanos of day from the specified value.
*
* @param value
* value to extract fields from
* @param provider
* the cast information provider
* @return array with date value and nanos of day
*/
public static long[] dateAndTimeFromValue(Value value, CastDataProvider provider) {
long dateValue = EPOCH_DATE_VALUE;
long timeNanos = 0;
if (value instanceof ValueTimestamp) {
ValueTimestamp v = (ValueTimestamp) value;
dateValue = v.getDateValue();
timeNanos = v.getTimeNanos();
} else if (value instanceof ValueDate) {
dateValue = ((ValueDate) value).getDateValue();
} else if (value instanceof ValueTime) {
timeNanos = ((ValueTime) value).getNanos();
} else if (value instanceof ValueTimestampTimeZone) {
ValueTimestampTimeZone v = (ValueTimestampTimeZone) value;
dateValue = v.getDateValue();
timeNanos = v.getTimeNanos();
} else if (value instanceof ValueTimeTimeZone) {
timeNanos = ((ValueTimeTimeZone) value).getNanos();
} else {
ValueTimestamp v = (ValueTimestamp) value.convertTo(TypeInfo.TYPE_TIMESTAMP, provider);
dateValue = v.getDateValue();
timeNanos = v.getTimeNanos();
}
return new long[] { dateValue, timeNanos };
}
use of org.h2.value.ValueDate in project h2database by h2database.
the class TestDate method testValueDate.
private void testValueDate() {
assertEquals("2000-01-01", LegacyDateTimeUtils.fromDate(null, null, Date.valueOf("2000-01-01")).getString());
assertEquals("0000-00-00", ValueDate.fromDateValue(0).getString());
assertEquals("9999-12-31", ValueDate.parse("9999-12-31").getString());
assertEquals("-9999-12-31", ValueDate.parse("-9999-12-31").getString());
ValueDate d1 = ValueDate.parse("2001-01-01");
assertEquals("2001-01-01", LegacyDateTimeUtils.toDate(null, null, d1).toString());
assertEquals("DATE '2001-01-01'", d1.getTraceSQL());
assertEquals("DATE '2001-01-01'", d1.toString());
assertEquals(Value.DATE, d1.getValueType());
long dv = d1.getDateValue();
assertEquals((int) ((dv >>> 32) ^ dv), d1.hashCode());
TypeInfo type = d1.getType();
assertEquals(d1.getString().length(), type.getDisplaySize());
assertEquals(ValueDate.PRECISION, type.getPrecision());
ValueDate d1b = ValueDate.parse("2001-01-01");
assertTrue(d1 == d1b);
Value.clearCache();
d1b = ValueDate.parse("2001-01-01");
assertFalse(d1 == d1b);
assertTrue(d1.equals(d1));
assertTrue(d1.equals(d1b));
assertTrue(d1b.equals(d1));
assertEquals(0, d1.compareTo(d1b, null, null));
assertEquals(0, d1b.compareTo(d1, null, null));
ValueDate d2 = ValueDate.parse("2002-02-02");
assertFalse(d1.equals(d2));
assertFalse(d2.equals(d1));
assertEquals(-1, d1.compareTo(d2, null, null));
assertEquals(1, d2.compareTo(d1, null, null));
}
use of org.h2.value.ValueDate in project h2database by h2database.
the class TestDate method testDateTimeUtils.
private void testDateTimeUtils() {
TimeZone old = TimeZone.getDefault();
/*
* java.util.TimeZone doesn't support LMT, so perform this test with
* fixed time zone offset
*/
TimeZone.setDefault(TimeZone.getTimeZone("GMT+01"));
DateTimeUtils.resetCalendar();
try {
ValueTimestamp ts1 = ValueTimestamp.parse("-999-08-07 13:14:15.16", null);
ValueTimestamp ts2 = ValueTimestamp.parse("19999-08-07 13:14:15.16", null);
ValueTime t1 = (ValueTime) ts1.convertTo(TypeInfo.TYPE_TIME);
ValueTime t2 = (ValueTime) ts2.convertTo(TypeInfo.TYPE_TIME);
ValueDate d1 = ts1.convertToDate(null);
ValueDate d2 = ts2.convertToDate(null);
assertEquals("-0999-08-07 13:14:15.16", ts1.getString());
assertEquals("-0999-08-07", d1.getString());
assertEquals("13:14:15.16", t1.getString());
assertEquals("19999-08-07 13:14:15.16", ts2.getString());
assertEquals("19999-08-07", d2.getString());
assertEquals("13:14:15.16", t2.getString());
TimeZone timeZone = TimeZone.getDefault();
ValueTimestamp ts1a = LegacyDateTimeUtils.fromTimestamp(null, timeZone, LegacyDateTimeUtils.toTimestamp(null, null, ts1));
ValueTimestamp ts2a = LegacyDateTimeUtils.fromTimestamp(null, timeZone, LegacyDateTimeUtils.toTimestamp(null, null, ts2));
assertEquals("-0999-08-07 13:14:15.16", ts1a.getString());
assertEquals("19999-08-07 13:14:15.16", ts2a.getString());
} finally {
TimeZone.setDefault(old);
DateTimeUtils.resetCalendar();
}
}
use of org.h2.value.ValueDate in project apollo by salesforce.
the class DateTimeUtils method dateAndTimeFromValue.
/**
* Extracts date value and nanos of day from the specified value.
*
* @param value
* value to extract fields from
* @param provider
* the cast information provider
* @return array with date value and nanos of day
*/
public static long[] dateAndTimeFromValue(Value value, CastDataProvider provider) {
long dateValue = EPOCH_DATE_VALUE;
long timeNanos = 0;
if (value instanceof ValueTimestamp) {
ValueTimestamp v = (ValueTimestamp) value;
dateValue = v.getDateValue();
timeNanos = v.getTimeNanos();
} else if (value instanceof ValueDate) {
dateValue = ((ValueDate) value).getDateValue();
} else if (value instanceof ValueTime) {
timeNanos = ((ValueTime) value).getNanos();
} else if (value instanceof ValueTimestampTimeZone) {
ValueTimestampTimeZone v = (ValueTimestampTimeZone) value;
dateValue = v.getDateValue();
timeNanos = v.getTimeNanos();
} else if (value instanceof ValueTimeTimeZone) {
timeNanos = ((ValueTimeTimeZone) value).getNanos();
} else {
ValueTimestamp v = (ValueTimestamp) value.convertTo(TypeInfo.TYPE_TIMESTAMP, provider);
dateValue = v.getDateValue();
timeNanos = v.getTimeNanos();
}
return new long[] { dateValue, timeNanos };
}
use of org.h2.value.ValueDate in project apollo by salesforce.
the class StreamTransfer method writeValue.
/**
* Write a value.
*
* @param v the value
* @throws IOException on failure
*/
public void writeValue(Value v, DataOutputStream out) throws IOException {
int type = v.getValueType();
switch(type) {
case Value.NULL:
writeInt(NULL, out);
break;
case Value.BINARY:
if (version >= Constants.TCP_PROTOCOL_VERSION_20) {
writeInt(BINARY, out);
writeBytes(v.getBytesNoCopy(), out);
break;
}
// $FALL-THROUGH$
case Value.VARBINARY:
writeInt(VARBINARY, out);
writeBytes(v.getBytesNoCopy(), out);
break;
case Value.JAVA_OBJECT:
writeInt(JAVA_OBJECT, out);
writeBytes(v.getBytesNoCopy(), out);
break;
case Value.UUID:
{
writeInt(UUID, out);
ValueUuid uuid = (ValueUuid) v;
writeLong(uuid.getHigh(), out);
writeLong(uuid.getLow(), out);
break;
}
case Value.BOOLEAN:
writeInt(BOOLEAN, out);
writeBoolean(v.getBoolean(), out);
break;
case Value.TINYINT:
writeInt(TINYINT, out);
writeByte(v.getByte(), out);
break;
case Value.TIME:
writeInt(TIME, out);
writeLong(((ValueTime) v).getNanos(), out);
break;
case Value.TIME_TZ:
{
ValueTimeTimeZone t = (ValueTimeTimeZone) v;
if (version >= Constants.TCP_PROTOCOL_VERSION_19) {
writeInt(TIME_TZ, out);
writeLong(t.getNanos(), out);
writeInt(t.getTimeZoneOffsetSeconds(), out);
} else {
writeInt(TIME, out);
/*
* Don't call SessionRemote.currentTimestamp(), it may require own remote call
* and old server will not return custom time zone anyway.
*/
ValueTimestampTimeZone current = DateTimeUtils.currentTimestamp(DateTimeUtils.getTimeZone());
writeLong(DateTimeUtils.normalizeNanosOfDay(t.getNanos() + (t.getTimeZoneOffsetSeconds() - current.getTimeZoneOffsetSeconds()) * DateTimeUtils.NANOS_PER_DAY), out);
}
break;
}
case Value.DATE:
writeInt(DATE, out);
writeLong(((ValueDate) v).getDateValue(), out);
break;
case Value.TIMESTAMP:
{
writeInt(TIMESTAMP, out);
ValueTimestamp ts = (ValueTimestamp) v;
writeLong(ts.getDateValue(), out);
writeLong(ts.getTimeNanos(), out);
break;
}
case Value.TIMESTAMP_TZ:
{
writeInt(TIMESTAMP_TZ, out);
ValueTimestampTimeZone ts = (ValueTimestampTimeZone) v;
writeLong(ts.getDateValue(), out);
writeLong(ts.getTimeNanos(), out);
int timeZoneOffset = ts.getTimeZoneOffsetSeconds();
writeInt(//
version >= Constants.TCP_PROTOCOL_VERSION_19 ? timeZoneOffset : timeZoneOffset / 60, out);
break;
}
case Value.DECFLOAT:
if (version >= Constants.TCP_PROTOCOL_VERSION_20) {
writeInt(DECFLOAT, out);
writeString(v.getString(), out);
break;
}
// $FALL-THROUGH$
case Value.NUMERIC:
writeInt(NUMERIC, out);
writeString(v.getString(), out);
break;
case Value.DOUBLE:
writeInt(DOUBLE, out);
writeDouble(v.getDouble(), out);
break;
case Value.REAL:
writeInt(REAL, out);
writeFloat(v.getFloat(), out);
break;
case Value.INTEGER:
writeInt(INTEGER, out);
writeInt(v.getInt(), out);
break;
case Value.BIGINT:
writeInt(BIGINT, out);
writeLong(v.getLong(), out);
break;
case Value.SMALLINT:
writeInt(SMALLINT, out);
if (version >= Constants.TCP_PROTOCOL_VERSION_20) {
writeShort(v.getShort(), out);
} else {
writeInt(v.getShort(), out);
}
break;
case Value.VARCHAR:
writeInt(VARCHAR, out);
writeString(v.getString(), out);
break;
case Value.VARCHAR_IGNORECASE:
writeInt(VARCHAR_IGNORECASE, out);
writeString(v.getString(), out);
break;
case Value.CHAR:
writeInt(CHAR, out);
writeString(v.getString(), out);
break;
case Value.BLOB:
{
writeInt(BLOB, out);
ValueBlob lob = (ValueBlob) v;
LobData lobData = lob.getLobData();
long length = lob.octetLength();
if (lobData instanceof LobDataDatabase) {
LobDataDatabase lobDataDatabase = (LobDataDatabase) lobData;
writeLong(-1, out);
writeInt(lobDataDatabase.getTableId(), out);
writeLong(lobDataDatabase.getLobId(), out);
writeBytes(calculateLobMac(lobDataDatabase.getLobId()), out);
writeLong(length, out);
break;
}
if (length < 0) {
throw DbException.get(ErrorCode.CONNECTION_BROKEN_1, "length=" + length);
}
writeLong(length, out);
long written = IOUtils.copyAndCloseInput(lob.getInputStream(), out);
if (written != length) {
throw DbException.get(ErrorCode.CONNECTION_BROKEN_1, "length:" + length + " written:" + written);
}
writeInt(LOB_MAGIC, out);
break;
}
case Value.CLOB:
{
writeInt(CLOB, out);
ValueClob lob = (ValueClob) v;
LobData lobData = lob.getLobData();
long charLength = lob.charLength();
if (lobData instanceof LobDataDatabase) {
LobDataDatabase lobDataDatabase = (LobDataDatabase) lobData;
writeLong(-1, out);
writeInt(lobDataDatabase.getTableId(), out);
writeLong(lobDataDatabase.getLobId(), out);
writeBytes(calculateLobMac(lobDataDatabase.getLobId()), out);
if (version >= Constants.TCP_PROTOCOL_VERSION_20) {
writeLong(lob.octetLength(), out);
}
writeLong(charLength, out);
break;
}
if (charLength < 0) {
throw DbException.get(ErrorCode.CONNECTION_BROKEN_1, "length=" + charLength);
}
writeLong(charLength, out);
Reader reader = lob.getReader();
Data.copyString(reader, out);
writeInt(LOB_MAGIC, out);
break;
}
case Value.ARRAY:
{
writeInt(ARRAY, out);
ValueArray va = (ValueArray) v;
Value[] list = va.getList();
int len = list.length;
writeInt(len, out);
for (Value value : list) {
writeValue(value, out);
}
break;
}
case Value.ROW:
{
writeInt(version >= Constants.TCP_PROTOCOL_VERSION_18 ? ROW : ARRAY, out);
ValueRow va = (ValueRow) v;
Value[] list = va.getList();
int len = list.length;
writeInt(len, out);
for (Value value : list) {
writeValue(value, out);
}
break;
}
case Value.ENUM:
{
writeInt(ENUM, out);
writeInt(v.getInt(), out);
if (version < Constants.TCP_PROTOCOL_VERSION_20) {
writeString(v.getString(), out);
}
break;
}
case Value.GEOMETRY:
writeInt(GEOMETRY, out);
writeBytes(v.getBytesNoCopy(), out);
break;
case Value.INTERVAL_YEAR:
case Value.INTERVAL_MONTH:
case Value.INTERVAL_DAY:
case Value.INTERVAL_HOUR:
case Value.INTERVAL_MINUTE:
if (version >= Constants.TCP_PROTOCOL_VERSION_18) {
ValueInterval interval = (ValueInterval) v;
int ordinal = type - Value.INTERVAL_YEAR;
if (interval.isNegative()) {
ordinal = ~ordinal;
}
writeInt(INTERVAL, out);
writeByte((byte) ordinal, out);
writeLong(interval.getLeading(), out);
} else {
writeInt(VARCHAR, out);
writeString(v.getString(), out);
}
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:
if (version >= Constants.TCP_PROTOCOL_VERSION_18) {
ValueInterval interval = (ValueInterval) v;
int ordinal = type - Value.INTERVAL_YEAR;
if (interval.isNegative()) {
ordinal = ~ordinal;
}
writeInt(INTERVAL, out);
writeByte((byte) ordinal, out);
writeLong(interval.getLeading(), out);
writeLong(interval.getRemaining(), out);
} else {
writeInt(VARCHAR, out);
writeString(v.getString(), out);
}
break;
case Value.JSON:
{
writeInt(JSON, out);
writeBytes(v.getBytesNoCopy(), out);
break;
}
default:
throw DbException.get(ErrorCode.CONNECTION_BROKEN_1, "type=" + type);
}
}
Aggregations