001: /*
002: * Copyright 2002 (C) TJDO.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the TJDO License version 1.0.
006: * See the terms of the TJDO License in the documentation provided with this software.
007: *
008: * $Id: BooleanMapping.java,v 1.9 2003/10/10 22:22:14 pierreg0 Exp $
009: */
010:
011: package com.triactive.jdo.store;
012:
013: import com.triactive.jdo.PersistenceManager;
014: import java.sql.PreparedStatement;
015: import java.sql.ResultSet;
016: import java.sql.SQLException;
017: import java.sql.Types;
018: import javax.jdo.JDODataStoreException;
019:
020: public class BooleanMapping extends ColumnMapping {
021: private boolean bitIsReallyBoolean;
022:
023: public BooleanMapping(DatabaseAdapter dba, Class type) {
024: super (dba, type);
025:
026: initTypeInfo();
027: }
028:
029: public BooleanMapping(Column col) {
030: super (col);
031:
032: col.checkPrimitive();
033:
034: initTypeInfo();
035: }
036:
037: public BooleanMapping(ClassBaseTable table, int relativeFieldNumber) {
038: this (table.newColumn(relativeFieldNumber));
039: }
040:
041: protected TypeInfo getTypeInfo() {
042: return dba.getTypeInfo(new int[] { Types.BIT, Types.CHAR });
043: }
044:
045: protected void initTypeInfo() {
046: super .initTypeInfo();
047:
048: if (col != null && typeInfo.dataType == Types.CHAR) {
049: col.setFixedLength(1);
050: col.checkString();
051:
052: StringBuffer constraints = new StringBuffer("CHECK ("
053: + col.getName() + " IN ('Y','N')");
054:
055: if (col.isNullable())
056: constraints.append(" OR " + col.getName() + " IS NULL");
057:
058: constraints.append(')');
059:
060: col.setConstraints(constraints.toString());
061: } else if (typeInfo.dataType == Types.BIT
062: && typeInfo.typeName.toLowerCase().startsWith("bool"))
063: bitIsReallyBoolean = true;
064: }
065:
066: public void setBoolean(PersistenceManager pm, PreparedStatement ps,
067: int param, boolean value) {
068: try {
069: if (typeInfo.dataType == Types.CHAR)
070: ps.setString(param, value ? "Y" : "N");
071: else
072: ps.setBoolean(param, value);
073: } catch (SQLException e) {
074: throw dba.newDataStoreException(
075: "Can't set boolean parameter: value = " + value, e);
076: }
077: }
078:
079: public boolean getBoolean(PersistenceManager pm, ResultSet rs,
080: int param) {
081: boolean value;
082:
083: try {
084: if (typeInfo.dataType == Types.CHAR) {
085: String s = rs.getString(param);
086:
087: if (s == null)
088: throw new NullValueException(
089: "Illegal null value in column " + col);
090:
091: if (s.equals("Y"))
092: value = true;
093: else if (s.equals("N"))
094: value = false;
095: else
096: throw new JDODataStoreException("Illegal value \""
097: + s + "\" in column " + col);
098: } else {
099: value = rs.getBoolean(param);
100:
101: if (rs.wasNull())
102: throw new NullValueException(
103: "Illegal null value in column " + col);
104: }
105: } catch (SQLException e) {
106: throw dba.newDataStoreException(
107: "Can't get boolean result: param = " + param, e);
108: }
109:
110: return value;
111: }
112:
113: public void setObject(PersistenceManager pm, PreparedStatement ps,
114: int param, Object value) {
115: try {
116: if (value == null)
117: ps.setNull(param, typeInfo.dataType);
118: else if (typeInfo.dataType == Types.CHAR)
119: ps.setString(param,
120: ((Boolean) value).booleanValue() ? "Y" : "N");
121: else
122: ps.setBoolean(param, ((Boolean) value).booleanValue());
123: } catch (SQLException e) {
124: throw dba.newDataStoreException(
125: "Can't set Boolean parameter: value = " + value, e);
126: }
127: }
128:
129: public Object getObject(PersistenceManager pm, ResultSet rs,
130: int param) {
131: Object value;
132:
133: try {
134: if (typeInfo.dataType == Types.CHAR) {
135: String s = rs.getString(param);
136:
137: if (s == null)
138: value = null;
139: else if (s.equals("Y"))
140: value = Boolean.TRUE;
141: else if (s.equals("N"))
142: value = Boolean.FALSE;
143: else
144: throw new JDODataStoreException("Illegal value \""
145: + s + "\" in column " + col);
146: } else {
147: boolean b = rs.getBoolean(param);
148: value = rs.wasNull() ? null : new Boolean(b);
149: }
150: } catch (SQLException e) {
151: throw dba.newDataStoreException(
152: "Can't get Boolean result: param = " + param, e);
153: }
154:
155: return value;
156: }
157:
158: public SQLExpression newSQLLiteral(QueryStatement qs, Object value) {
159: switch (typeInfo.dataType) {
160: case Types.BIT:
161: if (bitIsReallyBoolean)
162: return new BooleanLiteral(qs, ((Boolean) value)
163: .booleanValue());
164: else
165: return new BooleanBitColumnLiteral(qs,
166: ((Boolean) value).booleanValue());
167: case Types.CHAR:
168: return new BooleanCharColumnLiteral(qs, ((Boolean) value)
169: .booleanValue());
170: // case Types.BOOLEAN: // JDBC 3.0
171: default:
172: return new BooleanLiteral(qs, ((Boolean) value)
173: .booleanValue());
174: }
175: }
176:
177: public SQLExpression newSQLExpression(QueryStatement qs,
178: QueryStatement.QueryColumn qsc, String fieldName) {
179: switch (typeInfo.dataType) {
180: case Types.BIT:
181: if (bitIsReallyBoolean)
182: return new BooleanExpression(qs, qsc);
183: else
184: return new BooleanBitColumnExpression(qs, qsc);
185: case Types.CHAR:
186: return new BooleanCharColumnExpression(qs, qsc);
187: // case Types.BOOLEAN: // JDBC 3.0
188: default:
189: return new BooleanExpression(qs, qsc);
190: }
191: }
192: }
|