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()));
}
}
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;
}
Aggregations