KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > amber > type > SqlTimestampType


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.amber.type;
31
32 import com.caucho.amber.manager.AmberPersistenceUnit;
33 import com.caucho.bytecode.JClass;
34 import com.caucho.java.JavaWriter;
35 import com.caucho.util.L10N;
36
37 import java.io.IOException JavaDoc;
38 import java.sql.PreparedStatement JavaDoc;
39 import java.sql.ResultSet JavaDoc;
40 import java.sql.SQLException JavaDoc;
41 import java.sql.Timestamp JavaDoc;
42 import java.sql.Types JavaDoc;
43
44 /**
45  * The type of a property.
46  */

47 public class SqlTimestampType extends Type {
48   private static final L10N L = new L10N(SqlTimestampType.class);
49
50   private static final SqlTimestampType SQL_TIMESTAMP_TYPE = new SqlTimestampType();
51
52   private SqlTimestampType()
53   {
54   }
55
56   /**
57    * Returns the singleton SqlTimestamp type.
58    */

59   public static SqlTimestampType create()
60   {
61     return SQL_TIMESTAMP_TYPE;
62   }
63
64   /**
65    * Returns the type name.
66    */

67   public String JavaDoc getName()
68   {
69     return "java.sql.Timestamp";
70   }
71
72   /**
73    * Returns true if the value is assignable to the Java type.
74    */

75   @Override JavaDoc
76   public boolean isAssignableTo(JClass javaType)
77   {
78     return javaType.isAssignableFrom(java.sql.Timestamp JavaDoc.class);
79   }
80
81   /**
82    * Generates the type for the table.
83    */

84   public String JavaDoc generateCreateColumnSQL(AmberPersistenceUnit manager, int length, int precision, int scale)
85   {
86     return manager.getCreateColumnSQL(Types.TIMESTAMP, length, precision, scale);
87   }
88
89   /**
90    * Generates a string to load the property.
91    */

92   public int generateLoad(JavaWriter out, String JavaDoc rs,
93                           String JavaDoc indexVar, int index)
94     throws IOException JavaDoc
95   {
96     out.print(rs + ".getTimestamp(" + indexVar + " + " + index + ")");
97
98     return index + 1;
99   }
100
101   /**
102    * Generates a string to set the property.
103    */

104   public void generateSet(JavaWriter out, String JavaDoc pstmt,
105                           String JavaDoc index, String JavaDoc value)
106     throws IOException JavaDoc
107   {
108     out.println("if (" + value + " == null)");
109     out.println(" " + pstmt + ".setNull(" + index + "++, java.sql.Types.TIMESTAMP);");
110     out.println("else");
111     out.println(" " + pstmt + ".setTimestamp(" + index + "++, " + value + ");");
112   }
113
114   /**
115    * Generates a string to set the property.
116    */

117   public void generateSetVersion(JavaWriter out,
118                                  String JavaDoc pstmt,
119                                  String JavaDoc index,
120                                  String JavaDoc value)
121     throws IOException JavaDoc
122   {
123     value = "new java.sql.Timestamp(new java.util.Date().getTime())";
124     out.println(pstmt + ".setTimestamp(" + index + "++, " + value + ");");
125   }
126
127   /**
128    * Generates the increment version.
129    */

130   public String JavaDoc generateIncrementVersion(String JavaDoc value)
131     throws IOException JavaDoc
132   {
133     return "new java.sql.Timestamp(new java.util.Date().getTime())";
134   }
135
136   /**
137    * Sets the value.
138    */

139   public void setParameter(PreparedStatement JavaDoc pstmt, int index, Object JavaDoc value)
140     throws SQLException JavaDoc
141   {
142     pstmt.setTimestamp(index, (Timestamp JavaDoc) value);
143   }
144
145   /**
146    * Gets the value.
147    */

148   public Object JavaDoc getObject(ResultSet JavaDoc rs, int index)
149     throws SQLException JavaDoc
150   {
151     return rs.getTimestamp(index);
152   }
153 }
154
Popular Tags