Search in sources :

Example 1 with UnexpectedColumnException

use of org.datanucleus.store.rdbms.exceptions.UnexpectedColumnException in project datanucleus-rdbms by datanucleus.

the class ViewImpl method validate.

/**
 * 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 and validity
    RDBMSSchemaHandler handler = (RDBMSSchemaHandler) storeMgr.getSchemaHandler();
    String tableType = handler.getTableType(conn, this);
    if (tableType == null) {
        throw new MissingTableException(getCatalogName(), getSchemaName(), this.toString());
    } else if (// TODO Allow "MATERIALIZED VIEW" that some RDBMS support (e.g PostgreSQL)
    !tableType.equals("VIEW")) {
        throw new NotAViewException(this.toString(), tableType);
    }
    long startTime = System.currentTimeMillis();
    if (NucleusLogger.DATASTORE.isDebugEnabled()) {
        NucleusLogger.DATASTORE.debug(Localiser.msg("031004", this));
    }
    // Validate the column(s)
    Map<DatastoreIdentifier, Column> unvalidated = new HashMap(columnsByIdentifier);
    Iterator i = storeMgr.getColumnInfoForTable(this, conn).iterator();
    while (i.hasNext()) {
        RDBMSColumnInfo ci = (RDBMSColumnInfo) i.next();
        DatastoreIdentifier colIdentifier = storeMgr.getIdentifierFactory().newIdentifier(IdentifierType.COLUMN, ci.getColumnName());
        Column col = unvalidated.get(colIdentifier);
        if (col == null) {
            if (!hasColumnName(colIdentifier)) {
                throw new UnexpectedColumnException(this.toString(), ci.getColumnName(), 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(colIdentifier);
            } else {
                unvalidated.remove(colIdentifier);
            }
        }
    }
    if (unvalidated.size() > 0) {
        throw new MissingColumnException(this, unvalidated.values());
    }
    state = TABLE_STATE_VALIDATED;
    if (NucleusLogger.DATASTORE.isDebugEnabled()) {
        NucleusLogger.DATASTORE.debug(Localiser.msg("045000", (System.currentTimeMillis() - startTime)));
    }
    return false;
}
Also used : RDBMSColumnInfo(org.datanucleus.store.rdbms.schema.RDBMSColumnInfo) NotAViewException(org.datanucleus.store.rdbms.exceptions.NotAViewException) HashMap(java.util.HashMap) MissingColumnException(org.datanucleus.store.rdbms.exceptions.MissingColumnException) RDBMSSchemaHandler(org.datanucleus.store.rdbms.schema.RDBMSSchemaHandler) DatastoreIdentifier(org.datanucleus.store.rdbms.identifier.DatastoreIdentifier) Iterator(java.util.Iterator) MissingTableException(org.datanucleus.store.rdbms.exceptions.MissingTableException) UnexpectedColumnException(org.datanucleus.store.rdbms.exceptions.UnexpectedColumnException)

Example 2 with UnexpectedColumnException

use of org.datanucleus.store.rdbms.exceptions.UnexpectedColumnException in project datanucleus-rdbms by datanucleus.

the class TableImpl method getExistingPrimaryKeys.

/**
 * Accessor for the primary keys for this table in the datastore.
 * @param conn The JDBC Connection
 * @return Map of primary keys
 * @throws SQLException Thrown when an error occurs in the JDBC call.
 */
private Map<DatastoreIdentifier, PrimaryKey> getExistingPrimaryKeys(Connection conn) throws SQLException {
    Map<DatastoreIdentifier, PrimaryKey> primaryKeysByName = new HashMap<>();
    if (tableExistsInDatastore(conn)) {
        StoreSchemaHandler handler = storeMgr.getSchemaHandler();
        RDBMSTablePKInfo tablePkInfo = (RDBMSTablePKInfo) handler.getSchemaData(conn, "primary-keys", new Object[] { this });
        IdentifierFactory idFactory = storeMgr.getIdentifierFactory();
        Iterator pkColsIter = tablePkInfo.getChildren().iterator();
        while (pkColsIter.hasNext()) {
            PrimaryKeyInfo pkInfo = (PrimaryKeyInfo) pkColsIter.next();
            String pkName = (String) pkInfo.getProperty("pk_name");
            DatastoreIdentifier pkIdentifier;
            if (pkName == null) {
                pkIdentifier = idFactory.newPrimaryKeyIdentifier(this);
            } else {
                pkIdentifier = idFactory.newIdentifier(IdentifierType.COLUMN, pkName);
            }
            PrimaryKey pk = primaryKeysByName.get(pkIdentifier);
            if (pk == null) {
                pk = new PrimaryKey(this);
                pk.setName(pkIdentifier.getName());
                primaryKeysByName.put(pkIdentifier, pk);
            }
            int keySeq = (((Short) pkInfo.getProperty("key_seq")).shortValue()) - 1;
            String colName = (String) pkInfo.getProperty("column_name");
            DatastoreIdentifier colIdentifier = idFactory.newIdentifier(IdentifierType.COLUMN, colName);
            Column col = columnsByIdentifier.get(colIdentifier);
            if (col == null) {
                throw new UnexpectedColumnException(this.toString(), colIdentifier.getName(), this.getSchemaName(), this.getCatalogName());
            }
            pk.setColumn(keySeq, col);
        }
    }
    return primaryKeysByName;
}
Also used : HashMap(java.util.HashMap) PrimaryKey(org.datanucleus.store.rdbms.key.PrimaryKey) StoreSchemaHandler(org.datanucleus.store.schema.StoreSchemaHandler) IdentifierFactory(org.datanucleus.store.rdbms.identifier.IdentifierFactory) PrimaryKeyInfo(org.datanucleus.store.rdbms.schema.PrimaryKeyInfo) DatastoreIdentifier(org.datanucleus.store.rdbms.identifier.DatastoreIdentifier) Iterator(java.util.Iterator) RDBMSTablePKInfo(org.datanucleus.store.rdbms.schema.RDBMSTablePKInfo) UnexpectedColumnException(org.datanucleus.store.rdbms.exceptions.UnexpectedColumnException)

Aggregations

HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2 UnexpectedColumnException (org.datanucleus.store.rdbms.exceptions.UnexpectedColumnException)2 DatastoreIdentifier (org.datanucleus.store.rdbms.identifier.DatastoreIdentifier)2 MissingColumnException (org.datanucleus.store.rdbms.exceptions.MissingColumnException)1 MissingTableException (org.datanucleus.store.rdbms.exceptions.MissingTableException)1 NotAViewException (org.datanucleus.store.rdbms.exceptions.NotAViewException)1 IdentifierFactory (org.datanucleus.store.rdbms.identifier.IdentifierFactory)1 PrimaryKey (org.datanucleus.store.rdbms.key.PrimaryKey)1 PrimaryKeyInfo (org.datanucleus.store.rdbms.schema.PrimaryKeyInfo)1 RDBMSColumnInfo (org.datanucleus.store.rdbms.schema.RDBMSColumnInfo)1 RDBMSSchemaHandler (org.datanucleus.store.rdbms.schema.RDBMSSchemaHandler)1 RDBMSTablePKInfo (org.datanucleus.store.rdbms.schema.RDBMSTablePKInfo)1 StoreSchemaHandler (org.datanucleus.store.schema.StoreSchemaHandler)1