001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.amber.type;
031:
032: import com.caucho.amber.manager.AmberPersistenceUnit;
033: import com.caucho.bytecode.JClass;
034: import com.caucho.java.JavaWriter;
035: import com.caucho.util.L10N;
036:
037: import java.io.IOException;
038: import java.sql.PreparedStatement;
039: import java.sql.ResultSet;
040: import java.sql.SQLException;
041: import java.sql.Timestamp;
042: import java.sql.Types;
043:
044: /**
045: * The type of a property.
046: */
047: public class SqlTimestampType extends Type {
048: private static final L10N L = new L10N(SqlTimestampType.class);
049:
050: private static final SqlTimestampType SQL_TIMESTAMP_TYPE = new SqlTimestampType();
051:
052: private SqlTimestampType() {
053: }
054:
055: /**
056: * Returns the singleton SqlTimestamp type.
057: */
058: public static SqlTimestampType create() {
059: return SQL_TIMESTAMP_TYPE;
060: }
061:
062: /**
063: * Returns the type name.
064: */
065: public String getName() {
066: return "java.sql.Timestamp";
067: }
068:
069: /**
070: * Returns true if the value is assignable to the Java type.
071: */
072: @Override
073: public boolean isAssignableTo(JClass javaType) {
074: return javaType.isAssignableFrom(java.sql.Timestamp.class);
075: }
076:
077: /**
078: * Generates the type for the table.
079: */
080: public String generateCreateColumnSQL(AmberPersistenceUnit manager,
081: int length, int precision, int scale) {
082: return manager.getCreateColumnSQL(Types.TIMESTAMP, length,
083: precision, scale);
084: }
085:
086: /**
087: * Generates a string to load the property.
088: */
089: public int generateLoad(JavaWriter out, String rs, String indexVar,
090: int index) throws IOException {
091: out.print(rs + ".getTimestamp(" + indexVar + " + " + index
092: + ")");
093:
094: return index + 1;
095: }
096:
097: /**
098: * Generates a string to set the property.
099: */
100: public void generateSet(JavaWriter out, String pstmt, String index,
101: String value) throws IOException {
102: out.println("if (" + value + " == null)");
103: out.println(" " + pstmt + ".setNull(" + index
104: + "++, java.sql.Types.TIMESTAMP);");
105: out.println("else");
106: out.println(" " + pstmt + ".setTimestamp(" + index + "++, "
107: + value + ");");
108: }
109:
110: /**
111: * Generates a string to set the property.
112: */
113: public void generateSetVersion(JavaWriter out, String pstmt,
114: String index, String value) throws IOException {
115: value = "new java.sql.Timestamp(new java.util.Date().getTime())";
116: out.println(pstmt + ".setTimestamp(" + index + "++, " + value
117: + ");");
118: }
119:
120: /**
121: * Generates the increment version.
122: */
123: public String generateIncrementVersion(String value)
124: throws IOException {
125: return "new java.sql.Timestamp(new java.util.Date().getTime())";
126: }
127:
128: /**
129: * Sets the value.
130: */
131: public void setParameter(PreparedStatement pstmt, int index,
132: Object value) throws SQLException {
133: pstmt.setTimestamp(index, (Timestamp) value);
134: }
135:
136: /**
137: * Gets the value.
138: */
139: public Object getObject(ResultSet rs, int index)
140: throws SQLException {
141: return rs.getTimestamp(index);
142: }
143: }
|