Package org.jpox.store.rdbms.table

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

/**********************************************************************
Copyright (c) 2002 Mike Martin (TJDO) 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:
2003 Andy Jefferson - added localiser
2003 Andy Jefferson - added output of logging when an exception is thrown
2004 Andy Jefferson - allowed updateable catalogName, schemaName
2004 Andy Jefferson - rewritten to remove levels of inheritance
2004 Andy Jefferson - added capability to add columns after initialisation.
2006 Andy Jefferson - removed catalog/schema flags and use tableIdentifier instead
    ...
**********************************************************************/
package org.jpox.store.rdbms.table;

import java.io.IOException;
import java.io.Writer;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

import org.jpox.ClassLoaderResolver;
import org.jpox.exceptions.JPOXUserException;
import org.jpox.metadata.AbstractClassMetaData;
import org.jpox.metadata.ColumnMetaData;
import org.jpox.metadata.DiscriminatorMetaData;
import org.jpox.metadata.MetaData;
import org.jpox.metadata.VersionMetaData;
import org.jpox.store.mapped.DatastoreField;
import org.jpox.store.mapped.DatastoreIdentifier;
import org.jpox.store.mapped.MappedStoreManager;
import org.jpox.store.mapped.mapping.JavaTypeMapping;
import org.jpox.store.rdbms.Column;
import org.jpox.store.rdbms.JDBCUtils;
import org.jpox.store.rdbms.RDBMSManager;
import org.jpox.store.rdbms.RDBMSStoreHelper;
import org.jpox.store.rdbms.SQLWarnings;
import org.jpox.store.rdbms.adapter.RDBMSAdapter;
import org.jpox.store.rdbms.exceptions.DuplicateColumnNameException;
import org.jpox.store.rdbms.exceptions.MissingTableException;
import org.jpox.store.rdbms.sqlidentifier.SQLIdentifier;
import org.jpox.util.JPOXLogger;
import org.jpox.util.Localiser;

/**
* Abstract implementation of a table in the datastore.
* The table exists in various states.
* After initialisation it can be created in the datastore by calling <B>create</B>.
* At any point after initialisation it can be modified, but only by addition of columns.
* The table can be dropped from the datastore by calling <B>drop</B>.
*
* @version $Revision: 1.63 $
**/
public abstract class AbstractTable implements Table
{
    /** Localiser for messages. */
    protected static final Localiser LOCALISER = Localiser.getInstance("org.jpox.store.rdbms.Localisation",
        RDBMSManager.class.getClassLoader());

    /** Manager for this table. */
    protected final RDBMSManager storeMgr;

    /** Database Adapter being used. */
    protected final RDBMSAdapter dba;

    /** Identifier name for the table. This will include the catalog/schema internally (if defined by the user). */
    protected final DatastoreIdentifier identifier;

    /** State of the table */
    protected int state = TABLE_STATE_NEW;

    /** Columns for this table. */
    protected List columns = new ArrayList();

    /** Index to the columns, keyed by name. */
    protected HashMap columnsByName = new HashMap();

    /** Writer to output any DDL. TODO Refactor this out and provide cleaner interface to DDL */
    protected static Writer ddlWriter = null;

    /** Whether we must produce complete DDL (when allowDDLOutput() && isOutputtingDdl()), or only for missing elements */
    protected static boolean completeDdl = false;

    /** Fully qualified name of this table. */
    private String fullyQualifiedName;

    //----------------------- convenience fields to improve performance ----------------------//
    /** compute hashCode in advance to improve performance **/
    private final int hashCode;
   
    /**
     * Constructor taking the table name and the RDBMSManager managing this
     * table.
     * @param identifier Name of the table
     * @param storeMgr The RDBMS Manager
     */
    public AbstractTable(DatastoreIdentifier identifier, RDBMSManager storeMgr)
    {
        this.storeMgr = storeMgr;
        this.dba = (RDBMSAdapter) storeMgr.getDatastoreAdapter();
        this.identifier = identifier;
        this.hashCode = identifier.hashCode() ^ storeMgr.hashCode();
    }

    /**
     * Accessor for whether the table is initialised.
     * @return Whether it is initialised
     */
    public boolean isInitialized()
    {
        // All of the states from initialised onwards imply that it has (at some time) been initialised
        return state >= TABLE_STATE_INITIALIZED;
    }

    /**
     * Accessor for whether the primary key of the table is initialised.
     * @return Whether the primary key of the table is initialised
     */
    public boolean isPKInitialized()
    {
        return state >= TABLE_STATE_PK_INITIALIZED;
    }

