Skip to content

Commit

Permalink
feat: variable prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
hishidama committed Apr 12, 2024
1 parent a1c9a4d commit 0344639
Show file tree
Hide file tree
Showing 19 changed files with 1,087 additions and 240 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,34 @@

import com.tsurugidb.console.core.config.ScriptCvKey;
import com.tsurugidb.console.core.config.ScriptCvKey.ScriptCvKeyColor;
import com.tsurugidb.console.core.config.ScriptCvKey.ScriptCvKeyPrompt;

/**
* client variable key.
*/
public final class ReplCvKey {

/** console.prompt1.default . */
public static final ScriptCvKeyPrompt PROMPT1_DEFAULT = new ScriptCvKeyPrompt("console.prompt1.default"); //$NON-NLS-1$
/** console.prompt1.tx . */
public static final ScriptCvKeyPrompt PROMPT1_TRANSACTION = new ScriptCvKeyPrompt("console.prompt1.tx"); //$NON-NLS-1$
/** console.prompt1.occ . */
public static final ScriptCvKeyPrompt PROMPT1_OCC = new ScriptCvKeyPrompt("console.prompt1.occ"); //$NON-NLS-1$
/** console.prompt1.ltx . */
public static final ScriptCvKeyPrompt PROMPT1_LTX = new ScriptCvKeyPrompt("console.prompt1.ltx"); //$NON-NLS-1$
/** console.prompt1.rtx . */
public static final ScriptCvKeyPrompt PROMPT1_RTX = new ScriptCvKeyPrompt("console.prompt1.rtx"); //$NON-NLS-1$
/** console.prompt2.default . */
public static final ScriptCvKeyPrompt PROMPT2_DEFAULT = new ScriptCvKeyPrompt("console.prompt2.default"); //$NON-NLS-1$
/** console.prompt2.tx . */
public static final ScriptCvKeyPrompt PROMPT2_TRANSACTION = new ScriptCvKeyPrompt("console.prompt2.tx"); //$NON-NLS-1$
/** console.prompt2.occ . */
public static final ScriptCvKeyPrompt PROMPT2_OCC = new ScriptCvKeyPrompt("console.prompt2.occ"); //$NON-NLS-1$
/** console.prompt2.ltx . */
public static final ScriptCvKeyPrompt PROMPT2_LTX = new ScriptCvKeyPrompt("console.prompt2.ltx"); //$NON-NLS-1$
/** console.prompt2.rtx . */
public static final ScriptCvKeyPrompt PROMPT2_RTX = new ScriptCvKeyPrompt("console.prompt2.rtx"); //$NON-NLS-1$

/** console.info.color . */
public static final ScriptCvKeyColor CONSOLE_INFO_COLOR = new ScriptCvKeyColor("console.info.color"); //$NON-NLS-1$
/** console.implicit.color . */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.tsurugidb.console.core.executor.engine.Engine;
import com.tsurugidb.console.core.executor.engine.EngineException;
import com.tsurugidb.console.core.executor.report.ScriptReporter;
import com.tsurugidb.console.core.executor.sql.TransactionWrapper;
import com.tsurugidb.console.core.model.CallStatement;
import com.tsurugidb.console.core.model.CommitStatement;
import com.tsurugidb.console.core.model.ErroneousStatement;
Expand Down Expand Up @@ -50,6 +51,11 @@ public void connect() throws ServerException, IOException, InterruptedException
delegate.connect();
}

@Override
public TransactionWrapper getTransaction() {
return delegate.getTransaction();
}

