Search in sources :

Example 26 with DBRProgressMonitor

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

the class PostgreStructureAssistant method findTablesByMask.

private void findTablesByMask(JDBCSession session, @Nullable final List<PostgreSchema> schema, String tableNameMask, boolean caseSensitive, int maxResults, List<DBSObjectReference> objects) throws SQLException, DBException {
    DBRProgressMonitor monitor = session.getProgressMonitor();
    // Load tables
    try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT x.oid,x.relname,x.relnamespace,x.relkind FROM pg_catalog.pg_class x " + "WHERE x.relkind in('r','v','m') AND x.relname " + (caseSensitive ? "LIKE" : "ILIKE") + " ? " + (CommonUtils.isEmpty(schema) ? "" : " AND x.relnamespace IN (" + SQLUtils.generateParamList(schema.size()) + ")") + " ORDER BY x.relname LIMIT " + maxResults)) {
        dbStat.setString(1, tableNameMask);
        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, "relnamespace");
                final long tableId = JDBCUtils.safeGetLong(dbResult, "oid");
                final String tableName = JDBCUtils.safeGetString(dbResult, "relname");
                final PostgreClass.RelKind tableType = PostgreClass.RelKind.valueOf(JDBCUtils.safeGetString(dbResult, "relkind"));
                final PostgreSchema tableSchema = dataSource.getDefaultInstance().getSchema(session.getProgressMonitor(), schemaId);
                objects.add(new AbstractObjectReference(tableName, tableSchema, null, tableType == PostgreClass.RelKind.r ? PostgreTable.class : (tableType == PostgreClass.RelKind.v ? PostgreView.class : PostgreMaterializedView.class), RelationalObjectType.TYPE_TABLE) {

                    @Override
                    public DBSObject resolveObject(DBRProgressMonitor monitor) throws DBException {
                        PostgreTableBase table = tableSchema.getTable(monitor, tableId);
                        if (table == null) {
                            throw new DBException("Table '" + tableName + "' not found in schema '" + tableSchema.getName() + "'");
                        }
                        return table;
                    }
                });
            }
        }
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) 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 27 with DBRProgressMonitor

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

the class PostgreStructureAssistant method findConstraintsByMask.

private void findConstraintsByMask(JDBCSession session, @Nullable final List<PostgreSchema> schema, String constrNameMask, boolean caseSensitive, int maxResults, List<DBSObjectReference> objects) throws SQLException, DBException {
    DBRProgressMonitor monitor = session.getProgressMonitor();
    // Load constraints
    try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT x.oid,x.conname,x.connamespace FROM pg_catalog.pg_constraint x " + "WHERE x.conname " + (caseSensitive ? "LIKE" : "ILIKE") + " ? " + (CommonUtils.isEmpty(schema) ? "" : " AND x.connamespace IN (" + SQLUtils.generateParamList(schema.size()) + ")") + " ORDER BY x.conname LIMIT " + maxResults)) {
        dbStat.setString(1, constrNameMask);
        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, "connamespace");
                final long constrId = JDBCUtils.safeGetLong(dbResult, "oid");
                final String constrName = JDBCUtils.safeGetString(dbResult, "conname");
                final PostgreSchema constrSchema = dataSource.getDefaultInstance().getSchema(session.getProgressMonitor(), schemaId);
                objects.add(new AbstractObjectReference(constrName, constrSchema, null, PostgreTableConstraintBase.class, RelationalObjectType.TYPE_TABLE) {

                    @Override
                    public DBSObject resolveObject(DBRProgressMonitor monitor) throws DBException {
                        final PostgreTableConstraintBase constraint = PostgreUtils.getObjectById(monitor, constrSchema.constraintCache, constrSchema, constrId);
                        if (constraint == null) {
                            throw new DBException("Constraint '" + constrName + "' not found in schema '" + constrSchema.getName() + "'");
                        }
                        return constraint;
                    }
                });
            }
        }
    }
}
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 28 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 29 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 30 with DBRProgressMonitor

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

the class ResultSetViewer method createStatusBar.

