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 Rodrigo Westrupp
027: */
028:
029: package com.caucho.amber.type;
030:
031: import com.caucho.java.JavaWriter;
032: import com.caucho.util.L10N;
033:
034: import java.io.IOException;
035: import java.sql.PreparedStatement;
036: import java.sql.ResultSet;
037: import java.sql.SQLException;
038:
039: /**
040: * The type of a property.
041: */
042: public class CharacterArrayType extends ArrayType {
043: private static final L10N L = new L10N(CharacterArrayType.class);
044:
045: private CharacterArrayType() {
046: }
047:
048: /**
049: * Returns the singleton CharacterArray type.
050: */
051: public static CharacterArrayType create() {
052: return new CharacterArrayType();
053: }
054:
055: /**
056: * Returns the type name.
057: */
058: public String getName() {
059: return "java.lang.Character[]";
060: }
061:
062: /**
063: * Returns the java type.
064: */
065: public String getJavaTypeName() {
066: return "java.lang.Character[]";
067: }
068:
069: /**
070: * Returns the java type for a single entry.
071: */
072: public String getJavaObjectTypeName() {
073: return "java.lang.Character";
074: }
075:
076: /**
077: * Returns the corresponding primitive array
078: * type name.
079: */
080: public String getPrimitiveArrayTypeName() {
081: return "char[]";
082: }
083:
084: /**
085: * Generates a string to load the property.
086: */
087: public int generateLoad(JavaWriter out, String rs, String indexVar,
088: int index) throws IOException {
089: out.print(rs + ".getString(" + indexVar + " + " + index + ")");
090: out.print(" == null || " + rs + ".wasNull() ? null : ");
091: out.print(rs + ".getString(" + indexVar + " + " + index
092: + ").toCharArray()");
093:
094: return index + 1;
095: }
096:
097: /**
098: * Generates a string to set the property.
099: */
100: public void generateSet(JavaWriter out, String pstmt, String index,
101: String value) throws IOException {
102: out.println("if (" + value + " == null)");
103: out.println(" " + pstmt + ".setNull(" + index
104: + "++, java.sql.Types.CHAR);");
105: out.println("else {");
106: out.println(" java.lang.Character[] super_temp_" + index
107: + " = " + value + ";");
108: out.println(" char[] temp_" + index
109: + " = new char[super_temp_" + index + ".length];");
110: out.println(" for (int i=0; i < temp_" + index
111: + ".length; i++)");
112: out.println(" temp_" + index + "[i] = super_temp_" + index
113: + "[i].charValue();");
114: out.println(" " + pstmt + ".setString(" + index
115: + "++, new String(temp_" + index + "));");
116: out.println("}");
117: }
118:
119: /**
120: * Sets the value.
121: */
122: public void setParameter(PreparedStatement pstmt, int index,
123: Object value) throws SQLException {
124: Character[] wrapperCharacter = (Character[]) value;
125:
126: char[] primitiveCharacter = new char[wrapperCharacter.length];
127: for (int i = 0; i < wrapperCharacter.length; i++)
128: primitiveCharacter[i] = wrapperCharacter[i].charValue();
129:
130: pstmt.setString(index, new String(primitiveCharacter));
131: }
132:
133: /**
134: * Gets the value.
135: */
136: public Object getObject(ResultSet rs, int index)
137: throws SQLException {
138: char[] primitiveCharacter = rs.getString(index).toCharArray();
139:
140: if (rs.wasNull())
141: return null;
142:
143: Character[] wrapperCharacter = new Character[primitiveCharacter.length];
144: for (int i = 0; i < primitiveCharacter.length; i++)
145: wrapperCharacter[i] = new Character(primitiveCharacter[i]);
146:
147: return wrapperCharacter;
148: }
149: }
|