KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 character type.
43  */

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

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

64   public String JavaDoc getName()
65   {
66     return "java.lang.Character";
67   }
68
69   /**
70    * Generates the type for the table.
71    */

72   public String JavaDoc generateCreateColumnSQL(AmberPersistenceUnit manager, int length, int precision, int scale)
73   {
74     return manager.getCreateColumnSQL(Types.CHAR, 0, precision, scale);
75   }
76
77   /**
78    * Generates a string to load the property.
79    */

80   public int generateLoad(JavaWriter out, String JavaDoc rs,
81               String JavaDoc indexVar, int index)
82     throws IOException JavaDoc
83   {
84     out.print("com.caucho.amber.type.CharacterType.toChar(" + rs + ".getString(" + indexVar + " + " + index + "))");
85
86     return index + 1;
87   }
88
89   /**
90    * Generates a string to set the property.
91    */

92   public void generateSet(JavaWriter out, String JavaDoc pstmt,
93               String JavaDoc index, String JavaDoc value)
94     throws IOException JavaDoc
95   {
96     out.println("if (" + value + " == null)");
97     out.println(" " + pstmt + ".setNull(" + index + "++, java.sql.Types.CHAR);");
98     out.println("else");
99     out.println(" " + pstmt + ".setString(" + index + "++, String.valueOf(" + value + "));");
100   }
101
102   /**
103    * Generates a string to set the property.
104    */

105   public void generateSetNull(JavaWriter out, String JavaDoc pstmt, String JavaDoc index)
106     throws IOException JavaDoc
107   {
108     out.println(pstmt + ".setNull(" + index + "++, java.sql.Types.CHAR);");
109   }
110
111   /**
112    * Converts a value to a char.
113    */

114   public static Character JavaDoc toChar(String JavaDoc value)
115   {
116     if (value == null || value.length() == 0)
117       return null;
118     else
119       return new Character JavaDoc(value.charAt(0));
120   }
121
122   /**
123    * Gets the value.
124    */

125   public Object JavaDoc getObject(ResultSet JavaDoc rs, int index)
126     throws SQLException JavaDoc
127   {
128     String JavaDoc value = rs.getString(index);
129     
130     return (value == null || value.length() == 0) ? null : new Character JavaDoc(value.charAt(0));
131   }
132 }
133
Popular Tags