Search in sources :

Example 26 with DBCException

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

the class DB2BaseTableToolDialog method getScriptListener.

@Override
protected SQLScriptProgressListener<DB2Table> getScriptListener() {
    final int nbExtraColumns = getNumberExtraResultingColumns();
    return new SQLScriptStatusDialog<DB2Table>(getShell(), getTitle() + " " + DB2Messages.dialog_table_tools_progress, null) {

        @Override
        protected void createStatusColumns(Tree objectTree) {
            TreeColumn msgColumn = new TreeColumn(objectTree, SWT.NONE);
            msgColumn.setText(DB2Messages.dialog_table_tools_result);
            for (int i = 0; i < nbExtraColumns; i++) {
                new TreeColumn(objectTree, SWT.NONE);
            }
        }

        @Override
        public void endObjectProcessing(@NotNull DB2Table db2Table, Exception exception) {
            TreeItem treeItem = getTreeItem(db2Table);
            if (exception == null) {
                treeItem.setText(1, DB2Messages.dialog_table_tools_success_title);
            } else {
                treeItem.setText(1, exception.getMessage());
            }
            UIUtils.packColumns(treeItem.getParent(), false, null);
        }

        // DF: This method is for tools that return resultsets
        @Override
        public void processObjectResults(@NotNull DB2Table db2Table, @Nullable DBCStatement statement, @Nullable DBCResultSet resultSet) throws DBCException {
            if (resultSet == null) {
                return;
            }
            // Retrive column names
            JDBCResultSetMetaDataImpl rsMetaData = (JDBCResultSetMetaDataImpl) resultSet.getMeta();
            try {
                TreeItem treeItem = getTreeItem(db2Table);
                Font f = UIUtils.makeBoldFont(treeItem.getFont());
                if (treeItem != null) {
                    // Display the column names
                    TreeItem subItem = null;
                    subItem = new TreeItem(treeItem, SWT.NONE);
                    subItem.setFont(f);
                    for (int i = 0; i < rsMetaData.getColumnCount(); i++) {
                        subItem.setText(i, rsMetaData.getColumnName(i + 1));
                        subItem.setGrayed(true);
                    }
                    // Display the data for each row
                    while (resultSet.nextRow()) {
                        subItem = new TreeItem(treeItem, SWT.NONE);
                        for (int i = 0; i < rsMetaData.getColumnCount(); i++) {
                            subItem.setText(i, CommonUtils.toString(resultSet.getAttributeValue(i)));
                        }
                    }
                    treeItem.setExpanded(true);
                }
            } catch (SQLException e) {
                throw new DBCException(e.getMessage());
            }
        }
    };
}
Also used : TreeItem(org.eclipse.swt.widgets.TreeItem) SQLException(java.sql.SQLException) DBCException(org.jkiss.dbeaver.model.exec.DBCException) NotNull(org.jkiss.code.NotNull) DBCStatement(org.jkiss.dbeaver.model.exec.DBCStatement) JDBCResultSetMetaDataImpl(org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCResultSetMetaDataImpl) SQLScriptStatusDialog(org.jkiss.dbeaver.ui.dialogs.sql.SQLScriptStatusDialog) DBCException(org.jkiss.dbeaver.model.exec.DBCException) SQLException(java.sql.SQLException) Font(org.eclipse.swt.graphics.Font) DB2Table(org.jkiss.dbeaver.ext.db2.model.DB2Table) TreeColumn(org.eclipse.swt.widgets.TreeColumn) Tree(org.eclipse.swt.widgets.Tree) DBCResultSet(org.jkiss.dbeaver.model.exec.DBCResultSet) Nullable(org.jkiss.code.Nullable)

Example 27 with DBCException

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

the class CompileHandler method compileUnit.

public static boolean compileUnit(DBRProgressMonitor monitor, DBCCompileLog compileLog, OracleSourceObject unit) throws DBCException {
    final DBEPersistAction[] compileActions = unit.getCompileActions();
    if (ArrayUtils.isEmpty(compileActions)) {
        return true;
    }
    try (JDBCSession session = DBUtils.openUtilSession(monitor, unit.getDataSource(), "Compile '" + unit.getName() + "'")) {
        boolean success = true;
        for (DBEPersistAction action : compileActions) {
            final String script = action.getScript();
            compileLog.trace(script);
            if (monitor.isCanceled()) {
                break;
            }
            try {
                try (DBCStatement dbStat = session.prepareStatement(DBCStatementType.QUERY, script, false, false, false)) {
                    dbStat.executeStatement();
                }
                action.handleExecute(session, null);
            } catch (DBCException e) {
                action.handleExecute(session, e);
                throw e;
            }
            if (action instanceof OracleObjectPersistAction) {
                if (!logObjectErrors(session, compileLog, unit, ((OracleObjectPersistAction) action).getObjectType())) {
                    success = false;
                }
            }
        }
        final DBSObjectState oldState = unit.getObjectState();
        unit.refreshObjectState(monitor);
        if (unit.getObjectState() != oldState) {
            unit.getDataSource().getContainer().fireEvent(new DBPEvent(DBPEvent.Action.OBJECT_UPDATE, unit));
        }
        return success;
    }
}
Also used : JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) DBSObjectState(org.jkiss.dbeaver.model.struct.DBSObjectState) DBEPersistAction(org.jkiss.dbeaver.model.edit.DBEPersistAction) DBCException(org.jkiss.dbeaver.model.exec.DBCException) DBPEvent(org.jkiss.dbeaver.model.DBPEvent) DBCStatement(org.jkiss.dbeaver.model.exec.DBCStatement) OracleObjectPersistAction(org.jkiss.dbeaver.ext.oracle.model.OracleObjectPersistAction)

