use of herddb.model.ForeignKeyDef in project herddb by diennea.
the class TableSpaceManager method alterTable.
private StatementExecutionResult alterTable(AlterTableStatement statement, TransactionContext transactionContext, StatementEvaluationContext context) throws StatementExecutionException {
boolean lockAcquired = false;
if (context.getTableSpaceLock() == 0) {
long lockStamp = acquireWriteLock(statement);
context.setTableSpaceLock(lockStamp);
lockAcquired = true;
}
try {
if (transactionContext.transactionId > 0) {
Transaction transaction = transactions.get(transactionContext.transactionId);
if (transactionContext.transactionId > 0 && transaction == null) {
throw new StatementExecutionException("transaction " + transactionContext.transactionId + " does not exist on tablespace " + tableSpaceName);
}
if (transaction != null && !transaction.tableSpace.equals(tableSpaceName)) {
throw new StatementExecutionException("transaction " + transaction.transactionId + " is for tablespace " + transaction.tableSpace + ", not for " + tableSpaceName);
}
LOGGER.log(Level.INFO, "Implicitly committing transaction " + transactionContext.transactionId + " due to an ALTER TABLE statement in tablespace " + tableSpaceName);
try {
commitTransaction(new CommitTransactionStatement(tableSpaceName, transactionContext.transactionId), context).join();
} catch (CompletionException err) {
throw new StatementExecutionException(err);
}
transactionContext = TransactionContext.NO_TRANSACTION;
}
AbstractTableManager tableManager = tables.get(statement.getTable());
if (tableManager == null) {
throw new TableDoesNotExistException("no table " + statement.getTable() + " in tablespace " + tableSpaceName + "," + " only " + tables.keySet());
}
Table oldTable = tableManager.getTable();
Table[] childrenTables = collectChildrenTables(oldTable);
if (childrenTables != null) {
for (Table child : childrenTables) {
for (String col : statement.getDropColumns()) {
for (ForeignKeyDef fk : child.foreignKeys) {
if (fk.parentTableId.equals(oldTable.uuid)) {
if (Stream.of(fk.parentTableColumns).anyMatch(c -> c.equalsIgnoreCase(col))) {
throw new StatementExecutionException("Cannot drop column " + oldTable.name + "." + col + " because of foreign key constraint " + fk.name + " on table " + child.name);
}
}
}
}
}
}
Table newTable;
try {
newTable = tableManager.getTable().applyAlterTable(statement);
} catch (IllegalArgumentException error) {
throw new StatementExecutionException(error);
}
validateAlterTable(newTable, context);
LogEntry entry = LogEntryFactory.alterTable(newTable, null);
try {
CommitLogResult pos = log.log(entry, entry.transactionId <= 0);
apply(pos, entry, false);
} catch (Exception err) {
throw new StatementExecutionException(err);
}
// Here transactionId is always 0, because transaction is implicitly committed
return new DDLStatementExecutionResult(transactionContext.transactionId);
} finally {
if (lockAcquired) {
releaseWriteLock(context.getTableSpaceLock(), statement);
context.setTableSpaceLock(0);
}
}
}
Aggregations