Search in sources :

Example 1 with DBSObjectType

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

the class DB2StructureAssistant method findObjectsByMask.

@NotNull
@Override
public List<DBSObjectReference> findObjectsByMask(DBRProgressMonitor monitor, DBSObject parentObject, DBSObjectType[] objectTypes, String objectNameMask, boolean caseSensitive, boolean globalSearch, int maxResults) throws DBException {
    LOG.debug(objectNameMask);
    List<DB2ObjectType> db2ObjectTypes = new ArrayList<>(objectTypes.length);
    for (DBSObjectType dbsObjectType : objectTypes) {
        db2ObjectTypes.add((DB2ObjectType) dbsObjectType);
    }
    DB2Schema schema = parentObject instanceof DB2Schema ? (DB2Schema) parentObject : null;
    if (schema == null && !globalSearch) {
        schema = dataSource.getDefaultObject();
    }
    try (JDBCSession session = DBUtils.openMetaSession(monitor, dataSource, "Find objects by name")) {
        return searchAllObjects(session, schema, objectNameMask, db2ObjectTypes, caseSensitive, maxResults);
    } catch (SQLException ex) {
        throw new DBException(ex, dataSource);
    }
}
Also used : JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) DBException(org.jkiss.dbeaver.DBException) SQLException(java.sql.SQLException) DBSObjectType(org.jkiss.dbeaver.model.struct.DBSObjectType) ArrayList(java.util.ArrayList) DB2Schema(org.jkiss.dbeaver.ext.db2.model.DB2Schema) NotNull(org.jkiss.code.NotNull)

Example 2 with DBSObjectType

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

the class DB2StructureAssistant method findObjectsByMask.

@NotNull
@Override
public List<DBSObjectReference> findObjectsByMask(DBRProgressMonitor monitor, DBSObject parentObject, DBSObjectType[] objectTypes, String objectNameMask, boolean caseSensitive, boolean globalSearch, int maxResults) throws DBException {
    List<DB2ObjectType> db2ObjectTypes = new ArrayList<>(objectTypes.length);
    for (DBSObjectType dbsObjectType : objectTypes) {
        db2ObjectTypes.add((DB2ObjectType) dbsObjectType);
    }
    DB2Schema schema = parentObject instanceof DB2Schema ? (DB2Schema) parentObject : null;
    if (schema == null && !globalSearch) {
        schema = dataSource.getDefaultObject();
    }
    try (JDBCSession session = DBUtils.openMetaSession(monitor, dataSource, "Find objects by name")) {
        return searchAllObjects(session, schema, objectNameMask, db2ObjectTypes, caseSensitive, maxResults);
    } catch (SQLException ex) {
        throw new DBException(ex, dataSource);
    }
}
Also used : JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) DBException(org.jkiss.dbeaver.DBException) SQLException(java.sql.SQLException) DBSObjectType(org.jkiss.dbeaver.model.struct.DBSObjectType) ArrayList(java.util.ArrayList) DB2Schema(org.jkiss.dbeaver.ext.db2.model.DB2Schema) NotNull(org.jkiss.code.NotNull)

Example 3 with DBSObjectType

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

the class SearchMetadataQuery method run.

