private boolean extractTargets()

in core/src/main/java/com/jetbrains/youtrackdb/internal/core/sql/filter/SQLTarget.java [151:304]


  private boolean extractTargets(DatabaseSessionInternal session) {
    parserSkipWhiteSpaces();

    if (parserIsEnded()) {
      throw new QueryParsingException(null, "No query target found", parserText, 0);
    }

    final var c = parserGetCurrentChar();

    if (c == '$') {
      targetVariable = parserRequiredWord(false, "No valid target", session.getDatabaseName());
      targetVariable = targetVariable.substring(1);
    } else if (c == StringSerializerHelper.LINK || Character.isDigit(c)) {
      // UNIQUE RID
      targetRecords = new ArrayList<Identifiable>();
      ((List<Identifiable>) targetRecords)
          .add(RecordIdInternal.fromString(
              parserRequiredWord(true, "No valid RID", session.getDatabaseName()), false));

    } else if (c == StringSerializerHelper.EMBEDDED_BEGIN) {
      // SUB QUERY
      final var subText = new StringBuilder(256);
      parserSetCurrentPosition(
          StringSerializerHelper.getEmbedded(parserText, parserGetCurrentPosition(), -1, subText)
              + 1);
    } else if (c == StringSerializerHelper.LIST_BEGIN) {
      // COLLECTION OF RIDS
      final List<String> rids = new ArrayList<String>();
      parserSetCurrentPosition(
          StringSerializerHelper.getCollection(parserText, parserGetCurrentPosition(), rids));

      targetRecords = new ArrayList<Identifiable>();
      for (var rid : rids) {
        ((List<Identifiable>) targetRecords).add(RecordIdInternal.fromString(rid, false));
      }

      parserMoveCurrentPosition(1);
    } else {

      while (!parserIsEnded()
          && (targetClasses == null
          && targetCollections == null
          && targetIndex == null
          && targetIndexValues == null
          && targetRecords == null)) {
        var originalSubjectName = parserRequiredWord(false, "Target not found",
            session.getDatabaseName());
        var subjectName = originalSubjectName.toUpperCase(Locale.ENGLISH);

        final String alias;
        if (subjectName.equals("AS")) {
          alias = parserRequiredWord(true, "Alias not found", session.getDatabaseName());
        } else {
          alias = subjectName;
        }

        final var subjectToMatch = subjectName;
        if (subjectToMatch.startsWith(CommandExecutorSQLAbstract.COLLECTION_PREFIX)) {
          // REGISTER AS COLLECTION
          if (targetCollections == null) {
            targetCollections = new HashMap<String, String>();
          }
          final var collectionNames =
              subjectName.substring(CommandExecutorSQLAbstract.COLLECTION_PREFIX.length());
          if (collectionNames.startsWith("[") && collectionNames.endsWith("]")) {
            final Collection<String> collections = new HashSet<String>(3);
            StringSerializerHelper.getCollection(collectionNames, 0, collections);
            for (var cl : collections) {
              targetCollections.put(cl, cl);
            }
          } else {
            targetCollections.put(collectionNames, alias);
          }

        } else if (subjectToMatch.startsWith(CommandExecutorSQLAbstract.INDEX_PREFIX)) {
          // REGISTER AS INDEX
          targetIndex =
              originalSubjectName.substring(CommandExecutorSQLAbstract.INDEX_PREFIX.length());
        } else if (subjectToMatch.startsWith(CommandExecutorSQLAbstract.METADATA_PREFIX)) {
          // METADATA
          final var metadataTarget =
              subjectName.substring(CommandExecutorSQLAbstract.METADATA_PREFIX.length());
          targetRecords = new ArrayList<Identifiable>();

          if (metadataTarget.equals(CommandExecutorSQLAbstract.METADATA_SCHEMA)) {
            ((ArrayList<Identifiable>) targetRecords)
                .add(
                    RecordIdInternal.fromString(
                        session
                            .getStorageInfo()
                            .getConfiguration()
                            .getSchemaRecordId(), false));
          } else if (metadataTarget.equals(CommandExecutorSQLAbstract.METADATA_INDEXMGR)) {
            ((ArrayList<Identifiable>) targetRecords)
                .add(
                    RecordIdInternal.fromString(
                        session
                            .getStorageInfo()
                            .getConfiguration()
                            .getIndexMgrRecordId(), false));
          } else {
            throw new QueryParsingException(session.getDatabaseName(),
                "Metadata entity not supported: " + metadataTarget);
          }

        } else if (subjectToMatch.startsWith(CommandExecutorSQLAbstract.INDEX_VALUES_PREFIX)) {
          targetIndexValues =
              originalSubjectName.substring(
                  CommandExecutorSQLAbstract.INDEX_VALUES_PREFIX.length());
          targetIndexValuesAsc = true;
        } else if (subjectToMatch.startsWith(CommandExecutorSQLAbstract.INDEX_VALUES_ASC_PREFIX)) {
          targetIndexValues =
              originalSubjectName.substring(
                  CommandExecutorSQLAbstract.INDEX_VALUES_ASC_PREFIX.length());
          targetIndexValuesAsc = true;
        } else if (subjectToMatch.startsWith(
            CommandExecutorSQLAbstract.INDEX_VALUES_DESC_PREFIX)) {
          targetIndexValues =
              originalSubjectName.substring(
                  CommandExecutorSQLAbstract.INDEX_VALUES_DESC_PREFIX.length());
          targetIndexValuesAsc = false;
        } else {
          if (subjectToMatch.startsWith(CommandExecutorSQLAbstract.CLASS_PREFIX))
          // REGISTER AS CLASS
          {
            subjectName = subjectName.substring(CommandExecutorSQLAbstract.CLASS_PREFIX.length());
          }

          // REGISTER AS CLASS
          if (targetClasses == null) {
            targetClasses = new HashMap<String, String>();
          }

          final var cls =
              session
                  .getMetadata()
                  .getImmutableSchemaSnapshot()
                  .getClass(subjectName);
          if (cls == null) {
            throw new CommandExecutionException(session,
                "Class '"
                    + subjectName
                    + "' was not found in database '"
                    + session.getDatabaseName()
                    + "'");
          }

          targetClasses.put(cls.getName(), alias);
        }
      }
    }

    return !parserIsEnded();
  }