Search in sources :

Example 46 with DBCException

use of org.jkiss.dbeaver.model.exec.DBCException in project dbeaver by serge-rider.

the class EntityEditor method saveCommandContext.

private boolean saveCommandContext(final DBRProgressMonitor monitor) {
    int previewResult = IDialogConstants.PROCEED_ID;
    if (DBeaverCore.getGlobalPreferenceStore().getBoolean(DBeaverPreferences.NAVIGATOR_SHOW_SQL_PREVIEW)) {
        monitor.beginTask(CoreMessages.editors_entity_monitor_preview_changes, 1);
        previewResult = showChanges(true);
        monitor.done();
    }
    if (previewResult != IDialogConstants.PROCEED_ID) {
        return true;
    }
    monitor.beginTask("Save entity", 1);
    Throwable error = null;
    final DBECommandContext commandContext = getCommandContext();
    if (commandContext == null) {
        log.warn("Null command context");
        return true;
    }
    try {
        commandContext.saveChanges(monitor);
    } catch (DBException e) {
        error = e;
    }
    if (getDatabaseObject() instanceof DBPStatefulObject) {
        try {
            ((DBPStatefulObject) getDatabaseObject()).refreshObjectState(monitor);
        } catch (DBCException e) {
            // Just report an error
            log.error(e);
        }
    }
    if (error == null) {
        // Refresh underlying node
        // It'll refresh database object and all it's descendants
        // So we'll get actual data from database
        final DBNDatabaseNode treeNode = getEditorInput().getNavigatorNode();
        try {
            DBeaverUI.runInProgressService(new DBRRunnableWithProgress() {

                @Override
                public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                    try {
                        treeNode.refreshNode(monitor, DBNEvent.FORCE_REFRESH);
                    } catch (DBException e) {
                        throw new InvocationTargetException(e);
                    }
                }
            });
        } catch (InvocationTargetException e) {
            error = e.getTargetException();
        } catch (InterruptedException e) {
        // ok
        }
    }
    monitor.done();
    if (error == null) {
        return true;
    } else {
        // Try to handle error in nested editors
        final Throwable vError = error;
        DBeaverUI.syncExec(new Runnable() {

            @Override
            public void run() {
                final IErrorVisualizer errorVisualizer = getAdapter(IErrorVisualizer.class);
                if (errorVisualizer != null) {
                    errorVisualizer.visualizeError(monitor, vError);
                }
            }
        });
        // Show error dialog
        DBeaverUI.asyncExec(new Runnable() {

            @Override
            public void run() {
                UIUtils.showErrorDialog(getSite().getShell(), "Can't save '" + getDatabaseObject().getName() + "'", null, vError);
            }
        });
        return false;
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBCException(org.jkiss.dbeaver.model.exec.DBCException) Point(org.eclipse.swt.graphics.Point) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBECommandContext(org.jkiss.dbeaver.model.edit.DBECommandContext) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)

Example 47 with DBCException

use of org.jkiss.dbeaver.model.exec.DBCException in project dbeaver by serge-rider.

the class JDBCCollection method makeCollectionFromString.

@NotNull
public static DBDCollection makeCollectionFromString(@NotNull JDBCSession session, String value) throws DBCException {
    String stringType = DBUtils.getDefaultDataTypeName(session.getDataSource(), DBPDataKind.STRING);
    if (stringType == null) {
        throw new DBCException("String data type not supported by database");
    }
    DBSDataType dataType = DBUtils.getLocalDataType(session.getDataSource(), stringType);
    if (dataType == null) {
        throw new DBCException("String data type '" + stringType + "' not supported by database");
    }
    DBDValueHandler valueHandler = DBUtils.findValueHandler(session, dataType);
    // Try to divide on string elements
    if (!CommonUtils.isEmpty(value)) {
        if (value.startsWith("[") && value.endsWith("]")) {
            // FIXME: use real parser (nested arrays, quotes escape, etc)
            String arrayString = value.substring(1, value.length() - 1);
            List<Object> items = new ArrayList<>();
            StringTokenizer st = new StringTokenizer(arrayString, ",", false);
            while (st.hasMoreTokens()) {
                String token = st.nextToken().trim();
                if (token.startsWith("\"") && token.endsWith("\"")) {
                    token = token.substring(1, token.length() - 1);
                }
                items.add(token);
            }
            return new JDBCCollectionString(dataType, valueHandler, value, items.toArray());
        }
    }
    return new JDBCCollectionString(dataType, valueHandler, value);
}
Also used : StringTokenizer(java.util.StringTokenizer) DBSDataType(org.jkiss.dbeaver.model.struct.DBSDataType) ArrayList(java.util.ArrayList) DBCException(org.jkiss.dbeaver.model.exec.DBCException) DBSTypedObject(org.jkiss.dbeaver.model.struct.DBSTypedObject) NotNull(org.jkiss.code.NotNull)

Example 48 with DBCException

use of org.jkiss.dbeaver.model.exec.DBCException in project dbeaver by serge-rider.

the class JDBCCollection method makeCollectionFromArray.

