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;
}
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;
}
Aggregations