01: /*
02: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
03: * (http://h2database.com/html/license.html).
04: * Initial Developer: H2 Group
05: */
06: package org.h2.value;
07:
08: import java.sql.PreparedStatement;
09: import java.sql.SQLException;
10: import java.sql.Time;
11: import org.h2.constant.ErrorCode;
12: import org.h2.util.DateTimeUtils;
13:
14: /**
15: * Implementation of the TIME data type.
16: */
17: public class ValueTime extends Value {
18: public static final int PRECISION = 6;
19: public static final int DISPLAY_SIZE = 8; // 10:00:00
20:
21: private final Time value;
22:
23: private ValueTime(Time value) {
24: this .value = value;
25: }
26:
27: public static Time parseTime(String s) throws SQLException {
28: return (Time) DateTimeUtils.parseDateTime(s, Value.TIME,
29: ErrorCode.TIME_CONSTANT_2);
30: }
31:
32: public Time getTime() {
33: // this class is mutable - must copy the object
34: return (Time) value.clone();
35: }
36:
37: public Time getTimeNoCopy() {
38: return value;
39: }
40:
41: public String getSQL() {
42: return "TIME '" + getString() + "'";
43: }
44:
45: public int getType() {
46: return Value.TIME;
47: }
48:
49: protected int compareSecure(Value o, CompareMode mode) {
50: ValueTime v = (ValueTime) o;
51: int c = value.compareTo(v.value);
52: return c == 0 ? 0 : (c < 0 ? -1 : 1);
53: }
54:
55: public String getString() {
56: return value.toString();
57: }
58:
59: public long getPrecision() {
60: return PRECISION;
61: }
62:
63: public int hashCode() {
64: return value.hashCode();
65: }
66:
67: public Object getObject() {
68: return getTime();
69: }
70:
71: public void set(PreparedStatement prep, int parameterIndex)
72: throws SQLException {
73: prep.setTime(parameterIndex, value);
74: }
75:
76: public static ValueTime get(Time time) {
77: time = DateTimeUtils.cloneAndNormalizeTime(time);
78: return getNoCopy(time);
79: }
80:
81: public static ValueTime getNoCopy(Time time) {
82: return (ValueTime) Value.cache(new ValueTime(time));
83: }
84:
85: public int getDisplaySize() {
86: return DISPLAY_SIZE;
87: }
88:
89: public boolean equals(Object other) {
90: return other instanceof ValueTime
91: && value.equals(((ValueTime) other).value);
92: }
93:
94: }
|