001: /**********************************************************************
002: Copyright (c) 2004 Andy Jefferson 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: 2004 Erik Bengtson - AdapterTime methods
018: ...
019: **********************************************************************/package org.jpox.store;
020:
021: import java.sql.Timestamp;
022: import java.util.HashSet;
023:
024: import org.jpox.ClassLoaderResolver;
025: import org.jpox.plugin.PluginManager;
026: import org.jpox.store.expression.NumericExpression;
027: import org.jpox.store.expression.QueryExpression;
028: import org.jpox.store.expression.ScalarExpression;
029: import org.jpox.store.mapping.JavaTypeMapping;
030: import org.jpox.store.mapping.MappingManager;
031: import org.jpox.util.Localiser;
032:
033: /**
034: * Generalised datastore representation. Each datastore is assumed to have a
035: * "name", and a "version" (of the form major.minor.revision) and will store
036: * "identifiers". In addition, each field will have a Mapping to the datastore
037: * <P>
038: * This will typically be extended to provide e.g RDBMS connection using JDBC,
039: * or XML file connection using a DOM parser.
040: * </P>
041: * @version $Revision: 1.31 $
042: */
043: public abstract class AbstractDatastoreAdapter implements
044: DatastoreAdapter {
045: protected static final Localiser LOCALISER_BASE = Localiser
046: .getInstance("org.jpox.store.Localisation");
047:
048: /** The set of reserved keywords for this datastore. */
049: protected final HashSet reservedKeywords = new HashSet();
050:
051: /** The product name of the underlying datastore. */
052: protected String datastoreProductName;
053:
054: /** The version number of the underlying datastore as a string. */
055: protected String datastoreProductVersion;
056:
057: /** The major version number of the underlying datastore. */
058: protected int datastoreMajorVersion;
059:
060: /** The minor version number of the underlying datastore. */
061: protected int datastoreMinorVersion;
062:
063: /** The revision version number of the underlying datastore. */
064: protected int datastoreRevisionVersion = 0;
065:
066: /** The String used to quote identifiers. */
067: protected String identifierQuoteString;
068:
069: /** Manager for the mapping between Java and datastore types. */
070: protected MappingManager mappingManager;
071:
072: /**
073: * Constructor.
074: */
075: public AbstractDatastoreAdapter() {
076: }
077:
078: /**
079: * Accessor for a new mapping manager.
080: * Must be implemented by subclasses.
081: * @return The new mapping manager
082: */
083: protected abstract MappingManager getNewMappingManager();
084:
085: /**
086: * Load the datastore mapping declared as Plug-in
087: * @param mgr the PluginManager
088: * @param clr the ClassLoaderResolver
089: */
090: public void loadDatastoreMapping(PluginManager mgr,
091: ClassLoaderResolver clr) {
092: getMappingManager().loadDatastoreMapping(mgr, clr,
093: getVendorID());
094: }
095:
096: /**
097: * Acessor for the MappingManager
098: * @return the MappingManager
099: */
100: public MappingManager getMappingManager() {
101: if (mappingManager == null) {
102: mappingManager = getNewMappingManager();
103: }
104: return mappingManager;
105: }
106:
107: /**
108: * Convenience accessor for the mapping for the specified class when not serialised and not embedded.
109: * @param c Java type
110: * @param storeMgr the StoreManager
111: * @return The mapping for the class.
112: */
113: public JavaTypeMapping getMapping(Class c, StoreManager storeMgr) {
114: // Relay to the method below
115: return getMapping(c, storeMgr, false, false, null);
116: }
117:
118: /**
119: * Convenience accessor for the mapping for the specified class.
120: * @param c Java type
121: * @param storeMgr the StoreManager
122: * @param serialised Whether the type is serialised
123: * @param embedded Whether the type is embedded
124: * @param fieldName Name of field (for logging only).
125: * @return The mapping for the class.
126: */
127: public JavaTypeMapping getMapping(Class c, StoreManager storeMgr,
128: boolean serialised, boolean embedded, String fieldName) {
129: return getMappingManager().getMapping(c, serialised, embedded,
130: storeMgr, fieldName);
131: }
132:
133: /**
134: * Accessor for the mapping for the specified class when not serialised and not embedded.
135: * @param c Java type
136: * @param storeMgr The Store manager
137: * @param clr ClassLoader resolver
138: * @return The mapping for the class.
139: */
140: public JavaTypeMapping getMapping(Class c, StoreManager storeMgr,
141: ClassLoaderResolver clr) {
142: // Relay to the method below
143: return getMapping(c, storeMgr, clr, false, false);
144: }
145:
146: /**
147: * Convenience accessor for the mapping for the specified class.
148: * @param c Java type
149: * @param storeMgr The Store manager
150: * @param clr ClassLoader resolver
151: * @param serialised Whether the type is serialised
152: * @param embedded Whether the type is embedded
153: * @return The mapping for the class.
154: **/
155: public JavaTypeMapping getMapping(Class c, StoreManager storeMgr,
156: ClassLoaderResolver clr, boolean serialised,
157: boolean embedded) {
158: return getMappingManager().getMapping(c, serialised, embedded,
159: storeMgr, clr);
160: }
161:
162: /**
163: * Convenience accessor for the mapping for the specified class.
164: * Provides a wrapper to the method on the MappingManager.
165: * @param c Java type
166: * @param expr the ScalarExpression
167: * @return The mapping for the class.
168: */
169: protected final JavaTypeMapping getMapping(Class c,
170: ScalarExpression expr) {
171: return getMappingManager().getMapping(c, false, false,
172: expr.getQueryExpression().getStoreManager(),
173: expr.getQueryExpression().getClassLoaderResolver());
174: }
175:
176: /* (non-Javadoc)
177: * @see org.jpox.store.DatastoreAdapter#getVendorID()
178: */
179: public String getVendorID() {
180: return null;
181: }
182:
183: /* (non-Javadoc)
184: * @see org.jpox.store.DatastoreAdapter#isReservedKeyword(java.lang.String)
185: */
186: public boolean isReservedKeyword(String word) {
187: return reservedKeywords.contains(word);
188: }
189:
190: /**
191: * Accessor for an identifier quote string.
192: * @return Identifier quote string.
193: **/
194: public String getIdentifierQuoteString() {
195: return identifierQuoteString;
196: }
197:
198: /* (non-Javadoc)
199: * @see org.jpox.store.DatastoreAdapter#newQueryStatement(org.jpox.store.DatastoreContainerObject, org.jpox.ClassLoaderResolver)
200: */
201: public abstract QueryExpression newQueryStatement(
202: DatastoreContainerObject container, ClassLoaderResolver clr);
203:
204: /* (non-Javadoc)
205: * @see org.jpox.store.DatastoreAdapter#newQueryStatement(org.jpox.store.DatastoreContainerObject, org.jpox.store.DatastoreIdentifier, org.jpox.ClassLoaderResolver)
206: */
207: public abstract QueryExpression newQueryStatement(
208: DatastoreContainerObject container,
209: DatastoreIdentifier rangeVar, ClassLoaderResolver clr);
210:
211: /* (non-Javadoc)
212: * @see org.jpox.store.DatastoreAdapter#getAdapterTime(java.sql.Timestamp)
213: */
214: public long getAdapterTime(Timestamp time) {
215: long timestamp = getTime(time.getTime(), time.getNanos());
216: int ms = getMiliseconds(time.getNanos());
217:
218: return timestamp + ms;
219: }
220:
221: protected long getTime(long time, long nanos) {
222: if (nanos < 0) {
223: return (((time / 1000) - 1) * 1000);
224: }
225: return (time / 1000) * 1000;
226: }
227:
228: protected int getMiliseconds(long nanos) {
229: return (int) (nanos / 1000000);
230: }
231:
232: /* (non-Javadoc)
233: * @see org.jpox.store.DatastoreAdapter#getDatastoreMajorVersion()
234: */
235: public int getDatastoreMajorVersion() {
236: return datastoreMajorVersion;
237: }
238:
239: /* (non-Javadoc)
240: * @see org.jpox.store.DatastoreAdapter#getDatastoreMinorVersion()
241: */
242: public int getDatastoreMinorVersion() {
243: return datastoreMinorVersion;
244: }
245:
246: /* (non-Javadoc)
247: * @see org.jpox.store.DatastoreAdapter#modOperator(org.jpox.store.expression.ScalarExpression, org.jpox.store.expression.ScalarExpression)
248: */
249: public NumericExpression modOperator(ScalarExpression operand1,
250: ScalarExpression operand2) {
251: return new NumericExpression(operand1, ScalarExpression.OP_MOD,
252: operand2);
253: }
254:
255: /**
256: * Accessor for whether autoincrementing fields are supported.
257: * @return Whether we support autoincrementing fields
258: */
259: public boolean supportsIdentityFields() {
260: return false;
261: }
262:
263: /**
264: * Accessor for whether sequences are supported.
265: * @return whether we support sequences.
266: **/
267: public boolean supportsSequences() {
268: return false;
269: }
270:
271: /**
272: * Whether the datastore will support setting the query fetch size to the supplied value.
273: * @param size The value to set to
274: * @return Whether it is supported.
275: */
276: public boolean supportsQueryFetchSize(int size) {
277: // Default to supported all possible values
278: return true;
279: }
280:
281: /**
282: * Accessor for whether a bit is really mapped in the datastore to boolean.
283: * @return Whether bit is really stored as a boolean
284: */
285: public boolean isBitReallyBoolean() {
286: return false;
287: }
288: }
|