Search in sources :

Example 1 with DBSObjectFilter

use of org.jkiss.dbeaver.model.struct.DBSObjectFilter in project dbeaver by serge-rider.

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();
        }
        boolean globalFilter = folder.getValueObject() instanceof DBPDataSource;
        String parentName = "?";
        if (folder.getValueObject() instanceof DBSObject) {
            parentName = ((DBSObject) folder.getValueObject()).getName();
        }
        EditObjectFilterDialog dialog = new EditObjectFilterDialog(shell, 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, "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) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) DBNDatabaseFolder(org.jkiss.dbeaver.model.navigator.DBNDatabaseFolder)

Example 2 with DBSObjectFilter

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

the class SQLServerMetaModel method loadSchemas.

@Override
public List<GenericSchema> loadSchemas(JDBCSession session, GenericDataSource dataSource, GenericCatalog catalog) throws DBException {
    if (catalog == null) {
        // Schemas MUST be in catalog
        return null;
    }
    boolean showAllSchemas = ((SQLServerDataSource) dataSource).isShowAllSchemas();
    final DBSObjectFilter schemaFilters = dataSource.getContainer().getObjectFilter(GenericSchema.class, catalog, false);
    String sysSchema = getSystemSchemaFQN(dataSource, catalog);
    String sql;
    if (showAllSchemas) {
        if (getServerType() == ServerType.SQL_SERVER && dataSource.isServerVersionAtLeast(SQLServerConstants.SQL_SERVER_2005_VERSION_MAJOR, 0)) {
            sql = "SELECT name FROM " + sysSchema + ".schemas";
        } else {
            sql = "SELECT name FROM " + sysSchema + ".sysusers";
        }
    } else {
        if (getServerType() == ServerType.SQL_SERVER) {
            sql = "SELECT DISTINCT s.name\n" + "FROM " + sysSchema + ".schemas s, " + sysSchema + ".sysobjects o\n" + "WHERE s.schema_id=o.uid\n" + "ORDER BY 1";
        } else {
            sql = "SELECT DISTINCT u.name\n" + "FROM " + sysSchema + ".sysusers u, " + sysSchema + ".sysobjects o\n" + "WHERE u.uid=o.uid\n" + "ORDER BY 1";
        }
    }
    boolean schemaReadFailed = false;
    List<GenericSchema> result = new ArrayList<>();
    try (JDBCPreparedStatement dbStat = session.prepareStatement(sql)) {
        try (JDBCResultSet dbResult = dbStat.executeQuery()) {
            while (dbResult.next()) {
                String name = JDBCUtils.safeGetString(dbResult, 1);
                if (name == null) {
                    continue;
                }
                name = name.trim();
                if (schemaFilters != null && !schemaFilters.matches(name)) {
                    // Doesn't match filter
                    continue;
                }
                SQLServerSchema schema = createSchemaImpl(dataSource, catalog, name);
                result.add(schema);
            }
        }
    } catch (SQLException e) {
        if (dataSource.discoverErrorType(e) == DBPErrorAssistant.ErrorType.CONNECTION_LOST) {
            throw new DBException(e, dataSource);
        } else {
            log.warn("Schema read failed: empty list returned. Try generic method.", e);
            schemaReadFailed = true;
        }
    }
    if (result.isEmpty()) {
        if (!schemaReadFailed && !showAllSchemas) {
            // Perhaps all schemas were filtered out
            result.add(new GenericSchema(dataSource, catalog, SQLServerConstants.DEFAULT_SCHEMA_NAME));
        } else {
            // Maybe something went wrong. LEt's try to use native function
            return super.loadSchemas(session, dataSource, catalog);
        }
    }
    return result;
}
Also used : JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) DBSObjectFilter(org.jkiss.dbeaver.model.struct.DBSObjectFilter) DBException(org.jkiss.dbeaver.DBException) SQLException(java.sql.SQLException) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) ArrayList(java.util.ArrayList)

Example 3 with DBSObjectFilter

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

