Package org.jpox.store.rdbms.schema

Source Code of org.jpox.store.rdbms.schema.RDBMSSchemaHandler

/**********************************************************************
Copyright (c) 2008 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:
    ...
**********************************************************************/
package org.jpox.store.rdbms.schema;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.jpox.ManagedConnection;
import org.jpox.exceptions.JPOXDataStoreException;
import org.jpox.exceptions.JPOXUserException;
import org.jpox.store.StoreSchemaData;
import org.jpox.store.StoreSchemaHandler;
import org.jpox.store.rdbms.RDBMSManager;
import org.jpox.store.rdbms.adapter.RDBMSAdapter;

/**
* Handler for RDBMS schema information.
* Provides two types of schema data
* <ul>
* <li><b>tables</b> : hierarchy of schema-tables-columns.</li>
* <li><b>types</b> : type information for the datastore columns</li>
* </ul>
*/
public class RDBMSSchemaHandler implements StoreSchemaHandler
{
    protected final RDBMSManager storeMgr;

    /** Map of schema data, keyed by its symbolic name. Can be "types", "tables" etc. */
    protected Map schemaDataByName = new HashMap();

    public RDBMSSchemaHandler(RDBMSManager storeMgr)
    {
        this.storeMgr = storeMgr;
    }

    /**
     * Method to initialise the schema info for this datastore.
     * @param mconn Datastore connection
     */
    public void initialise(ManagedConnection mconn)
    {
        // TODO Populate the schema information
        RDBMSSchemaInfo schema = new RDBMSSchemaInfo();

        // TODO Do we want all tables for this schema or just those we are mapping

        // Retrieve table info
        schema.addProperty("catalog", storeMgr.getCatalogName());
        schema.addProperty("schema", storeMgr.getSchemaName());
        try
        {
            // Get tables for this catalog, and any schema
            Connection conn = (Connection)mconn.getConnection();
            ResultSet rs = ((RDBMSAdapter)storeMgr.getDatastoreAdapter()).getTables(conn,
                storeMgr.getCatalogName(), null);
            try
            {
                while (rs.next())
                {
                    RDBMSTableInfo table = new RDBMSTableInfo();
                    table.addProperty("table_cat", rs.getString(1));
                    table.addProperty("table_schem", rs.getString(2));
                    table.addProperty("table_name", rs.getString(3));
                    table.addProperty("table_type", rs.getString(4));
                    table.addProperty("remarks", rs.getString(5));
                    if (rs.getMetaData().getColumnCount() > 5)
                    {
                        table.addProperty("type_cat", rs.getString(6));
                        table.addProperty("type_schem", rs.getString(7));
                        table.addProperty("type_name", rs.getString(8));
                        table.addProperty("self_referencing_col_name", rs.getString(9));
                        table.addProperty("ref_generation", rs.getString(10));
                    }

                    schema.addChild(table);
                }
            }
            finally
            {
                rs.close();
            }
        }
        catch (SQLException sqle)
        {
            throw new JPOXDataStoreException("Exception thrown obtaining schema table information from datastore", sqle);
        }

        // Retrieve column info
        try
        {
            // Get columns for this catalog, schema
            Connection conn = (Connection)mconn.getConnection();
            ResultSet rs = ((RDBMSAdapter)storeMgr.getDatastoreAdapter()).getColumns(conn,
                storeMgr.getCatalogName(), storeMgr.getSchemaName(), null);
            while (rs.next())
            {
                // Construct a fully-qualified name for the table in this row of the ResultSet
                String tableIdentifierCatalogName = rs.getString(1);
                String tableIdentifierSchemaName = rs.getString(2);
                String tableIdentifierTableName = rs.getString(3);

                if (rs.wasNull() ||
                    (tableIdentifierCatalogName != null && tableIdentifierCatalogName.length() < 1))
                {
                    tableIdentifierCatalogName = null;
                }
                if (rs.wasNull() ||
                    (tableIdentifierSchemaName != null && tableIdentifierSchemaName.length() < 1))
                {
                    tableIdentifierSchemaName = null;
                }
                if (rs.wasNull() ||
                    (tableIdentifierTableName != null && tableIdentifierTableName.length() < 1))
                {
                    tableIdentifierTableName = null;
                }
                //at least the JDBC driver should return the table name
                if (tableIdentifierTableName == null)
                {
                    throw new JPOXDataStoreException("Invalid 'null' table name identifier returned by database. Check with your JDBC driver vendor (ref:DatabaseMetaData.getColumns).");
                }

                // TODO This should create RDBMSColumnInfo
                ((RDBMSAdapter)storeMgr.getDatastoreAdapter()).newColumnInfo(rs);
            }
        }
        catch (SQLException sqle)
        {
            throw new JPOXDataStoreException("Exception thrown obtaining schema column information from datastore", sqle);
        }

        schemaDataByName.put("tables", schema);
    }

    /**
     * Method to create the schema with the supplied name.
     * @param conn Connection
     * @param schemaName Name of the schema
     */
    public void createSchema(ManagedConnection conn, String schemaName)
    {
        throw new JPOXUserException("JPOX doesnt currently support creation of schemas for RDBMS");
    }

    /**
     * Method to delete the schema with the supplied name.
     * @param conn Connection
     * @param schemaName Name of the schema
     */
    public void deleteSchema(ManagedConnection conn, String schemaName)
    {
        throw new JPOXUserException("JPOX doesnt currently support deletion of schemas for RDBMS");
    }

    /**
     * Accessor for the schema data for the specified name.
     * @return Schema data
     */
    public StoreSchemaData getSchemaData(String name)
    {
        return (StoreSchemaData)schemaDataByName.get(name);
    }
}
TOP

Related Classes of org.jpox.store.rdbms.schema.RDBMSSchemaHandler

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.