KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Free Software Foundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

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

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

58   public static SqlDateType create()
59   {
60     return SQL_DATE_TYPE;
61   }
62
63   /**
64    * Returns the type name.
65    */

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

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

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

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

103   public void generateSet(JavaWriter out, String JavaDoc pstmt,
104               String JavaDoc index, String JavaDoc value)
105     throws IOException JavaDoc
106   {
107     out.println("if (" + value + " == null)");
108     out.println(" " + pstmt + ".setNull(" + index + "++, java.sql.Types.DATE);");
109     out.println("else");
110     out.println(" " + pstmt + ".setDate(" + index + "++, " + value + ");");
111   }
112
113   /**
114    * Sets the value.
115    */

116   public void setParameter(PreparedStatement JavaDoc pstmt, int index, Object JavaDoc value)
117     throws SQLException JavaDoc
118   {
119     pstmt.setDate(index, (Date JavaDoc) value);
120   }
121
122   /**
123    * Gets the value.
124    */

125   public Object JavaDoc getObject(ResultSet JavaDoc rs, int index)
126     throws SQLException JavaDoc
127   {
128     return rs.getDate(index);
129   }
130 }
131
Popular Tags