    /**
     * Accessor for whether the table is validated.
     * @return Whether it is validated.
     */
    public boolean isValidated()
    {
        return state == TABLE_STATE_VALIDATED;
    }
   
    /**
     * Accessor for whether the table has been modified since initialisation.
     * @return Whether it is modified since initialisation.
     */
    public boolean isInitializedModified()
    {
        return state == TABLE_STATE_INITIALIZED_MODIFIED;
    }

    /**
     * Accessor for the Store Manager.
     * @return Store Manager
     **/
    public MappedStoreManager getStoreManager()
    {
        return storeMgr;
    }

    /**
     * Accessor for the Catalog Name.
     * This will be part of the fully qualified name IF the user has specified
     * the catalog in the MetaData, OR if they have specified the catalog in the PMF.
     * @return Catalog Name
     **/
    public String getCatalogName()
    {
        return ((SQLIdentifier)identifier).getCatalogName();
    }

    /**
     * Accessor for the Schema Name.
     * This will be part of the fully qualified name IF the user has specified
     * the schema in the MetaData, OR if they have specified the schema in the PMF.
     * @return Schema Name
     **/
    public String getSchemaName()
    {
        return ((SQLIdentifier)identifier).getSchemaName();
    }

    /**
     * Accessor for the SQL identifier (the table name).
     * @return The name
     **/
    public DatastoreIdentifier getIdentifier()
    {
        return identifier;
    }

    /**
     * Creates a new column in the table.
     * Will add the new Column and return it. If the new column clashes in name with an existing column of the
     * required name will throw a DuplicateColumnNameException except when :-
     * <ul>
     * <li>The 2 columns are for same named fields in the class or its subclasses with the subclass(es) using
     * "superclass-table" inheritance strategy. One of the columns has to come from a subclass - cant have
     * both from the same class.</li>
     * </ul>
     * @param storedJavaType the java type of the datastore field
     * @param name the SQL identifier for the column to be added
     * @param mapping the mapping for the column to be added
     * @param colmd ColumnMetaData for the column to be added to the table
     * @return the new Column
     * @throws DuplicateColumnNameException if a column already exists with same name and not a supported situation.
     */
    public synchronized DatastoreField addDatastoreField(String storedJavaType, DatastoreIdentifier name,
            JavaTypeMapping mapping, MetaData colmd)
    {
        boolean duplicateName = false;
        if (hasColumnName(name))
        {
            duplicateName = true;
        }

        // Create the column
        Column col = new Column(this, storedJavaType, name, (ColumnMetaData) colmd);

        if (duplicateName && colmd != null)
        {
            // Verify if a duplicate column is valid. A duplicate column name is (currently) valid when :-
            // 1. subclasses defining the duplicated column are using "super class table" strategy
            //
            // Find the MetaData for the existing column
            Column existingCol = (Column) columnsByName.get(name);
            MetaData md = existingCol.getColumnMetaData().getParent();
            while (!(md instanceof AbstractClassMetaData))
            {
                if (md == null)
                {
                    // ColumnMetaData for existing column has no parent class somehow!
                    throw new JPOXUserException(LOCALISER.msg("057043",
                        name.getIdentifier(), getDatastoreIdentifierFullyQualified(), colmd.toString()));
                }
                md = md.getParent();
            }

            // Find the MetaData for the column to be added
            MetaData dupMd = colmd.getParent();
            while (!(dupMd instanceof AbstractClassMetaData))
            {
                dupMd = dupMd.getParent();
                if (dupMd == null)
                {
                    // ColumnMetaData for required column has no parent class somehow!
                    throw new JPOXUserException(LOCALISER.msg("057044",
                        name.getIdentifier(), getDatastoreIdentifierFullyQualified(), colmd.toString()));
                }
            }
            if (((AbstractClassMetaData)md).getFullClassName().equals(((AbstractClassMetaData)dupMd).getFullClassName()))
            {
                // compare the current column defining class and the duplicated column defining class. if the same class,
                // we raise an exception when within one class it is defined a column twice
                // in some cases it could still be possible to have these duplicated columns, but does not make too
                // much sense in most of the cases. (this whole block of duplicated column check, could be optional, like a pmf property)
                throw new DuplicateColumnNameException(this.toString(), existingCol, col);
            }

            // Make sure the field JavaTypeMappings are compatible
            if (mapping != null &&
                !mapping.getClass().isAssignableFrom(existingCol.getMapping().getClass()) &&
                !existingCol.getMapping().getClass().isAssignableFrom(mapping.getClass()))
            {
                // the mapping class must be the same (not really required, but to avoid user mistakes)
                throw new DuplicateColumnNameException(this.toString(), existingCol, col);
            }

            // Make sure the field java types are compatible
            Class fieldStoredJavaTypeClass = null;
            Class existingColStoredJavaTypeClass = null;
            try
            {
                ClassLoaderResolver clr = storeMgr.getOMFContext().getClassLoaderResolver(null);
                fieldStoredJavaTypeClass = clr.classForName(storedJavaType);
                existingColStoredJavaTypeClass = clr.classForName(col.getStoredJavaType());
            }
            catch (RuntimeException cnfe)
            {
                // Do nothing
            }
            if (fieldStoredJavaTypeClass != null && existingColStoredJavaTypeClass != null &&
                !fieldStoredJavaTypeClass.isAssignableFrom(existingColStoredJavaTypeClass) &&
                !existingColStoredJavaTypeClass.isAssignableFrom(fieldStoredJavaTypeClass))
            {
                // the stored java type must be the same (not really required, but to avoid user mistakes)
                throw new DuplicateColumnNameException(this.toString(), existingCol, col);
            }
        }

        if (!duplicateName)
        {
            // Only add to our internal list when it is not a dup (since it would try to create a table with the same col twice)
            addColumnInternal(col);
        }

        if (isInitialized())
        {
            // Set state to modified
            state = TABLE_STATE_INITIALIZED_MODIFIED;
        }

        return col;
    }

