KickJava   Java API By Example, From Geeks To Geeks.

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


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.java.JavaWriter;
33 import com.caucho.util.L10N;
34
35 import java.io.IOException JavaDoc;
36 import java.sql.PreparedStatement JavaDoc;
37 import java.sql.ResultSet JavaDoc;
38 import java.sql.SQLException JavaDoc;
39 import java.sql.Types JavaDoc;
40
41 /**
42  * The primitive char type.
43  */

44 public class PrimitiveCharType extends PrimitiveType {
45   private static final L10N L = new L10N(PrimitiveCharType.class);
46
47   private static final PrimitiveCharType CHAR_TYPE = new PrimitiveCharType();
48
49   private PrimitiveCharType()
50   {
51   }
52
53   /**
54    * Returns the boolean type.
55    */

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

64   public String JavaDoc getName()
65   {
66     return "char";
67   }
68
69   /**
70    * Returns the type as a foreign key.
71    */

72   public Type getForeignType()
73   {
74     return CharacterType.create();
75   }
76
77   /**
78    * Generates the type for the table.
79    */

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

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

100   public void generateSet(JavaWriter out, String JavaDoc pstmt,
101                           String JavaDoc index, String JavaDoc value)
102     throws IOException JavaDoc
103   {
104     out.println(pstmt + ".setString(" + index + "++, String.valueOf(" + value + "));");
105   }
106
107   /**
108    * Generates a string to set the property.
109    */

110   public void generateSetNull(JavaWriter out, String JavaDoc pstmt, String JavaDoc index)
111     throws IOException JavaDoc
112   {
113     out.println(pstmt + ".setNull(" + index + "++, java.sql.Types.CHAR);");
114   }
115
116   /**
117    * Converts to an object.
118    */

119   public String JavaDoc toObject(String JavaDoc value)
120   {
121     return "new Character(" + value + ")";
122   }
123
124   /**
125    * Converts the value.
126    */

127   public String JavaDoc generateCastFromObject(String JavaDoc value)
128   {
129     return "((Character) " + value + ").charValue()";
130   }
131
132   /**
133    * Gets the value.
134    */

135   public Object JavaDoc getObject(ResultSet JavaDoc rs, int index)
136     throws SQLException JavaDoc
137   {
138     String JavaDoc v = rs.getString(index);
139
140     return rs.wasNull() ? null : new Character JavaDoc(v.charAt(0));
141   }
142
143   /**
144    * Converts a value to a char.
145    */

146   public static char toChar(String JavaDoc value)
147   {
148     if (value == null || value.length() == 0)
149       return 0;
150     else
151       return value.charAt(0);
152   }
153
154   /**
155    * Sets the value.
156    */

157   public void setParameter(PreparedStatement JavaDoc pstmt, int index, Object JavaDoc value)
158     throws SQLException JavaDoc
159   {
160     if ((value instanceof Character JavaDoc) || (value instanceof String JavaDoc))
161       pstmt.setString(index, value.toString());
162     else
163       throw new IllegalArgumentException JavaDoc("Invalid argument for setParameter.");
164   }
165 }
166
Popular Tags