Skip to content

Commit

Permalink
feat(tgsql): Addd client variable 'display.implicit'
Browse files Browse the repository at this point in the history
  • Loading branch information
hishidama committed Apr 18, 2024
1 parent b68b143 commit 964718a
Show file tree
Hide file tree
Showing 11 changed files with 233 additions and 152 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private void executeDot(PlanGraph plan) throws EngineException, InterruptedExcep
var path = CommandPath.system();
var handler = DotOutputHandler.fromOptions(options, path);

handler.handle(new BasicReporter(), plan);
handler.handle(new BasicReporter(config), plan);
}

private Map<Regioned<String>, Optional<Regioned<Value>>> getOptions() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,22 @@
public class ReplReporter extends TgsqlReporter {

private final Terminal terminal;
private final TgsqlConfig config;

/**
* Creates a new instance.
*
* @param terminal JLine Terminal
* @param config SQL scripts configuration
* @param config tgsql configuration
*/
public ReplReporter(@Nonnull Terminal terminal, @Nonnull TgsqlConfig config) {
super(config);
this.terminal = Objects.requireNonNull(terminal);
this.config = Objects.requireNonNull(config);
}

// for test
ReplReporter() {
super(new TgsqlConfig());
this.terminal = null;
this.config = null;
}

protected int red() {
Expand All @@ -62,23 +61,23 @@ protected int color(TgsqlCvKeyColor key, int defaultColor) {
}

@Override
public void info(String message) {
protected void doInfo(String message) {
int color = color(ReplCvKey.CONSOLE_INFO_COLOR, -1);
println(message, color);
}

@Override
public void implicit(String message) {
protected void doImplicit(String message) {
println(message, yellow());
}

@Override
public void succeed(String message) {
protected void doSucceed(String message) {
println(message, green());
}

@Override
public void warn(String message) {
protected void doWarn(String message) {
println(message, red());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public static boolean execute(//

try (var sqlProcessor = new BasicSqlProcessor(); //
var resultProcessor = new BasicResultProcessor()) {
var reporter = new BasicReporter();
var reporter = new BasicReporter(config);
return execute(script, new BasicEngine(config, sqlProcessor, resultProcessor, reporter));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
*/
public abstract class TgsqlCvKey<T> {

/** display.implicit . */
public static final TgsqlCvKeyBoolean DISPLAY_IMPLICIT = new TgsqlCvKeyBoolean("display.implicit"); //$NON-NLS-1$
/** display.succeed . */
public static final TgsqlCvKeyBoolean DISPLAY_SUCCEED = new TgsqlCvKeyBoolean("display.succeed"); //$NON-NLS-1$

/** implicit-transaction.label.suffix-time . */
public static final TgsqlCvKeyDateTimeFormat IMPLICIT_TX_LABEL_SUFFIX_TIME = new TgsqlCvKeyDateTimeFormat("implicit-transaction.label.suffix-time"); //$NON-NLS-1$
/** implicit-transaction.auto-commit . */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,44 @@
package com.tsurugidb.tgsql.core.executor.report;

import javax.annotation.Nonnull;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.tsurugidb.tgsql.core.config.TgsqlConfig;

/**
* A basic implementation of {@link TgsqlReporter}.
*/
public class BasicReporter extends TgsqlReporter {
private static final Logger LOG = LoggerFactory.getLogger(BasicReporter.class);

/**
* Creates a new instance.
*
* @param config tgsql configuration
*/
public BasicReporter(@Nonnull TgsqlConfig config) {
super(config);
}

@Override
public void info(String message) {
protected void doInfo(String message) {
LOG.info(message);
}

@Override
public void implicit(String message) {
protected void doImplicit(String message) {
LOG.debug(message);
}

@Override
public void succeed(String message) {
protected void doSucceed(String message) {
LOG.info(message);
}

@Override
public void warn(String message) {
protected void doWarn(String message) {
LOG.warn(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
import com.tsurugidb.sql.proto.SqlCommon;
import com.tsurugidb.sql.proto.SqlRequest.CommitStatus;
import com.tsurugidb.sql.proto.SqlRequest.TransactionOption;
import com.tsurugidb.tgsql.core.config.TgsqlConfig;
import com.tsurugidb.tgsql.core.config.TgsqlCvKey;
import com.tsurugidb.tgsql.core.config.TgsqlCvKey.TgsqlCvKeyBoolean;
import com.tsurugidb.tsubakuro.exception.ServerException;
import com.tsurugidb.tsubakuro.explain.PlanGraph;
import com.tsurugidb.tsubakuro.sql.CounterType;
Expand All @@ -28,33 +31,61 @@
*/
public abstract class TgsqlReporter {

/** tgsql configuration. */
protected final TgsqlConfig config;

/**
* Creates a new instance.
*
* @param config tgsql configuration
*/
public TgsqlReporter(@Nonnull TgsqlConfig config) {
this.config = Objects.requireNonNull(config);
}

/**
* output information message.
*
* @param message message
*/
public abstract void info(String message);
public void info(String message) {
if (isDisplayInfo()) {
doInfo(message);
}
}

/**
* output implicit message.
*
* @param message message
*/
public abstract void implicit(String message);
public void implicit(String message) {
if (isDisplayImplicit()) {
doImplicit(message);
}
}

/**
* output succeed message.
*
* @param message message
*/
public abstract void succeed(String message);
public void succeed(String message) {
if (isDisplaySucceed()) {
doSucceed(message);
}
}

/**
* output warning message.
*
* @param message message
*/
public abstract void warn(String message);
public void warn(String message) {
if (isDisplayWarn()) {
doWarn(message);
}
}

/**
* output warning message.
Expand All @@ -68,6 +99,74 @@ public void warn(ServerException e) {
warn(message);
}

/**
* is output information message.
*
* @return {@code true} if output
*/
protected boolean isDisplayInfo() {
return true;
}

/**
* is output implicit message.
*
* @return {@code true} if output
*/
protected boolean isDisplayImplicit() {
return isDisplay(TgsqlCvKey.DISPLAY_IMPLICIT);
}

/**
* is output succeed message.
*
* @return {@code true} if output
*/
protected boolean isDisplaySucceed() {
return isDisplay(TgsqlCvKey.DISPLAY_SUCCEED);
}

/**
* is output warning message.
*
* @return {@code true} if output
*/
protected boolean isDisplayWarn() {
return true;
}

protected boolean isDisplay(TgsqlCvKeyBoolean key) {
return config.getClientVariableMap().get(key, true);
}

/**
* output information message.
*
* @param message message
*/
protected abstract void doInfo(String message);

/**
* output implicit message.
*
* @param message message
*/
protected abstract void doImplicit(String message);

/**
* output succeed message.
*
* @param message message
*/
protected abstract void doSucceed(String message);

/**
* output warning message.
*
* @param message message
*/
protected abstract void doWarn(String message);

//

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import com.tsurugidb.tgsql.core.config.TgsqlCvKey;
import com.tsurugidb.tgsql.core.exception.TgsqlNoMessageException;
import com.tsurugidb.tgsql.core.executor.explain.StatementMetadataHandler;
import com.tsurugidb.tgsql.core.executor.report.BasicReporter;
import com.tsurugidb.tgsql.core.executor.report.TestReporter;
import com.tsurugidb.tgsql.core.executor.result.ResultProcessor;
import com.tsurugidb.tgsql.core.executor.sql.PreparedStatementResult;
import com.tsurugidb.tgsql.core.executor.sql.SqlProcessor;
Expand Down Expand Up @@ -744,7 +744,7 @@ public StatementMetadata explain(String statement, Region region) throws IOExcep
};
MockResultProcessor rs = new MockResultProcessor();
var reached = new AtomicBoolean();
var reporter = new BasicReporter() {
var reporter = new TestReporter() {
@Override
public void reportExecutionPlan(String source, PlanGraph plan) {
reached.set(true);
Expand Down Expand Up @@ -772,7 +772,7 @@ public StatementMetadata explain(String statement, Region region) throws IOExcep
};
MockResultProcessor rs = new MockResultProcessor();
var reached = new AtomicBoolean();
var reporter = new BasicReporter() {
var reporter = new TestReporter() {
@Override
public void reportExecutionPlan(String source, PlanGraph plan) {
reached.set(true);
Expand All @@ -792,7 +792,7 @@ public void reportExecutionPlan(String source, PlanGraph plan) {
void explain_statement_option_unknown() throws Exception {
MockSqlProcessor sql = new MockSqlProcessor(true);
MockResultProcessor rs = new MockResultProcessor();
var reporter = new BasicReporter();
var reporter = new TestReporter();
var engine = new BasicEngine(new TgsqlConfig(), sql, rs, reporter);
assertThrows(EngineException.class, () -> engine.execute(parse("EXPLAIN (INVALID_OPTION=TRUE) SELECT 1")));
}
Expand All @@ -801,7 +801,7 @@ void explain_statement_option_unknown() throws Exception {
void explain_statement_option_invalid() throws Exception {
MockSqlProcessor sql = new MockSqlProcessor(true);
MockResultProcessor rs = new MockResultProcessor();
var reporter = new BasicReporter();
var reporter = new TestReporter();
var engine = new BasicEngine(new TgsqlConfig(), sql, rs, reporter);
assertThrows(EngineException.class, () -> engine.execute(parse(String.format("EXPLAIN (%s=NULL) SELECT 1", StatementMetadataHandler.KEY_VERBOSE))));
}
Expand All @@ -816,7 +816,7 @@ public StatementMetadata explain(String statement, Region region) throws IOExcep
}
};
MockResultProcessor rs = new MockResultProcessor();
var reporter = new BasicReporter();
var reporter = new TestReporter();
var engine = new BasicEngine(new TgsqlConfig(), sql, rs, reporter);
var e = assertThrows(TgsqlNoMessageException.class, () -> engine.execute(parse("EXPLAIN SELECT 1")));
assertInstanceOf(EngineException.class, e.getCause());
Expand Down Expand Up @@ -929,7 +929,7 @@ void erroneous_statement_unknown() throws Exception {

private static BasicEngine newBasicEngine(MockSqlProcessor sql, MockResultProcessor rs) {
var config = new TgsqlConfig();
var reporter = new BasicReporter();
var reporter = new TestReporter(config);
return new BasicEngine(config, sql, rs, reporter);
}

Expand Down
Loading

0 comments on commit 964718a

Please sign in to comment.