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.SQLException;
038: import java.sql.Types;
039:
040: /**
041: * Represents a primitive Java byte type
042: */
043: public class PrimitiveByteType extends PrimitiveType {
044: private static final L10N L = new L10N(PrimitiveByteType.class);
045:
046: private static final PrimitiveByteType BYTE_TYPE = new PrimitiveByteType();
047:
048: private PrimitiveByteType() {
049: }
050:
051: /**
052: * Returns the byte type.
053: */
054: public static PrimitiveByteType create() {
055: return BYTE_TYPE;
056: }
057:
058: /**
059: * Returns the type name.
060: */
061: public String getName() {
062: return "byte";
063: }
064:
065: /**
066: * Returns true for a numeric type.
067: */
068: public boolean isNumeric() {
069: return true;
070: }
071:
072: /**
073: * Returns the foreign key type.
074: */
075: public Type getForeignType() {
076: return ByteType.create();
077: }
078:
079: /**
080: * Generates the type for the table.
081: */
082: public String generateCreateColumnSQL(AmberPersistenceUnit manager,
083: int length, int precision, int scale) {
084: return manager.getCreateColumnSQL(Types.TINYINT, length,
085: precision, scale);
086: }
087:
088: /**
089: * Generates a string to load the property.
090: */
091: public int generateLoad(JavaWriter out, String rs, String indexVar,
092: int index) throws IOException {
093: out.print(rs + ".getByte(" + indexVar + " + " + index + ")");
094:
095: return index + 1;
096: }
097:
098: /**
099: * Generates a string to set the property.
100: */
101: public void generateSet(JavaWriter out, String pstmt, String index,
102: String value) throws IOException {
103: out
104: .println(pstmt + ".setByte(" + index + "++, " + value
105: + ");");
106: }
107:
108: /**
109: * Generates a string to set the property.
110: */
111: public void generateSetNull(JavaWriter out, String pstmt,
112: String index) throws IOException {
113: out.println(pstmt + ".setNull(" + index
114: + "++, java.sql.Types.TINYINT);");
115: }
116:
117: /**
118: * Converts to an object.
119: */
120: public String toObject(String value) {
121: return "new Byte(" + value + ")";
122: }
123:
124: /**
125: * Converts the value.
126: */
127: public String generateCastFromObject(String value) {
128: return "((Number) " + value + ").byteValue()";
129: }
130:
131: /**
132: * Sets the value.
133: */
134: public void setParameter(PreparedStatement pstmt, int index,
135: Object value) throws SQLException {
136: if (value instanceof Number)
137: pstmt.setString(index, value.toString());
138: else
139: throw new IllegalArgumentException(
140: "Invalid argument for setParameter.");
141: }
142: }
|