Search in sources :

Example 31 with DBSObject

use of org.jkiss.dbeaver.model.struct.DBSObject in project dbeaver by dbeaver.

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 32 with DBSObject

use of org.jkiss.dbeaver.model.struct.DBSObject in project dbeaver by dbeaver.

the class DataSourceManagementToolbar method changeDataBaseSelection.

private void changeDataBaseSelection(DBNDatabaseNode node) {
    DBPDataSourceContainer dsContainer = getDataSourceContainer();
    final String newName = node.getNodeName();
    if (dsContainer != null && dsContainer.isConnected()) {
        final DBPDataSource dataSource = dsContainer.getDataSource();
        new AbstractJob("Change active database") {

            @Override
            protected IStatus run(DBRProgressMonitor monitor) {
                try {
                    DBSObjectContainer oc = DBUtils.getAdapter(DBSObjectContainer.class, dataSource);
                    DBSObjectSelector os = DBUtils.getAdapter(DBSObjectSelector.class, dataSource);
                    if (os != null) {
                        final DBSObject defObject = os.getDefaultObject();
                        if (defObject instanceof DBSObjectContainer) {
                            // USe seconds level of active object
                            DBSObjectSelector os2 = DBUtils.getAdapter(DBSObjectSelector.class, defObject);
                            if (os2 != null && os2.supportsDefaultChange()) {
                                oc = (DBSObjectContainer) defObject;
                                os = os2;
                            }
                        }
                    }
                    if (oc != null && os != null && os.supportsDefaultChange()) {
                        DBSObject newChild = oc.getChild(monitor, newName);
                        if (newChild != null) {
                            os.setDefaultObject(monitor, newChild);
                        } else {
                            throw new DBException(MessageFormat.format(CoreMessages.toolbar_datasource_selector_error_database_not_found, newName));
                        }
                    } else {
                        throw new DBException(CoreMessages.toolbar_datasource_selector_error_database_change_not_supported);
                    }
                } catch (DBException e) {
                    return GeneralUtils.makeExceptionStatus(e);
                }
                return Status.OK_STATUS;
            }
        }.schedule();
    }
}
Also used : AbstractJob(org.jkiss.dbeaver.model.runtime.AbstractJob) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBException(org.jkiss.dbeaver.DBException) IStatus(org.eclipse.core.runtime.IStatus) DBSObjectContainer(org.jkiss.dbeaver.model.struct.DBSObjectContainer) DBSObjectSelector(org.jkiss.dbeaver.model.struct.DBSObjectSelector) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Example 33 with DBSObject

use of org.jkiss.dbeaver.model.struct.DBSObject in project dbeaver by dbeaver.

the class OpenHandler method getDataSourceContainers.

private static List<DBPDataSourceContainer> getDataSourceContainers(ExecutionEvent event) {
    List<DBPDataSourceContainer> containers = new ArrayList<>();
    ISelection selection = HandlerUtil.getCurrentSelection(event);
    if (selection instanceof IStructuredSelection) {
        for (Object obj : ((IStructuredSelection) selection).toArray()) {
            if (obj instanceof DBNLocalFolder) {
                for (DBNDataSource ds : ((DBNLocalFolder) obj).getDataSources()) {
                    containers.add(ds.getDataSourceContainer());
                }
            } else {
                DBSObject selectedObject = DBUtils.getFromObject(obj);
                if (selectedObject != null) {
                    if (selectedObject instanceof DBPDataSourceContainer) {
                        containers.add((DBPDataSourceContainer) selectedObject);
                    } else {
                        containers.add(selectedObject.getDataSource().getContainer());
                    }
                }
            }
        }
    }
    if (containers.isEmpty()) {
        IWorkbenchPart activePart = HandlerUtil.getActivePart(event);
        DBPDataSourceContainer partContainer = getDataSourceContainers(activePart);
        if (partContainer != null) {
            containers.add(partContainer);
        }
    }
    return containers;
}
Also used : DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBNLocalFolder(org.jkiss.dbeaver.model.navigator.DBNLocalFolder) IWorkbenchPart(org.eclipse.ui.IWorkbenchPart) DBNDataSource(org.jkiss.dbeaver.model.navigator.DBNDataSource) ArrayList(java.util.ArrayList) ISelection(org.eclipse.jface.viewers.ISelection) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer)

Example 34 with DBSObject

use of org.jkiss.dbeaver.model.struct.DBSObject in project dbeaver by dbeaver.

the class AbstractDataSourceHandler method getDataSourceContainer.

