KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

64   public String JavaDoc getName()
65   {
66     return "short";
67   }
68
69   /**
70    * Returns true for a numeric type.
71    */

72   public boolean isNumeric()
73   {
74     return true;
75   }
76
77   /**
78    * Returns the type as a foreign key.
79    */

80   public Type getForeignType()
81   {
82     return ShortType.create();
83   }
84
85   /**
86    * Generates the type for the table.
87    */

88   public String JavaDoc generateCreateColumnSQL(AmberPersistenceUnit manager, int length, int precision, int scale)
89   {
90     return manager.getCreateColumnSQL(Types.SMALLINT, length, precision, scale);
91   }
92
93   /**
94    * Generates a string to load the property.
95    */

96   public int generateLoad(JavaWriter out, String JavaDoc rs,
97                           String JavaDoc indexVar, int index)
98     throws IOException JavaDoc
99   {
100     out.print(rs + ".getShort(" + indexVar + " + " + index + ")");
101
102     return index + 1;
103   }
104
105   /**
106    * Generates a string to set the property.
107    */

108   public void generateSet(JavaWriter out, String JavaDoc pstmt,
109                           String JavaDoc index, String JavaDoc value)
110     throws IOException JavaDoc
111   {
112     out.println(pstmt + ".setShort(" + index + "++, " + value + ");");
113   }
114
115   /**
116    * Generates a string to set the property.
117    */

118   public void generateSetNull(JavaWriter out, String JavaDoc pstmt, String JavaDoc index)
119     throws IOException JavaDoc
120   {
121     out.println(pstmt + ".setNull(" + index + "++, java.sql.Types.SMALLINT);");
122   }
123
124   /**
125    * Generates a string to set the property.
126    */

127   public void generateSetVersion(JavaWriter out,
128                                  String JavaDoc pstmt,
129                                  String JavaDoc index,
130                                  String JavaDoc value)
131     throws IOException JavaDoc
132   {
133     out.println(pstmt + ".setShort(" + index + "++, " + value + " + 1);");
134   }
135
136   /**
137    * Converts to an object.
138    */

139   public String JavaDoc toObject(String JavaDoc value)
140   {
141     return "new Short(" + value + ")";
142   }
143
144   /**
145    * Converts the value.
146    */

147   public String JavaDoc generateCastFromObject(String JavaDoc value)
148   {
149     return "((Number) " + value + ").shortValue()";
150   }
151
152   /**
153    * Gets the value.
154    */

155   public Object JavaDoc getObject(ResultSet JavaDoc rs, int index)
156     throws SQLException JavaDoc
157   {
158     short v = rs.getShort(index);
159
160     return rs.wasNull() ? null : new Short JavaDoc(v);
161   }
162
163   /**
164    * Converts to an object.
165    */

166   public Object JavaDoc toObject(long value)
167   {
168     return new Short JavaDoc((short) value);
169   }
170
171   /**
172    * Sets the value.
173    */

174   public void setParameter(PreparedStatement JavaDoc pstmt, int index, Object JavaDoc value)
175     throws SQLException JavaDoc
176   {
177     if (value instanceof Number JavaDoc)
178       pstmt.setString(index, value.toString());
179     else
180       throw new IllegalArgumentException JavaDoc("Invalid argument for setParameter.");
181   }
182 }
183
Popular Tags