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: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.amber.type;
031:
032: import com.caucho.amber.manager.AmberPersistenceUnit;
033: import com.caucho.java.JavaWriter;
034: import com.caucho.util.L10N;
035:
036: import java.io.IOException;
037: import java.sql.PreparedStatement;
038: import java.sql.ResultSet;
039: import java.sql.SQLException;
040:
041: /**
042: * The type of a property.
043: */
044: public class StringType extends Type {
045: private static final L10N L = new L10N(StringType.class);
046:
047: private static final StringType STRING_TYPE = new StringType();
048:
049: private StringType() {
050: }
051:
052: /**
053: * Returns the string type.
054: */
055: public static StringType create() {
056: return STRING_TYPE;
057: }
058:
059: /**
060: * Returns the type name.
061: */
062: public String getName() {
063: return "java.lang.String";
064: }
065:
066: /**
067: * Generates the type for the table.
068: */
069: public String generateCreateColumnSQL(AmberPersistenceUnit manager,
070: int length, int precision, int scale) {
071: if (length == 0)
072: length = 255;
073:
074: return "varchar(" + length + ")";
075: // return manager.getCreateColumnSQL(Types.VARCHAR, length);
076: }
077:
078: /**
079: * Generates a string to load the property.
080: */
081: public int generateLoad(JavaWriter out, String rs, String indexVar,
082: int index) throws IOException {
083: out.print(rs + ".getString(" + indexVar + " + " + index + ")");
084:
085: return index + 1;
086: }
087:
088: /**
089: * Generates a string to set the property.
090: */
091: public void generateSet(JavaWriter out, String pstmt, String index,
092: String value) throws IOException {
093: if (pstmt.startsWith("query"))
094: out.println(pstmt + ".setString(" + index + "++, " + value
095: + ");");
096: else
097: out.println("__caucho_setInternalString(" + pstmt + ", "
098: + index + "++, " + value + ");");
099: }
100:
101: /**
102: * Sets the value.
103: */
104: public void setParameter(PreparedStatement pstmt, int index,
105: Object value) throws SQLException {
106: if (value == null) {
107: // XXX: issue with derby.
108: // pstmt.setNull(index, java.sql.Types.OTHER);
109: pstmt.setString(index, null);
110: } else if (value instanceof String)
111: pstmt.setString(index, (String) value);
112: else
113: // ejb/0623
114: pstmt.setObject(index, value);
115: }
116:
117: /**
118: * Gets the value.
119: */
120: public Object getObject(ResultSet rs, int index)
121: throws SQLException {
122: return rs.getString(index);
123: }
124: }
|