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.Date;
09: import java.sql.PreparedStatement;
10: import java.sql.SQLException;
11:
12: import org.h2.constant.ErrorCode;
13: import org.h2.util.DateTimeUtils;
14:
15: /**
16: * Implementation of the DATE data type.
17: */
18: public class ValueDate extends Value {
19: public static final int PRECISION = 8;
20: public static final int DISPLAY_SIZE = 10; // 2000-01-02
21:
22: private final Date value;
23:
24: private ValueDate(Date value) {
25: this .value = value;
26: }
27:
28: public static Date parseDate(String s) throws SQLException {
29: return (Date) DateTimeUtils.parseDateTime(s, Value.DATE,
30: ErrorCode.DATE_CONSTANT_2);
31: }
32:
33: public Date getDate() {
34: // this class is mutable - must copy the object
35: return (Date) value.clone();
36: }
37:
38: public Date getDateNoCopy() {
39: return value;
40: }
41:
42: public String getSQL() {
43: return "DATE '" + getString() + "'";
44: }
45:
46: public int getType() {
47: return Value.DATE;
48: }
49:
50: protected int compareSecure(Value o, CompareMode mode) {
51: ValueDate v = (ValueDate) o;
52: int c = value.compareTo(v.value);
53: return c == 0 ? 0 : (c < 0 ? -1 : 1);
54: }
55:
56: public String getString() {
57: return value.toString();
58: }
59:
60: public long getPrecision() {
61: return PRECISION;
62: }
63:
64: public int hashCode() {
65: return value.hashCode();
66: }
67:
68: public Object getObject() {
69: // this class is mutable - must copy the object
70: return getDate();
71: }
72:
73: public void set(PreparedStatement prep, int parameterIndex)
74: throws SQLException {
75: prep.setDate(parameterIndex, value);
76: }
77:
78: public static ValueDate get(Date date) {
79: date = DateTimeUtils.cloneAndNormalizeDate(date);
80: return getNoCopy(date);
81: }
82:
83: public static ValueDate getNoCopy(Date date) {
84: return (ValueDate) Value.cache(new ValueDate(date));
85: }
86:
87: public int getDisplaySize() {
88: return DISPLAY_SIZE;
89: }
90:
91: public boolean equals(Object other) {
92: return other instanceof ValueDate
93: && value.equals(((ValueDate) other).value);
94: }
95:
96: }
|