001: /**********************************************************************
002: Copyright (c) 2002 Mike Martin (TJDO) and others. All rights reserved.
003: Licensed under the Apache License, Version 2.0 (the "License");
004: you may not use this file except in compliance with the License.
005: You may obtain a copy of the License at
006:
007: http://www.apache.org/licenses/LICENSE-2.0
008:
009: Unless required by applicable law or agreed to in writing, software
010: distributed under the License is distributed on an "AS IS" BASIS,
011: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: See the License for the specific language governing permissions and
013: limitations under the License.
014:
015:
016: Contributors:
017: 2003 Andy Jefferson - coding standards
018: 2005 Andy Jefferson - added "value" field for cases where a parameter is put in a query "result"
019: 2006 Andy Jefferson - remove typeInfo
020: ...
021: **********************************************************************/package org.jpox.store.mapping;
022:
023: import org.jpox.ClassLoaderResolver;
024: import org.jpox.ObjectManager;
025: import org.jpox.metadata.AbstractMemberMetaData;
026: import org.jpox.store.DatastoreAdapter;
027: import org.jpox.store.DatastoreContainerObject;
028: import org.jpox.store.DatastoreField;
029: import org.jpox.store.expression.ScalarExpression;
030:
031: /**
032: * Simple mapping for a java field mapping to a single datastore field.
033: * @version $Revision: 1.32 $
034: **/
035: public abstract class SingleFieldMapping extends JavaTypeMapping {
036: /**
037: * Initialize this JavaTypeMapping with the given DatastoreAdapter for the given FieldMetaData.
038: * @param dba The Datastore Adapter that this Mapping should use.
039: * @param fmd FieldMetaData for the field to be mapped (if any)
040: * @param container The datastore container storing this mapping (if any)
041: * @param clr the ClassLoaderResolver
042: */
043: public void initialize(DatastoreAdapter dba,
044: AbstractMemberMetaData fmd,
045: DatastoreContainerObject container, ClassLoaderResolver clr) {
046: super .initialize(dba, fmd, container, clr);
047: prepareDatastoreMapping();
048: }
049:
050: /**
051: * Method to prepare a field mapping for use in the datastore.
052: * This creates the column in the table.
053: */
054: protected void prepareDatastoreMapping() {
055: MappingManager mmgr = datastoreContainer.getStoreManager()
056: .getMappingManager();
057: DatastoreField col = mmgr.createDatastoreField(this ,
058: getJavaTypeForDatastoreMapping(0), 0);
059: mmgr.createDatastoreMapping(this , fmd, 0, datastoreContainer
060: .getStoreManager(), col);
061: }
062:
063: /**
064: * Accessor for the default length for this type in the datastore (if applicable).
065: * @param index requested datastore field index.
066: * @return Default length
067: */
068: public int getDefaultLength(int index) {
069: return -1;
070: }
071:
072: /**
073: * Accessor for an array of valid values that this type can take.
074: * This can be used at the datastore side for restricting the values to be inserted.
075: * @param index requested datastore field index.
076: * @return The valid values
077: */
078: public ScalarExpression[] getValidValues(int index) {
079: return null;
080: }
081:
082: /**
083: * Accessor for the name of the java-type actually used when mapping the particular datastore
084: * field. This java-type must have an entry in the datastore mappings.
085: * @param index requested datastore field index.
086: * @return the name of java-type for the requested datastore field.
087: */
088: public String getJavaTypeForDatastoreMapping(int index) {
089: if (getJavaType() == null) {
090: return null;
091: }
092: return getJavaType().getName();
093: }
094:
095: /**
096: * Equality operator
097: * @param obj The object to compare with
098: * @return Whether the objects are equal
099: */
100: public boolean equals(Object obj) {
101: if (this == obj) {
102: return true;
103: }
104:
105: if (!(obj instanceof SingleFieldMapping)) {
106: return false;
107: }
108:
109: SingleFieldMapping cm = (SingleFieldMapping) obj;
110:
111: return getClass().equals(cm.getClass()) && dba.equals(cm.dba);
112: }
113:
114: public void setBoolean(ObjectManager om, Object preparedStatement,
115: int[] exprIndex, boolean value) {
116: getDataStoreMapping(0).setBoolean(preparedStatement,
117: exprIndex[0], value);
118: }
119:
120: public boolean getBoolean(ObjectManager om, Object resultSet,
121: int[] exprIndex) {
122: return getDataStoreMapping(0).getBoolean(resultSet,
123: exprIndex[0]);
124: }
125:
126: public void setChar(ObjectManager om, Object preparedStatement,
127: int[] exprIndex, char value) {
128: getDataStoreMapping(0).setChar(preparedStatement, exprIndex[0],
129: value);
130: }
131:
132: public char getChar(ObjectManager om, Object resultSet,
133: int[] exprIndex) {
134: return getDataStoreMapping(0).getChar(resultSet, exprIndex[0]);
135: }
136:
137: public void setByte(ObjectManager om, Object preparedStatement,
138: int[] exprIndex, byte value) {
139: getDataStoreMapping(0).setByte(preparedStatement, exprIndex[0],
140: value);
141: }
142:
143: public byte getByte(ObjectManager om, Object resultSet,
144: int[] exprIndex) {
145: return getDataStoreMapping(0).getByte(resultSet, exprIndex[0]);
146: }
147:
148: public void setShort(ObjectManager om, Object preparedStatement,
149: int[] exprIndex, short value) {
150: getDataStoreMapping(0).setShort(preparedStatement,
151: exprIndex[0], value);
152: }
153:
154: public short getShort(ObjectManager om, Object resultSet,
155: int[] exprIndex) {
156: return getDataStoreMapping(0).getShort(resultSet, exprIndex[0]);
157: }
158:
159: public void setInt(ObjectManager om, Object preparedStatement,
160: int[] exprIndex, int value) {
161: getDataStoreMapping(0).setInt(preparedStatement, exprIndex[0],
162: value);
163: }
164:
165: public int getInt(ObjectManager om, Object resultSet,
166: int[] exprIndex) {
167: return getDataStoreMapping(0).getInt(resultSet, exprIndex[0]);
168: }
169:
170: public void setLong(ObjectManager om, Object preparedStatement,
171: int[] exprIndex, long value) {
172: getDataStoreMapping(0).setLong(preparedStatement, exprIndex[0],
173: value);
174: }
175:
176: public long getLong(ObjectManager om, Object resultSet,
177: int[] exprIndex) {
178: return getDataStoreMapping(0).getLong(resultSet, exprIndex[0]);
179: }
180:
181: public void setFloat(ObjectManager om, Object preparedStatement,
182: int[] exprIndex, float value) {
183: getDataStoreMapping(0).setFloat(preparedStatement,
184: exprIndex[0], value);
185: }
186:
187: public float getFloat(ObjectManager om, Object resultSet,
188: int[] exprIndex) {
189: return getDataStoreMapping(0).getFloat(resultSet, exprIndex[0]);
190: }
191:
192: public void setDouble(ObjectManager om, Object preparedStatement,
193: int[] exprIndex, double value) {
194: getDataStoreMapping(0).setDouble(preparedStatement,
195: exprIndex[0], value);
196: }
197:
198: public double getDouble(ObjectManager om, Object resultSet,
199: int[] exprIndex) {
200: return getDataStoreMapping(0)
201: .getDouble(resultSet, exprIndex[0]);
202: }
203:
204: public void setString(ObjectManager om, Object preparedStatement,
205: int[] exprIndex, String value) {
206: getDataStoreMapping(0).setString(preparedStatement,
207: exprIndex[0], value);
208: }
209:
210: public String getString(ObjectManager om, Object resultSet,
211: int[] exprIndex) {
212: return getDataStoreMapping(0)
213: .getString(resultSet, exprIndex[0]);
214: }
215:
216: public void setObject(ObjectManager om, Object preparedStatement,
217: int[] exprIndex, Object value) {
218: getDataStoreMapping(0).setObject(preparedStatement,
219: exprIndex[0], value);
220: }
221:
222: public Object getObject(ObjectManager om, Object resultSet,
223: int[] exprIndex) {
224: if (exprIndex == null) {
225: return null;
226: }
227: return getDataStoreMapping(0)
228: .getObject(resultSet, exprIndex[0]);
229: }
230: }
|