001: /*
002: * Copyright 2004 (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: ColumnMapping.java,v 1.5 2004/01/18 03:01:06 jackknifebarber 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 javax.jdo.JDOFatalInternalException;
017:
018: /**
019: * A database mapping that maps a Java type to a single column.
020: * <p>
021: * A ColumnMapping can set Java objects directly into JDBC PreparedStatement
022: * parameters, and get Java objects directly from JDBC ResultSet columns.
023: *
024: * @author <a href="mailto:mmartin5@austin.rr.com">Mike Martin</a>
025: * @version $Revision: 1.5 $
026: *
027: * @see DatabaseAdapter#getMapping(Column)
028: */
029:
030: public abstract class ColumnMapping extends Mapping {
031: protected final Column col;
032:
033: protected TypeInfo typeInfo;
034:
035: public ColumnMapping(DatabaseAdapter dba, Class type) {
036: super (dba, type);
037: this .col = null;
038: }
039:
040: public ColumnMapping(Column col) {
041: super (col.getStoreManager().getDatabaseAdapter(), col.getType());
042: this .col = col;
043: }
044:
045: protected abstract TypeInfo getTypeInfo();
046:
047: protected void initTypeInfo() {
048: TypeInfo typeInfo = getTypeInfo();
049:
050: if (typeInfo == null)
051: throw new UnsupportedDataTypeException(
052: "No suitable JDBC type found for column " + col);
053:
054: if (this .typeInfo != null)
055: throw new IllegalStateException(
056: "JDBC type already selected for column " + col);
057:
058: this .typeInfo = typeInfo;
059:
060: if (col != null)
061: col.setTypeInfo(typeInfo);
062: }
063:
064: public Column getColumn() {
065: return col;
066: }
067:
068: public String getSQLInsertionValue() {
069: return "?";
070: }
071:
072: public String getSQLUpdateValue() {
073: return "?";
074: }
075:
076: public SQLExpression newSQLExpression(QueryStatement qs,
077: TableExpression te, String fieldName) {
078: if (col == null)
079: throw new JDOFatalInternalException(
080: "Mapping has no specific column: " + this );
081:
082: return newSQLExpression(qs, qs.getColumn(te, col), fieldName);
083: }
084:
085: protected String failureMessage(String method) {
086: return "Somehow " + getClass().getName() + "." + method
087: + "() was called, which should not have been possible";
088: }
089:
090: public void setBoolean(PersistenceManager pm, PreparedStatement ps,
091: int param, boolean value) {
092: throw new JDOFatalInternalException(
093: failureMessage("setBoolean"));
094: }
095:
096: public boolean getBoolean(PersistenceManager pm, ResultSet rs,
097: int column) {
098: throw new JDOFatalInternalException(
099: failureMessage("setBoolean"));
100: }
101:
102: public void setChar(PersistenceManager pm, PreparedStatement ps,
103: int param, char value) {
104: throw new JDOFatalInternalException(failureMessage("setChar"));
105: }
106:
107: public char getChar(PersistenceManager pm, ResultSet rs, int column) {
108: throw new JDOFatalInternalException(failureMessage("getChar"));
109: }
110:
111: public void setByte(PersistenceManager pm, PreparedStatement ps,
112: int param, byte value) {
113: throw new JDOFatalInternalException(failureMessage("setByte"));
114: }
115:
116: public byte getByte(PersistenceManager pm, ResultSet rs, int column) {
117: throw new JDOFatalInternalException(failureMessage("getByte"));
118: }
119:
120: public void setShort(PersistenceManager pm, PreparedStatement ps,
121: int param, short value) {
122: throw new JDOFatalInternalException(failureMessage("setShort"));
123: }
124:
125: public short getShort(PersistenceManager pm, ResultSet rs,
126: int column) {
127: throw new JDOFatalInternalException(failureMessage("getShort"));
128: }
129:
130: public void setInt(PersistenceManager pm, PreparedStatement ps,
131: int param, int value) {
132: throw new JDOFatalInternalException(failureMessage("setInt"));
133: }
134:
135: public int getInt(PersistenceManager pm, ResultSet rs, int column) {
136: throw new JDOFatalInternalException(failureMessage("getInt"));
137: }
138:
139: public void setLong(PersistenceManager pm, PreparedStatement ps,
140: int param, long value) {
141: throw new JDOFatalInternalException(failureMessage("setLong"));
142: }
143:
144: public long getLong(PersistenceManager pm, ResultSet rs, int column) {
145: throw new JDOFatalInternalException(failureMessage("getLong"));
146: }
147:
148: public void setFloat(PersistenceManager pm, PreparedStatement ps,
149: int param, float value) {
150: throw new JDOFatalInternalException(failureMessage("setFloat"));
151: }
152:
153: public float getFloat(PersistenceManager pm, ResultSet rs,
154: int column) {
155: throw new JDOFatalInternalException(failureMessage("getFloat"));
156: }
157:
158: public void setDouble(PersistenceManager pm, PreparedStatement ps,
159: int param, double value) {
160: throw new JDOFatalInternalException(failureMessage("setDouble"));
161: }
162:
163: public double getDouble(PersistenceManager pm, ResultSet rs,
164: int column) {
165: throw new JDOFatalInternalException(failureMessage("getDouble"));
166: }
167:
168: public void setString(PersistenceManager pm, PreparedStatement ps,
169: int param, String value) {
170: throw new JDOFatalInternalException(failureMessage("setString"));
171: }
172:
173: public String getString(PersistenceManager pm, ResultSet rs,
174: int column) {
175: throw new JDOFatalInternalException(failureMessage("getString"));
176: }
177:
178: public void setObject(PersistenceManager pm, PreparedStatement ps,
179: int param, Object value) {
180: throw new JDOFatalInternalException(failureMessage("setObject"));
181: }
182:
183: public Object getObject(PersistenceManager pm, ResultSet rs,
184: int column) {
185: throw new JDOFatalInternalException(failureMessage("getObject"));
186: }
187:
188: public boolean equals(Object obj) {
189: if (this == obj)
190: return true;
191:
192: if (!(obj instanceof ColumnMapping))
193: return false;
194:
195: ColumnMapping cm = (ColumnMapping) obj;
196:
197: return getClass().equals(cm.getClass())
198: && dba.equals(cm.dba)
199: && (col == null ? cm.col == null : col.equals(cm.col))
200: && (typeInfo == null ? cm.typeInfo == null : typeInfo
201: .equals(cm.typeInfo));
202: }
203:
204: public int hashCode() {
205: return dba.hashCode() ^ (col == null ? 0 : col.hashCode())
206: ^ (typeInfo == null ? 0 : typeInfo.hashCode());
207: }
208: }
|