@Override
public IStatus run(IProgressMonitor monitor) throws OperationCanceledException {
    try {
        List<DBSObjectType> objectTypes = params.getObjectTypes();
        String objectNameMask = params.getObjectNameMask();
        if (params.getMatchType() == SearchMetadataConstants.MATCH_INDEX_STARTS_WITH) {
            if (!objectNameMask.endsWith("%")) {
                // $NON-NLS-1$
                // $NON-NLS-1$
                objectNameMask = objectNameMask + "%";
            }
        } else if (params.getMatchType() == SearchMetadataConstants.MATCH_INDEX_CONTAINS) {
            if (!objectNameMask.startsWith("%")) {
                // $NON-NLS-1$
                // $NON-NLS-1$
                objectNameMask = "%" + objectNameMask;
            }
            if (!objectNameMask.endsWith("%")) {
                // $NON-NLS-1$
                // $NON-NLS-1$
                objectNameMask = objectNameMask + "%";
            }
        }
        int totalObjects = 0;
        DBNModel navigatorModel = DBWorkbench.getPlatform().getNavigatorModel();
        DBRProgressMonitor localMonitor = RuntimeUtils.makeMonitor(monitor);
        Collection<DBSObjectReference> objects = structureAssistant.findObjectsByMask(localMonitor, executionContext, params.getParentObject(), objectTypes.toArray(new DBSObjectType[0]), objectNameMask, params.isCaseSensitive(), true, params.getMaxResults());
        for (DBSObjectReference reference : objects) {
            if (monitor.isCanceled()) {
                break;
            }
            try {
                DBSObject object = reference.resolveObject(localMonitor);
                if (object != null) {
                    DBNNode node = navigatorModel.getNodeByObject(localMonitor, object, false);
                    if (node != null) {
                        searchResult.addObjects(Collections.singletonList(node));
                        totalObjects++;
                    }
                }
            } catch (DBException e) {
                log.error(e);
            }
        }
        searchResult.fireChange(new AbstractSearchResult.DatabaseSearchFinishEvent(searchResult, totalObjects));
        return Status.OK_STATUS;
    } catch (DBException e) {
        log.debug(e);
        return GeneralUtils.makeExceptionStatus(e);
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) DBSObjectType(org.jkiss.dbeaver.model.struct.DBSObjectType) DBNModel(org.jkiss.dbeaver.model.navigator.DBNModel) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBSObjectReference(org.jkiss.dbeaver.model.struct.DBSObjectReference) AbstractSearchResult(org.jkiss.dbeaver.ui.search.AbstractSearchResult) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Example 4 with DBSObjectType

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

the class DB2StructureAssistant method findObjectsByMask.

@NotNull
@Override
public List<DBSObjectReference> findObjectsByMask(@NotNull DBRProgressMonitor monitor, @NotNull DB2ExecutionContext executionContext, DBSObject parentObject, DBSObjectType[] objectTypes, String objectNameMask, boolean caseSensitive, boolean globalSearch, int maxResults) throws DBException {
    List<DB2ObjectType> db2ObjectTypes = new ArrayList<>(objectTypes.length);
    for (DBSObjectType dbsObjectType : objectTypes) {
        db2ObjectTypes.add((DB2ObjectType) dbsObjectType);
    }
    DB2Schema schema = parentObject instanceof DB2Schema ? (DB2Schema) parentObject : null;
    if (schema == null && !globalSearch) {
        schema = executionContext.getContextDefaults().getDefaultSchema();
    }
    try (JDBCSession session = executionContext.openSession(monitor, DBCExecutionPurpose.META, "Find objects by name")) {
        return searchAllObjects(session, schema, objectNameMask, db2ObjectTypes, caseSensitive, maxResults);
    } catch (SQLException ex) {
        throw new DBException(ex, dataSource);
    }
}
Also used : JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) DBException(org.jkiss.dbeaver.DBException) SQLException(java.sql.SQLException) DBSObjectType(org.jkiss.dbeaver.model.struct.DBSObjectType) ArrayList(java.util.ArrayList) NotNull(org.jkiss.code.NotNull)

Example 5 with DBSObjectType

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

the class SQLServerStructureAssistant method searchAllObjects.

