Search in sources :

Example 41 with DBRProgressMonitor

use of org.jkiss.dbeaver.model.runtime.DBRProgressMonitor in project dbeaver by serge-rider.

the class ContentPanelEditor method primeEditorValue.

@Override
public void primeEditorValue(@Nullable final Object value) throws DBException {
    final DBDContent content = (DBDContent) valueController.getValue();
    if (content == null) {
        valueController.showMessage("NULL content value. Must be DBDContent.", DBPMessageType.ERROR);
        return;
    }
    if (streamEditor == null) {
        valueController.showMessage("NULL content editor.", DBPMessageType.ERROR);
        return;
    }
    DBeaverUI.runInUI(valueController.getValueSite().getWorkbenchWindow(), new DBRRunnableWithProgress() {

        @Override
        public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
            try {
                streamEditor.primeEditorValue(monitor, control, content);
            } catch (Throwable e) {
                log.debug(e);
                valueController.showMessage(e.getMessage(), DBPMessageType.ERROR);
            }
        }
    });
}
Also used : DBDContent(org.jkiss.dbeaver.model.data.DBDContent) DBRRunnableWithProgress(org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 42 with DBRProgressMonitor

use of org.jkiss.dbeaver.model.runtime.DBRProgressMonitor in project dbeaver by serge-rider.

the class DB2GranteeAuthCache method fetchObject.

@Override
protected DB2AuthBase fetchObject(@NotNull JDBCSession session, @NotNull DB2Grantee db2Grantee, @NotNull JDBCResultSet resultSet) throws SQLException, DBException {
    DB2DataSource db2DataSource = db2Grantee.getDataSource();
    DBRProgressMonitor monitor = session.getProgressMonitor();
    String objectSchemaName = JDBCUtils.safeGetStringTrimmed(resultSet, "OBJ_SCHEMA");
    String objectName = JDBCUtils.safeGetStringTrimmed(resultSet, "OBJ_NAME");
    DB2ObjectType objectType = CommonUtils.valueOf(DB2ObjectType.class, JDBCUtils.safeGetString(resultSet, "OBJ_TYPE"));
    switch(objectType) {
        case COLUMN:
            String columnName = JDBCUtils.safeGetStringTrimmed(resultSet, "USAGEAUTH");
            DB2TableColumn db2TableColumn = DB2Utils.findColumnBySchemaNameAndTableNameAndName(monitor, db2DataSource, objectSchemaName, objectName, columnName);
            return new DB2AuthColumn(monitor, db2Grantee, db2TableColumn, resultSet);
        case INDEX:
            DB2Index db2Index = DB2Utils.findIndexBySchemaNameAndName(monitor, db2DataSource, objectSchemaName, objectName);
            return new DB2AuthIndex(monitor, db2Grantee, db2Index, resultSet);
        case MODULE:
            DB2Module db2Module = DB2Utils.findModuleBySchemaNameAndName(monitor, db2DataSource, objectSchemaName, objectName);
            return new DB2AuthModule(monitor, db2Grantee, db2Module, resultSet);
        case PACKAGE:
            DB2Package db2Package = DB2Utils.findPackageBySchemaNameAndName(monitor, db2DataSource, objectSchemaName, objectName);
            return new DB2AuthPackage(monitor, db2Grantee, db2Package, resultSet);
        case PROCEDURE:
            // Can be a Function or a Procedure
            DB2RoutineType routineType = CommonUtils.valueOf(DB2RoutineType.class, JDBCUtils.safeGetStringTrimmed(resultSet, "USAGEAUTH"));
            switch(routineType) {
                case F:
                    DB2Routine db2Udf = DB2Utils.findUDFBySchemaNameAndName(monitor, db2DataSource, objectSchemaName, objectName);
                    return new DB2AuthUDF(monitor, db2Grantee, db2Udf, resultSet);
                case M:
                    DB2Routine db2Method = DB2Utils.findMethodBySchemaNameAndName(monitor, db2DataSource, objectSchemaName, objectName);
                    return new DB2AuthMethod(monitor, db2Grantee, db2Method, resultSet);
                case P:
                    DB2Routine db2Procedure = DB2Utils.findProcedureBySchemaNameAndName(monitor, db2DataSource, objectSchemaName, objectName);
                    return new DB2AuthProcedure(monitor, db2Grantee, db2Procedure, resultSet);
                default:
                    throw new DBException(routineType + " is not a valid DB2RoutineType");
            }
        case SCHEMA:
            DB2Schema db2Schema = db2DataSource.getSchema(monitor, objectName);
            return new DB2AuthSchema(monitor, db2Grantee, db2Schema, resultSet);
        case SEQUENCE:
            DB2Sequence db2Sequence = DB2Utils.findSequenceBySchemaNameAndName(monitor, db2DataSource, objectSchemaName, objectName);
            return new DB2AuthSequence(monitor, db2Grantee, db2Sequence, resultSet);
        case TABLE:
            // Can be a Table, a View or an MQT..
            DB2TableBase db2TableBase = DB2Utils.findTableBySchemaNameAndName(monitor, db2DataSource, objectSchemaName, objectName);
            if (db2TableBase != null) {
                return new DB2AuthTable(monitor, db2Grantee, db2TableBase, resultSet);
            } else {
                db2TableBase = DB2Utils.findViewBySchemaNameAndName(monitor, db2DataSource, objectSchemaName, objectName);
                if (db2TableBase != null) {
                    return new DB2AuthView(monitor, db2Grantee, db2TableBase, resultSet);
                } else {
                    db2TableBase = DB2Utils.findMQTBySchemaNameAndName(monitor, db2DataSource, objectSchemaName, objectName);
                    return new DB2AuthMaterializedQueryTable(monitor, db2Grantee, db2TableBase, resultSet);
                }
            }
        case TABLESPACE:
            DB2Tablespace db2Tablespace = db2DataSource.getTablespace(monitor, objectName);
            return new DB2AuthTablespace(monitor, db2Grantee, db2Tablespace, resultSet);
        case VARIABLE:
            DB2Variable db2Variable = db2DataSource.getVariable(monitor, objectName);
            return new DB2AuthVariable(monitor, db2Grantee, db2Variable, resultSet);
        case XML_SCHEMA:
            Long xmlSchemaId = Long.valueOf(objectName);
            DB2XMLSchema db2XmlSchema = DB2Utils.findXMLSchemaByById(monitor, db2DataSource, xmlSchemaId);
            return new DB2AuthXMLSchema(monitor, db2Grantee, db2XmlSchema, resultSet);
        default:
            throw new DBException("Programming error: " + objectType + " is not supported yet and the SELECT statement must exclude it");
    }
}
Also used : DB2Routine(org.jkiss.dbeaver.ext.db2.model.DB2Routine) DBException(org.jkiss.dbeaver.DBException) DB2Sequence(org.jkiss.dbeaver.ext.db2.model.DB2Sequence) DB2ObjectType(org.jkiss.dbeaver.ext.db2.editors.DB2ObjectType) DB2TableBase(org.jkiss.dbeaver.ext.db2.model.DB2TableBase) DB2Index(org.jkiss.dbeaver.ext.db2.model.DB2Index) DB2Package(org.jkiss.dbeaver.ext.db2.model.DB2Package) DB2Schema(org.jkiss.dbeaver.ext.db2.model.DB2Schema) DB2Tablespace(org.jkiss.dbeaver.ext.db2.model.DB2Tablespace) DB2DataSource(org.jkiss.dbeaver.ext.db2.model.DB2DataSource) DB2XMLSchema(org.jkiss.dbeaver.ext.db2.model.DB2XMLSchema) DB2TableColumn(org.jkiss.dbeaver.ext.db2.model.DB2TableColumn) DB2Module(org.jkiss.dbeaver.ext.db2.model.module.DB2Module) DB2RoutineType(org.jkiss.dbeaver.ext.db2.model.dict.DB2RoutineType) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) DB2Variable(org.jkiss.dbeaver.ext.db2.model.DB2Variable)

