Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ private void closeTerminal() {

private LineReader createLineReader(Terminal terminal, ExecutionMode mode) {
SqlMultiLineParser parser =
new SqlMultiLineParser(new SqlCommandParserImpl(), executor, mode);
new SqlMultiLineParser(new SqlCommandParserImpl(), executor, mode, historyFilePath);

// initialize line lineReader
LineReaderBuilder builder =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public AttributedString build() {
.commandDescription("HELP", "Prints the available commands.")
.commandDescription("QUIT/EXIT", "Quits the SQL CLI client.")
.commandDescription("CLEAR", "Clears the current terminal.")
.commandDescription("HISTORY", "Shows the SQL execution history.")
.commandDescription(
"SET",
"Sets a session configuration property. Syntax: \"SET '<key>'='<value>';\". Use \"SET;\" for listing all properties.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ static HelpCommandPrinter createHelpCommandPrinter() {
return HelpCommandPrinter.INSTANCE;
}

static HistoryCommandPrinter createHistoryCommandPrinter(java.nio.file.Path historyFilePath) {
return new HistoryCommandPrinter(historyFilePath);
}

static StatementResultPrinter createStatementCommandPrinter(
StatementResult result, ReadableConfig sessionConfig, long startTime) {
return new StatementResultPrinter(result, sessionConfig, startTime);
Expand Down Expand Up @@ -96,6 +100,56 @@ public void print(Terminal terminal) {
public void close() {}
}

/** Printer to print the SQL execution history. */
class HistoryCommandPrinter implements Printer {

private final java.nio.file.Path historyFilePath;

public HistoryCommandPrinter(java.nio.file.Path historyFilePath) {
this.historyFilePath = historyFilePath;
}

@Override
public boolean isQuitCommand() {
return false;
}

@Override
public void print(Terminal terminal) {
if (historyFilePath == null || !java.nio.file.Files.exists(historyFilePath)) {
terminal.writer().println("No history file found.");
terminal.flush();
return;
}

try {
java.util.List<String> lines = java.nio.file.Files.readAllLines(historyFilePath);
if (lines.isEmpty()) {
terminal.writer().println("History is empty.");
} else {
terminal.writer().println("SQL Execution History:");
terminal.writer().println("========================");
for (int i = 0; i < lines.size(); i++) {
String line = lines.get(i);
// Filter out non-SQL lines (like timestamps)
if (!line.isEmpty() && !line.startsWith("#")) {
terminal.writer().println(String.format("%4d %s", i + 1, line));
}
}
terminal.writer().println("========================");
terminal.writer().println(String.format("Total: %d statements", lines.size()));
}
terminal.flush();
} catch (java.io.IOException e) {
terminal.writer().println("Error reading history file: " + e.getMessage());
terminal.flush();
}
}

@Override
public void close() {}
}

/** Printer to print the QUIT messages. */
class QuitCommandPrinter implements Printer {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public enum Command {
CLEAR,
/** Command to print help message. */
HELP,
/** Command to show SQL execution history. */
SHOW_HISTORY,
/** Unknown command. */
OTHER
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ private Command getPotentialCommandType(String image) {
return Command.CLEAR;
case "HELP":
return Command.HELP;
case "HISTORY":
return Command.SHOW_HISTORY;
default:
return Command.OTHER;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ public class SqlMultiLineParser extends DefaultParser {
/** Sql command executor. */
private final Executor executor;

/** Path to the history file. */
private final java.nio.file.Path historyFilePath;

/** Exception caught in parsing. */
private SqlExecutionException parseException = null;

Expand All @@ -68,9 +71,18 @@ public class SqlMultiLineParser extends DefaultParser {

public SqlMultiLineParser(
SqlCommandParser parser, Executor executor, CliClient.ExecutionMode mode) {
this(parser, executor, mode, null);
}

public SqlMultiLineParser(
SqlCommandParser parser,
Executor executor,
CliClient.ExecutionMode mode,
java.nio.file.Path historyFilePath) {
this.parser = parser;
this.mode = mode;
this.executor = executor;
this.historyFilePath = historyFilePath;
setEscapeChars(null);
setQuoteChars(null);
}
Expand Down Expand Up @@ -108,6 +120,9 @@ public ParsedLine parse(String line, int cursor, ParseContext context) {
case HELP:
printer = Printer.createHelpCommandPrinter();
break;
case SHOW_HISTORY:
printer = Printer.createHistoryCommandPrinter(historyFilePath);
break;
default:
{
if (mode == CliClient.ExecutionMode.INITIALIZATION) {
Expand Down