Example 28 with DBCException

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

the class CompileHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    final List<OracleSourceObject> objects = getSelectedObjects(event);
    if (!objects.isEmpty()) {
        final Shell activeShell = HandlerUtil.getActiveShell(event);
        if (objects.size() == 1) {
            final OracleSourceObject unit = objects.get(0);
            DBCSourceHost sourceHost = null;
            final IWorkbenchPart activePart = HandlerUtil.getActiveEditor(event);
            if (activePart != null) {
                sourceHost = RuntimeUtils.getObjectAdapter(activePart, DBCSourceHost.class);
                if (sourceHost == null) {
                    sourceHost = activePart.getAdapter(DBCSourceHost.class);
                }
            }
            if (sourceHost != null && sourceHost.getSourceObject() != unit) {
                sourceHost = null;
            }
            final DBCCompileLog compileLog = sourceHost == null ? new DBCCompileLogBase() : sourceHost.getCompileLog();
            compileLog.clearLog();
            Throwable error = null;
            try {
                DBeaverUI.runInProgressService(new DBRRunnableWithProgress() {

                    @Override
                    public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                        try {
                            compileUnit(monitor, compileLog, unit);
                        } catch (DBCException e) {
                            throw new InvocationTargetException(e);
                        }
                    }
                });
                if (compileLog.getError() != null) {
                    error = compileLog.getError();
                }
            } catch (InvocationTargetException e) {
                error = e.getTargetException();
            } catch (InterruptedException e) {
                return null;
            }
            if (error != null) {
                UIUtils.showErrorDialog(activeShell, "Unexpected compilation error", null, error);
            } else if (!CommonUtils.isEmpty(compileLog.getErrorStack())) {
                // Show compile errors
                int line = -1, position = -1;
                StringBuilder fullMessage = new StringBuilder();
                for (DBCCompileError oce : compileLog.getErrorStack()) {
                    fullMessage.append(oce.toString()).append(GeneralUtils.getDefaultLineSeparator());
                    if (line < 0) {
                        line = oce.getLine();
                        position = oce.getPosition();
                    }
                }
                // If compiled object is currently open in editor - try to position on error line
                if (sourceHost != null && sourceHost.getSourceObject() == unit && line > 0 && position > 0) {
                    sourceHost.positionSource(line, position);
                    activePart.getSite().getPage().activate(activePart);
                }
                String errorTitle = unit.getName() + " compilation failed";
                if (sourceHost != null) {
                    sourceHost.setCompileInfo(errorTitle, true);
                    sourceHost.showCompileLog();
                }
                UIUtils.showErrorDialog(activeShell, errorTitle, fullMessage.toString());
            } else {
                String message = unit.getName() + " compiled successfully";
                if (sourceHost != null) {
                    sourceHost.setCompileInfo(message, true);
                }
                UIUtils.showMessageBox(activeShell, "Done", message, SWT.ICON_INFORMATION);
            }
        } else {
            OracleCompilerDialog dialog = new OracleCompilerDialog(activeShell, objects);
            dialog.open();
        }
    }
    return null;
}
Also used : DBCSourceHost(org.jkiss.dbeaver.model.exec.compile.DBCSourceHost) DBCException(org.jkiss.dbeaver.model.exec.DBCException) DBCCompileError(org.jkiss.dbeaver.model.exec.compile.DBCCompileError) InvocationTargetException(java.lang.reflect.InvocationTargetException) OracleCompilerDialog(org.jkiss.dbeaver.ext.oracle.views.OracleCompilerDialog) DBCCompileLogBase(org.jkiss.dbeaver.model.exec.compile.DBCCompileLogBase) Shell(org.eclipse.swt.widgets.Shell) IWorkbenchPart(org.eclipse.ui.IWorkbenchPart) DBCCompileLog(org.jkiss.dbeaver.model.exec.compile.DBCCompileLog) DBRRunnableWithProgress(org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) OracleSourceObject(org.jkiss.dbeaver.ext.oracle.model.source.OracleSourceObject)

Example 29 with DBCException

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

the class OracleDataType method getComponentType.

