Package org.jpox.store.rdbms.table

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

/**********************************************************************
Copyright (c) 2002 Mike Martin 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 Andy Jefferson - rewritten to remove many levels of inheritance
    ...
**********************************************************************/
package org.jpox.store.rdbms.table;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.jpox.ClassLoaderResolver;
import org.jpox.store.mapped.DatastoreIdentifier;
import org.jpox.store.mapped.IdentifierFactory;
import org.jpox.store.rdbms.Column;
import org.jpox.store.rdbms.RDBMSManager;
import org.jpox.store.rdbms.RDBMSStoreHelper;
import org.jpox.store.rdbms.columninfo.ColumnInfo;
import org.jpox.store.rdbms.exceptions.MissingColumnException;
import org.jpox.store.rdbms.exceptions.MissingTableException;
import org.jpox.store.rdbms.exceptions.NotAViewException;
import org.jpox.store.rdbms.exceptions.PrimaryKeyColumnNotAllowedException;
import org.jpox.store.rdbms.exceptions.UnexpectedColumnException;
import org.jpox.util.JPOXLogger;

/**
* Representation of a View in a datastore (RDBMS).
*
* @version $Revision: 1.23 $
**/
public abstract class ViewImpl extends AbstractTable
{
    /**
     * Constructor, taking the table identifier.
     * @param name The identifier for the table.
     * @param storeMgr The Store Manager
     **/
    public ViewImpl(DatastoreIdentifier name, RDBMSManager storeMgr)
    {
        super(name, storeMgr);
    }

    /**
     * Pre initilize. For things that must be initialized right after constructor
     * @param clr the ClassLoaderResolver
     */
    public void preInitialize(final ClassLoaderResolver clr)
    {
        assertIsUninitialized();
        //nothing to do here
    }   
   
   
    /**
     * Post initilize. For things that must be set after all classes have been initialized before
     * @param clr the ClassLoaderResolver
     */
    public void postInitialize(final ClassLoaderResolver clr)
    {
        assertIsInitialized();
        //nothing to do here
    }
   
    /**
     * Method to validate the view in the datastore. Validates the existence of
     * the table, and then the specifications of the Columns.
     * @param conn The JDBC Connection
     * @param validateColumnStructure Whether to validate down to column structure, or just their existence
     * @param autoCreate Whether to update the view to fix errors (not used).
     * @param autoCreateErrors Errors found during the auto-create process
     * @return Whether the database was modified
     * @throws SQLException Thrown when an error occurs in the JDBC calls
     */
    public boolean validate(Connection conn, boolean validateColumnStructure, boolean autoCreate, Collection autoCreateErrors)
    throws SQLException
    {
        assertIsInitialized();

        // Check existence
        int tableType = RDBMSStoreHelper.getTableType(storeMgr, this, conn);
        if (tableType == TABLE_TYPE_MISSING)
        {
            throw new MissingTableException(getCatalogName(),getSchemaName(),this.toString());
        }

        long startTime = System.currentTimeMillis();
        if (JPOXLogger.DATASTORE.isDebugEnabled())
        {
            JPOXLogger.DATASTORE.debug(LOCALISER.msg("031004",this));
        }

        // Check type
        if (tableType != TABLE_TYPE_VIEW)
        {
            throw new NotAViewException(this.toString(),tableType);
        }

        // Validate the column(s)
        HashMap unvalidated = new HashMap(columnsByName);
        Iterator i = storeMgr.getColumnInfoForTable(this, conn).iterator();
        while (i.hasNext())
        {
            ColumnInfo ci = (ColumnInfo)i.next();
            DatastoreIdentifier colName = storeMgr.getIdentifierFactory().newIdentifier(IdentifierFactory.COLUMN, ci.getColumnName());
            Column col = (Column)unvalidated.get(colName);
            if (col == null)
            {
                if (!hasColumnName(colName))
                {
                    throw new UnexpectedColumnException(this.toString(),colName.getIdentifier(), this.getSchemaName(), this.getCatalogName());
                }
                /*
                 * Otherwise it's a duplicate column name in the
                 * metadata and we ignore it.  Cloudscape is known to
                 * do this, although I think that's probably a bug.
                 */
            }
            else
            {
                if (validateColumnStructure)
                {
                    col.validate(ci);
                    unvalidated.remove(colName);
                }
                else
                {
                    unvalidated.remove(colName);
                }
            }
        }
        if (unvalidated.size() > 0)
        {
            throw new MissingColumnException(this, unvalidated.values());
        }

        state = TABLE_STATE_VALIDATED;
        if (JPOXLogger.DATASTORE.isDebugEnabled())
        {
            JPOXLogger.DATASTORE.debug(LOCALISER.msg("045000", (System.currentTimeMillis() - startTime)));
        }

        return false;
    }

    /**
     * Internal method to generate the SQL statements for dropping the view.
     * @return The List of SQL statements.
     **/
    protected List getSQLDropStatements()
    {
        assertIsInitialized();

        ArrayList stmts = new ArrayList();
        stmts.add(dba.getDropViewStatement(this));

        return stmts;
    }

    /**
     * Method to add a Column to the View.
     * @param col The column
     **/
    protected synchronized void addColumnInternal(Column col)
    {
        if (col.isPrimaryKey())
        {
            throw new PrimaryKeyColumnNotAllowedException(this.toString(), col.getIdentifier().toString());
        }

        super.addColumnInternal(col);
    }
}
TOP

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

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.