the class NavigatorHandlerFilterClear method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    final ISelection selection = HandlerUtil.getCurrentSelection(event);
    DBNNode node = NavigatorUtils.getSelectedNode(selection);
    if (node instanceof DBNDatabaseItem) {
        node = node.getParentNode();
    }
    if (node instanceof DBNDatabaseFolder) {
        final DBNDatabaseFolder folder = (DBNDatabaseFolder) node;
        DBXTreeItem itemsMeta = folder.getItemsMeta();
        if (itemsMeta != null) {
            folder.setNodeFilter(itemsMeta, new DBSObjectFilter());
            NavigatorHandlerRefresh.refreshNavigator(Collections.singleton(folder));
        }
    }
    return null;
}
Also used : DBNDatabaseItem(org.jkiss.dbeaver.model.navigator.DBNDatabaseItem) DBSObjectFilter(org.jkiss.dbeaver.model.struct.DBSObjectFilter) DBXTreeItem(org.jkiss.dbeaver.model.navigator.meta.DBXTreeItem) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) ISelection(org.eclipse.jface.viewers.ISelection) DBNDatabaseFolder(org.jkiss.dbeaver.model.navigator.DBNDatabaseFolder)

Example 4 with DBSObjectFilter

use of org.jkiss.dbeaver.model.struct.DBSObjectFilter 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)

Example 5 with DBSObjectFilter

use of org.jkiss.dbeaver.model.struct.DBSObjectFilter in project dbeaver by serge-rider.

the class NavigatorUtils method filterSelection.

public static void filterSelection(final ISelection selection, boolean exclude) {
    if (selection instanceof IStructuredSelection) {
        Map<DBNDatabaseFolder, DBSObjectFilter> folders = new HashMap<>();
        for (Object item : ((IStructuredSelection) selection).toArray()) {
            if (item instanceof DBNNode) {
                final DBNNode node = (DBNNode) item;
                DBNDatabaseFolder folder = (DBNDatabaseFolder) node.getParentNode();
                DBSObjectFilter nodeFilter = folders.get(folder);
                if (nodeFilter == null) {
                    nodeFilter = folder.getNodeFilter(folder.getItemsMeta(), true);
                    if (nodeFilter == null) {
                        nodeFilter = new DBSObjectFilter();
                    }
                    folders.put(folder, nodeFilter);
                }
                if (exclude) {
                    nodeFilter.addExclude(node.getNodeName());
                } else {
                    nodeFilter.addInclude(node.getNodeName());
                }
                nodeFilter.setEnabled(true);
            }
        }
        // Save folders
        for (Map.Entry<DBNDatabaseFolder, DBSObjectFilter> entry : folders.entrySet()) {
            entry.getKey().setNodeFilter(entry.getKey().getItemsMeta(), entry.getValue());
        }
        // Refresh all folders
        NavigatorHandlerRefresh.refreshNavigator(folders.keySet());
    }
}
Also used : DBSObjectFilter(org.jkiss.dbeaver.model.struct.DBSObjectFilter) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject)

Aggregations

DBSObjectFilter (org.jkiss.dbeaver.model.struct.DBSObjectFilter)44 DBNDatabaseFolder (org.jkiss.dbeaver.model.navigator.DBNDatabaseFolder)12 DBXTreeItem (org.jkiss.dbeaver.model.navigator.meta.DBXTreeItem)12 DBException (org.jkiss.dbeaver.DBException)10 ArrayList (java.util.ArrayList)9 ISelection (org.eclipse.jface.viewers.ISelection)8 DBNDatabaseItem (org.jkiss.dbeaver.model.navigator.DBNDatabaseItem)8 DBNNode (org.jkiss.dbeaver.model.navigator.DBNNode)8 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)8 DBPDataSourceRegistry (org.jkiss.dbeaver.model.app.DBPDataSourceRegistry)7 JDBCResultSet (org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)6 List (java.util.List)5 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)5 SelectionEvent (org.eclipse.swt.events.SelectionEvent)5 GridData (org.eclipse.swt.layout.GridData)5 TypeToken (com.google.gson.reflect.TypeToken)4 SQLException (java.sql.SQLException)4 NotNull (org.jkiss.code.NotNull)4 Nullable (org.jkiss.code.Nullable)4 DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)4