Example 43 with DBRProgressMonitor

use of org.jkiss.dbeaver.model.runtime.DBRProgressMonitor 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 44 with DBRProgressMonitor

use of org.jkiss.dbeaver.model.runtime.DBRProgressMonitor in project dbeaver by serge-rider.

the class OracleStructureAssistant method searchAllObjects.

private void searchAllObjects(final JDBCSession session, final OracleSchema schema, String objectNameMask, DBSObjectType[] objectTypes, boolean caseSensitive, int maxResults, List<DBSObjectReference> objects) throws SQLException, DBException {
    StringBuilder objectTypeClause = new StringBuilder(100);
    final List<OracleObjectType> oracleObjectTypes = new ArrayList<>(objectTypes.length + 2);
    for (DBSObjectType objectType : objectTypes) {
        if (objectType instanceof OracleObjectType) {
            oracleObjectTypes.add((OracleObjectType) objectType);
            if (objectType == OracleObjectType.PROCEDURE) {
                oracleObjectTypes.add(OracleObjectType.FUNCTION);
            } else if (objectType == OracleObjectType.TABLE) {
                oracleObjectTypes.add(OracleObjectType.VIEW);
                oracleObjectTypes.add(OracleObjectType.MATERIALIZED_VIEW);
            }
        }
    }
    for (OracleObjectType objectType : oracleObjectTypes) {
        if (objectTypeClause.length() > 0)
            objectTypeClause.append(",");
        objectTypeClause.append("'").append(objectType.getTypeName()).append("'");
    }
    if (objectTypeClause.length() == 0) {
        return;
    }
    // Always search for synonyms
    objectTypeClause.append(",'").append(OracleObjectType.SYNONYM.getTypeName()).append("'");
    // Seek for objects (join with public synonyms)
    try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT " + OracleUtils.getSysCatalogHint((OracleDataSource) session.getDataSource()) + " DISTINCT OWNER,OBJECT_NAME,OBJECT_TYPE FROM (SELECT OWNER,OBJECT_NAME,OBJECT_TYPE FROM ALL_OBJECTS WHERE " + "OBJECT_TYPE IN (" + objectTypeClause + ") AND OBJECT_NAME LIKE ? " + (schema == null ? "" : " AND OWNER=?") + "UNION ALL\n" + "SELECT " + OracleUtils.getSysCatalogHint((OracleDataSource) session.getDataSource()) + " O.OWNER,O.OBJECT_NAME,O.OBJECT_TYPE\n" + "FROM ALL_SYNONYMS S,ALL_OBJECTS O\n" + "WHERE O.OWNER=S.TABLE_OWNER AND O.OBJECT_NAME=S.TABLE_NAME AND S.OWNER='PUBLIC' AND S.SYNONYM_NAME LIKE ?)" + "\nORDER BY OBJECT_NAME")) {
        if (!caseSensitive) {
            objectNameMask = objectNameMask.toUpperCase();
        }
        dbStat.setString(1, objectNameMask);
        if (schema != null) {
            dbStat.setString(2, schema.getName());
        }
        dbStat.setString(schema != null ? 3 : 2, objectNameMask);
        dbStat.setFetchSize(DBConstants.METADATA_FETCH_SIZE);
        try (JDBCResultSet dbResult = dbStat.executeQuery()) {
            while (objects.size() < maxResults && dbResult.next()) {
                if (session.getProgressMonitor().isCanceled()) {
                    break;
                }
                final String schemaName = JDBCUtils.safeGetString(dbResult, "OWNER");
                final String objectName = JDBCUtils.safeGetString(dbResult, "OBJECT_NAME");
                final String objectTypeName = JDBCUtils.safeGetString(dbResult, "OBJECT_TYPE");
                final OracleObjectType objectType = OracleObjectType.getByType(objectTypeName);
                if (objectType != null && objectType != OracleObjectType.SYNONYM && objectType.isBrowsable() && oracleObjectTypes.contains(objectType)) {
                    OracleSchema objectSchema = dataSource.getSchema(session.getProgressMonitor(), schemaName);
                    if (objectSchema == null) {
                        log.debug("Schema '" + schemaName + "' not found. Probably was filtered");
                        continue;
                    }
                    objects.add(new AbstractObjectReference(objectName, objectSchema, null, objectType.getTypeClass(), objectType) {

                        @Override
                        public DBSObject resolveObject(DBRProgressMonitor monitor) throws DBException {
                            OracleSchema tableSchema = (OracleSchema) getContainer();
                            DBSObject object = objectType.findObject(session.getProgressMonitor(), tableSchema, objectName);
                            if (object == null) {
                                throw new DBException(objectTypeName + " '" + objectName + "' not found in schema '" + tableSchema.getName() + "'");
                            }
                            return object;
                        }
                    });
                }
            }
        }
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) AbstractObjectReference(org.jkiss.dbeaver.model.impl.struct.AbstractObjectReference) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Example 45 with DBRProgressMonitor

