001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (http://h2database.com/html/license.html).
004: * Initial Developer: H2 Group
005: */
006: package org.h2.value;
007:
008: import java.math.BigDecimal;
009: import java.sql.PreparedStatement;
010: import java.sql.SQLException;
011: import java.sql.Timestamp;
012:
013: import org.h2.constant.ErrorCode;
014: import org.h2.message.Message;
015: import org.h2.util.DateTimeUtils;
016: import org.h2.util.MathUtils;
017:
018: /**
019: * Implementation of the TIMESTAMP data type.
020: */
021: public class ValueTimestamp extends Value {
022: public static final int PRECISION = 23;
023: public static final int DISPLAY_SIZE = 23; // 2001-01-01 23:59:59.000
024: public static final int DEFAULT_SCALE = 10;
025: private final Timestamp value;
026:
027: private ValueTimestamp(Timestamp value) {
028: this .value = value;
029: }
030:
031: public Timestamp getTimestamp() {
032: return (Timestamp) value.clone();
033: }
034:
035: public Timestamp getTimestampNoCopy() {
036: return value;
037: }
038:
039: public String getSQL() {
040: return "TIMESTAMP '" + getString() + "'";
041: }
042:
043: public static Timestamp parseTimestamp(String s)
044: throws SQLException {
045: return (Timestamp) DateTimeUtils.parseDateTime(s,
046: Value.TIMESTAMP, ErrorCode.TIMESTAMP_CONSTANT_2);
047: }
048:
049: public int getType() {
050: return Value.TIMESTAMP;
051: }
052:
053: protected int compareSecure(Value o, CompareMode mode) {
054: ValueTimestamp v = (ValueTimestamp) o;
055: int c = value.compareTo(v.value);
056: return c == 0 ? 0 : (c < 0 ? -1 : 1);
057: }
058:
059: public String getString() {
060: return value.toString();
061: }
062:
063: public long getPrecision() {
064: return PRECISION;
065: }
066:
067: public int getScale() {
068: return DEFAULT_SCALE;
069: }
070:
071: public int hashCode() {
072: return value.hashCode();
073: }
074:
075: public Object getObject() {
076: // this class is mutable - must copy the object
077: return getTimestamp();
078: }
079:
080: public void set(PreparedStatement prep, int parameterIndex)
081: throws SQLException {
082: prep.setTimestamp(parameterIndex, value);
083: }
084:
085: public static ValueTimestamp get(Timestamp timestamp) {
086: timestamp = (Timestamp) timestamp.clone();
087: return getNoCopy(timestamp);
088: }
089:
090: public static ValueTimestamp getNoCopy(Timestamp timestamp) {
091: return (ValueTimestamp) Value.cache(new ValueTimestamp(
092: timestamp));
093: }
094:
095: public Value convertScale(boolean onlyToSmallerScale,
096: int targetScale) throws SQLException {
097: if (targetScale < 0 || targetScale > DEFAULT_SCALE) {
098: // TODO convertScale for Timestamps: may throw an exception?
099: throw Message.getInvalidValueException("" + targetScale,
100: "scale");
101: }
102: int nanos = value.getNanos();
103: BigDecimal bd = new BigDecimal("" + nanos);
104: bd = bd.movePointLeft(9);
105: bd = MathUtils.setScale(bd, targetScale);
106: bd = bd.movePointRight(9);
107: int n2 = bd.intValue();
108: if (n2 == nanos) {
109: return this ;
110: }
111: long t = value.getTime();
112: while (n2 >= 1000000000) {
113: t += 1000;
114: n2 -= 1000000000;
115: }
116: Timestamp t2 = new Timestamp(t);
117: t2.setNanos(n2);
118: return ValueTimestamp.getNoCopy(t2);
119: }
120:
121: public int getDisplaySize() {
122: return DISPLAY_SIZE;
123: }
124:
125: public boolean equals(Object other) {
126: return other instanceof ValueTimestamp
127: && value.equals(((ValueTimestamp) other).value);
128: }
129:
130: }
|