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 PrimitiveCharArrayType extends Type {
043: private static final L10N L = new L10N(PrimitiveCharArrayType.class);
044:
045: private PrimitiveCharArrayType() {
046: }
047:
048: /**
049: * Returns the singleton PrimitiveCharArray type.
050: */
051: public static PrimitiveCharArrayType create() {
052: return new PrimitiveCharArrayType();
053: }
054:
055: /**
056: * Returns the type name.
057: */
058: public String getName() {
059: return "char[]";
060: }
061:
062: /**
063: * Returns the java type.
064: */
065: public String getJavaTypeName() {
066: return "char[]";
067: }
068:
069: /**
070: * Generates a string to load the property.
071: */
072: public int generateLoad(JavaWriter out, String rs, String indexVar,
073: int index) throws IOException {
074: out.print(rs + ".getString(" + indexVar + " + " + index + ")");
075: out.print(" == null || " + rs + ".wasNull() ? null : ");
076: out.print(rs + ".getString(" + indexVar + " + " + index
077: + ").toCharArray()");
078:
079: return index + 1;
080: }
081:
082: /**
083: * Generates a string to set the property.
084: */
085: public void generateSet(JavaWriter out, String pstmt, String index,
086: String value) throws IOException {
087: out.println("if (" + value + " == null)");
088: out.println(" " + pstmt + ".setNull(" + index
089: + "++, java.sql.Types.CHAR);");
090: out.println("else");
091: out.println(" " + pstmt + ".setString(" + index
092: + "++, new String(" + value + "));");
093: }
094:
095: /**
096: * Sets the value.
097: */
098: public void setParameter(PreparedStatement pstmt, int index,
099: Object value) throws SQLException {
100: if (value == null)
101: pstmt.setNull(index, java.sql.Types.CHAR);
102: else
103: pstmt.setString(index, new String((char[]) value));
104: }
105:
106: /**
107: * Gets the value.
108: */
109: public Object getObject(ResultSet rs, int index)
110: throws SQLException {
111: String s = rs.getString(index);
112:
113: if (rs.wasNull())
114: return null;
115:
116: return s.toCharArray();
117: }
118: }
|