KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > store > ColumnMapping


1 /*
2  * Copyright 2004 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: ColumnMapping.java,v 1.5 2004/01/18 03:01:06 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo.store;
12
13 import com.triactive.jdo.PersistenceManager;
14 import java.sql.PreparedStatement JavaDoc;
15 import java.sql.ResultSet JavaDoc;
16 import javax.jdo.JDOFatalInternalException;
17
18
19 /**
20  * A database mapping that maps a Java type to a single column.
21  * <p>
22  * A ColumnMapping can set Java objects directly into JDBC PreparedStatement
23  * parameters, and get Java objects directly from JDBC ResultSet columns.
24  *
25  * @author <a HREF="mailto:mmartin5@austin.rr.com">Mike Martin</a>
26  * @version $Revision: 1.5 $
27  *
28  * @see DatabaseAdapter#getMapping(Column)
29  */

30
31 public abstract class ColumnMapping extends Mapping
32 {
33     protected final Column col;
34
35     protected TypeInfo typeInfo;
36
37     public ColumnMapping(DatabaseAdapter dba, Class JavaDoc type)
38     {
39         super(dba, type);
40         this.col = null;
41     }
42
43     public ColumnMapping(Column col)
44     {
45         super(col.getStoreManager().getDatabaseAdapter(), col.getType());
46         this.col = col;
47     }
48
49     protected abstract TypeInfo getTypeInfo();
50
51     protected void initTypeInfo()
52     {
53         TypeInfo typeInfo = getTypeInfo();
54
55         if (typeInfo == null)
56             throw new UnsupportedDataTypeException("No suitable JDBC type found for column " + col);
57
58         if (this.typeInfo != null)
59             throw new IllegalStateException JavaDoc("JDBC type already selected for column " + col);
60
61         this.typeInfo = typeInfo;
62
63         if (col != null)
64             col.setTypeInfo(typeInfo);
65     }
66
67     public Column getColumn()
68     {
69         return col;
70     }
71
72     public String JavaDoc getSQLInsertionValue()
73     {
74         return "?";
75     }
76
77     public String JavaDoc getSQLUpdateValue()
78     {
79         return "?";
80     }
81
82     public SQLExpression newSQLExpression(QueryStatement qs, TableExpression te, String JavaDoc fieldName)
83     {
84         if (col == null)
85             throw new JDOFatalInternalException("Mapping has no specific column: " + this);
86
87         return newSQLExpression(qs, qs.getColumn(te, col), fieldName);
88     }
89
90     protected String JavaDoc failureMessage(String JavaDoc method)
91     {
92         return "Somehow " + getClass().getName() + "." + method + "() was called, which should not have been possible";
93     }
94
95     public void setBoolean(PersistenceManager pm, PreparedStatement JavaDoc ps, int param, boolean value)
96     {
97         throw new JDOFatalInternalException(failureMessage("setBoolean"));
98     }
99
100     public boolean getBoolean(PersistenceManager pm, ResultSet JavaDoc rs, int column)
101     {
102         throw new JDOFatalInternalException(failureMessage("setBoolean"));
103     }
104
105     public void setChar(PersistenceManager pm, PreparedStatement JavaDoc ps, int param, char value)
106     {
107         throw new JDOFatalInternalException(failureMessage("setChar"));
108     }
109
110     public char getChar(PersistenceManager pm, ResultSet JavaDoc rs, int column)
111     {
112         throw new JDOFatalInternalException(failureMessage("getChar"));
113     }
114
115     public void setByte(PersistenceManager pm, PreparedStatement JavaDoc ps, int param, byte value)
116     {
117         throw new JDOFatalInternalException(failureMessage("setByte"));
118     }
119
120     public byte getByte(PersistenceManager pm, ResultSet JavaDoc rs, int column)
121     {
122         throw new JDOFatalInternalException(failureMessage("getByte"));
123     }
124
125     public void setShort(PersistenceManager pm, PreparedStatement JavaDoc ps, int param, short value)
126     {
127         throw new JDOFatalInternalException(failureMessage("setShort"));
128     }
129
130     public short getShort(PersistenceManager pm, ResultSet JavaDoc rs, int column)
131     {
132         throw new JDOFatalInternalException(failureMessage("getShort"));
133     }
134
135     public void setInt(PersistenceManager pm, PreparedStatement JavaDoc ps, int param, int value)
136     {
137         throw new JDOFatalInternalException(failureMessage("setInt"));
138     }
139
140     public int getInt(PersistenceManager pm, ResultSet JavaDoc rs, int column)
141     {
142         throw new JDOFatalInternalException(failureMessage("getInt"));
143     }
144
145     public void setLong(PersistenceManager pm, PreparedStatement JavaDoc ps, int param, long value)
146     {
147         throw new JDOFatalInternalException(failureMessage("setLong"));
148     }
149
150     public long getLong(PersistenceManager pm, ResultSet JavaDoc rs, int column)
151     {
152         throw new JDOFatalInternalException(failureMessage("getLong"));
153     }
154
155     public void setFloat(PersistenceManager pm, PreparedStatement JavaDoc ps, int param, float value)
156     {
157         throw new JDOFatalInternalException(failureMessage("setFloat"));
158     }
159
160     public float getFloat(PersistenceManager pm, ResultSet JavaDoc rs, int column)
161     {
162         throw new JDOFatalInternalException(failureMessage("getFloat"));
163     }
164
165     public void setDouble(PersistenceManager pm, PreparedStatement JavaDoc ps, int param, double value)
166     {
167         throw new JDOFatalInternalException(failureMessage("setDouble"));
168     }
169
170     public double getDouble(PersistenceManager pm, ResultSet JavaDoc rs, int column)
171     {
172         throw new JDOFatalInternalException(failureMessage("getDouble"));
173     }
174
175     public void setString(PersistenceManager pm, PreparedStatement JavaDoc ps, int param, String JavaDoc value)
176     {
177         throw new JDOFatalInternalException(failureMessage("setString"));
178     }
179
180     public String JavaDoc getString(PersistenceManager pm, ResultSet JavaDoc rs, int column)
181     {
182         throw new JDOFatalInternalException(failureMessage("getString"));
183     }
184
185     public void setObject(PersistenceManager pm, PreparedStatement JavaDoc ps, int param, Object JavaDoc value)
186     {
187         throw new JDOFatalInternalException(failureMessage("setObject"));
188     }
189
190     public Object JavaDoc getObject(PersistenceManager pm, ResultSet JavaDoc rs, int column)
191     {
192         throw new JDOFatalInternalException(failureMessage("getObject"));
193     }
194
195     public boolean equals(Object JavaDoc obj)
196     {
197         if (this == obj)
198             return true;
199
200         if (!(obj instanceof ColumnMapping))
201             return false;
202
203         ColumnMapping cm = (ColumnMapping)obj;
204
205         return getClass().equals(cm.getClass())
206             && dba.equals(cm.dba)
207             && (col == null ? cm.col == null : col.equals(cm.col))
208             && (typeInfo == null ? cm.typeInfo == null : typeInfo.equals(cm.typeInfo));
209     }
210
211     public int hashCode()
212     {
213         return dba.hashCode()
214              ^ (col == null ? 0 : col.hashCode())
215              ^ (typeInfo == null ? 0 : typeInfo.hashCode());
216     }
217 }
218
Popular Tags