  /**
   * Checks if there is a DatastoreField for the identifier
   * @param identifier the identifier of the DatastoreField
   * @return true if the DatastoreField exists for the identifier
   */
  public boolean hasDatastoreField(DatastoreIdentifier identifier)
  {
        return (hasColumnName(identifier));
  }

    /**
     * Accessor for the DatastoreFields infered from the java and metadata files.
     * @return the DatastoreField[]
     */
    public DatastoreField[] getDatastoreFieldsMetaData()
    {
        return (DatastoreField[]) columns.toArray(new DatastoreField[columns.size()]);
    }
   
    /**
     * Method to create this table.
     * @param conn Connection to the datastore.
     * @return true if the table was created
     * @throws SQLException Thrown if an error occurs creating the table.
     **/
    public boolean create(Connection conn)
    throws SQLException
    {
        assertIsInitialized();

        if (JPOXLogger.DATASTORE_SCHEMA.isInfoEnabled())
        {
            JPOXLogger.DATASTORE_SCHEMA.info(LOCALISER.msg("057029", this));
        }

        List createStmts = getSQLCreateStatements(null);
       
        executeDdlStatementList(createStmts, conn);
        return !createStmts.isEmpty();
    }

    /**
     * Method to drop this table.
     * @param conn Connection to the datastore.
     * @throws SQLException Thrown if an error occurs dropping the table.
     **/
    public void drop(Connection conn)
    throws SQLException
    {
        assertIsInitialized();

        if (JPOXLogger.DATASTORE_SCHEMA.isInfoEnabled())
        {
            JPOXLogger.DATASTORE_SCHEMA.info(LOCALISER.msg("057030", this));
        }

        executeDdlStatementList(getSQLDropStatements(), conn);
    }

    /** Cache what we learned in a call to exists() */
    protected Boolean existsInDatastore = null;
   
    /**
     * Method to check the existence of the table/view, optionally auto creating it
     * where required. If it doesn't exist and auto creation isn't specified this
     * throws a MissingTableException.
     * @param conn The JDBC Connection
     * @param auto_create Whether to auto create the table if not existing
     * @return Whether the table was added
     * @throws SQLException Thrown when an error occurs in the JDBC calls
     */
    public boolean exists(Connection conn, boolean auto_create)
    throws SQLException
    {
        assertIsInitialized();

        int tableType = RDBMSStoreHelper.getTableType(storeMgr, this, conn);
        if (tableType == TABLE_TYPE_MISSING || (allowDDLOutput() && isOutputtingDDL() && completeDdl))
        {
            // Table is missing, or we're running SchemaTool with a DDL file
            if (!auto_create)
            {
                throw new MissingTableException(getCatalogName(),getSchemaName(),this.toString());
            }

            boolean created = create(conn);

            if (!isOutputtingDDL() || (tableType != TABLE_TYPE_MISSING))
            {
                // table either already existed or we really just created it
                existsInDatastore = Boolean.TRUE;
            } // else: we would have thrown MissingTableException above

            state = TABLE_STATE_VALIDATED;
            return created;
        }
        else
        {
            // table already existed
            existsInDatastore = Boolean.TRUE;
        }

        return false;
    }

