Search in sources :

Example 1 with InputReader

use of io.prestosql.cli.InputReader in project hetu-core by openlookeng.

the class Console method runConsole.

private void runConsole(SessionProperties session, AtomicBoolean exiting) {
    try (InputReader reader = new InputReader(getHistoryFile(), commandCompleter())) {
        StringBuilder buffer = new StringBuilder();
        while (!exiting.get()) {
            // setup prompt
            String commandPrompt = PROMPT_NAME;
            if (session.getSourceType() != null) {
                commandPrompt += ":" + session.getSourceType().name();
            }
            // read a line of input from user
            if (buffer.length() > 0) {
                commandPrompt = Strings.repeat(" ", commandPrompt.length() - 1) + "->";
            } else {
                commandPrompt += ">";
            }
            String line;
            try {
                line = reader.readLine(commandPrompt, buffer.toString());
            } catch (UserInterruptException e) {
                // add buffer to history and clear on user interrupt
                if (!squeezeStatement(buffer.toString()).isEmpty()) {
                    reader.getHistory().add(squeezeStatement(buffer.toString()));
                }
                buffer = new StringBuilder();
                continue;
            }
            // exit on EOF
            if (line == null) {
                System.out.println("Exiting...");
                System.out.println();
                return;
            }
            // check for special commands if this is the first line
            if (buffer.length() == 0) {
                String controlCommand = line.trim();
                if (controlCommand.endsWith(";")) {
                    controlCommand = controlCommand.substring(0, controlCommand.length() - 1).trim();
                    if (SET_COMMAND_PATTERN.matcher(controlCommand).matches()) {
                        controlCommand = controlCommand.replaceAll(" +", " ");
                        setSessionProperties(controlCommand.replace("!", ""), session);
                        continue;
                    }
                }
                switch(controlCommand.toLowerCase(ENGLISH)) {
                    case COMMAND_EXIT:
                    case COMMAND_QUIT:
                        System.out.println("Exiting...");
                        System.out.println();
                        return;
                    case COMMAND_HISTORY:
                        for (History.Entry entry : reader.getHistory()) {
                            System.out.printf("%5d  %s%n", entry.index() + 1, entry.line());
                        }
                        System.out.println();
                        System.out.printf("Choose and run the history command by index number. e.g. !10");
                        System.out.println();
                        continue;
                    case COMMAND_HELP:
                        System.out.println();
                        System.out.println(SqlMigrationHelp.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, STATEMENT_SPLITTER);
            SqlSyntaxConverter sqlConverter = SqlConverterFactory.getSqlConverter(session);
            for (StatementSplitter.Statement split : splitter.getCompleteStatements()) {
                reader.getHistory().add(squeezeStatement(split.statement()) + split.terminator());
                JSONObject result = sqlConverter.convert(split.statement());
                consolePrint(result);
            }
            // replace buffer with trailing partial statement
            buffer = new StringBuilder();
            if (!splitter.getPartialStatement().isEmpty()) {
                buffer.append(splitter.getPartialStatement()).append('\n');
            }
        }
    } catch (IOException e) {
        System.out.println(format("Readline error: %s", e.getMessage()));
    } catch (ParsingException | SqlMigrationException e) {
        System.out.println(format("Failed to migrate the sql due to error: %s", e.getMessage()));
    }
}
Also used : StatementSplitter(io.prestosql.sql.parser.StatementSplitter) IOException(java.io.IOException) UserInterruptException(org.jline.reader.UserInterruptException) History(org.jline.reader.History) InputReader(io.prestosql.cli.InputReader) JSONObject(org.codehaus.jettison.json.JSONObject) SqlMigrationException(io.hetu.core.sql.migration.SqlMigrationException) ParsingException(io.prestosql.sql.parser.ParsingException)

Aggregations

SqlMigrationException (io.hetu.core.sql.migration.SqlMigrationException)1 InputReader (io.prestosql.cli.InputReader)1 ParsingException (io.prestosql.sql.parser.ParsingException)1 StatementSplitter (io.prestosql.sql.parser.StatementSplitter)1 IOException (java.io.IOException)1 JSONObject (org.codehaus.jettison.json.JSONObject)1 History (org.jline.reader.History)1 UserInterruptException (org.jline.reader.UserInterruptException)1