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.java.JavaWriter;
032: import com.caucho.util.L10N;
033:
034: import java.io.IOException;
035: import java.sql.ResultSet;
036: import java.sql.SQLException;
037:
038: /**
039: * The type of a property.
040: */
041: public class YesNoType extends Type {
042: private static final L10N L = new L10N(YesNoType.class);
043:
044: private static final YesNoType YES_NO_TYPE = new YesNoType();
045:
046: private YesNoType() {
047: }
048:
049: /**
050: * Returns the singleton type.
051: */
052: public static YesNoType create() {
053: return YES_NO_TYPE;
054: }
055:
056: /**
057: * Returns the type name.
058: */
059: public String getName() {
060: return "java.lang.Boolean";
061: }
062:
063: /**
064: * Generates a string to load the property.
065: */
066: public int generateLoad(JavaWriter out, String rs, String indexVar,
067: int index) throws IOException {
068: out.print("com.caucho.amber.type.YesNoType.toBoolean(" + rs
069: + ".getString(" + indexVar + " + " + index + "), " + rs
070: + ".wasNull())");
071:
072: return index + 1;
073: }
074:
075: /**
076: * Generates a string to set the property.
077: */
078: public void generateSet(JavaWriter out, String pstmt, String index,
079: String value) throws IOException {
080: out.println("if (" + value + " == null)");
081: out.println(" " + pstmt + ".setNull(" + index + "++, 0);");
082: out.println("else");
083: out.println(" " + pstmt + ".setString(" + index + "++, "
084: + value + ".booleanValue() ? \"y\" : \"n\");");
085: }
086:
087: /**
088: * Converts a value to a boolean.
089: */
090: public static Boolean toBoolean(String value, boolean wasNull) {
091: if (wasNull)
092: return null;
093: else if ("y".equalsIgnoreCase(value))
094: return Boolean.TRUE;
095: else if ("n".equalsIgnoreCase(value))
096: return Boolean.FALSE;
097: else
098: return Boolean.FALSE;
099: }
100:
101: /**
102: * Gets the value.
103: */
104: public Object getObject(ResultSet rs, int index)
105: throws SQLException {
106: String value = rs.getString(index);
107:
108: if (rs.wasNull())
109: return null;
110: else if ("y".equalsIgnoreCase(value))
111: return Boolean.TRUE;
112: else if ("n".equalsIgnoreCase(value))
113: return Boolean.FALSE;
114: else
115: return Boolean.FALSE;
116: }
117: }
|