Search in sources :

Example 1 with WrongPrimaryKeyException

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

the class TableImpl method validate.

/**
 * Method to validate the table in the datastore.
 * @param conn The JDBC Connection
 * @param validateColumnStructure Whether to validate the column structure, or just the column existence
 * @param autoCreate Whether to update the table to fix any validation errors. Only applies to missing columns.
 * @param autoCreateErrors Exceptions found in 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 (!tableType.equals("TABLE")) {
        throw new NotATableException(this.toString(), tableType);
    }
    long startTime = System.currentTimeMillis();
    if (NucleusLogger.DATASTORE_SCHEMA.isDebugEnabled()) {
        NucleusLogger.DATASTORE_SCHEMA.debug(Localiser.msg("057032", this));
    }
    // Validate the column(s)
    validateColumns(conn, validateColumnStructure, autoCreate, autoCreateErrors);
    // Validate the primary key(s)
    try {
        validatePrimaryKey(conn);
    } catch (WrongPrimaryKeyException wpke) {
        if (autoCreateErrors != null) {
            autoCreateErrors.add(wpke);
        } else {
            throw wpke;
        }
    }
    state = TABLE_STATE_VALIDATED;
    if (NucleusLogger.DATASTORE_SCHEMA.isDebugEnabled()) {
        NucleusLogger.DATASTORE_SCHEMA.debug(Localiser.msg("045000", (System.currentTimeMillis() - startTime)));
    }
    return false;
}
Also used : NotATableException(org.datanucleus.store.rdbms.exceptions.NotATableException) WrongPrimaryKeyException(org.datanucleus.store.rdbms.exceptions.WrongPrimaryKeyException) RDBMSSchemaHandler(org.datanucleus.store.rdbms.schema.RDBMSSchemaHandler) MissingTableException(org.datanucleus.store.rdbms.exceptions.MissingTableException)

Example 2 with WrongPrimaryKeyException

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

the class TableImpl method validatePrimaryKey.

/**
 * Utility method to validate the primary key of the table.
 * Will throw a WrongPrimaryKeyException if the PK is incorrect.
 * TODO Add an auto_create parameter on this
 * @param conn Connection to use
 * @return Whether it validates
 * @throws SQLException When an error occurs in the valdiation
 */
protected boolean validatePrimaryKey(Connection conn) throws SQLException {
    Map actualPKs = getExistingPrimaryKeys(conn);
    PrimaryKey expectedPK = getPrimaryKey();
    if (expectedPK.size() == 0) {
        if (!actualPKs.isEmpty()) {
            throw new WrongPrimaryKeyException(this.toString(), expectedPK.toString(), StringUtils.collectionToString(actualPKs.values()));
        }
    } else {
        if (actualPKs.size() != 1 || !actualPKs.values().contains(expectedPK)) {
            throw new WrongPrimaryKeyException(this.toString(), expectedPK.toString(), StringUtils.collectionToString(actualPKs.values()));
        }
    }
    return true;
}
Also used : WrongPrimaryKeyException(org.datanucleus.store.rdbms.exceptions.WrongPrimaryKeyException) PrimaryKey(org.datanucleus.store.rdbms.key.PrimaryKey) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

WrongPrimaryKeyException (org.datanucleus.store.rdbms.exceptions.WrongPrimaryKeyException)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 MissingTableException (org.datanucleus.store.rdbms.exceptions.MissingTableException)1 NotATableException (org.datanucleus.store.rdbms.exceptions.NotATableException)1 PrimaryKey (org.datanucleus.store.rdbms.key.PrimaryKey)1 RDBMSSchemaHandler (org.datanucleus.store.rdbms.schema.RDBMSSchemaHandler)1