    /**
     * Equality operator.
     * @param obj The object to compare against
     * @return Whether the objects are equal
     **/
    public final boolean equals(Object obj)
    {
        if (obj == this)
        {
            return true;
        }

        if (!(obj instanceof AbstractTable))
        {
            return false;
        }

        AbstractTable t = (AbstractTable)obj;

        return getClass().equals(t.getClass()) && identifier.equals(t.identifier) && storeMgr.equals(t.storeMgr);
    }

    /**
     * Accessor for the hascode of this table.
     * @return The hash code.
     **/
    public final int hashCode()
    {
        return hashCode;
    }

    /**
     * Method to return a string version of this table. This name is the
     * fully-qualified name of the table,including catalog/schema names, where
     * these are appropriate. They are included where the user has either specified
     * the catalog/schema for the PMF, or in the MetaData. They are also only included
     * where the datastore adapter supports their use.
     * @return String name of the table (catalog.schema.table)
     */
    public final String toString()
    {
        if (fullyQualifiedName != null)
        {
            return fullyQualifiedName;
        }

        fullyQualifiedName = identifier.getFullyQualifiedName(false);

        return fullyQualifiedName;
    }

    /**
     * Method that operates like toString except it returns a fully-qualified name that will always
     * be fully-qualified even when the user hasnt specified the catalog/schema in PMF or MetaData.
     * That is, it will add on any auto-calculated catalog/schema for the datastore.
     * Note that this will never include any quoting strings required for insert/select etc.
     * @return The fully qualified name
     */
    public DatastoreIdentifier getDatastoreIdentifierFullyQualified()
    {
        String catalog = ((SQLIdentifier)identifier).getCatalogName();
        if (catalog != null)
        {
            // Remove any identifier quotes
            catalog = JDBCUtils.getIdentifierNameStripped(catalog, dba);
        }

        String schema = ((SQLIdentifier)identifier).getSchemaName();
        if (schema != null)
        {
            // Remove any identifier quotes
            schema = JDBCUtils.getIdentifierNameStripped(schema, dba);
        }

        String table = identifier.getIdentifier();
        table = JDBCUtils.getIdentifierNameStripped(table, dba);

        SQLIdentifier di = (SQLIdentifier)storeMgr.getIdentifierFactory().newDatastoreContainerIdentifier(table);
        di.setCatalogName(catalog);
        di.setSchemaName(schema);
        return di;
    }

    // -------------------------------- Internal Implementation ---------------------------
   
    /**
     * Utility method to add a column to the internal representation
     * @param col The column
     */
    protected synchronized void addColumnInternal(Column col)
    {
        DatastoreIdentifier colName = col.getIdentifier();

        columns.add(col);
        columnsByName.put(colName, col);

        if (JPOXLogger.DATASTORE_SCHEMA.isDebugEnabled())
        {
            JPOXLogger.DATASTORE_SCHEMA.debug(LOCALISER.msg("057034", col));
        }
    }
   
    /**
     * Utility to return if a column of this name exists.
     * @param colName The column name
     * @return Whether the column of this name exists
     */
    protected boolean hasColumnName(DatastoreIdentifier colName)
    {
        return columnsByName.get(colName) != null;
    }

    /**
     * Accessor for the SQL create statements.
     * @param props Properties controlling the table creation
     * @return The SQL Create statements
     **/
    protected abstract List getSQLCreateStatements(Properties props);

    /**
     * Accessor for the SQL drop statements.
     * @return The SQL Drop statements
     **/
    protected abstract List getSQLDropStatements();

    protected void assertIsPKUninitialized()
    {
        if (isPKInitialized())
        {
            throw new IllegalStateException(LOCALISER.msg("057000",this));
        }
    }

    protected void assertIsUninitialized()
    {
        if (isInitialized())
        {
            throw new IllegalStateException(LOCALISER.msg("057000",this));
        }
    }

    protected void assertIsInitialized()
    {
        if (!isInitialized())
        {
            throw new IllegalStateException(LOCALISER.msg("057001",this));
        }
    }

    protected void assertIsInitializedModified()
    {
        if (!isInitializedModified())
        {
            throw new IllegalStateException(LOCALISER.msg("RDBMS.Table.UnmodifiedError",this));
        }
    }
   
    protected void assertIsPKInitialized()
    {
        if (!isPKInitialized())
        {
            throw new IllegalStateException(LOCALISER.msg("057001",this));
        }
    }

    protected void assertIsValidated()
    {
        if (!isValidated())
        {
            throw new IllegalStateException(LOCALISER.msg("057002",this));
        }
    }
   