protected static DBPDataSourceContainer getDataSourceContainer(ExecutionEvent event, boolean useEditor) {
    if (useEditor) {
        IEditorPart editor = HandlerUtil.getActiveEditor(event);
        if (editor != null) {
            DBPDataSourceContainer container = getDataSourceContainer(editor);
            if (container != null) {
                return container;
            }
        }
        return null;
    }
    IWorkbenchPart activePart = HandlerUtil.getActivePart(event);
    DBPDataSourceContainer container = getDataSourceContainer(activePart);
    if (container != null) {
        return container;
    }
    ISelection selection = HandlerUtil.getCurrentSelection(event);
    DBSObject selectedObject = NavigatorUtils.getSelectedObject(selection);
    if (selectedObject instanceof DBPDataSourceContainer) {
        return (DBPDataSourceContainer) selectedObject;
    } else if (selectedObject != null) {
        DBPDataSource dataSource = selectedObject.getDataSource();
        return dataSource == null ? null : dataSource.getContainer();
    }
    return null;
}
Also used : DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) IWorkbenchPart(org.eclipse.ui.IWorkbenchPart) ISelection(org.eclipse.jface.viewers.ISelection) IEditorPart(org.eclipse.ui.IEditorPart) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer)

Example 35 with DBSObject

use of org.jkiss.dbeaver.model.struct.DBSObject in project dbeaver by dbeaver.

the class NavigatorHandlerFilterConfig method configureFilters.

public static void configureFilters(Shell shell, DBNNode node) {
    final DBNDatabaseFolder folder = (DBNDatabaseFolder) node;
    DBXTreeItem itemsMeta = folder.getItemsMeta();
    if (itemsMeta != null) {
        DBSObjectFilter objectFilter = folder.getNodeFilter(itemsMeta, true);
        if (objectFilter == null) {
            objectFilter = new DBSObjectFilter();
        }
        final DBPDataSourceRegistry dsRegistry = DBeaverCore.getInstance().getProjectManager().getDataSourceRegistry(folder.getOwnerProject());
        final boolean globalFilter = folder.getValueObject() instanceof DBPDataSource;
        String parentName = "?";
        if (folder.getValueObject() instanceof DBSObject) {
            parentName = ((DBSObject) folder.getValueObject()).getName();
        }
        EditObjectFilterDialog dialog = new EditObjectFilterDialog(shell, dsRegistry, globalFilter ? "All " + node.getNodeType() : node.getNodeType() + " of " + parentName, objectFilter, globalFilter);
        switch(dialog.open()) {
            case IDialogConstants.OK_ID:
                folder.setNodeFilter(itemsMeta, dialog.getFilter());
                NavigatorHandlerRefresh.refreshNavigator(Collections.singletonList(folder));
                break;
            case EditObjectFilterDialog.SHOW_GLOBAL_FILTERS_ID:
                objectFilter = folder.getDataSource().getContainer().getObjectFilter(folder.getChildrenClass(), null, false);
                dialog = new EditObjectFilterDialog(shell, dsRegistry, "All " + node.getNodeType(), objectFilter != null ? objectFilter : new DBSObjectFilter(), true);
                if (dialog.open() == IDialogConstants.OK_ID) {
                    // Set global filter
                    folder.getDataSource().getContainer().setObjectFilter(folder.getChildrenClass(), null, dialog.getFilter());
                    folder.getDataSource().getContainer().persistConfiguration();
                    NavigatorHandlerRefresh.refreshNavigator(Collections.singletonList(folder));
                }
                break;
        }
    }
}
Also used : DBSObjectFilter(org.jkiss.dbeaver.model.struct.DBSObjectFilter) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBXTreeItem(org.jkiss.dbeaver.model.navigator.meta.DBXTreeItem) EditObjectFilterDialog(org.jkiss.dbeaver.ui.dialogs.connection.EditObjectFilterDialog) DBPDataSourceRegistry(org.jkiss.dbeaver.model.app.DBPDataSourceRegistry) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) DBNDatabaseFolder(org.jkiss.dbeaver.model.navigator.DBNDatabaseFolder)

Aggregations

DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)374 DBException (org.jkiss.dbeaver.DBException)129 DBNDatabaseNode (org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)66 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)66 ArrayList (java.util.ArrayList)54 InvocationTargetException (java.lang.reflect.InvocationTargetException)52 ISelection (org.eclipse.jface.viewers.ISelection)46 JDBCResultSet (org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)46 AbstractObjectReference (org.jkiss.dbeaver.model.impl.struct.AbstractObjectReference)44 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)38 GridData (org.eclipse.swt.layout.GridData)38 JDBCPreparedStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement)38 DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)30 DBPDataSourceContainer (org.jkiss.dbeaver.model.DBPDataSourceContainer)29 DBNNode (org.jkiss.dbeaver.model.navigator.DBNNode)29 DBSObjectContainer (org.jkiss.dbeaver.model.struct.DBSObjectContainer)28 Composite (org.eclipse.swt.widgets.Composite)27 IEditorPart (org.eclipse.ui.IEditorPart)26 DBCExecutionContext (org.jkiss.dbeaver.model.exec.DBCExecutionContext)24 IWorkbenchPart (org.eclipse.ui.IWorkbenchPart)22