use of org.jkiss.dbeaver.model.exec.DBCSession in project dbeaver by serge-rider.
the class AbstractCommandContext method saveChanges.
@Override
public void saveChanges(DBRProgressMonitor monitor) throws DBException {
if (!executionContext.isConnected()) {
throw new DBException("Context [" + executionContext.getContextName() + "] isn't connected to the database");
}
List<CommandQueue> commandQueues = getCommandQueues();
// Validate commands
for (CommandQueue queue : commandQueues) {
for (CommandInfo cmd : queue.commands) {
cmd.command.validateCommand();
}
}
// Execute commands
List<CommandInfo> executedCommands = new ArrayList<>();
try {
for (CommandQueue queue : commandQueues) {
// Make list of not-executed commands
for (int i = 0; i < queue.commands.size(); i++) {
if (monitor.isCanceled()) {
break;
}
CommandInfo cmd = queue.commands.get(i);
while (cmd.mergedBy != null) {
cmd = cmd.mergedBy;
}
if (!cmd.executed) {
// Persist changes
//if (CommonUtils.isEmpty(cmd.persistActions)) {
DBEPersistAction[] persistActions = cmd.command.getPersistActions();
if (!ArrayUtils.isEmpty(persistActions)) {
cmd.persistActions = new ArrayList<>(persistActions.length);
for (DBEPersistAction action : persistActions) {
cmd.persistActions.add(new PersistInfo(action));
}
}
//}
if (!CommonUtils.isEmpty(cmd.persistActions)) {
try (DBCSession session = openCommandPersistContext(monitor, cmd.command)) {
DBException error = null;
for (PersistInfo persistInfo : cmd.persistActions) {
DBEPersistAction.ActionType actionType = persistInfo.action.getType();
if (persistInfo.executed && actionType == DBEPersistAction.ActionType.NORMAL) {
continue;
}
if (monitor.isCanceled()) {
break;
}
try {
if (error == null || actionType == DBEPersistAction.ActionType.FINALIZER) {
queue.objectManager.executePersistAction(session, cmd.command, persistInfo.action);
}
persistInfo.executed = true;
} catch (DBException e) {
persistInfo.error = e;
persistInfo.executed = false;
if (actionType != DBEPersistAction.ActionType.OPTIONAL) {
error = e;
}
}
}
if (error != null) {
throw error;
}
// Commit metadata changes
DBCTransactionManager txnManager = DBUtils.getTransactionManager(session.getExecutionContext());
if (txnManager != null && !txnManager.isAutoCommit()) {
txnManager.commit(session);
}
}
cmd.executed = true;
}
}
if (cmd.executed) {
// should remain - they constructs queue by merging with other commands
synchronized (commands) {
// Remove original command from stack
//final CommandInfo thisCommand = queue.commands.get(i);
commands.remove(cmd);
}
}
if (!executedCommands.contains(cmd)) {
executedCommands.add(cmd);
}
}
}
// Let's clear commands
// If everything went well then there should be nothing to do else.
// But some commands may still remain in queue if they merged each other
// (e.g. create + delete of the same entity produce 2 commands and zero actions).
// There were no exceptions during save so we assume that everything went well
commands.clear();
userParams.clear();
/*
// Refresh object states
for (CommandQueue queue : commandQueues) {
if (queue.getObject() instanceof DBPStatefulObject) {
try {
((DBPStatefulObject) queue.getObject()).refreshObjectState(monitor);
} catch (DBCException e) {
// Just report an error
log.error(e);
}
}
}
*/
} finally {
try {
// Update UI
if (atomic) {
for (CommandInfo cmd : executedCommands) {
if (cmd.reflector != null) {
cmd.reflector.redoCommand(cmd.command);
}
}
}
// Update model
for (CommandInfo cmd : executedCommands) {
cmd.command.updateModel();
}
} catch (Exception e) {
log.warn("Error updating model", e);
}
clearCommandQueues();
clearUndidCommands();
// Notify listeners
for (DBECommandListener listener : getListeners()) {
listener.onSave();
}
}
}
use of org.jkiss.dbeaver.model.exec.DBCSession in project dbeaver by serge-rider.
the class JDBCComposite method getStructValue.
public Struct getStructValue() throws DBCException {
Object[] attrs = new Object[values.length];
for (int i = 0; i < values.length; i++) {
Object attr = values[i];
if (attr instanceof DBDValue) {
attr = ((DBDValue) attr).getRawValue();
}
attrs[i] = attr;
}
final DBSDataType dataType = getDataType();
try (DBCSession session = DBUtils.openUtilSession(VoidProgressMonitor.INSTANCE, dataType.getDataSource(), "Create JDBC struct")) {
if (session instanceof Connection) {
return ((Connection) session).createStruct(dataType.getTypeName(), attrs);
} else {
return new JDBCStructImpl(dataType.getTypeName(), attrs);
}
} catch (Throwable e) {
throw new DBCException("Error creating struct", e);
}
}
Aggregations