KickJava   Java API By Example, From Geeks To Geeks.

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


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.ResultSet JavaDoc;
38 import java.sql.SQLException JavaDoc;
39 import java.sql.Types JavaDoc;
40
41 /**
42  * The type of a property.
43  */

44 public class SqlTimeType extends Type {
45   private static final L10N L = new L10N(SqlTimeType.class);
46
47   private static final SqlTimeType SQL_TIME_TYPE = new SqlTimeType();
48
49   private SqlTimeType()
50   {
51   }
52
53   /**
54    * Returns the singleton SqlTime type.
55    */

56   public static SqlTimeType create()
57   {
58     return SQL_TIME_TYPE;
59   }
60
61   /**
62    * Returns the type name.
63    */

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

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

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

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

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

114   public Object JavaDoc getObject(ResultSet JavaDoc rs, int index)
115     throws SQLException JavaDoc
116   {
117     return rs.getTime(index);
118   }
119 }
120
Popular Tags