Search in sources :

Example 41 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 {
    boolean showAllSchemas = SQLServerUtils.isShowAllSchemas(dataSource);
    final DBSObjectFilter schemaFilters = dataSource.getContainer().getObjectFilter(GenericSchema.class, catalog, false);
    String sysSchema = SQLServerUtils.getSystemSchemaFQN(dataSource, catalog.getName(), getSystemSchema());
    String sql;
    if (showAllSchemas) {
        if (getServerType() == ServerType.SQL_SERVER && dataSource.isServerVersionAtLeast(SQLServerConstants.SQL_SERVER_2005_VERSION_MAJOR, 0)) {
            sql = "SELECT * FROM " + sysSchema + ".schemas";
        } else {
            sql = "SELECT * FROM " + sysSchema + ".sysusers";
        }
    } else {
        if (getServerType() == ServerType.SQL_SERVER) {
            sql = "SELECT DISTINCT s.*\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,u.uid\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, "name");
                if (name == null) {
                    continue;
                }
                name = name.trim();
                if (schemaFilters != null && !schemaFilters.matches(name)) {
                    // Doesn't match filter
                    continue;
                }
                long schemaId = JDBCUtils.safeGetLong(dbResult, "schema_id");
                SQLServerGenericSchema schema = new SQLServerGenericSchema(dataSource, catalog, name, schemaId);
                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 42 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 43 with DBSObjectFilter

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

the class FilterMapping method getFilter.

@Nullable
DBSObjectFilter getFilter(@Nullable DBSObject parentObject, boolean firstMatch) {
    if (parentObject == null) {
        return defaultFilter;
    }
    if (!customFilters.isEmpty()) {
        String objectID = getFilterContainerUniqueID(parentObject);
        DBSObjectFilter filter = customFilters.get(objectID);
        if ((filter != null && !filter.isNotApplicable()) || firstMatch) {
            return filter;
        }
    }
    return firstMatch ? null : defaultFilter;
}
Also used : DBSObjectFilter(org.jkiss.dbeaver.model.struct.DBSObjectFilter) Nullable(org.jkiss.code.Nullable)

Example 44 with DBSObjectFilter

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

the class DataSourceRegistry method updateSavedFilter.

@Override
public void updateSavedFilter(DBSObjectFilter filter) {
    DBSObjectFilter filterCopy = new DBSObjectFilter(filter);
    for (int i = 0; i < savedFilters.size(); i++) {
        if (CommonUtils.equalObjects(savedFilters.get(i).getName(), filter.getName())) {
            savedFilters.set(i, filterCopy);
            return;
        }
    }
    savedFilters.add(filterCopy);
}
Also used : DBSObjectFilter(org.jkiss.dbeaver.model.struct.DBSObjectFilter)

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