Search in sources :

Example 1 with OutputFormat

use of com.facebook.presto.cli.ClientOptions.OutputFormat in project presto by prestodb.

the class Console method runConsole.

private static void runConsole(QueryRunner queryRunner, ClientSession session, AtomicBoolean exiting) {
    try (TableNameCompleter tableNameCompleter = new TableNameCompleter(queryRunner);
        LineReader reader = new LineReader(getHistory(), commandCompleter(), lowerCaseCommandCompleter(), tableNameCompleter)) {
        tableNameCompleter.populateCache();
        StringBuilder buffer = new StringBuilder();
        while (!exiting.get()) {
            // read a line of input from user
            String prompt = PROMPT_NAME;
            if (session.getSchema() != null) {
                prompt += ":" + session.getSchema();
            }
            if (buffer.length() > 0) {
                prompt = Strings.repeat(" ", prompt.length() - 1) + "-";
            }
            String commandPrompt = prompt + "> ";
            String line = reader.readLine(commandPrompt);
            // 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 (HISTORY_INDEX_PATTERN.matcher(command).matches()) {
                    int historyIndex = parseInt(command.substring(1));
                    History history = reader.getHistory();
                    if ((historyIndex <= 0) || (historyIndex > history.index())) {
                        System.err.println("Command does not exist");
                        continue;
                    }
                    line = history.get(historyIndex - 1).toString();
                    System.out.println(commandPrompt + line);
                }
                if (command.endsWith(";")) {
                    command = command.substring(0, command.length() - 1).trim();
                }
                switch(command.toLowerCase(ENGLISH)) {
                    case "exit":
                    case "quit":
                        return;
                    case "history":
                        for (History.Entry entry : reader.getHistory()) {
                            System.out.printf("%5d  %s%n", entry.index() + 1, entry.value());
                        }
                        continue;
                    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())) {
                    Map<String, String> properties = queryRunner.getSession().getProperties();
                    Map<String, String> preparedStatements = queryRunner.getSession().getPreparedStatements();
                    session = processSessionParameterChange(statement.get(), session, properties, preparedStatements);
                    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());
    }
}
Also used : StatementSplitter(com.facebook.presto.sql.parser.StatementSplitter) StatementSplitter.isEmptyStatement(com.facebook.presto.sql.parser.StatementSplitter.isEmptyStatement) StatementSplitter.squeezeStatement(com.facebook.presto.sql.parser.StatementSplitter.squeezeStatement) Statement(com.facebook.presto.sql.parser.StatementSplitter.Statement) OutputFormat(com.facebook.presto.cli.ClientOptions.OutputFormat) IOException(java.io.IOException) FileHistory(jline.console.history.FileHistory) MemoryHistory(jline.console.history.MemoryHistory) History(jline.console.history.History)

Aggregations

OutputFormat (com.facebook.presto.cli.ClientOptions.OutputFormat)1 StatementSplitter (com.facebook.presto.sql.parser.StatementSplitter)1 Statement (com.facebook.presto.sql.parser.StatementSplitter.Statement)1 StatementSplitter.isEmptyStatement (com.facebook.presto.sql.parser.StatementSplitter.isEmptyStatement)1 StatementSplitter.squeezeStatement (com.facebook.presto.sql.parser.StatementSplitter.squeezeStatement)1 IOException (java.io.IOException)1 FileHistory (jline.console.history.FileHistory)1 History (jline.console.history.History)1 MemoryHistory (jline.console.history.MemoryHistory)1