private static void collectTables( DBRProgressMonitor monitor, DBSObjectContainer container, Set<DBSEntity> tables) throws DBException { if (monitor.isCanceled()) { return; } container.cacheStructure(monitor, DBSObjectContainer.STRUCT_ALL); final Collection<? extends DBSObject> children = container.getChildren(monitor); if (!CommonUtils.isEmpty(children)) { Class<? extends DBSObject> childType = container.getChildType(monitor); DBSObjectFilter objectFilter = container.getDataSource().getContainer().getObjectFilter(childType, container, true); for (DBSObject entity : children) { if (monitor.isCanceled()) { break; } if (objectFilter != null && !objectFilter.matches(entity.getName())) { continue; } if (entity instanceof DBSEntity) { tables.add((DBSEntity) entity); } else if (entity instanceof DBSObjectContainer) { collectTables(monitor, (DBSObjectContainer) entity, tables); } } } }
static void makeProposalsFromChildren( DBRProgressMonitor monitor, DBSObjectContainer container, List<DBSEntity> names) throws DBException { Collection<? extends DBSObject> children = container.getChildren(monitor); if (CommonUtils.isEmpty(children)) { return; } for (DBSObject child : children) { if (child instanceof DBSEntity) { names.add((DBSEntity) child); } } }
@Nullable private DBSObject getTableFromAlias( DBRProgressMonitor monitor, DBSObjectContainer sc, @Nullable String token) { final DBPDataSource dataSource = editor.getDataSource(); if (!(dataSource instanceof SQLDataSource)) { return null; } if (activeQuery == null) { activeQuery = editor.extractQueryAtPos(documentOffset).getQuery() + " "; } final List<String> nameList = new ArrayList<>(); if (token == null) { token = ""; } { Matcher matcher; Pattern aliasPattern; SQLDialect sqlDialect = ((SQLDataSource) dataSource).getSQLDialect(); String quoteString = sqlDialect.getIdentifierQuoteString(); String quote = quoteString == null ? SQLConstants.STR_QUOTE_DOUBLE : SQLConstants.STR_QUOTE_DOUBLE.equals(quoteString) ? quoteString : Pattern.quote(quoteString); String catalogSeparator = sqlDialect.getCatalogSeparator(); while (token.endsWith(catalogSeparator)) token = token.substring(0, token.length() - 1); String tableNamePattern = "((?:" + quote + "(?:[.[^" + quote + "]]+)" + quote + ")|(?:[\\w" + Pattern.quote(catalogSeparator) + "]+))"; String structNamePattern; if (CommonUtils.isEmpty(token)) { structNamePattern = "(?:from|update|join|into)\\s*" + tableNamePattern; } else { structNamePattern = tableNamePattern + "(?:\\s*\\.\\s*" + tableNamePattern + ")?" + "\\s+(?:(?:AS)\\s)?" + token + "[\\s,]+"; } try { aliasPattern = Pattern.compile(structNamePattern, Pattern.CASE_INSENSITIVE); } catch (PatternSyntaxException e) { // Bad pattern - seems to be a bad token return null; } matcher = aliasPattern.matcher(activeQuery); if (!matcher.find()) { return null; } int groupCount = matcher.groupCount(); for (int i = 1; i <= groupCount; i++) { String group = matcher.group(i); if (!CommonUtils.isEmpty(group)) { String[] allNames = group.split(Pattern.quote(catalogSeparator)); for (String name : allNames) { if (quoteString != null && name.startsWith(quoteString) && name.endsWith(quoteString)) { name = name.substring(1, name.length() - 1); } nameList.add(name); } } } } if (nameList.isEmpty()) { return null; } for (int i = 0; i < nameList.size(); i++) { nameList.set( i, DBObjectNameCaseTransformer.transformName(sc.getDataSource(), nameList.get(i))); } try { DBSObject childObject = null; while (childObject == null) { childObject = DBUtils.findNestedObject(monitor, sc, nameList); if (childObject == null) { DBSObjectContainer parentSc = DBUtils.getParentAdapter(DBSObjectContainer.class, sc); if (parentSc == null) { break; } sc = parentSc; } } if (childObject == null && nameList.size() <= 1) { // No such object found - may be it's start of table name DBSStructureAssistant structureAssistant = DBUtils.getAdapter(DBSStructureAssistant.class, sc); if (structureAssistant != null) { String objectNameMask = nameList.get(0); Collection<DBSObjectReference> tables = structureAssistant.findObjectsByMask( monitor, sc, structureAssistant.getAutoCompleteObjectTypes(), wordDetector.removeQuotes(objectNameMask), wordDetector.isQuoted(objectNameMask), 2); if (!tables.isEmpty()) { return tables.iterator().next().resolveObject(monitor); } } return null; } else { return childObject; } } catch (DBException e) { log.error(e); return null; } }
private void makeStructureProposals( DBRProgressMonitor monitor, DBPDataSource dataSource, List<SQLCompletionProposal> proposals) { final DBSObjectContainer rootContainer = DBUtils.getAdapter(DBSObjectContainer.class, dataSource); if (rootContainer == null) { return; } DBSObjectContainer selectedContainer = null; { DBSObject selectedObject = getSelectedObject(dataSource); if (selectedObject != null) { selectedContainer = DBUtils.getAdapter(DBSObjectContainer.class, selectedObject); } } DBSObjectContainer sc = rootContainer; DBSObject childObject = sc; List<String> tokens = wordDetector.splitWordPart(); String lastToken = null; for (int i = 0; i < tokens.size(); i++) { String token = tokens.get(i); if (i == tokens.size() - 1 && !wordDetector.getWordPart().endsWith(".")) { lastToken = token; break; } if (sc == null) { break; } // Get next structure container try { String objectName = DBObjectNameCaseTransformer.transformName(dataSource, token); childObject = sc.getChild(monitor, objectName); if (childObject == null && i == 0 && selectedContainer != null) { // Probably it is from selected object, let's try it childObject = selectedContainer.getChild(monitor, objectName); if (childObject != null) { sc = selectedContainer; } } if (childObject == null) { if (i == 0) { // Assume it's a table alias ? childObject = this.getTableFromAlias(monitor, sc, token); if (childObject == null) { DBSStructureAssistant structureAssistant = DBUtils.getAdapter(DBSStructureAssistant.class, sc); if (structureAssistant != null) { Collection<DBSObjectReference> references = structureAssistant.findObjectsByMask( monitor, null, structureAssistant.getAutoCompleteObjectTypes(), wordDetector.removeQuotes(token), wordDetector.isQuoted(token), 2); if (!references.isEmpty()) { childObject = references.iterator().next().resolveObject(monitor); } } } } else { // Path element not found. Damn - can't do anything. return; } } if (childObject instanceof DBSObjectContainer) { sc = (DBSObjectContainer) childObject; } else { sc = null; } } catch (DBException e) { log.error(e); return; } } if (childObject == null) { return; } if (lastToken == null) { // Get all children objects as proposals makeProposalsFromChildren(monitor, childObject, null, proposals); } else { // Get matched children makeProposalsFromChildren(monitor, childObject, lastToken, proposals); if (proposals.isEmpty() || tokens.size() == 1) { // At last - try to find child tables by pattern DBSStructureAssistant structureAssistant = null; for (DBSObject object = childObject; object != null; object = object.getParentObject()) { structureAssistant = DBUtils.getAdapter(DBSStructureAssistant.class, object); if (structureAssistant != null) { break; } } if (structureAssistant != null) { makeProposalsFromAssistant(monitor, structureAssistant, sc, lastToken, proposals); } } } }