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: * Free Software Foundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.amber.type;
030:
031: import com.caucho.amber.manager.AmberPersistenceUnit;
032: import com.caucho.java.JavaWriter;
033: import com.caucho.util.L10N;
034:
035: import java.io.IOException;
036: import java.sql.PreparedStatement;
037: import java.sql.ResultSet;
038: import java.sql.SQLException;
039: import java.sql.Types;
040:
041: /**
042: * The primitive long type.
043: */
044: public class PrimitiveLongType extends PrimitiveType {
045: private static final L10N L = new L10N(PrimitiveLongType.class);
046:
047: private static final PrimitiveLongType LONG_TYPE = new PrimitiveLongType();
048:
049: private PrimitiveLongType() {
050: }
051:
052: /**
053: * Returns the boolean type.
054: */
055: public static PrimitiveLongType create() {
056: return LONG_TYPE;
057: }
058:
059: /**
060: * Returns the type name.
061: */
062: public String getName() {
063: return "long";
064: }
065:
066: /**
067: * Returns true for a numeric type.
068: */
069: public boolean isNumeric() {
070: return true;
071: }
072:
073: /**
074: * Returns the type as a foreign key.
075: */
076: public Type getForeignType() {
077: return LongType.create();
078: }
079:
080: /**
081: * Generates the type for the table.
082: */
083: public String generateCreateColumnSQL(AmberPersistenceUnit manager,
084: int length, int precision, int scale) {
085: return manager.getCreateColumnSQL(Types.BIGINT, length,
086: precision, scale);
087: }
088:
089: /**
090: * Generates a string to load the property.
091: */
092: public int generateLoad(JavaWriter out, String rs, String indexVar,
093: int index) throws IOException {
094: out.print(rs + ".getLong(" + indexVar + " + " + index + ")");
095:
096: return index + 1;
097: }
098:
099: /**
100: * Generates a string to load the property.
101: */
102: public int generateLoadForeign(JavaWriter out, String rs,
103: String indexVar, int index) throws IOException {
104: out
105: .print("com.caucho.amber.type.PrimitiveLongType.toForeignLong("
106: + rs
107: + ".getLong("
108: + indexVar
109: + " + "
110: + index
111: + "), " + rs + ".wasNull())");
112:
113: return index + 1;
114: }
115:
116: /**
117: * Generates a string to set the property.
118: */
119: public void generateSet(JavaWriter out, String pstmt, String index,
120: String value) throws IOException {
121: out
122: .println(pstmt + ".setLong(" + index + "++, " + value
123: + ");");
124: }
125:
126: /**
127: * Generates a string to set the property.
128: */
129: public void generateSetNull(JavaWriter out, String pstmt,
130: String index) throws IOException {
131: out.println(pstmt + ".setNull(" + index
132: + "++, java.sql.Types.BIGINT);");
133: }
134:
135: /**
136: * Generates a string to set the property.
137: */
138: public void generateSetVersion(JavaWriter out, String pstmt,
139: String index, String value) throws IOException {
140: out.println(pstmt + ".setLong(" + index + "++, " + value
141: + " + 1);");
142: }
143:
144: /**
145: * Converts to an object.
146: */
147: public String toObject(String value) {
148: return "new Long(" + value + ")";
149: }
150:
151: /**
152: * Converts the value.
153: */
154: public String generateCastFromObject(String value) {
155: return "((Number) " + value + ").longValue()";
156: }
157:
158: /**
159: * Gets the value.
160: */
161: public Object getObject(ResultSet rs, int index)
162: throws SQLException {
163: long v = rs.getLong(index);
164:
165: return rs.wasNull() ? null : new Long(v);
166: }
167:
168: /**
169: * Converts a value to a int.
170: */
171: public static Long toForeignLong(long value, boolean wasNull) {
172: // XXX: backwards compat
173: if (wasNull || value == 0)
174: return null;
175: else
176: return new Long(value);
177: }
178:
179: /**
180: * Sets the value.
181: */
182: public void setParameter(PreparedStatement pstmt, int index,
183: Object value) throws SQLException {
184: if (value == null) {
185: // jpa/141e
186: pstmt.setString(index, null);
187: } else if (value instanceof Number)
188: pstmt.setString(index, value.toString());
189: else
190: throw new IllegalArgumentException(
191: "Invalid argument for setParameter.");
192: }
193: }
|