Exemplo n.º 1
0
 static ClientSession processSessionParameterChange(
     Object parsedStatement, ClientSession session) {
   if (parsedStatement instanceof Use) {
     Use use = (Use) parsedStatement;
     return ClientSession.withCatalogAndSchema(
         session, use.getCatalog().orElse(session.getCatalog()), use.getSchema());
   }
   return session;
 }
Exemplo n.º 2
0
  @Override
  public void run() {
    ClientSession session = clientOptions.toClientSession();
    boolean hasQuery = !Strings.isNullOrEmpty(clientOptions.execute);
    boolean isFromFile = !Strings.isNullOrEmpty(clientOptions.file);

    if (!hasQuery || !isFromFile) {
      AnsiConsole.systemInstall();
    }

    initializeLogging(session.isDebug());

    String query = clientOptions.execute;
    if (hasQuery) {
      query += ";";
    }

    if (isFromFile) {
      if (hasQuery) {
        throw new RuntimeException("both --execute and --file specified");
      }
      try {
        query = Files.toString(new File(clientOptions.file), UTF_8);
        hasQuery = true;
      } catch (IOException e) {
        throw new RuntimeException(
            format("Error reading from file %s: %s", clientOptions.file, e.getMessage()));
      }
    }

    try (QueryRunner queryRunner =
        QueryRunner.create(session, Optional.ofNullable(clientOptions.socksProxy))) {
      if (hasQuery) {
        executeCommand(queryRunner, query, clientOptions.outputFormat);
      } else {
        runConsole(queryRunner, session);
      }
    }
  }
Exemplo n.º 3
0
    private static Request buildQueryRequest(ClientSession session, String query) {
      Request.Builder builder =
          preparePost()
              .setUri(uriBuilderFrom(session.getServer()).replacePath("/v1/execute").build())
              .setBodyGenerator(createStaticBodyGenerator(query, Charsets.UTF_8));

      if (session.getUser() != null) {
        builder.setHeader(PrestoHeaders.PRESTO_USER, session.getUser());
      }
      if (session.getSource() != null) {
        builder.setHeader(PrestoHeaders.PRESTO_SOURCE, session.getSource());
      }
      if (session.getCatalog() != null) {
        builder.setHeader(PrestoHeaders.PRESTO_CATALOG, session.getCatalog());
      }
      if (session.getSchema() != null) {
        builder.setHeader(PrestoHeaders.PRESTO_SCHEMA, session.getSchema());
      }
      builder.setHeader(PrestoHeaders.PRESTO_SCHEMA, session.getTimeZoneId());
      builder.setHeader(USER_AGENT, USER_AGENT_VALUE);

      return builder.build();
    }
Exemplo n.º 4
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());
    }
  }