Beispiel #1
0
  private static void process(
      QueryRunner queryRunner, String sql, OutputFormat outputFormat, boolean interactive) {
    try (Query query = queryRunner.startQuery(sql)) {
      query.renderOutput(System.out, outputFormat, interactive);

      // update session properties if present
      if (!query.getSetSessionProperties().isEmpty()
          || !query.getResetSessionProperties().isEmpty()) {
        Map<String, String> sessionProperties =
            new HashMap<>(queryRunner.getSession().getProperties());
        sessionProperties.putAll(query.getSetSessionProperties());
        sessionProperties.keySet().removeAll(query.getResetSessionProperties());
        queryRunner.setSession(withProperties(queryRunner.getSession(), sessionProperties));
      }
    } catch (RuntimeException e) {
      System.out.println("Error running command: " + e.getMessage());
      if (queryRunner.getSession().isDebug()) {
        e.printStackTrace();
      }
    }
  }
Beispiel #2
0
  private static void runConsole(QueryRunner queryRunner, ClientSession session) {
    try (TableNameCompleter tableNameCompleter = new TableNameCompleter(queryRunner);
        LineReader reader = new LineReader(getHistory(), tableNameCompleter)) {
      tableNameCompleter.populateCache();
      StringBuilder buffer = new StringBuilder();
      while (true) {
        // read a line of input from user
        String prompt = PROMPT_NAME + ":" + session.getSchema();
        if (buffer.length() > 0) {
          prompt = Strings.repeat(" ", prompt.length() - 1) + "-";
        }
        String line = reader.readLine(prompt + "> ");

        // add buffer to history and clear on user interrupt
        if (reader.interrupted()) {
          String partial = squeezeStatement(buffer.toString());
          if (!partial.isEmpty()) {
            reader.getHistory().add(partial);
          }
          buffer = new StringBuilder();
          continue;
        }

        // exit on EOF
        if (line == null) {
          System.out.println();
          return;
        }

        // check for special commands if this is the first line
        if (buffer.length() == 0) {
          String command = line.trim();
          if (command.endsWith(";")) {
            command = command.substring(0, command.length() - 1).trim();
          }
          switch (command.toLowerCase(ENGLISH)) {
            case "exit":
            case "quit":
              return;
            case "help":
              System.out.println();
              System.out.println(getHelpText());
              continue;
          }
        }

        // not a command, add line to buffer
        buffer.append(line).append("\n");

        // execute any complete statements
        String sql = buffer.toString();
        StatementSplitter splitter = new StatementSplitter(sql, ImmutableSet.of(";", "\\G"));
        for (Statement split : splitter.getCompleteStatements()) {
          Optional<Object> statement = getParsedStatement(split.statement());
          if (statement.isPresent() && isSessionParameterChange(statement.get())) {
            session = processSessionParameterChange(statement.get(), session);
            queryRunner.setSession(session);
            tableNameCompleter.populateCache();
          } else {
            OutputFormat outputFormat = OutputFormat.ALIGNED;
            if (split.terminator().equals("\\G")) {
              outputFormat = OutputFormat.VERTICAL;
            }

            process(queryRunner, split.statement(), outputFormat, true);
          }
          reader.getHistory().add(squeezeStatement(split.statement()) + split.terminator());
        }

        // replace buffer with trailing partial statement
        buffer = new StringBuilder();
        String partial = splitter.getPartialStatement();
        if (!partial.isEmpty()) {
          buffer.append(partial).append('\n');
        }
      }
    } catch (IOException e) {
      System.err.println("Readline error: " + e.getMessage());
    }
  }