use of org.jkiss.dbeaver.model.runtime.DBRProgressMonitor in project dbeaver by serge-rider.

the class OracleStructureAssistant method findConstraintsByMask.

private void findConstraintsByMask(JDBCSession session, final OracleSchema schema, String constrNameMask, DBSObjectType[] objectTypes, int maxResults, List<DBSObjectReference> objects) throws SQLException, DBException {
    DBRProgressMonitor monitor = session.getProgressMonitor();
    List<DBSObjectType> objectTypesList = Arrays.asList(objectTypes);
    final boolean hasFK = objectTypesList.contains(OracleObjectType.FOREIGN_KEY);
    final boolean hasConstraints = objectTypesList.contains(OracleObjectType.CONSTRAINT);
    // Load tables
    try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT " + OracleUtils.getSysCatalogHint((OracleDataSource) session.getDataSource()) + " OWNER, TABLE_NAME, CONSTRAINT_NAME, CONSTRAINT_TYPE\n" + "FROM SYS.ALL_CONSTRAINTS\n" + "WHERE CONSTRAINT_NAME like ?" + (!hasFK ? " AND CONSTRAINT_TYPE<>'R'" : "") + (schema != null ? " AND OWNER=?" : ""))) {
        dbStat.setString(1, constrNameMask);
        if (schema != null) {
            dbStat.setString(2, schema.getName());
        }
        try (JDBCResultSet dbResult = dbStat.executeQuery()) {
            int tableNum = maxResults;
            while (dbResult.next() && tableNum-- > 0) {
                if (monitor.isCanceled()) {
                    break;
                }
                final String schemaName = JDBCUtils.safeGetString(dbResult, OracleConstants.COL_OWNER);
                final String tableName = JDBCUtils.safeGetString(dbResult, OracleConstants.COL_TABLE_NAME);
                final String constrName = JDBCUtils.safeGetString(dbResult, OracleConstants.COL_CONSTRAINT_NAME);
                final String constrType = JDBCUtils.safeGetString(dbResult, OracleConstants.COL_CONSTRAINT_TYPE);
                final DBSEntityConstraintType type = OracleTableConstraint.getConstraintType(constrType);
                objects.add(new AbstractObjectReference(constrName, dataSource.getSchema(session.getProgressMonitor(), schemaName), null, type == DBSEntityConstraintType.FOREIGN_KEY ? OracleTableForeignKey.class : OracleTableConstraint.class, type == DBSEntityConstraintType.FOREIGN_KEY ? OracleObjectType.FOREIGN_KEY : OracleObjectType.CONSTRAINT) {

                    @Override
                    public DBSObject resolveObject(DBRProgressMonitor monitor) throws DBException {
                        OracleSchema tableSchema = schema != null ? schema : dataSource.getSchema(monitor, schemaName);
                        if (tableSchema == null) {
                            throw new DBException("Constraint schema '" + schemaName + "' not found");
                        }
                        OracleTable table = tableSchema.getTable(monitor, tableName);
                        if (table == null) {
                            throw new DBException("Constraint table '" + tableName + "' not found in catalog '" + tableSchema.getName() + "'");
                        }
                        DBSObject constraint = null;
                        if (hasFK && type == DBSEntityConstraintType.FOREIGN_KEY) {
                            constraint = table.getForeignKey(monitor, constrName);
                        }
                        if (hasConstraints && type != DBSEntityConstraintType.FOREIGN_KEY) {
                            constraint = table.getConstraint(monitor, constrName);
                        }
                        if (constraint == null) {
                            throw new DBException("Constraint '" + constrName + "' not found in table '" + table.getFullyQualifiedName(DBPEvaluationContext.DDL) + "'");
                        }
                        return constraint;
                    }
                });
            }
        }
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) AbstractObjectReference(org.jkiss.dbeaver.model.impl.struct.AbstractObjectReference) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Aggregations

DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)79 DBException (org.jkiss.dbeaver.DBException)45 InvocationTargetException (java.lang.reflect.InvocationTargetException)36 DBRRunnableWithProgress (org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress)30 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)14 JDBCResultSet (org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)12 AbstractJob (org.jkiss.dbeaver.model.runtime.AbstractJob)12 JDBCPreparedStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement)10 AbstractObjectReference (org.jkiss.dbeaver.model.impl.struct.AbstractObjectReference)10 ArrayList (java.util.ArrayList)8 IStatus (org.eclipse.core.runtime.IStatus)8 IJobChangeEvent (org.eclipse.core.runtime.jobs.IJobChangeEvent)6 JobChangeAdapter (org.eclipse.core.runtime.jobs.JobChangeAdapter)6 GridData (org.eclipse.swt.layout.GridData)6 DBNDatabaseNode (org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)6 List (java.util.List)5 NotNull (org.jkiss.code.NotNull)5 File (java.io.File)4 IOException (java.io.IOException)4 IFile (org.eclipse.core.resources.IFile)4