    /**
     * Determine whether we or our concrete class allow DDL to be written into a file instead of
     * sending it to the DB. Defaults to true.
     * @return Whether it allows DDL outputting
     */
    protected boolean allowDDLOutput()
    {
        return true;
    }

    /**
     * Whether a writer is defined to output DDL
     * @return true if a writer is set
     */
    public static boolean isOutputtingDDL()
    {
        return ddlWriter != null;
    }

    /**
     * Set the file where any subsequent DDL SQL commands will be written to
     * instead of issuing them to the database. The default null means normal
     * operation, i.e. DDL SQL commands will be sent to the DB.
     * @param writer The writer to use for outputting DDL
     */
    public static void setDdlWriter(Writer writer)
    {
        ddlWriter = writer;
    }

    /**
     * Method to perform the required SQL statements.
     * @param stmts A List of statements
     * @param conn The Connection to the datastore
     * @throws SQLException Any exceptions thrown by the statements
     **/
    protected void executeDdlStatementList(List stmts, Connection conn)
    throws SQLException
    {
        Statement stmt = conn.createStatement();

        String stmtText=null;
        try
        {
            Iterator i = stmts.iterator();
            while (i.hasNext())
            {
                stmtText = (String)i.next();
                executeDdlStatement(stmt, stmtText);
            }
        }
        catch (SQLException sqe)
        {
            JPOXLogger.DATASTORE.error(LOCALISER.msg("057028",stmtText,sqe));
            throw sqe;
        }
        finally
        {
            stmt.close();
        }
    }

    /**
     * Execute a single DDL SQL statement with appropriate logging.
     * If ddlWriter is set, do not actually execute the SQL but write it to that Writer.
     * @param stmt The JDBC Statement object to execute on
     * @param stmtText The actual SQL statement text
     * @throws SQLException Thrown if an error occurs
     * @see #setDdlWriter(Writer)
     */
    protected void executeDdlStatement(Statement stmt, String stmtText)
    throws SQLException
    {
        if (ddlWriter != null && allowDDLOutput())
        {
            try
            {
                // we shall write the SQL to a file instead of issuing to DB
                ddlWriter.write(stmtText + ";\n\n");
            }
            catch (IOException e)
            {
                JPOXLogger.DATASTORE_SCHEMA.error("error writing DDL into file", e);
            }
        }
        else
        {
            if (JPOXLogger.DATASTORE_SCHEMA.isDebugEnabled())
            {
                JPOXLogger.DATASTORE_SCHEMA.debug(stmtText);
            }
            long startTime = System.currentTimeMillis();
            stmt.execute(stmtText);
            if (JPOXLogger.DATASTORE_SCHEMA.isDebugEnabled())
            {
                JPOXLogger.DATASTORE_SCHEMA.debug(LOCALISER.msg("045000",(System.currentTimeMillis() - startTime)));
            }
        }

        SQLWarnings.log(stmt);
    }

    /**
     * Accessor for Discriminator MetaData
     * @return Returns the Discriminator MetaData.
     */
    public DiscriminatorMetaData getDiscriminatorMetaData()
    {
        return null;
    }

    /**
     * Accessor for the discriminator mapping specified .
     * @return The mapping for the discriminator datastore field
     **/
    public JavaTypeMapping getDiscriminatorMapping(boolean allowSuperclasses)
    {
        return null;
    }

    /**
     * Accessor for Version MetaData
     * @return Returns the Version MetaData.
     */
    public VersionMetaData getVersionMetaData()
    {
        return null;
   

    /**
     * Accessor for the version mapping specified.
     * @return The mapping for the version datastore field
     **/
    public JavaTypeMapping getVersionMapping(boolean allowSuperclasses)
    {
        return null;
    }

    /**
     * Determine whether our table exists in the datastore (without modifying datastore).
     * Result is cached
     * @param conn The Connection
     * @return Whether the table exists in the datastore
     * @throws SQLException Thrown if an error occurs
     */
    protected boolean tableExistsInDatastore(Connection conn) throws SQLException
    {
        if (existsInDatastore == null)
        {
            // exists() has not been called yet
            try
            {
                exists(conn, false);
            }
            catch (MissingTableException mte)
            {
            }
            // existsInDatastore will be set now
        }
        return existsInDatastore.booleanValue();
    }

    public static boolean isCompleteDdl()
    {
        return completeDdl;
    }
   

    /**
     * Whether DDL should be generated regardless of whether table is missing or not
     * @param completeDdl
     */
    public static void setCompleteDdl(boolean completeDdl)
    {
        AbstractTable.completeDdl = completeDdl;
    }
}
TOP

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

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.