001: //$Id: TimestampType.java 8891 2005-12-21 05:13:29Z oneovthafew $
002: package org.hibernate.type;
003:
004: import java.sql.PreparedStatement;
005: import java.sql.ResultSet;
006: import java.sql.SQLException;
007: import java.sql.Timestamp;
008: import java.sql.Types;
009: import java.text.ParseException;
010: import java.text.SimpleDateFormat;
011: import java.util.Comparator;
012:
013: import org.hibernate.EntityMode;
014: import org.hibernate.HibernateException;
015: import org.hibernate.cfg.Environment;
016: import org.hibernate.dialect.Dialect;
017: import org.hibernate.engine.SessionImplementor;
018: import org.hibernate.util.ComparableComparator;
019:
020: /**
021: * <tt>timestamp</tt>: A type that maps an SQL TIMESTAMP to a Java
022: * java.util.Date or java.sql.Timestamp.
023: * @author Gavin King
024: */
025: public class TimestampType extends MutableType implements VersionType,
026: LiteralType {
027:
028: private static final String TIMESTAMP_FORMAT = "yyyy-MM-dd HH:mm:ss";
029:
030: public Object get(ResultSet rs, String name) throws SQLException {
031: return rs.getTimestamp(name);
032: }
033:
034: public Class getReturnedClass() {
035: return java.util.Date.class;
036: }
037:
038: public void set(PreparedStatement st, Object value, int index)
039: throws SQLException {
040: Timestamp ts;
041: if (value instanceof Timestamp) {
042: ts = (Timestamp) value;
043: } else {
044: ts = new Timestamp(((java.util.Date) value).getTime());
045: }
046: st.setTimestamp(index, ts);
047: }
048:
049: public int sqlType() {
050: return Types.TIMESTAMP;
051: }
052:
053: public String getName() {
054: return "timestamp";
055: }
056:
057: public String toString(Object val) {
058: return new SimpleDateFormat(TIMESTAMP_FORMAT)
059: .format((java.util.Date) val);
060: }
061:
062: public Object deepCopyNotNull(Object value) {
063: if (value instanceof Timestamp) {
064: Timestamp orig = (Timestamp) value;
065: Timestamp ts = new Timestamp(orig.getTime());
066: ts.setNanos(orig.getNanos());
067: return ts;
068: } else {
069: java.util.Date orig = (java.util.Date) value;
070: return new java.util.Date(orig.getTime());
071: }
072: }
073:
074: public boolean isEqual(Object x, Object y) {
075:
076: if (x == y)
077: return true;
078: if (x == null || y == null)
079: return false;
080:
081: long xTime = ((java.util.Date) x).getTime();
082: long yTime = ((java.util.Date) y).getTime();
083: boolean xts = x instanceof Timestamp;
084: boolean yts = y instanceof Timestamp;
085: int xNanos = xts ? ((Timestamp) x).getNanos() : 0;
086: int yNanos = yts ? ((Timestamp) y).getNanos() : 0;
087: if (!Environment.jvmHasJDK14Timestamp()) {
088: xTime += xNanos / 1000000;
089: yTime += yNanos / 1000000;
090: }
091: if (xTime != yTime)
092: return false;
093: if (xts && yts) {
094: // both are Timestamps
095: int xn = xNanos % 1000000;
096: int yn = yNanos % 1000000;
097: return xn == yn;
098: } else {
099: // at least one is a plain old Date
100: return true;
101: }
102:
103: }
104:
105: public int getHashCode(Object x, EntityMode entityMode) {
106: java.util.Date ts = (java.util.Date) x;
107: return new Long(ts.getTime() / 1000).hashCode();
108: }
109:
110: public Object next(Object current, SessionImplementor session) {
111: return seed(session);
112: }
113:
114: public Object seed(SessionImplementor session) {
115: return new Timestamp(System.currentTimeMillis());
116: }
117:
118: public Comparator getComparator() {
119: return ComparableComparator.INSTANCE;
120: }
121:
122: public String objectToSQLString(Object value, Dialect dialect)
123: throws Exception {
124: return '\'' + new Timestamp(((java.util.Date) value).getTime())
125: .toString() + '\'';
126: }
127:
128: public Object fromStringValue(String xml) throws HibernateException {
129: try {
130: return new Timestamp(new SimpleDateFormat(TIMESTAMP_FORMAT)
131: .parse(xml).getTime());
132: } catch (ParseException pe) {
133: throw new HibernateException("could not parse XML", pe);
134: }
135: }
136:
137: }
|