@Override
public boolean executeErroneousStatement(ErroneousStatement statement) throws EngineException, ServerException, IOException, InterruptedException {
return delegate.executeErroneousStatement(statement);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.List;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import org.jline.reader.EndOfFileException;
import org.jline.reader.LineReader;
Expand All @@ -11,8 +12,12 @@
import org.slf4j.LoggerFactory;

import com.tsurugidb.console.cli.repl.jline.ReplJLineParser.ParsedStatement;
import com.tsurugidb.console.core.ScriptRunner.StatementSupplier;
import com.tsurugidb.console.core.config.ScriptConfig;
import com.tsurugidb.console.core.config.ScriptCvKey.ScriptCvKeyPrompt;
import com.tsurugidb.console.core.config.ScriptPrompt;
import com.tsurugidb.console.core.exception.ScriptInterruptedException;
import com.tsurugidb.console.core.executor.IoSupplier;
import com.tsurugidb.console.core.executor.sql.TransactionWrapper;
import com.tsurugidb.console.core.model.Region;
import com.tsurugidb.console.core.model.SimpleStatement;
import com.tsurugidb.console.core.model.Statement;
Expand All @@ -21,7 +26,7 @@
/**
* Tsurugi SQL console repl script.
*/
public class ReplScript implements IoSupplier<List<Statement>> {
public class ReplScript implements StatementSupplier {
private static final Logger LOG = LoggerFactory.getLogger(ReplScript.class);

private static final String PROMPT1 = "tgsql> "; //$NON-NLS-1$
Expand All @@ -36,15 +41,17 @@ public class ReplScript implements IoSupplier<List<Statement>> {
*/
public ReplScript(@Nonnull LineReader lineReader) {
this.lineReader = lineReader;

lineReader.setVariable(LineReader.SECONDARY_PROMPT_PATTERN, PROMPT2);
}

@Override
public List<Statement> get() {
public List<Statement> get(ScriptConfig config, @Nullable TransactionWrapper transaction) {
String prompt2 = getPrompt(config, ReplCvKey.PROMPT2_DEFAULT, ReplCvKey.PROMPT2_TRANSACTION, ReplCvKey.PROMPT2_OCC, ReplCvKey.PROMPT2_LTX, ReplCvKey.PROMPT2_RTX, PROMPT2, transaction);
lineReader.setVariable(LineReader.SECONDARY_PROMPT_PATTERN, prompt2);

String text;
try {
text = lineReader.readLine(PROMPT1);
String prompt1 = getPrompt(config, ReplCvKey.PROMPT1_DEFAULT, ReplCvKey.PROMPT1_TRANSACTION, ReplCvKey.PROMPT1_OCC, ReplCvKey.PROMPT1_LTX, ReplCvKey.PROMPT1_RTX, PROMPT1, transaction);
text = lineReader.readLine(prompt1);
} catch (UserInterruptException e) {
throw new ScriptInterruptedException(e);
} catch (EndOfFileException e) {
Expand All @@ -63,4 +70,48 @@ public List<Statement> get() {
}
throw new AssertionError(line);
}

private String getPrompt(ScriptConfig config, ScriptCvKeyPrompt keyDefault, ScriptCvKeyPrompt keyTx, ScriptCvKeyPrompt keyOcc, ScriptCvKeyPrompt keyLtx, ScriptCvKeyPrompt keyRtx,
String defaultPrompt, @Nullable TransactionWrapper transaction) {
var variableMap = config.getClientVariableMap();

ScriptPrompt prompt = null;
ScriptCvKeyPrompt key = null;
if (transaction != null) {
switch (transaction.getOption().getType()) {
case SHORT:
key = keyOcc;
break;
case LONG:
key = keyLtx;
break;
case READ_ONLY:
key = keyRtx;
break;
default:
break;
}
if (key != null) {
prompt = variableMap.get(key);
}
if (prompt == null) {
key = keyTx;
prompt = variableMap.get(key);
}
}
if (prompt == null) {
key = keyDefault;
prompt = variableMap.get(key);
if (prompt == null) {
return defaultPrompt;
}
}

try {
return prompt.getPrompt(config, transaction);
} catch (Exception e) {
LOG.debug("ReplScript.getPrompt error (key={}, prompt={})", key, prompt, e); //$NON-NLS-1$
return defaultPrompt;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.function.Function;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -32,6 +33,7 @@
import com.tsurugidb.console.core.executor.result.BasicResultProcessor;
import com.tsurugidb.console.core.executor.result.ResultProcessor;
import com.tsurugidb.console.core.executor.sql.BasicSqlProcessor;
import com.tsurugidb.console.core.executor.sql.TransactionWrapper;
import com.tsurugidb.console.core.model.Statement;
import com.tsurugidb.console.core.model.Statement.Kind;
import com.tsurugidb.console.core.parser.SqlParser;
Expand Down Expand Up @@ -224,7 +226,7 @@ private static IoSupplier<? extends Reader> toReaderSupplier(String script) thro
* @throws InterruptedException if interrupted while establishing connection
*/
public static void repl(//
@Nonnull IoSupplier<? extends List<Statement>> script, //
@Nonnull StatementSupplier script, //
@Nonnull ScriptConfig config, //
@Nonnull Function<AbstractEngine, Engine> engineWrapper, //
@Nonnull ResultProcessor resultProcessor, //
Expand All @@ -239,6 +241,22 @@ public static void repl(//
}
}

/**
* statement supplier.
*/
@FunctionalInterface
public interface StatementSupplier {
/**
* get statement.
*
* @param config script configuration
* @param transaction transaction
* @return list of statement
* @throws IOException if I/O error was occurred
*/
List<Statement> get(ScriptConfig config, @Nullable TransactionWrapper transaction) throws IOException;
}

/**
* Executes REPL.
*
Expand All @@ -249,7 +267,7 @@ public static void repl(//
* @throws InterruptedException if interrupted while establishing connection
*/
public static void repl(//
@Nonnull IoSupplier<? extends List<Statement>> script, //
@Nonnull StatementSupplier script, //
@Nonnull Engine engine) throws ServerException, IOException, InterruptedException {
Objects.requireNonNull(script);
Objects.requireNonNull(engine);
Expand All @@ -259,7 +277,8 @@ public static void repl(//
LOG.info("start repl");
loop: while (true) {
try {
List<Statement> statementList = script.get();
var transaction = engine.getTransaction();
List<Statement> statementList = script.get(engine.getConfig(), transaction);
if (statementList == null) {
LOG.trace("EOF");
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public abstract class ScriptCvKey<T> {
public static final ScriptCvKeyInt SELECT_MAX_LINES = new ScriptCvKeyInt("select.maxlines"); //$NON-NLS-1$
/** sql.timing . */
public static final ScriptCvKeyBoolean SQL_TIMING = new ScriptCvKeyBoolean("sql.timing"); //$NON-NLS-1$
/** sql.timing . */
/** auto-commit.when-transaction-started-implicitly . */
public static final ScriptCvKeyBoolean AUTO_COMMIT_TX_STARTED_IMPLICITLY = new ScriptCvKeyBoolean("auto-commit.when-transaction-started-implicitly"); //$NON-NLS-1$

/** dot.verbose . */
Expand Down Expand Up @@ -147,6 +147,26 @@ public ScriptColor convertValue(@Nonnull String s) {
}
}

/**
* client variable key for Prompt.
*/
public static class ScriptCvKeyPrompt extends ScriptCvKey<ScriptPrompt> {

/**
* Creates a new instance.
*
* @param name variable name
*/
public ScriptCvKeyPrompt(String name) {
super(name);
}

@Override
public ScriptPrompt convertValue(String s) {
return ScriptPrompt.create(s);
}
}

private static final Map<String, ScriptCvKey<?>> KEY_MAP = new ConcurrentHashMap<>();
static {
registerKey(ScriptCvKey.class);
Expand Down
Loading

0 comments on commit 0344639

Please sign in to comment.