Search in sources :

Example 1 with SqlMigrationException

use of io.hetu.core.sql.migration.SqlMigrationException in project hetu-core by openlookeng.

the class Console method executeCommand.

public boolean executeCommand(String query, String outputFile, SessionProperties session) {
    JSONArray output = new JSONArray();
    try {
        StatementSplitter splitter = new StatementSplitter(query);
        SqlSyntaxConverter sqlConverter = SqlConverterFactory.getSqlConverter(session);
        for (StatementSplitter.Statement split : splitter.getCompleteStatements()) {
            if (session.isDebugEnable()) {
                log.info(String.format("Processing sql: %s", split.toString()));
            }
            JSONObject result = sqlConverter.convert(split.statement());
            output.put(result);
            if (session.isConsolePrintEnable()) {
                consolePrint(result);
            }
        }
        log.info(format("Migration Completed."));
        // write the result to file if output file was specified
        if (outputFile != null) {
            SqlResultHandleUtils.writeToHtmlFile(output, outputFile);
        }
        return true;
    } catch (ParsingException | SqlMigrationException e) {
        log.error(format("Failed to migrate the sql due to error:", e.getMessage()));
    }
    return false;
}
Also used : JSONObject(org.codehaus.jettison.json.JSONObject) StatementSplitter(io.prestosql.sql.parser.StatementSplitter) SqlMigrationException(io.hetu.core.sql.migration.SqlMigrationException) ParsingException(io.prestosql.sql.parser.ParsingException) JSONArray(org.codehaus.jettison.json.JSONArray)

Example 2 with SqlMigrationException

use of io.hetu.core.sql.migration.SqlMigrationException 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)2 ParsingException (io.prestosql.sql.parser.ParsingException)2 StatementSplitter (io.prestosql.sql.parser.StatementSplitter)2 JSONObject (org.codehaus.jettison.json.JSONObject)2 InputReader (io.prestosql.cli.InputReader)1 IOException (java.io.IOException)1 JSONArray (org.codehaus.jettison.json.JSONArray)1 History (org.jline.reader.History)1 UserInterruptException (org.jline.reader.UserInterruptException)1