Package org.jpox.store.rdbms.table

Source Code of org.jpox.store.rdbms.table.ProbeTable

/**********************************************************************
Copyright (c) 2002 Kelly Grizzle 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:
2002 Mike Martin - unknown changes
2003 Andy Jefferson - added localiser
2003 Andy Jefferson - added catalog name
2003 Andy Jefferson - added default catalog/schema
    ...
**********************************************************************/
package org.jpox.store.rdbms.table;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.jpox.ClassLoaderResolver;
import org.jpox.PersistenceConfiguration;
import org.jpox.exceptions.JPOXDataStoreException;
import org.jpox.exceptions.JPOXException;
import org.jpox.metadata.AbstractMemberMetaData;
import org.jpox.store.mapped.DatastoreField;
import org.jpox.store.mapped.IdentifierFactory;
import org.jpox.store.mapped.mapping.JavaTypeMapping;
import org.jpox.store.rdbms.RDBMSManager;
import org.jpox.util.JPOXLogger;

/**
* Utility class used for detecting database schema existence and provides
* means of determining the schema name.
*
* @version $Revision: 1.33 $
**/
public class ProbeTable extends TableImpl
{
    /**
     * Constructor
     * @param storeMgr The RDBMSManager for this datastore
     **/
    public ProbeTable(RDBMSManager storeMgr)
    {
        super(storeMgr.getIdentifierFactory().newDatastoreContainerIdentifier("DELETEME" + System.currentTimeMillis()),storeMgr);
    }

    /**
     * Method to initialise the table.
     * @param clr The ClassLoaderResolver
     **/
    public void initialize(ClassLoaderResolver clr)
    {
        assertIsUninitialized();

    JavaTypeMapping mapping = dba.getMapping(int.class, storeMgr);
    DatastoreField column = addDatastoreField(int.class.getName(),
            storeMgr.getIdentifierFactory().newDatastoreFieldIdentifier("UNUSED"), mapping, null);
    getStoreManager().getMappingManager().createDatastoreMapping(mapping, storeMgr, column, int.class.getName());

        state = TABLE_STATE_INITIALIZED;
    }

    /**
     * Accessor for a mapping for the ID (PersistenceCapable) for this table.
     * @return The (PersistenceCapable) ID mapping.
     **/
    public JavaTypeMapping getIDMapping()
    {
        throw new JPOXException("Attempt to get ID mapping of ProbeTable!").setFatal();
    }

    /**
     * Accessor for the Schema details. This will return a String array with 2
     * elements. The first is the Catalog name, and the second the Schema name.
     * @param conn Connection for this datastore.
     * @return The Schema details
     * @throws SQLException Thrown when an error occurs in the process.
     **/
    public String[] findSchemaDetails(Connection conn)
    throws SQLException
    {
        String[] schemaDetails = new String[2];

        DatabaseMetaData dmd = conn.getMetaData();

        // Make sure the table name is in the correct case.
        // This is required by RDBMS such as PostgreSQL which allow creation in one format yet
        // actually store it in another.
        String table_name = identifier.getIdentifier();
        if (storeMgr.getIdentifierFactory().getIdentifierCase() == IdentifierFactory.IDENTIFIER_LOWER_CASE ||
            storeMgr.getIdentifierFactory().getIdentifierCase() == IdentifierFactory.IDENTIFIER_LOWER_CASE_QUOTED)
        {
            table_name = table_name.toLowerCase();
        }
        else if (storeMgr.getIdentifierFactory().getIdentifierCase() == IdentifierFactory.IDENTIFIER_UPPER_CASE ||
            storeMgr.getIdentifierFactory().getIdentifierCase() == IdentifierFactory.IDENTIFIER_UPPER_CASE_QUOTED)
        {
            table_name = table_name.toUpperCase();
        }

        // Utilise default catalog/schema if available and applicable
        PersistenceConfiguration conf = storeMgr.getOMFContext().getPersistenceConfiguration();
        String catalog_name = conf.getStringProperty("org.jpox.mapping.Catalog");
        String schema_name = conf.getStringProperty("org.jpox.mapping.Schema");
        if (!dba.supportsCatalogsInTableDefinitions())
        {
            catalog_name = null;
        }
        if (!dba.supportsSchemasInTableDefinitions())
        {
            schema_name = null;
        }

        // Find the schema details
        ResultSet rs = dmd.getTables(catalog_name,schema_name,table_name,null);
        try
        {
            if (!rs.next())
            {
                throw new JPOXDataStoreException(LOCALISER.msg("057027",identifier));
            }

            schemaDetails[0] = rs.getString(1);
            schemaDetails[1] = rs.getString(2);
        }
        finally
        {
            rs.close();
        }

        // Log any failures in this process due to the database adapter
        if (schemaDetails[0] == null)
        {
            JPOXLogger.DATASTORE_SCHEMA.info(LOCALISER.msg("057026"));
        }
        if (schemaDetails[1] == null)
        {
            JPOXLogger.DATASTORE_SCHEMA.info(LOCALISER.msg("057025"));
        }

        return schemaDetails;
    }

    /**
     * Override to always really create ProbeTables in the DB. Needed to
     * determine schema name.
     * @see org.jpox.store.rdbms.table.AbstractTable#allowDDLOutput()
     */
    protected boolean allowDDLOutput()
    {
        return false;
    }

    /**
     * Accessor the for the mapping for a field store in this table
     * @param fmd MetaData for the field whose mapping we want
     * @return The mapping
     */
    public JavaTypeMapping getFieldMapping(AbstractMemberMetaData fmd)
    {
        return null;
    }
}
TOP

Related Classes of org.jpox.store.rdbms.table.ProbeTable

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.