@Property(viewable = true, order = 8)
public OracleDataType getComponentType(@NotNull DBRProgressMonitor monitor) throws DBCException {
    if (componentType != null) {
        return componentType;
    }
    OracleSchema schema = getSchema();
    if (schema == null || !TYPE_CODE_COLLECTION.equals(typeCode) || !getDataSource().isAtLeastV10()) {
        return null;
    }
    try (JDBCSession session = DBUtils.openMetaSession(monitor, getDataSource(), "Load collection types")) {
        try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT ELEM_TYPE_OWNER,ELEM_TYPE_NAME,ELEM_TYPE_MOD FROM SYS.ALL_COLL_TYPES WHERE OWNER=? AND TYPE_NAME=?")) {
            dbStat.setString(1, schema.getName());
            dbStat.setString(2, getName());
            try (JDBCResultSet dbResults = dbStat.executeQuery()) {
                if (dbResults.next()) {
                    String compTypeSchema = JDBCUtils.safeGetString(dbResults, "ELEM_TYPE_OWNER");
                    String compTypeName = JDBCUtils.safeGetString(dbResults, "ELEM_TYPE_NAME");
                    //String compTypeMod = JDBCUtils.safeGetString(dbResults, "ELEM_TYPE_MOD");
                    componentType = OracleDataType.resolveDataType(monitor, getDataSource(), compTypeSchema, compTypeName);
                } else {
                    log.warn("Can't resolve collection type [" + getName() + "]");
                }
            }
        }
    } catch (Exception e) {
        log.warn("Error reading collection types", e);
    }
    return componentType;
}
Also used : JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) DBCException(org.jkiss.dbeaver.model.exec.DBCException) SQLException(java.sql.SQLException) DBException(org.jkiss.dbeaver.DBException) Property(org.jkiss.dbeaver.model.meta.Property)

Example 30 with DBCException

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

the class MySQLTable method loadAdditionalInfo.

private void loadAdditionalInfo(DBRProgressMonitor monitor) throws DBCException {
    if (!isPersisted()) {
        additionalInfo.loaded = true;
        return;
    }
    MySQLDataSource dataSource = getDataSource();
    try (JDBCSession session = DBUtils.openMetaSession(monitor, dataSource, "Load table status")) {
        try (JDBCPreparedStatement dbStat = session.prepareStatement("SHOW TABLE STATUS FROM " + DBUtils.getQuotedIdentifier(getContainer()) + " LIKE '" + getName() + "'")) {
            try (JDBCResultSet dbResult = dbStat.executeQuery()) {
                if (dbResult.next()) {
                    // filer table description (for INNODB it contains some system information)
                    String desc = JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_TABLE_COMMENT);
                    if (desc != null) {
                        if (desc.startsWith(INNODB_COMMENT)) {
                            desc = "";
                        } else if (!CommonUtils.isEmpty(desc)) {
                            int divPos = desc.indexOf("; " + INNODB_COMMENT);
                            if (divPos != -1) {
                                desc = desc.substring(0, divPos);
                            }
                        }
                        additionalInfo.description = desc;
                    }
                    additionalInfo.engine = dataSource.getEngine(JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_ENGINE));
                    additionalInfo.rowCount = JDBCUtils.safeGetLong(dbResult, MySQLConstants.COL_TABLE_ROWS);
                    additionalInfo.autoIncrement = JDBCUtils.safeGetLong(dbResult, MySQLConstants.COL_AUTO_INCREMENT);
                    additionalInfo.createTime = JDBCUtils.safeGetTimestamp(dbResult, MySQLConstants.COL_CREATE_TIME);
                    additionalInfo.updateTime = JDBCUtils.safeGetTimestamp(dbResult, "Update_time");
                    additionalInfo.checkTime = JDBCUtils.safeGetTimestamp(dbResult, "Check_time");
                    additionalInfo.collation = dataSource.getCollation(JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_COLLATION));
                    if (additionalInfo.collation != null) {
                        additionalInfo.charset = additionalInfo.collation.getCharset();
                    }
                    additionalInfo.avgRowLength = JDBCUtils.safeGetLong(dbResult, MySQLConstants.COL_AVG_ROW_LENGTH);
                    additionalInfo.dataLength = JDBCUtils.safeGetLong(dbResult, MySQLConstants.COL_DATA_LENGTH);
                    additionalInfo.maxDataLength = JDBCUtils.safeGetLong(dbResult, "Max_data_length");
                    additionalInfo.dataFree = JDBCUtils.safeGetLong(dbResult, "Data_free");
                    additionalInfo.indexLength = JDBCUtils.safeGetLong(dbResult, "Index_length");
                    additionalInfo.rowFormat = JDBCUtils.safeGetString(dbResult, "Row_format");
                }
                additionalInfo.loaded = true;
            }
        }
    } catch (SQLException e) {
        throw new DBCException(e, dataSource);
    }
}
Also used : SQLException(java.sql.SQLException) DBCException(org.jkiss.dbeaver.model.exec.DBCException)

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