Package org.jpox.store.mapped

Source Code of org.jpox.store.mapped.AbstractDatastoreAdapter

/**********************************************************************
Copyright (c) 2004 Andy Jefferson and others. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.


Contributors:
2004 Erik Bengtson - AdapterTime methods
   ...
**********************************************************************/
package org.jpox.store.mapped;

import java.sql.Timestamp;
import java.util.HashSet;

import org.jpox.ClassLoaderResolver;
import org.jpox.ObjectManagerFactoryImpl;
import org.jpox.plugin.PluginManager;
import org.jpox.store.mapped.expression.NumericExpression;
import org.jpox.store.mapped.expression.QueryExpression;
import org.jpox.store.mapped.expression.ScalarExpression;
import org.jpox.store.mapped.mapping.JavaTypeMapping;
import org.jpox.store.mapped.mapping.MappingManager;
import org.jpox.util.Localiser;

/**
* Generalised datastore representation. Each datastore is assumed to have a
* "name", and a "version" (of the form major.minor.revision) and will store
* "identifiers". In addition, each field will have a Mapping to the datastore
* <P>
* This will typically be extended to provide e.g RDBMS connection using JDBC,
* or XML file connection using a DOM parser.
* </P>
* @version $Revision: 1.31 $
*/
public abstract class AbstractDatastoreAdapter implements DatastoreAdapter
{
    protected static final Localiser LOCALISER_BASE = Localiser.getInstance("org.jpox.store.Localisation",
        ObjectManagerFactoryImpl.class.getClassLoader());

    /** The set of reserved keywords for this datastore. */
    protected final HashSet reservedKeywords = new HashSet();

    /** The product name of the underlying datastore. */
    protected String datastoreProductName;

    /** The version number of the underlying datastore as a string. */
    protected String datastoreProductVersion;

    /** The major version number of the underlying datastore. */
    protected int datastoreMajorVersion;

    /** The minor version number of the underlying datastore. */
    protected int datastoreMinorVersion;

    /** The revision version number of the underlying datastore. */
    protected int datastoreRevisionVersion = 0;

    /** The String used to quote identifiers. */
    protected String identifierQuoteString;

    /** Manager for the mapping between Java and datastore types. */
    protected MappingManager mappingManager;

    /**
     * Constructor.
     */
    public AbstractDatastoreAdapter()
    {
    }

    /**
     * Accessor for a new mapping manager.
     * Must be implemented by subclasses.
     * @return The new mapping manager
     */
    protected abstract MappingManager getNewMappingManager();

    /**
     * Load the datastore mapping declared as Plug-in
     * @param mgr the PluginManager
     * @param clr the ClassLoaderResolver
     */
    public void loadDatastoreMapping(PluginManager mgr, ClassLoaderResolver clr)
    {
        getMappingManager().loadDatastoreMapping(mgr, clr, getVendorID());
    }

    /**
     * Acessor for the MappingManager
     * @return the MappingManager
     */
    public MappingManager getMappingManager()
    {
        if (mappingManager == null)
        {
            mappingManager = getNewMappingManager();
        }
        return mappingManager;
    }

    /**
     * Convenience accessor for the mapping for the specified class when not serialised and not embedded.
     * @param c Java type
     * @param storeMgr the StoreManager
     * @return The mapping for the class.
     */
    public JavaTypeMapping getMapping(Class c, MappedStoreManager storeMgr)
    {
        // Relay to the method below
        return getMapping(c, storeMgr, false, false, null);
    }

    /**
     * Convenience accessor for the mapping for the specified class.
     * @param c Java type
     * @param storeMgr the StoreManager
     * @param serialised Whether the type is serialised
     * @param embedded Whether the type is embedded
     * @param fieldName Name of field (for logging only).
     * @return The mapping for the class.
     */
    public JavaTypeMapping getMapping(Class c, MappedStoreManager storeMgr,
            boolean serialised, boolean embedded, String fieldName)
    {
        return getMappingManager().getMapping(c, serialised, embedded, storeMgr, fieldName);
    }

    /**
     * Accessor for the mapping for the specified class when not serialised and not embedded.
     * @param c Java type
     * @param storeMgr The Store manager
     * @param clr ClassLoader resolver
     * @return The mapping for the class.
     */
    public JavaTypeMapping getMapping(Class c, MappedStoreManager storeMgr, ClassLoaderResolver clr)
    {
        // Relay to the method below
        return getMapping(c, storeMgr, clr, false, false);
    }

