Search in sources :

Example 6 with DBCSession

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();
        }
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBException(org.jkiss.dbeaver.DBException) DBCSession(org.jkiss.dbeaver.model.exec.DBCSession) DBCTransactionManager(org.jkiss.dbeaver.model.exec.DBCTransactionManager)

Example 7 with DBCSession

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);
    }
}
Also used : DBDValue(org.jkiss.dbeaver.model.data.DBDValue) JDBCStructImpl(org.jkiss.dbeaver.model.impl.jdbc.JDBCStructImpl) DBCException(org.jkiss.dbeaver.model.exec.DBCException) DBCSession(org.jkiss.dbeaver.model.exec.DBCSession)

Aggregations

DBCSession (org.jkiss.dbeaver.model.exec.DBCSession)7 DBException (org.jkiss.dbeaver.DBException)3 DBCException (org.jkiss.dbeaver.model.exec.DBCException)2 DBSTypedObject (org.jkiss.dbeaver.model.struct.DBSTypedObject)2 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 MalformedURLException (java.net.MalformedURLException)1 Connection (java.sql.Connection)1 ArrayList (java.util.ArrayList)1 Clipboard (org.eclipse.swt.dnd.Clipboard)1 MySQLDataSource (org.jkiss.dbeaver.ext.mysql.model.MySQLDataSource)1 DBDContent (org.jkiss.dbeaver.model.data.DBDContent)1 DBDValue (org.jkiss.dbeaver.model.data.DBDValue)1 DBEPersistAction (org.jkiss.dbeaver.model.edit.DBEPersistAction)1 DBCExecutionPurpose (org.jkiss.dbeaver.model.exec.DBCExecutionPurpose)1 DBCTransactionManager (org.jkiss.dbeaver.model.exec.DBCTransactionManager)1 JDBCSession (org.jkiss.dbeaver.model.exec.jdbc.JDBCSession)1 SQLDatabasePersistAction (org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistAction)1 JDBCArrayImpl (org.jkiss.dbeaver.model.impl.jdbc.JDBCArrayImpl)1 JDBCStructImpl (org.jkiss.dbeaver.model.impl.jdbc.JDBCStructImpl)1