Search in sources :

Example 21 with DBRProgressMonitor

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

the class PostgreStructureAssistant method findProceduresByMask.

private void findProceduresByMask(JDBCSession session, @Nullable final List<PostgreSchema> schema, String procNameMask, boolean caseSensitive, int maxResults, List<DBSObjectReference> objects) throws SQLException, DBException {
    DBRProgressMonitor monitor = session.getProgressMonitor();
    // Load procedures
    try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT DISTINCT x.oid,x.proname,x.pronamespace FROM pg_catalog.pg_proc x " + "WHERE x.proname " + (caseSensitive ? "LIKE" : "ILIKE") + " ? " + (CommonUtils.isEmpty(schema) ? "" : " AND x.pronamespace IN (" + SQLUtils.generateParamList(schema.size()) + ")") + " ORDER BY x.proname LIMIT " + maxResults)) {
        dbStat.setString(1, procNameMask);
        if (!CommonUtils.isEmpty(schema)) {
            PostgreUtils.setArrayParameter(dbStat, 2, schema);
        }
        try (JDBCResultSet dbResult = dbStat.executeQuery()) {
            int tableNum = maxResults;
            while (dbResult.next() && tableNum-- > 0) {
                if (monitor.isCanceled()) {
                    break;
                }
                final long schemaId = JDBCUtils.safeGetLong(dbResult, "pronamespace");
                final String procName = JDBCUtils.safeGetString(dbResult, "proname");
                final long procId = JDBCUtils.safeGetLong(dbResult, "oid");
                final PostgreSchema procSchema = dataSource.getDefaultInstance().getSchema(session.getProgressMonitor(), schemaId);
                objects.add(new AbstractObjectReference(procName, procSchema, null, PostgreProcedure.class, RelationalObjectType.TYPE_PROCEDURE) {

                    @Override
                    public DBSObject resolveObject(DBRProgressMonitor monitor) throws DBException {
                        PostgreProcedure procedure = procSchema.getProcedure(monitor, procId);
                        if (procedure == null) {
                            throw new DBException("Procedure '" + procName + "' not found in schema '" + procSchema.getName() + "'");
                        }
                        return procedure;
                    }
                });
            }
        }
    }
}
Also used : JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) DBException(org.jkiss.dbeaver.DBException) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) AbstractObjectReference(org.jkiss.dbeaver.model.impl.struct.AbstractObjectReference) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Example 22 with DBRProgressMonitor

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

the class PostgreBackupWizardPageObjects method loadTables.

private void loadTables(final PostgreSchema catalog) {
    if (catalog != null) {
        curSchema = catalog;
    }
    if (curSchema == null) {
        return;
    }
    final boolean isCatalogChecked = isChecked(curSchema);
    final Set<PostgreTableBase> checkedObjects = this.checkedObjects.get(curSchema);
    new AbstractJob("Load '" + curSchema.getName() + "' tables") {

        {
            setUser(true);
        }

        @Override
        protected IStatus run(DBRProgressMonitor monitor) {
            try {
                final List<PostgreTableBase> objects = new ArrayList<>();
                objects.addAll(curSchema.getTables(monitor));
                if (wizard.showViews) {
                    objects.addAll(curSchema.getViews(monitor));
                }
                Collections.sort(objects, DBUtils.nameComparator());
                DBeaverUI.syncExec(new Runnable() {

                    @Override
                    public void run() {
                        tablesTable.removeAll();
                        for (PostgreTableBase table : objects) {
                            TableItem item = new TableItem(tablesTable, SWT.NONE);
                            item.setImage(DBeaverIcons.getImage(table.isView() ? DBIcon.TREE_VIEW : DBIcon.TREE_TABLE));
                            item.setText(0, table.getName());
                            item.setData(table);
                            item.setChecked(isCatalogChecked && (checkedObjects == null || checkedObjects.contains(table)));
                        }
                    }
                });
            } catch (DBException e) {
                UIUtils.showErrorDialog(null, "Table list", "Can't read table list", e);
            }
            return Status.OK_STATUS;
        }
    }.schedule();
}
Also used : AbstractJob(org.jkiss.dbeaver.model.runtime.AbstractJob) DBException(org.jkiss.dbeaver.DBException) IStatus(org.eclipse.core.runtime.IStatus) PostgreTableBase(org.jkiss.dbeaver.ext.postgresql.model.PostgreTableBase) List(java.util.List) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Example 23 with DBRProgressMonitor

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

the class SSHTunnelImpl method closeTunnel.

@Override
public void closeTunnel(DBRProgressMonitor monitor) throws DBException, IOException {
    if (session != null) {
        RuntimeUtils.runTask(new DBRRunnableWithProgress() {

            @Override
            public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                try {
                    session.disconnect();
                } catch (Exception e) {
                    throw new InvocationTargetException(e);
                }
            }
        }, "Close SSH session", 1000);
        session = null;
    }
}
Also used : DBRRunnableWithProgress(org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBException(org.jkiss.dbeaver.DBException)

Example 24 with DBRProgressMonitor

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

the class SQLUtils method convertStreamToSQL.

public static String convertStreamToSQL(DBSAttributeBase attribute, DBDContent content, DBDValueHandler valueHandler, SQLDataSource dataSource) {
    try {
        DBRProgressMonitor monitor = VoidProgressMonitor.INSTANCE;
        if (valueHandler instanceof DBDContentValueHandler) {
            StringWriter buffer = new StringWriter();
            ((DBDContentValueHandler) valueHandler).writeStreamValue(monitor, dataSource, attribute, content, buffer);
            return buffer.toString();
        } else {
            if (ContentUtils.isTextContent(content)) {
                String strValue = ContentUtils.getContentStringValue(monitor, content);
                strValue = dataSource.getSQLDialect().escapeString(strValue);
                return "'" + strValue + "'";
            } else {
                byte[] binValue = ContentUtils.getContentBinaryValue(monitor, content);
                return dataSource.getSQLDialect().getNativeBinaryFormatter().toString(binValue, 0, binValue.length);
            }
        }
    } catch (Throwable e) {
        log.warn(e);
        return SQLConstants.NULL_VALUE;
    }
}
Also used : StringWriter(java.io.StringWriter) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Example 25 with DBRProgressMonitor

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

the class CompareObjectsWizard method performFinish.

@Override
public boolean performFinish() {
    // Save settings
    getSettings().saveTo(getDialogSettings());
    showError(null);
    // Compare
    final CompareObjectsExecutor executor = new CompareObjectsExecutor(settings);
    try {
        DBeaverUI.run(getContainer(), true, true, new DBRRunnableWithProgress() {

            @Override
            public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                try {
                    CompareReport report = generateReport(monitor, executor);
                    renderReport(monitor, report);
                } catch (DBException e) {
                    throw new InvocationTargetException(e);
                }
            }
        });
        UIUtils.showMessageBox(getShell(), "Objects compare", "Objects compare finished", SWT.ICON_INFORMATION);
    } catch (InvocationTargetException e) {
        if (executor.getInitializeError() != null) {
            showError(executor.getInitializeError().getMessage());
        } else {
            log.error(e.getTargetException());
            showError(e.getTargetException().getMessage());
        }
        return false;
    } catch (InterruptedException e) {
        showError("Compare interrupted");
        return false;
    } finally {
        executor.dispose();
    }
    // Done
    return true;
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBRRunnableWithProgress(org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) InvocationTargetException(java.lang.reflect.InvocationTargetException)

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