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 char type.
043: */
044: public class PrimitiveCharType extends PrimitiveType {
045: private static final L10N L = new L10N(PrimitiveCharType.class);
046:
047: private static final PrimitiveCharType CHAR_TYPE = new PrimitiveCharType();
048:
049: private PrimitiveCharType() {
050: }
051:
052: /**
053: * Returns the boolean type.
054: */
055: public static PrimitiveCharType create() {
056: return CHAR_TYPE;
057: }
058:
059: /**
060: * Returns the type name.
061: */
062: public String getName() {
063: return "char";
064: }
065:
066: /**
067: * Returns the type as a foreign key.
068: */
069: public Type getForeignType() {
070: return CharacterType.create();
071: }
072:
073: /**
074: * Generates the type for the table.
075: */
076: public String generateCreateColumnSQL(AmberPersistenceUnit manager,
077: int length, int precision, int scale) {
078: return manager.getCreateColumnSQL(Types.CHAR, 0, precision,
079: scale);
080: }
081:
082: /**
083: * Generates a string to load the property.
084: */
085: public int generateLoad(JavaWriter out, String rs, String indexVar,
086: int index) throws IOException {
087: out.print("com.caucho.amber.type.PrimitiveCharType.toChar("
088: + rs + ".getString(" + indexVar + " + " + index + "))");
089:
090: return index + 1;
091: }
092:
093: /**
094: * Generates a string to set the property.
095: */
096: public void generateSet(JavaWriter out, String pstmt, String index,
097: String value) throws IOException {
098: out.println(pstmt + ".setString(" + index
099: + "++, String.valueOf(" + value + "));");
100: }
101:
102: /**
103: * Generates a string to set the property.
104: */
105: public void generateSetNull(JavaWriter out, String pstmt,
106: String index) throws IOException {
107: out.println(pstmt + ".setNull(" + index
108: + "++, java.sql.Types.CHAR);");
109: }
110:
111: /**
112: * Converts to an object.
113: */
114: public String toObject(String value) {
115: return "new Character(" + value + ")";
116: }
117:
118: /**
119: * Converts the value.
120: */
121: public String generateCastFromObject(String value) {
122: return "((Character) " + value + ").charValue()";
123: }
124:
125: /**
126: * Gets the value.
127: */
128: public Object getObject(ResultSet rs, int index)
129: throws SQLException {
130: String v = rs.getString(index);
131:
132: return rs.wasNull() ? null : new Character(v.charAt(0));
133: }
134:
135: /**
136: * Converts a value to a char.
137: */
138: public static char toChar(String value) {
139: if (value == null || value.length() == 0)
140: return 0;
141: else
142: return value.charAt(0);
143: }
144:
145: /**
146: * Sets the value.
147: */
148: public void setParameter(PreparedStatement pstmt, int index,
149: Object value) throws SQLException {
150: if ((value instanceof Character) || (value instanceof String))
151: pstmt.setString(index, value.toString());
152: else
153: throw new IllegalArgumentException(
154: "Invalid argument for setParameter.");
155: }
156: }
|