/////////////////////////////////////////////////////////////////////////////////////
// Utilities
/////////////////////////////////////////////////////////////////////////////////////
@NotNull
public static JDBCCollection makeCollectionFromArray(@NotNull JDBCSession session, @NotNull DBSTypedObject column, Array array) throws DBCException {
    DBRProgressMonitor monitor = session.getProgressMonitor();
    DBSDataType elementType = null;
    if (column instanceof DBSTypedObjectEx) {
        DBSDataType arrayType = ((DBSTypedObjectEx) column).getDataType();
        if (arrayType != null) {
            elementType = arrayType.getComponentType(monitor);
        }
    }
    if (elementType == null) {
        try {
            if (array == null) {
                String arrayTypeName = column.getTypeName();
                DBSDataType arrayType = session.getDataSource().resolveDataType(monitor, arrayTypeName);
                if (arrayType != null) {
                    elementType = arrayType.getComponentType(monitor);
                }
            } else {
                String baseTypeName = array.getBaseTypeName();
                elementType = session.getDataSource().resolveDataType(monitor, baseTypeName);
            }
        } catch (Exception e) {
            throw new DBCException("Error resolving data type", e);
        }
    }
    try {
        if (elementType == null) {
            if (array == null) {
                throw new DBCException("Can't resolve NULL array data type");
            }
            try {
                return makeCollectionFromResultSet(session, array, null);
            } catch (SQLException e) {
                //$NON-NLS-1$
                throw new DBCException(e, session.getDataSource());
            }
        }
        try {
            return makeCollectionFromArray(session, array, elementType);
        } catch (SQLException e) {
            if (array == null) {
                //$NON-NLS-1$
                throw new DBCException(e, session.getDataSource());
            }
            try {
                return makeCollectionFromResultSet(session, array, elementType);
            } catch (SQLException e1) {
                //$NON-NLS-1$
                throw new DBCException(e1, session.getDataSource());
            }
        }
    } catch (DBException e) {
        //$NON-NLS-1$
        throw new DBCException("Can't extract array data from JDBC array", e);
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBSDataType(org.jkiss.dbeaver.model.struct.DBSDataType) SQLException(java.sql.SQLException) DBSTypedObjectEx(org.jkiss.dbeaver.model.struct.DBSTypedObjectEx) DBCException(org.jkiss.dbeaver.model.exec.DBCException) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) DBCException(org.jkiss.dbeaver.model.exec.DBCException) SQLException(java.sql.SQLException) DBException(org.jkiss.dbeaver.DBException) NotNull(org.jkiss.code.NotNull)

Example 49 with DBCException

use of org.jkiss.dbeaver.model.exec.DBCException in project dbeaver by serge-rider.

the class JDBCCollection method getArrayValue.

public Array getArrayValue() throws DBCException {
    Object[] attrs = new Object[contents.length];
    for (int i = 0; i < contents.length; i++) {
        Object attr = contents[i];
        if (attr instanceof DBDValue) {
            attr = ((DBDValue) attr).getRawValue();
        }
        attrs[i] = attr;
    }
    final DBSDataType dataType = getComponentType();
    try (DBCSession session = DBUtils.openUtilSession(VoidProgressMonitor.INSTANCE, dataType.getDataSource(), "Create JDBC array")) {
        if (session instanceof Connection) {
            return ((Connection) session).createArrayOf(dataType.getTypeName(), attrs);
        } else {
            return new JDBCArrayImpl(dataType.getTypeName(), dataType.getTypeID(), attrs);
        }
    } catch (Throwable e) {
        throw new DBCException("Error creating struct", e);
    }
}
Also used : JDBCArrayImpl(org.jkiss.dbeaver.model.impl.jdbc.JDBCArrayImpl) DBSDataType(org.jkiss.dbeaver.model.struct.DBSDataType) Connection(java.sql.Connection) DBSTypedObject(org.jkiss.dbeaver.model.struct.DBSTypedObject) DBCException(org.jkiss.dbeaver.model.exec.DBCException) DBCSession(org.jkiss.dbeaver.model.exec.DBCSession) JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession)

Example 50 with DBCException

use of org.jkiss.dbeaver.model.exec.DBCException in project dbeaver by serge-rider.

the class AbstractObjectManager method executePersistAction.

@Override
public void executePersistAction(DBCSession session, DBECommand<OBJECT_TYPE> command, DBEPersistAction action) throws DBException {
    String script = action.getScript();
    if (script == null) {
        action.handleExecute(session, null);
    } else {
        DBCStatement dbStat = DBUtils.createStatement(session, script, false);
        try {
            dbStat.executeStatement();
            action.handleExecute(session, null);
        } catch (DBCException e) {
            action.handleExecute(session, e);
            throw e;
        } finally {
            dbStat.close();
        }
    }
}
Also used : DBCException(org.jkiss.dbeaver.model.exec.DBCException) DBCStatement(org.jkiss.dbeaver.model.exec.DBCStatement)

Aggregations

DBCException (org.jkiss.dbeaver.model.exec.DBCException)60 SQLException (java.sql.SQLException)28 JDBCSession (org.jkiss.dbeaver.model.exec.jdbc.JDBCSession)16 JDBCResultSet (org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)14 JDBCPreparedStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement)13 DBException (org.jkiss.dbeaver.DBException)11 NotNull (org.jkiss.code.NotNull)9 DBCStatement (org.jkiss.dbeaver.model.exec.DBCStatement)5 DBSDataType (org.jkiss.dbeaver.model.struct.DBSDataType)5 DBCResultSet (org.jkiss.dbeaver.model.exec.DBCResultSet)4 DBSTypedObject (org.jkiss.dbeaver.model.struct.DBSTypedObject)4 IOException (java.io.IOException)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Tree (org.eclipse.swt.widgets.Tree)3 TreeColumn (org.eclipse.swt.widgets.TreeColumn)3 TreeItem (org.eclipse.swt.widgets.TreeItem)3 Nullable (org.jkiss.code.Nullable)3 DBPPlatform (org.jkiss.dbeaver.model.app.DBPPlatform)3 TemporaryContentStorage (org.jkiss.dbeaver.model.impl.TemporaryContentStorage)3 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)3