Search in sources :

Example 6 with StatementSplitter

use of io.prestosql.sql.parser.StatementSplitter 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)

Example 7 with StatementSplitter

use of io.prestosql.sql.parser.StatementSplitter in project hetu-core by openlookeng.

the class Console method executeCommand.

private boolean executeCommand(QueryRunner queryRunner, AtomicBoolean exiting, String query, ClientOptions.OutputFormat outputFormat, boolean ignoreErrors, boolean showProgress) {
    boolean success = true;
    StatementSplitter splitter = new StatementSplitter(query);
    for (Statement split : splitter.getCompleteStatements()) {
        if (!isEmptyStatement(split.statement())) {
            try (Terminal terminal = terminal()) {
                String statement = split.statement();
                CubeConsole cubeConsole = new CubeConsole(this);
                queryRunner.setCubeConsole(cubeConsole);
                ReloadCubeConsole reloadCubeConsole = new ReloadCubeConsole(this);
                queryRunner.setReloadCubeConsole(reloadCubeConsole);
                if (CREATE_CUBE_PATTERN.matcher(statement).matches()) {
                    PrintStream out = System.out;
                    PrintStream errorChannel = System.out;
                    if (!cubeConsole.createCubeCommand(statement, queryRunner, ClientOptions.OutputFormat.NULL, () -> {
                    }, false, showProgress, terminal, out, errorChannel)) {
                        if (!ignoreErrors) {
                            return false;
                        }
                        success = false;
                    }
                }
                if (RELOAD_CUBE_PATTERN.matcher(statement).matches()) {
                    PrintStream out = System.out;
                    PrintStream errorChannel = System.out;
                    boolean isReloadCubeSucceed = reloadCubeConsole.reload(statement, queryRunner, ClientOptions.OutputFormat.NULL, () -> {
                    }, false, showProgress, terminal, out, errorChannel);
                    if (!isReloadCubeSucceed) {
                        if (!ignoreErrors) {
                            return false;
                        }
                        success = false;
                    } else {
                        boolean isCreateCubeSucceed = cubeConsole.createCubeCommand(reloadCubeConsole.getNewQuery(), queryRunner, ClientOptions.OutputFormat.NULL, () -> {
                        }, false, showProgress, terminal, out, errorChannel);
                        if (!isCreateCubeSucceed) {
                            System.err.println("An Error occurred. The cube needs to be created manually. Query to create the cube: " + reloadCubeConsole.getNewQuery());
                            if (!ignoreErrors) {
                                return false;
                            }
                            success = false;
                        }
                    }
                } else {
                    if (!process(queryRunner, statement, outputFormat, () -> {
                    }, false, showProgress, terminal, System.out, System.err)) {
                        if (!ignoreErrors) {
                            return false;
                        }
                        success = false;
                    }
                }
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        }
        if (exiting.get()) {
            return success;
        }
    }
    if (!isEmptyStatement(splitter.getPartialStatement())) {
        System.err.println("Non-terminated statement: " + splitter.getPartialStatement());
        return false;
    }
    return success;
}
Also used : PrintStream(java.io.PrintStream) StatementSplitter(io.prestosql.sql.parser.StatementSplitter) Statement(io.prestosql.sql.parser.StatementSplitter.Statement) StatementSplitter.isEmptyStatement(io.prestosql.sql.parser.StatementSplitter.isEmptyStatement) UncheckedIOException(java.io.UncheckedIOException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) Terminal(org.jline.terminal.Terminal)

Aggregations

StatementSplitter (io.prestosql.sql.parser.StatementSplitter)7 JSONObject (org.codehaus.jettison.json.JSONObject)4 IOException (java.io.IOException)3 SqlMigrationException (io.hetu.core.sql.migration.SqlMigrationException)2 ParsingException (io.prestosql.sql.parser.ParsingException)2 Statement (io.prestosql.sql.parser.StatementSplitter.Statement)2 StatementSplitter.isEmptyStatement (io.prestosql.sql.parser.StatementSplitter.isEmptyStatement)2 File (java.io.File)2 PrintStream (java.io.PrintStream)2 UncheckedIOException (java.io.UncheckedIOException)2 History (org.jline.reader.History)2 UserInterruptException (org.jline.reader.UserInterruptException)2 Test (org.testng.annotations.Test)2 InputReader (io.prestosql.cli.InputReader)1 JSONArray (org.codehaus.jettison.json.JSONArray)1 EOFError (org.jline.reader.EOFError)1 EndOfFileException (org.jline.reader.EndOfFileException)1 DefaultParser (org.jline.reader.impl.DefaultParser)1 Terminal (org.jline.terminal.Terminal)1 AttributedStringBuilder (org.jline.utils.AttributedStringBuilder)1