コード例 #1
0
 private void makeProposalsFromAssistant(
     DBRProgressMonitor monitor,
     DBSStructureAssistant assistant,
     @Nullable DBSObjectContainer rootSC,
     String objectName,
     List<SQLCompletionProposal> proposals) {
   try {
     Collection<DBSObjectReference> references =
         assistant.findObjectsByMask(
             monitor,
             rootSC,
             assistant.getAutoCompleteObjectTypes(),
             wordDetector.removeQuotes(objectName) + "%",
             wordDetector.isQuoted(objectName),
             100);
     for (DBSObjectReference reference : references) {
       proposals.add(makeProposalsFromObject(reference, reference.getObjectType().getImage()));
     }
   } catch (DBException e) {
     log.error(e);
   }
 }
コード例 #2
0
  @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;
    }
  }
コード例 #3
0
  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);
        }
      }
    }
  }