use of org.h2.value.ValueDate in project h2database by h2database.
the class DateTimeFunctions method extract.
/**
* Extracts specified field from the specified date-time value.
*
* @param part
* the date part
* @param value
* the date-time value
* @return extracted field
*/
public static Value extract(String part, Value value) {
Value result;
int field = getDatePart(part);
if (field != EPOCH) {
result = ValueInt.get(getIntDatePart(value, field));
} else {
// Case where we retrieve the EPOCH time.
// First we retrieve the dateValue and his time in nanoseconds.
long[] a = DateTimeUtils.dateAndTimeFromValue(value);
long dateValue = a[0];
long timeNanos = a[1];
// We compute the time in nanoseconds and the total number of days.
BigDecimal timeNanosBigDecimal = new BigDecimal(timeNanos);
BigDecimal numberOfDays = new BigDecimal(DateTimeUtils.absoluteDayFromDateValue(dateValue));
BigDecimal nanosSeconds = new BigDecimal(1_000_000_000);
BigDecimal secondsPerDay = new BigDecimal(DateTimeUtils.SECONDS_PER_DAY);
// Case where the value is of type time e.g. '10:00:00'
if (value instanceof ValueTime) {
// In order to retrieve the EPOCH time we only have to convert the time
// in nanoseconds (previously retrieved) in seconds.
result = ValueDecimal.get(timeNanosBigDecimal.divide(nanosSeconds));
} else if (value instanceof ValueDate) {
// Case where the value is of type date '2000:01:01', we have to retrieve the
// total number of days and multiply it by the number of seconds in a day.
result = ValueDecimal.get(numberOfDays.multiply(secondsPerDay));
} else if (value instanceof ValueTimestampTimeZone) {
// Case where the value is a of type ValueTimestampTimeZone
// ('2000:01:01 10:00:00+05').
// We retrieve the time zone offset in minutes
ValueTimestampTimeZone v = (ValueTimestampTimeZone) value;
BigDecimal timeZoneOffsetSeconds = new BigDecimal(v.getTimeZoneOffsetMins() * 60);
// Sum the time in nanoseconds and the total number of days in seconds
// and adding the timeZone offset in seconds.
result = ValueDecimal.get(timeNanosBigDecimal.divide(nanosSeconds).add(numberOfDays.multiply(secondsPerDay)).subtract(timeZoneOffsetSeconds));
} else {
// By default, we have the date and the time ('2000:01:01 10:00:00') if no type
// is given.
// We just have to sum the time in nanoseconds and the total number of days in
// seconds.
result = ValueDecimal.get(timeNanosBigDecimal.divide(nanosSeconds).add(numberOfDays.multiply(secondsPerDay)));
}
}
return result;
}
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
* @return array with date value and nanos of day
*/
public static long[] dateAndTimeFromValue(Value value) {
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 {
ValueTimestamp v = (ValueTimestamp) value.convertTo(Value.TIMESTAMP);
dateValue = v.getDateValue();
timeNanos = v.getTimeNanos();
}
return new long[] { dateValue, timeNanos };
}
use of org.h2.value.ValueDate in project h2database by h2database.
the class ValueDataType method writeValue.
private void writeValue(WriteBuffer buff, Value v) {
if (v == ValueNull.INSTANCE) {
buff.put((byte) 0);
return;
}
int type = v.getType();
switch(type) {
case Value.BOOLEAN:
buff.put((byte) (v.getBoolean() ? BOOLEAN_TRUE : BOOLEAN_FALSE));
break;
case Value.BYTE:
buff.put((byte) type).put(v.getByte());
break;
case Value.SHORT:
buff.put((byte) type).putShort(v.getShort());
break;
case Value.ENUM:
case Value.INT:
{
int x = v.getInt();
if (x < 0) {
buff.put((byte) INT_NEG).putVarInt(-x);
} else if (x < 16) {
buff.put((byte) (INT_0_15 + x));
} else {
buff.put((byte) type).putVarInt(x);
}
break;
}
case Value.LONG:
{
long x = v.getLong();
if (x < 0) {
buff.put((byte) LONG_NEG).putVarLong(-x);
} else if (x < 8) {
buff.put((byte) (LONG_0_7 + x));
} else {
buff.put((byte) type).putVarLong(x);
}
break;
}
case Value.DECIMAL:
{
BigDecimal x = v.getBigDecimal();
if (BigDecimal.ZERO.equals(x)) {
buff.put((byte) DECIMAL_0_1);
} else if (BigDecimal.ONE.equals(x)) {
buff.put((byte) (DECIMAL_0_1 + 1));
} else {
int scale = x.scale();
BigInteger b = x.unscaledValue();
int bits = b.bitLength();
if (bits <= 63) {
if (scale == 0) {
buff.put((byte) DECIMAL_SMALL_0).putVarLong(b.longValue());
} else {
buff.put((byte) DECIMAL_SMALL).putVarInt(scale).putVarLong(b.longValue());
}
} else {
byte[] bytes = b.toByteArray();
buff.put((byte) type).putVarInt(scale).putVarInt(bytes.length).put(bytes);
}
}
break;
}
case Value.TIME:
{
ValueTime t = (ValueTime) v;
long nanos = t.getNanos();
long millis = nanos / 1000000;
nanos -= millis * 1000000;
buff.put((byte) type).putVarLong(millis).putVarLong(nanos);
break;
}
case Value.DATE:
{
long x = ((ValueDate) v).getDateValue();
buff.put((byte) type).putVarLong(x);
break;
}
case Value.TIMESTAMP:
{
ValueTimestamp ts = (ValueTimestamp) v;
long dateValue = ts.getDateValue();
long nanos = ts.getTimeNanos();
long millis = nanos / 1000000;
nanos -= millis * 1000000;
buff.put((byte) type).putVarLong(dateValue).putVarLong(millis).putVarLong(nanos);
break;
}
case Value.TIMESTAMP_TZ:
{
ValueTimestampTimeZone ts = (ValueTimestampTimeZone) v;
long dateValue = ts.getDateValue();
long nanos = ts.getTimeNanos();
long millis = nanos / 1000000;
nanos -= millis * 1000000;
buff.put((byte) type).putVarLong(dateValue).putVarLong(millis).putVarLong(nanos).putVarInt(ts.getTimeZoneOffsetMins());
break;
}
case Value.JAVA_OBJECT:
{
byte[] b = v.getBytesNoCopy();
buff.put((byte) type).putVarInt(b.length).put(b);
break;
}
case Value.BYTES:
{
byte[] b = v.getBytesNoCopy();
int len = b.length;
if (len < 32) {
buff.put((byte) (BYTES_0_31 + len)).put(b);
} else {
buff.put((byte) type).putVarInt(b.length).put(b);
}
break;
}
case Value.UUID:
{
ValueUuid uuid = (ValueUuid) v;
buff.put((byte) type).putLong(uuid.getHigh()).putLong(uuid.getLow());
break;
}
case Value.STRING:
{
String s = v.getString();
int len = s.length();
if (len < 32) {
buff.put((byte) (STRING_0_31 + len)).putStringData(s, len);
} else {
buff.put((byte) type);
writeString(buff, s);
}
break;
}
case Value.STRING_IGNORECASE:
case Value.STRING_FIXED:
buff.put((byte) type);
writeString(buff, v.getString());
break;
case Value.DOUBLE:
{
double x = v.getDouble();
if (x == 1.0d) {
buff.put((byte) (DOUBLE_0_1 + 1));
} else {
long d = Double.doubleToLongBits(x);
if (d == ValueDouble.ZERO_BITS) {
buff.put((byte) DOUBLE_0_1);
} else {
buff.put((byte) type).putVarLong(Long.reverse(d));
}
}
break;
}
case Value.FLOAT:
{
float x = v.getFloat();
if (x == 1.0f) {
buff.put((byte) (FLOAT_0_1 + 1));
} else {
int f = Float.floatToIntBits(x);
if (f == ValueFloat.ZERO_BITS) {
buff.put((byte) FLOAT_0_1);
} else {
buff.put((byte) type).putVarInt(Integer.reverse(f));
}
}
break;
}
case Value.BLOB:
case Value.CLOB:
{
buff.put((byte) type);
ValueLobDb lob = (ValueLobDb) v;
byte[] small = lob.getSmall();
if (small == null) {
buff.putVarInt(-3).putVarInt(lob.getTableId()).putVarLong(lob.getLobId()).putVarLong(lob.getPrecision());
} else {
buff.putVarInt(small.length).put(small);
}
break;
}
case Value.ARRAY:
{
Value[] list = ((ValueArray) v).getList();
buff.put((byte) type).putVarInt(list.length);
for (Value x : list) {
writeValue(buff, x);
}
break;
}
case Value.RESULT_SET:
{
buff.put((byte) type);
try {
ResultSet rs = ((ValueResultSet) v).getResultSet();
rs.beforeFirst();
ResultSetMetaData meta = rs.getMetaData();
int columnCount = meta.getColumnCount();
buff.putVarInt(columnCount);
for (int i = 0; i < columnCount; i++) {
writeString(buff, meta.getColumnName(i + 1));
buff.putVarInt(meta.getColumnType(i + 1)).putVarInt(meta.getPrecision(i + 1)).putVarInt(meta.getScale(i + 1));
}
while (rs.next()) {
buff.put((byte) 1);
for (int i = 0; i < columnCount; i++) {
int t = org.h2.value.DataType.getValueTypeFromResultSet(meta, i + 1);
Value val = org.h2.value.DataType.readValue(null, rs, i + 1, t);
writeValue(buff, val);
}
}
buff.put((byte) 0);
rs.beforeFirst();
} catch (SQLException e) {
throw DbException.convert(e);
}
break;
}
case Value.GEOMETRY:
{
byte[] b = v.getBytes();
int len = b.length;
buff.put((byte) type).putVarInt(len).put(b);
break;
}
default:
if (JdbcUtils.customDataTypesHandler != null) {
byte[] b = v.getBytesNoCopy();
buff.put((byte) CUSTOM_DATA_TYPE).putVarInt(type).putVarInt(b.length).put(b);
break;
}
DbException.throwInternalError("type=" + v.getType());
}
}
use of org.h2.value.ValueDate in project h2database by h2database.
the class PgServerThread method writeDataColumn.
private void writeDataColumn(Value v, int pgType, boolean text) throws IOException {
if (v == ValueNull.INSTANCE) {
writeInt(-1);
return;
}
if (text) {
// plain text
switch(pgType) {
case PgServer.PG_TYPE_BOOL:
writeInt(1);
dataOut.writeByte(v.getBoolean() ? 't' : 'f');
break;
case PgServer.PG_TYPE_BYTEA:
{
byte[] bytes = v.getBytesNoCopy();
int length = bytes.length;
int cnt = length;
for (int i = 0; i < length; i++) {
byte b = bytes[i];
if (b < 32 || b > 126) {
cnt += 3;
} else if (b == 92) {
cnt++;
}
}
byte[] data = new byte[cnt];
for (int i = 0, j = 0; i < length; i++) {
byte b = bytes[i];
if (b < 32 || b > 126) {
data[j++] = '\\';
data[j++] = (byte) (((b >>> 6) & 3) + '0');
data[j++] = (byte) (((b >>> 3) & 7) + '0');
data[j++] = (byte) ((b & 7) + '0');
} else if (b == 92) {
data[j++] = '\\';
data[j++] = '\\';
} else {
data[j++] = b;
}
}
writeInt(data.length);
write(data);
break;
}
case PgServer.PG_TYPE_INT2_ARRAY:
case PgServer.PG_TYPE_INT4_ARRAY:
case PgServer.PG_TYPE_VARCHAR_ARRAY:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write('{');
Value[] values = ((ValueArray) v).getList();
Charset encoding = getEncoding();
for (int i = 0; i < values.length; i++) {
if (i > 0) {
baos.write(',');
}
String s = values[i].getString();
if (SHOULD_QUOTE.matcher(s).matches()) {
List<String> ss = new ArrayList<>();
for (String s0 : s.split("\\\\")) {
ss.add(s0.replace("\"", "\\\""));
}
s = "\"" + String.join("\\\\", ss) + "\"";
}
baos.write(s.getBytes(encoding));
}
baos.write('}');
writeInt(baos.size());
write(baos);
break;
default:
byte[] data = v.getString().getBytes(getEncoding());
writeInt(data.length);
write(data);
}
} else {
// binary
switch(pgType) {
case PgServer.PG_TYPE_BOOL:
writeInt(1);
dataOut.writeByte(v.getBoolean() ? 1 : 0);
break;
case PgServer.PG_TYPE_INT2:
writeInt(2);
writeShort(v.getShort());
break;
case PgServer.PG_TYPE_INT4:
writeInt(4);
writeInt(v.getInt());
break;
case PgServer.PG_TYPE_INT8:
writeInt(8);
dataOut.writeLong(v.getLong());
break;
case PgServer.PG_TYPE_FLOAT4:
writeInt(4);
dataOut.writeFloat(v.getFloat());
break;
case PgServer.PG_TYPE_FLOAT8:
writeInt(8);
dataOut.writeDouble(v.getDouble());
break;
case PgServer.PG_TYPE_NUMERIC:
writeNumericBinary(v.getBigDecimal());
break;
case PgServer.PG_TYPE_BYTEA:
{
byte[] data = v.getBytesNoCopy();
writeInt(data.length);
write(data);
break;
}
case PgServer.PG_TYPE_DATE:
writeInt(4);
writeInt((int) toPostgreDays(((ValueDate) v).getDateValue()));
break;
case PgServer.PG_TYPE_TIME:
writeTimeBinary(((ValueTime) v).getNanos(), 8);
break;
case PgServer.PG_TYPE_TIMETZ:
{
ValueTimeTimeZone t = (ValueTimeTimeZone) v;
long m = t.getNanos();
writeTimeBinary(m, 12);
dataOut.writeInt(-t.getTimeZoneOffsetSeconds());
break;
}
case PgServer.PG_TYPE_TIMESTAMP:
{
ValueTimestamp t = (ValueTimestamp) v;
long m = toPostgreDays(t.getDateValue()) * 86_400;
long nanos = t.getTimeNanos();
writeTimestampBinary(m, nanos);
break;
}
case PgServer.PG_TYPE_TIMESTAMPTZ:
{
ValueTimestampTimeZone t = (ValueTimestampTimeZone) v;
long m = toPostgreDays(t.getDateValue()) * 86_400;
long nanos = t.getTimeNanos() - t.getTimeZoneOffsetSeconds() * 1_000_000_000L;
if (nanos < 0L) {
m--;
nanos += DateTimeUtils.NANOS_PER_DAY;
}
writeTimestampBinary(m, nanos);
break;
}
default:
throw new IllegalStateException("output binary format is undefined");
}
}
}
use of org.h2.value.ValueDate in project h2database by h2database.
the class TestTimeStampWithTimeZone method testConversionsImpl.
private void testConversionsImpl(String timeStr, boolean testReverse, CastDataProvider provider) {
ValueTimestamp ts = ValueTimestamp.parse(timeStr, null);
ValueDate d = ts.convertToDate(provider);
ValueTime t = (ValueTime) ts.convertTo(TypeInfo.TYPE_TIME, provider);
ValueTimestampTimeZone tstz = ValueTimestampTimeZone.parse(timeStr, null);
assertEquals(ts, tstz.convertTo(TypeInfo.TYPE_TIMESTAMP, provider));
assertEquals(d, tstz.convertToDate(provider));
assertEquals(t, tstz.convertTo(TypeInfo.TYPE_TIME, provider));
assertEquals(LegacyDateTimeUtils.toTimestamp(provider, null, ts), LegacyDateTimeUtils.toTimestamp(provider, null, tstz));
if (testReverse) {
assertEquals(0, tstz.compareTo(ts.convertTo(TypeInfo.TYPE_TIMESTAMP_TZ, provider), null, null));
assertEquals(d.convertTo(TypeInfo.TYPE_TIMESTAMP, provider).convertTo(TypeInfo.TYPE_TIMESTAMP_TZ, provider), d.convertTo(TypeInfo.TYPE_TIMESTAMP_TZ, provider));
assertEquals(t.convertTo(TypeInfo.TYPE_TIMESTAMP, provider).convertTo(TypeInfo.TYPE_TIMESTAMP_TZ, provider), t.convertTo(TypeInfo.TYPE_TIMESTAMP_TZ, provider));
}
}
Aggregations