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.Types;
038:
039: /**
040: * The type of a property.
041: */
042: public class PrimitiveBooleanType extends PrimitiveType {
043: private static final L10N L = new L10N(PrimitiveBooleanType.class);
044:
045: private static final PrimitiveBooleanType BOOLEAN_TYPE = new PrimitiveBooleanType();
046:
047: private PrimitiveBooleanType() {
048: }
049:
050: /**
051: * Returns the boolean type.
052: */
053: public static PrimitiveBooleanType create() {
054: return BOOLEAN_TYPE;
055: }
056:
057: /**
058: * Returns the type name.
059: */
060: public String getName() {
061: return "boolean";
062: }
063:
064: /**
065: * Returns the foreign key type.
066: */
067: public Type getForeignType() {
068: return BooleanType.create();
069: }
070:
071: /**
072: * Returns true for a boolean type.
073: */
074: public boolean isBoolean() {
075: return true;
076: }
077:
078: /**
079: * Generates the type for the table.
080: */
081: public String generateCreateColumnSQL(AmberPersistenceUnit manager,
082: int length, int precision, int scale) {
083: return manager.getCreateColumnSQL(Types.BOOLEAN, length,
084: precision, scale);
085: }
086:
087: /**
088: * Generates a string to load the property.
089: */
090: public int generateLoad(JavaWriter out, String rs, String indexVar,
091: int index) throws IOException {
092: out.print(rs + ".getBoolean(" + indexVar + " + " + index + ")");
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(pstmt + ".setBoolean(" + index + "++, " + value
103: + ");");
104: }
105:
106: /**
107: * Generates a string to set the property.
108: */
109: public void generateSetNull(JavaWriter out, String pstmt,
110: String index) throws IOException {
111: out.println(pstmt + ".setNull(" + index
112: + "++, java.sql.Types.BIT);");
113: }
114:
115: /**
116: * Converts to an object.
117: */
118: public String toObject(String value) {
119: return "((" + value + ") ? Boolean.TRUE : Boolean.FALSE)";
120: }
121:
122: /**
123: * Converts the value.
124: */
125: public String generateCastFromObject(String value) {
126: return "((Boolean) " + value + ").booleanValue()";
127: }
128:
129: /**
130: * Returns a test for null.
131: */
132: public String generateIsNull(String value) {
133: return "! (" + value + ")";
134: }
135: }
|