    /**
     * Convenience accessor for the mapping for the specified class.
     * @param c Java type
     * @param storeMgr The Store manager
     * @param clr ClassLoader resolver
     * @param serialised Whether the type is serialised
     * @param embedded Whether the type is embedded
     * @return The mapping for the class.
     **/
    public JavaTypeMapping getMapping(Class c, MappedStoreManager storeMgr, ClassLoaderResolver clr,
            boolean serialised, boolean embedded)
    {
        return getMappingManager().getMapping(c, serialised, embedded, storeMgr, clr);
    }

    /**
     * Convenience accessor for the mapping for the specified class.
     * Provides a wrapper to the method on the MappingManager.
     * @param c Java type
     * @param expr the ScalarExpression
     * @return The mapping for the class.
     */
    protected final JavaTypeMapping getMapping(Class c, ScalarExpression expr)
    {
        return getMappingManager().getMapping(c, false, false, expr.getQueryExpression().getStoreManager(),
            expr.getQueryExpression().getClassLoaderResolver());
    }

    /* (non-Javadoc)
     * @see org.jpox.store.DatastoreAdapter#getVendorID()
     */
    public String getVendorID()
    {
        return null;
    }

    /* (non-Javadoc)
     * @see org.jpox.store.DatastoreAdapter#isReservedKeyword(java.lang.String)
     */
    public boolean isReservedKeyword(String word)
    {
        return reservedKeywords.contains(word);
    }

    /**
     * Accessor for an identifier quote string.
     * @return Identifier quote string.
     **/
    public String getIdentifierQuoteString()
    {
        return identifierQuoteString;
    }


    /* (non-Javadoc)
     * @see org.jpox.store.DatastoreAdapter#newQueryStatement(org.jpox.store.DatastoreContainerObject, org.jpox.ClassLoaderResolver)
     */
    public abstract QueryExpression newQueryStatement(DatastoreContainerObject container, ClassLoaderResolver clr);

    /* (non-Javadoc)
     * @see org.jpox.store.DatastoreAdapter#newQueryStatement(org.jpox.store.DatastoreContainerObject, org.jpox.store.DatastoreIdentifier, org.jpox.ClassLoaderResolver)
     */
    public abstract QueryExpression newQueryStatement(DatastoreContainerObject container, DatastoreIdentifier rangeVar, ClassLoaderResolver clr);

    /* (non-Javadoc)
     * @see org.jpox.store.DatastoreAdapter#getAdapterTime(java.sql.Timestamp)
     */
    public long getAdapterTime(Timestamp time)
    {
        long timestamp = getTime(time.getTime(), time.getNanos());
        int ms = getMiliseconds(time.getNanos());

        return timestamp + ms;
    }

    protected long getTime(long time, long nanos)
    {
        if (nanos < 0)
        {
            return (((time / 1000) - 1) * 1000);
        }
        return (time / 1000) * 1000;
    }

    protected int getMiliseconds(long nanos)
    {
        return (int) (nanos / 1000000);
    }

    /* (non-Javadoc)
     * @see org.jpox.store.DatastoreAdapter#getDatastoreMajorVersion()
     */
    public int getDatastoreMajorVersion()
    {
        return datastoreMajorVersion;
    }

    /* (non-Javadoc)
     * @see org.jpox.store.DatastoreAdapter#getDatastoreMinorVersion()
     */
    public int getDatastoreMinorVersion()
    {
        return datastoreMinorVersion;
    }

    /* (non-Javadoc)
     * @see org.jpox.store.DatastoreAdapter#modOperator(org.jpox.store.expression.ScalarExpression, org.jpox.store.expression.ScalarExpression)
     */
    public NumericExpression modOperator(ScalarExpression operand1, ScalarExpression operand2)
    {
        return new NumericExpression(operand1, ScalarExpression.OP_MOD, operand2);
    }

    /**
     * Accessor for whether autoincrementing fields are supported.
     * @return Whether we support autoincrementing fields
     */
    public boolean supportsIdentityFields()
    {
        return false;
    }

    /**
     * Accessor for whether sequences are supported.
     * @return whether we support sequences.
     **/
    public boolean supportsSequences()
    {
        return false;
    }

    /**
     * Whether the datastore will support setting the query fetch size to the supplied value.
     * @param size The value to set to
     * @return Whether it is supported.
     */
    public boolean supportsQueryFetchSize(int size)
    {
        // Default to supported all possible values
        return true;
    }

    /**
     * Accessor for whether a bit is really mapped in the datastore to boolean.
     * @return Whether bit is really stored as a boolean
     */
    public boolean isBitReallyBoolean()
    {
        return false;
    }
}
TOP

Related Classes of org.jpox.store.mapped.AbstractDatastoreAdapter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.