private void searchAllObjects(final JDBCSession session, final SQLServerDatabase database, final SQLServerSchema schema, String objectNameMask, DBSObjectType[] objectTypes, boolean caseSensitive, int maxResults, List<DBSObjectReference> objects) throws DBException {
    final List<SQLServerObjectType> supObjectTypes = new ArrayList<>(objectTypes.length + 2);
    for (DBSObjectType objectType : objectTypes) {
        if (objectType instanceof SQLServerObjectType) {
            supObjectTypes.add((SQLServerObjectType) objectType);
        } else if (objectType == RelationalObjectType.TYPE_PROCEDURE) {
            supObjectTypes.addAll(SQLServerObjectType.getTypesForClass(SQLServerProcedure.class));
        } else if (objectType == RelationalObjectType.TYPE_TABLE) {
            supObjectTypes.addAll(SQLServerObjectType.getTypesForClass(SQLServerTable.class));
        } else if (objectType == RelationalObjectType.TYPE_CONSTRAINT) {
            supObjectTypes.addAll(SQLServerObjectType.getTypesForClass(SQLServerTableCheckConstraint.class));
            supObjectTypes.addAll(SQLServerObjectType.getTypesForClass(SQLServerTableForeignKey.class));
        } else if (objectType == RelationalObjectType.TYPE_VIEW) {
            supObjectTypes.addAll(SQLServerObjectType.getTypesForClass(SQLServerView.class));
        }
    }
    if (supObjectTypes.isEmpty()) {
        return;
    }
    StringBuilder objectTypeClause = new StringBuilder(100);
    for (SQLServerObjectType objectType : supObjectTypes) {
        if (objectTypeClause.length() > 0)
            objectTypeClause.append(",");
        objectTypeClause.append("'").append(objectType.getTypeID()).append("'");
    }
    if (objectTypeClause.length() == 0) {
        return;
    }
    // Seek for objects (join with public synonyms)
    try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT * FROM " + SQLServerUtils.getSystemTableName(database, "all_objects") + " o " + "\nWHERE o.type IN (" + objectTypeClause.toString() + ") AND o.name LIKE ?" + (schema == null ? "" : " AND o.schema_id=? ") + "\nORDER BY o.name")) {
        dbStat.setString(1, objectNameMask);
        if (schema != null) {
            dbStat.setLong(2, schema.getObjectId());
        }
        dbStat.setFetchSize(DBConstants.METADATA_FETCH_SIZE);
        try (JDBCResultSet dbResult = dbStat.executeQuery()) {
            while (objects.size() < maxResults && dbResult.next()) {
                if (session.getProgressMonitor().isCanceled()) {
                    break;
                }
                final long schemaId = JDBCUtils.safeGetLong(dbResult, "schema_id");
                final String objectName = JDBCUtils.safeGetString(dbResult, "name");
                final String objectTypeName = JDBCUtils.safeGetStringTrimmed(dbResult, "type");
                final SQLServerObjectType objectType = SQLServerObjectType.valueOf(objectTypeName);
                {
                    SQLServerSchema objectSchema = schemaId == 0 ? null : database.getSchema(session.getProgressMonitor(), schemaId);
                    objects.add(new AbstractObjectReference(objectName, objectSchema != null ? objectSchema : database, null, objectType.getTypeClass(), objectType) {

                        @Override
                        public DBSObject resolveObject(DBRProgressMonitor monitor) throws DBException {
                            DBSObject object = objectType.findObject(session.getProgressMonitor(), database, objectSchema, objectName);
                            if (object == null) {
                                throw new DBException(objectTypeName + " '" + objectName + "' not found");
                            }
                            return object;
                        }
                    });
                }
            }
        }
    } catch (Throwable e) {
        throw new DBException("Error while searching in system catalog", e, dataSource);
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBSObjectType(org.jkiss.dbeaver.model.struct.DBSObjectType) ArrayList(java.util.ArrayList) 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)

Aggregations

DBException (org.jkiss.dbeaver.DBException)10 DBSObjectType (org.jkiss.dbeaver.model.struct.DBSObjectType)10 ArrayList (java.util.ArrayList)8 SQLException (java.sql.SQLException)6 NotNull (org.jkiss.code.NotNull)6 JDBCSession (org.jkiss.dbeaver.model.exec.jdbc.JDBCSession)6 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)4 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)4 DB2Schema (org.jkiss.dbeaver.ext.db2.model.DB2Schema)2 ExasolObjectType (org.jkiss.dbeaver.ext.exasol.editors.ExasolObjectType)2 JDBCPreparedStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement)2 JDBCResultSet (org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)2 AbstractObjectReference (org.jkiss.dbeaver.model.impl.struct.AbstractObjectReference)2 DBNModel (org.jkiss.dbeaver.model.navigator.DBNModel)2 DBNNode (org.jkiss.dbeaver.model.navigator.DBNNode)2 DBSObjectReference (org.jkiss.dbeaver.model.struct.DBSObjectReference)2 AbstractSearchResult (org.jkiss.dbeaver.ui.search.AbstractSearchResult)2