KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

64   public String JavaDoc getName()
65   {
66     return "java.lang.String";
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     if (length == 0)
75       length = 255;
76
77     return "varchar(" + length + ")";
78     // return manager.getCreateColumnSQL(Types.VARCHAR, length);
79
}
80
81   /**
82    * Generates a string to load the property.
83    */

84   public int generateLoad(JavaWriter out, String JavaDoc rs,
85                           String JavaDoc indexVar, int index)
86     throws IOException JavaDoc
87   {
88     out.print(rs + ".getString(" + indexVar + " + " + index + ")");
89
90     return index + 1;
91   }
92
93   /**
94    * Generates a string to set the property.
95    */

96   public void generateSet(JavaWriter out, String JavaDoc pstmt,
97                           String JavaDoc index, String JavaDoc value)
98     throws IOException JavaDoc
99   {
100     if (pstmt.startsWith("query"))
101       out.println(pstmt + ".setString(" + index + "++, " + value + ");");
102     else
103       out.println("__caucho_setInternalString(" + pstmt + ", " + index + "++, " + value + ");");
104   }
105
106   /**
107    * Sets the value.
108    */

109   public void setParameter(PreparedStatement JavaDoc pstmt, int index, Object JavaDoc value)
110     throws SQLException JavaDoc
111   {
112     if (value == null) {
113       // XXX: issue with derby.
114
// pstmt.setNull(index, java.sql.Types.OTHER);
115
pstmt.setString(index, null);
116     }
117     else if (value instanceof String JavaDoc)
118       pstmt.setString(index, (String JavaDoc) value);
119     else // ejb/0623
120
pstmt.setObject(index, value);
121   }
122
123   /**
124    * Gets the value.
125    */

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