use of org.datanucleus.store.rdbms.schema.RDBMSSchemaHandler 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;
}
use of org.datanucleus.store.rdbms.schema.RDBMSSchemaHandler 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.schema.RDBMSSchemaHandler in project datanucleus-rdbms by datanucleus.
the class DeleteTablesSchemaTransaction method run.
/* (non-Javadoc)
* @see org.datanucleus.store.rdbms.AbstractSchemaTransaction#run(org.datanucleus.ClassLoaderResolver)
*/
protected void run(ClassLoaderResolver clr) throws SQLException {
synchronized (rdbmsMgr) {
boolean success = true;
try {
NucleusLogger.DATASTORE_SCHEMA.debug(Localiser.msg("050045", rdbmsMgr.getCatalogName(), rdbmsMgr.getSchemaName()));
// Build up map of tables and views TODO Why use maps?
Map baseTablesByName = new HashMap();
Map viewsByName = new HashMap();
for (Iterator i = storeDataMgr.getManagedStoreData().iterator(); i.hasNext(); ) {
RDBMSStoreData data = (RDBMSStoreData) i.next();
if (NucleusLogger.DATASTORE_SCHEMA.isDebugEnabled()) {
NucleusLogger.DATASTORE_SCHEMA.debug(Localiser.msg("050046", data.getName()));
}
// If the class has a table/view to remove, add it to the list
if (data.hasTable()) {
if (data.mapsToView()) {
viewsByName.put(data.getDatastoreIdentifier(), data.getTable());
} else {
baseTablesByName.put(data.getDatastoreIdentifier(), data.getTable());
}
}
}
// Remove views
Iterator viewsIter = viewsByName.values().iterator();
while (viewsIter.hasNext()) {
ViewImpl view = (ViewImpl) viewsIter.next();
if (writer != null) {
try {
if (view instanceof ClassView) {
writer.write("-- ClassView " + view.toString() + " for classes " + StringUtils.objectArrayToString(((ClassView) view).getManagedClasses()) + "\n");
}
} catch (IOException ioe) {
NucleusLogger.DATASTORE_SCHEMA.error("error writing DDL into file", ioe);
}
((ViewImpl) viewsIter.next()).drop(getCurrentConnection());
} else {
// Drop view if exists in the datastore
StoreSchemaData info = rdbmsMgr.getSchemaHandler().getSchemaData(getCurrentConnection(), RDBMSSchemaHandler.TYPE_COLUMNS, new Object[] { view });
if (info != null) {
((ViewImpl) viewsIter.next()).drop(getCurrentConnection());
}
}
}
// Remove table constraints
Map<TableImpl, Boolean> schemaExistsForTableMap = new HashMap();
Iterator tablesIter = baseTablesByName.values().iterator();
while (tablesIter.hasNext()) {
TableImpl tbl = (TableImpl) tablesIter.next();
if (writer != null) {
try {
if (tbl instanceof ClassTable) {
writer.write("-- Constraints for ClassTable " + tbl.toString() + " for classes " + StringUtils.objectArrayToString(((ClassTable) tbl).getManagedClasses()) + "\n");
} else if (tbl instanceof JoinTable) {
writer.write("-- Constraints for JoinTable " + tbl.toString() + " for join relationship\n");
}
} catch (IOException ioe) {
NucleusLogger.DATASTORE_SCHEMA.error("error writing DDL into file", ioe);
}
tbl.dropConstraints(getCurrentConnection());
} else {
// Drop constraints if exists in the datastore
boolean exists = false;
try {
// Check table type as way of detecting existence
String tableType = ((RDBMSSchemaHandler) rdbmsMgr.getSchemaHandler()).getTableType(getCurrentConnection(), tbl);
if (tableType != null) {
exists = true;
}
} catch (Exception e) {
exists = false;
}
schemaExistsForTableMap.put(tbl, exists);
if (exists) {
tbl.dropConstraints(getCurrentConnection());
}
}
}
// Remove tables
tablesIter = baseTablesByName.values().iterator();
while (tablesIter.hasNext()) {
TableImpl tbl = (TableImpl) tablesIter.next();
if (writer != null) {
try {
if (tbl instanceof ClassTable) {
writer.write("-- ClassTable " + tbl.toString() + " for classes " + StringUtils.objectArrayToString(((ClassTable) tbl).getManagedClasses()) + "\n");
} else if (tbl instanceof JoinTable) {
writer.write("-- JoinTable " + tbl.toString() + " for join relationship\n");
}
} catch (IOException ioe) {
NucleusLogger.DATASTORE_SCHEMA.error("error writing DDL into file", ioe);
}
tbl.drop(getCurrentConnection());
} else {
// Drop table if exists in the datastore
Boolean schemaExists = schemaExistsForTableMap.get(tbl);
if (schemaExists != null && schemaExists == Boolean.TRUE) {
tbl.drop(getCurrentConnection());
}
}
}
} catch (Exception e) {
success = false;
String errorMsg = Localiser.msg("050047", e);
NucleusLogger.DATASTORE_SCHEMA.error(errorMsg);
throw new NucleusUserException(errorMsg, e);
}
if (!success) {
throw new NucleusException("DeleteTables operation failed");
}
}
}
Aggregations