private void createStatusBar() {
    UIUtils.createHorizontalLine(viewerPanel);
    statusBar = new Composite(viewerPanel, SWT.NONE);
    statusBar.setBackgroundMode(SWT.INHERIT_FORCE);
    GridData gd = new GridData(GridData.FILL_HORIZONTAL);
    statusBar.setLayoutData(gd);
    RowLayout toolbarsLayout = new RowLayout(SWT.HORIZONTAL);
    toolbarsLayout.marginTop = 0;
    toolbarsLayout.marginBottom = 0;
    toolbarsLayout.center = true;
    toolbarsLayout.wrap = true;
    toolbarsLayout.pack = true;
    //toolbarsLayout.fill = true;
    statusBar.setLayout(toolbarsLayout);
    {
        ToolBarManager editToolbar = new ToolBarManager(SWT.FLAT | SWT.HORIZONTAL | SWT.RIGHT);
        // handle own commands
        editToolbar.add(new Separator());
        editToolbar.add(ActionUtils.makeCommandContribution(site, ResultSetCommandHandler.CMD_APPLY_CHANGES, "Save", null, null, true));
        editToolbar.add(ActionUtils.makeCommandContribution(site, ResultSetCommandHandler.CMD_REJECT_CHANGES, "Cancel", null, null, true));
        editToolbar.add(ActionUtils.makeCommandContribution(site, ResultSetCommandHandler.CMD_GENERATE_SCRIPT, "Script", null, null, true));
        editToolbar.add(new Separator());
        editToolbar.add(ActionUtils.makeCommandContribution(site, ResultSetCommandHandler.CMD_ROW_EDIT));
        editToolbar.add(ActionUtils.makeCommandContribution(site, ResultSetCommandHandler.CMD_ROW_ADD));
        editToolbar.add(ActionUtils.makeCommandContribution(site, ResultSetCommandHandler.CMD_ROW_COPY));
        editToolbar.add(ActionUtils.makeCommandContribution(site, ResultSetCommandHandler.CMD_ROW_DELETE));
        editToolbar.createControl(statusBar);
        toolbarList.add(editToolbar);
    }
    {
        ToolBarManager navToolbar = new ToolBarManager(SWT.FLAT | SWT.HORIZONTAL | SWT.RIGHT);
        navToolbar.add(new Separator());
        navToolbar.add(ActionUtils.makeCommandContribution(site, ResultSetCommandHandler.CMD_ROW_FIRST));
        navToolbar.add(ActionUtils.makeCommandContribution(site, ResultSetCommandHandler.CMD_ROW_PREVIOUS));
        navToolbar.add(ActionUtils.makeCommandContribution(site, ResultSetCommandHandler.CMD_ROW_NEXT));
        navToolbar.add(ActionUtils.makeCommandContribution(site, ResultSetCommandHandler.CMD_ROW_LAST));
        navToolbar.add(new Separator());
        navToolbar.add(ActionUtils.makeCommandContribution(site, ResultSetCommandHandler.CMD_FETCH_PAGE));
        navToolbar.add(ActionUtils.makeCommandContribution(site, ResultSetCommandHandler.CMD_FETCH_ALL));
        navToolbar.createControl(statusBar);
        toolbarList.add(navToolbar);
    }
    {
        ToolBarManager configToolbar = new ToolBarManager(SWT.FLAT | SWT.HORIZONTAL | SWT.RIGHT);
        configToolbar.add(new Separator());
        {
            //configToolbar.add(new ToggleModeAction());
            ActionContributionItem item = new ActionContributionItem(new ToggleModeAction());
            item.setMode(ActionContributionItem.MODE_FORCE_TEXT);
            configToolbar.add(item);
        }
        {
            CommandContributionItemParameter ciParam = new CommandContributionItemParameter(site, "org.jkiss.dbeaver.core.resultset.panels", ResultSetCommandHandler.CMD_TOGGLE_PANELS, CommandContributionItem.STYLE_PULLDOWN);
            ciParam.label = "Panels";
            ciParam.mode = CommandContributionItem.MODE_FORCE_TEXT;
            configToolbar.add(new CommandContributionItem(ciParam));
        }
        configToolbar.add(new Separator());
        configToolbar.add(new ConfigAction());
        configToolbar.add(new Separator());
        configToolbar.createControl(statusBar);
        toolbarList.add(configToolbar);
    }
    {
        presentationSwitchToolbar = new ToolBar(statusBar, SWT.FLAT | SWT.HORIZONTAL | SWT.RIGHT);
        RowData rd = new RowData();
        rd.exclude = true;
        presentationSwitchToolbar.setLayoutData(rd);
    }
    {
        final int fontHeight = UIUtils.getFontHeight(statusBar);
        statusLabel = new StatusLabel(statusBar, SWT.NONE, this);
        statusLabel.setLayoutData(new RowData(40 * fontHeight, SWT.DEFAULT));
        rowCountLabel = new ActiveStatusMessage(statusBar, DBeaverIcons.getImage(UIIcon.RS_REFRESH), "Calculate total row count", this) {

            @Override
            protected boolean isActionEnabled() {
                return hasData() && isHasMoreData();
            }

            @Override
            protected ILoadService<String> createLoadService() {
                return new DatabaseLoadService<String>("Load row count", getExecutionContext()) {

                    @Override
                    public String evaluate(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                        try {
                            long rowCount = readRowCount(monitor);
                            return ROW_COUNT_FORMAT.format(rowCount);
                        } catch (DBException e) {
                            log.error(e);
                            return e.getMessage();
                        }
                    }
                };
            }
        };
        rowCountLabel.setLayoutData(new RowData(10 * fontHeight, SWT.DEFAULT));
        rowCountLabel.setMessage("Row Count");
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DatabaseLoadService(org.jkiss.dbeaver.model.runtime.load.DatabaseLoadService) RowData(org.eclipse.swt.layout.RowData) RowLayout(org.eclipse.swt.layout.RowLayout) CommandContributionItemParameter(org.eclipse.ui.menus.CommandContributionItemParameter) GridData(org.eclipse.swt.layout.GridData) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) CommandContributionItem(org.eclipse.ui.menus.CommandContributionItem)

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