001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.amber.type;
031:
032: import com.caucho.amber.entity.EntityItem;
033: import com.caucho.amber.manager.AmberConnection;
034: import com.caucho.amber.manager.AmberPersistenceUnit;
035: import com.caucho.bytecode.JClass;
036: import com.caucho.config.ConfigException;
037: import com.caucho.java.JavaWriter;
038: import com.caucho.util.L10N;
039:
040: import java.io.IOException;
041: import java.sql.PreparedStatement;
042: import java.sql.ResultSet;
043: import java.sql.SQLException;
044: import java.sql.Types;
045:
046: /**
047: * The type of a property.
048: */
049: abstract public class Type {
050: private static final L10N L = new L10N(Type.class);
051:
052: /**
053: * Returns the type name.
054: */
055: abstract public String getName();
056:
057: /**
058: * Returns true for a boolean type.
059: */
060: public boolean isBoolean() {
061: return false;
062: }
063:
064: /**
065: * Returns true for a numeric type.
066: */
067: public boolean isNumeric() {
068: return false;
069: }
070:
071: /**
072: * Returns the java type.
073: */
074: public String getJavaTypeName() {
075: return getName();
076: }
077:
078: /**
079: * Returns the number of columns the type takes up.
080: */
081: public int getColumnCount() {
082: return 1;
083: }
084:
085: /**
086: * Initialize the type.
087: */
088: public void init() throws ConfigException {
089: }
090:
091: /**
092: * Returns the type as a foreign key.
093: */
094: public Type getForeignType() {
095: return this ;
096: }
097:
098: /**
099: * Returns the java class of the type as a foreign key.
100: */
101: public String getForeignTypeName() {
102: return getForeignType().getJavaTypeName();
103: }
104:
105: /**
106: * Returns true if the value is assignable to the Java type.
107: */
108: public boolean isAssignableTo(JClass javaType) {
109: return true;
110: }
111:
112: /**
113: * Generates the type for the table.
114: */
115: public String generateCreateColumnSQL(AmberPersistenceUnit manager,
116: int length, int precision, int scale) {
117: if (length == 0)
118: length = 255;
119:
120: return manager.getCreateColumnSQL(Types.VARCHAR, length,
121: precision, scale);
122: }
123:
124: /**
125: * Generates a string to load the type as a property.
126: */
127: public int generateLoad(JavaWriter out, String rs, String indexVar,
128: int index) throws IOException {
129: throw new UnsupportedOperationException(getClass().getName());
130: }
131:
132: /**
133: * Generates a string to load the type as a property.
134: */
135: public int generateLoad(JavaWriter out, String rs, String indexVar,
136: int index, JClass targetType) throws IOException {
137: String i = indexVar + " + " + index;
138:
139: if ("java.lang.Byte".equals(targetType.getName()))
140: out.print("new Byte((byte) " + rs + ".getInt(" + i + "))");
141: else if ("java.lang.Short".equals(targetType.getName()))
142: out
143: .print("new Short((short) " + rs + ".getInt(" + i
144: + "))");
145: else if ("java.lang.Integer".equals(targetType.getName())) {
146: // ejb/0629
147: out.print("new Integer(" + rs + ".getInt(" + i + "))");
148: } else if ("java.lang.Long".equals(targetType.getName()))
149: out.print("new Long(" + rs + ".getLong(" + i + "))");
150: else if ("java.lang.Float".equals(targetType.getName()))
151: out.print("new Float((float) " + rs + ".getDouble(" + i
152: + "))");
153: else if ("java.lang.Double".equals(targetType.getName()))
154: out.print("new Double(" + rs + ".getDouble(" + i + "))");
155: else if ("java.lang.String".equals(targetType.getName()))
156: out.print(rs + ".getString(" + i + ")");
157: else {
158: out.print("(" + targetType.getName() + ") ");
159: out.print(rs + ".getObject(" + i + ")");
160: }
161:
162: return index + 1;
163: }
164:
165: /**
166: * Generates a string to load the type as a property.
167: */
168: public int generateLoadForeign(JavaWriter out, String rs,
169: String indexVar, int index) throws IOException {
170: return getForeignType().generateLoad(out, rs, indexVar, index);
171: }
172:
173: /**
174: * Generates a string to set the type as a property.
175: */
176: public void generateSet(JavaWriter out, String pstmt, String index,
177: String value) throws IOException {
178: throw new UnsupportedOperationException(getClass().getName());
179: }
180:
181: /**
182: * Generates a string to set the type as a property.
183: */
184: public void generateSetVersion(JavaWriter out, String pstmt,
185: String index, String value) throws IOException {
186: throw new UnsupportedOperationException(getClass().getName());
187: }
188:
189: /**
190: * Generates the increment version.
191: */
192: public String generateIncrementVersion(String value)
193: throws IOException {
194: return value + " + 1";
195: }
196:
197: /**
198: * Generates a string to set the property.
199: */
200: public void generateSetNull(JavaWriter out, String pstmt,
201: String index) throws IOException {
202: generateSet(out, pstmt, index, null);
203: }
204:
205: /**
206: * Sets the value.
207: */
208: public void setParameter(PreparedStatement pstmt, int index,
209: Object value) throws SQLException {
210: pstmt.setObject(index, value);
211: }
212:
213: /**
214: * Gets the value.
215: */
216: public Object getObject(ResultSet rs, int index)
217: throws SQLException {
218: return rs.getObject(index);
219: }
220:
221: /**
222: * Finds the object
223: */
224: public EntityItem findItem(AmberConnection aConn, ResultSet rs,
225: int index) throws SQLException {
226: throw new UnsupportedOperationException(getClass().getName());
227: }
228:
229: /**
230: * Gets the value.
231: */
232: public Object getObject(AmberConnection aConn, ResultSet rs,
233: int index) throws SQLException {
234: return getObject(rs, index);
235: }
236:
237: /**
238: * Converts to an object.
239: */
240: public String toObject(String value) {
241: return value;
242: }
243:
244: /**
245: * Converts from an object.
246: */
247: public String fromObject(String value) {
248: return generateCastFromObject(value);
249: }
250:
251: /**
252: * Converts to an object.
253: */
254: public Object toObject(long value) {
255: return new Long(value);
256: }
257:
258: /**
259: * Converts the value.
260: */
261: public String generateCastFromObject(String value) {
262: return "((" + getName() + ") " + value + ")";
263: }
264:
265: /**
266: * Returns a boolean equality.
267: */
268: public String generateEquals(String a, String b) {
269: return a + ".equals(" + b + ")";
270: }
271:
272: /**
273: * Returns a test for null.
274: */
275: public String generateIsNull(String value) {
276: return "(" + value + " == " + generateNull() + ")";
277: }
278:
279: /**
280: * Returns a test for null.
281: */
282: public String generateNull() {
283: return "null";
284: }
285:
286: /**
287: * Returns true for an auto-increment type.
288: */
289: public boolean isAutoIncrement() {
290: return false;
291: }
292: }
|