Skip to content

Commit

Permalink
feat: output the number of updated rows
Browse files Browse the repository at this point in the history
  • Loading branch information
hishidama committed Oct 24, 2023
1 parent ab553d3 commit 52a1916
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,18 @@ public boolean executeGenericStatement(@Nonnull Statement statement) throws Engi
boolean transactionSatrtedImplicitly = checkTransactionActive(statement, true);
try {
executeTiming(timingEnd -> {
try (var rs = sqlProcessor.execute(statement.getText(), statement.getRegion())) {
try (var result = sqlProcessor.execute(statement.getText(), statement.getRegion())) {
var rs = result.getResultSet();
if (rs != null) {
timingEnd.accept(resultSetProcessor.process(rs));
} else {
timingEnd.accept(System.nanoTime());
reporter.reportStatementResult();
var er = result.getExecuteResult();
if (er != null) {
reporter.reportStatementResult(er);
} else {
reporter.reportStatementResult();
}
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.text.MessageFormat;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
Expand All @@ -21,6 +22,8 @@
import com.tsurugidb.sql.proto.SqlRequest.WritePreserve;
import com.tsurugidb.tsubakuro.exception.ServerException;
import com.tsurugidb.tsubakuro.explain.PlanGraph;
import com.tsurugidb.tsubakuro.sql.CounterType;
import com.tsurugidb.tsubakuro.sql.ExecuteResult;
import com.tsurugidb.tsubakuro.sql.SqlServiceException;
import com.tsurugidb.tsubakuro.sql.TableMetadata;

Expand Down Expand Up @@ -405,6 +408,47 @@ protected void reportTransactionException(String message, SqlServiceException ex
}
}

/**
* output message for result of statement.
*
* @param result execute result
*/
public void reportStatementResult(ExecuteResult result) {
var counterMap = result.getCounters();
if (counterMap.isEmpty()) {
reportStatementResult();
return;
}
var sb = new StringBuilder();
sb.append("(");
for (var entry : counterMap.entrySet()) {
if (sb.charAt(sb.length() - 1) != '(') {
sb.append(", ");
}
sb.append(entry.getValue());
sb.append(" rows ");
sb.append(getCounterName(entry.getKey()));
}
sb.append(")");

succeed(sb.toString());
}

/**
* get counter name.
*
* @param counterType counter type
* @return counter name
*/
protected String getCounterName(CounterType counterType) {
String name = counterType.name().toLowerCase(Locale.ENGLISH);
String suffix = "_rows";
if (name.endsWith(suffix)) {
name = name.substring(0, name.length() - suffix.length());
}
return name;
}

/**
* output message for result of statement.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import com.tsurugidb.tsubakuro.common.Session;
import com.tsurugidb.tsubakuro.common.SessionBuilder;
import com.tsurugidb.tsubakuro.exception.ServerException;
import com.tsurugidb.tsubakuro.sql.ResultSet;
import com.tsurugidb.tsubakuro.sql.SqlClient;
import com.tsurugidb.tsubakuro.sql.SqlServiceException;
import com.tsurugidb.tsubakuro.sql.StatementMetadata;
Expand Down Expand Up @@ -164,19 +163,20 @@ public void rollbackTransaction() throws ServerException, IOException, Interrupt
}

@Override
public @Nullable ResultSet execute(@Nonnull String statement, @Nullable Region region) throws ServerException, IOException, InterruptedException {
public @Nullable PreparedStatementResult execute(@Nonnull String statement, @Nullable Region region) throws ServerException, IOException, InterruptedException {
Objects.requireNonNull(statement);
LOG.debug("start prepare: '{}'", statement);
desireActive();
var client = getSqlClient();
try (var prepared = client.prepare(statement).await()) {
if (prepared.hasResultRecords()) {
LOG.debug("start query: '{}'", statement);
return transaction.executeQuery(prepared).await();
var result = transaction.executeQuery(prepared).await();
return new PreparedStatementResult(result);
}
LOG.debug("start execute: '{}'", statement);
transaction.executeStatement(prepared).await();
return null;
var result = transaction.executeStatement(prepared).await();
return new PreparedStatementResult(result);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.tsurugidb.console.core.executor.sql;

import java.io.IOException;

import com.tsurugidb.tsubakuro.exception.ServerException;
import com.tsurugidb.tsubakuro.sql.ExecuteResult;
import com.tsurugidb.tsubakuro.sql.ResultSet;

public class PreparedStatementResult implements AutoCloseable {

private final ResultSet resultSet;
private final ExecuteResult executeResult;

public PreparedStatementResult(ResultSet resultSet) {
this.resultSet = resultSet;
this.executeResult = null;
}

public PreparedStatementResult(ExecuteResult executeResult) {
this.resultSet = null;
this.executeResult = executeResult;
}

public ResultSet getResultSet() {
return this.resultSet;
}

public ExecuteResult getExecuteResult() {
return this.executeResult;
}

@Override
public void close() throws ServerException, IOException, InterruptedException {
try (resultSet) {

Check warning on line 34 in modules/core/src/main/java/com/tsurugidb/console/core/executor/sql/PreparedStatementResult.java

View workflow job for this annotation

GitHub Actions / Checkstyle

blocks.EmptyBlockCheck

Must have at least one statement.
// close only
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import com.tsurugidb.console.core.model.Region;
import com.tsurugidb.sql.proto.SqlRequest;
import com.tsurugidb.tsubakuro.exception.ServerException;
import com.tsurugidb.tsubakuro.sql.ResultSet;
import com.tsurugidb.tsubakuro.sql.SqlServiceException;
import com.tsurugidb.tsubakuro.sql.StatementMetadata;
import com.tsurugidb.tsubakuro.sql.TableMetadata;
Expand Down Expand Up @@ -114,7 +113,7 @@ public interface SqlProcessor extends ServerResource {
* @throws InterruptedException if interrupted while executing the statement
*/
@Nullable
ResultSet execute(@Nonnull String statement, @Nullable Region region) throws ServerException, IOException, InterruptedException;
PreparedStatementResult execute(@Nonnull String statement, @Nullable Region region) throws ServerException, IOException, InterruptedException;

/**
* Starts a new transaction. After this operation, this object will hold the started transaction as active.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.io.IOException;
import java.io.StringReader;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Predicate;

Expand All @@ -22,6 +23,7 @@
import com.tsurugidb.console.core.executor.explain.StatementMetadataHandler;
import com.tsurugidb.console.core.executor.report.BasicReporter;
import com.tsurugidb.console.core.executor.result.ResultProcessor;
import com.tsurugidb.console.core.executor.sql.PreparedStatementResult;
import com.tsurugidb.console.core.executor.sql.SqlProcessor;
import com.tsurugidb.console.core.model.Region;
import com.tsurugidb.console.core.model.Statement;
Expand All @@ -32,6 +34,8 @@
import com.tsurugidb.tsubakuro.explain.PlanGraph;
import com.tsurugidb.tsubakuro.explain.PlanNode;
import com.tsurugidb.tsubakuro.explain.json.JsonPlanGraphLoader;
import com.tsurugidb.tsubakuro.sql.CounterType;
import com.tsurugidb.tsubakuro.sql.ExecuteResult;
import com.tsurugidb.tsubakuro.sql.ResultSet;
import com.tsurugidb.tsubakuro.sql.SqlServiceException;
import com.tsurugidb.tsubakuro.sql.StatementMetadata;
Expand Down Expand Up @@ -98,7 +102,7 @@ public SqlServiceException getTransactionException() throws ServerException, IOE
}

@Override
public ResultSet execute(String statement, Region region) throws ServerException, IOException, InterruptedException {
public PreparedStatementResult execute(String statement, Region region) throws ServerException, IOException, InterruptedException {
throw new UnsupportedOperationException();
}

Expand Down Expand Up @@ -145,12 +149,18 @@ void generic_staement_wo_result() throws Exception {
var reached = new AtomicBoolean();
MockSqlProcessor sql = new MockSqlProcessor(true) {
@Override
public ResultSet execute(String statement, Region region) {
public PreparedStatementResult execute(String statement, Region region) {
if (!reached.compareAndSet(false, true)) {
fail();
}
assertEquals("INSERT INTO A DEFAULT VALUES", statement);
return null;
var er = new ExecuteResult() {
@Override
public Map<CounterType, Long> getCounters() {
return Map.of(CounterType.INSERTED_ROWS, 1L);
}
};
return new PreparedStatementResult(er);
}
};
MockResultProcessor rs = new MockResultProcessor();
Expand All @@ -165,12 +175,13 @@ void generic_staement_w_result() throws Exception {
var reachedExec = new AtomicBoolean();
MockSqlProcessor sql = new MockSqlProcessor(true) {
@Override
public ResultSet execute(String statement, Region region) {
public PreparedStatementResult execute(String statement, Region region) {
if (!reachedExec.compareAndSet(false, true)) {
fail();
}
assertEquals("SELECT * FROM T", statement);
return Relation.of(new Object[][] { { 1 } }).getResultSet(new ResultSetMetadataAdapter(SqlResponse.ResultSetMetadata.newBuilder().addColumns(Types.column(int.class)).build()));
var rs = Relation.of(new Object[][] { { 1 } }).getResultSet(new ResultSetMetadataAdapter(SqlResponse.ResultSetMetadata.newBuilder().addColumns(Types.column(int.class)).build()));
return new PreparedStatementResult(rs);
}
};
var reachedRs = new AtomicBoolean();
Expand Down Expand Up @@ -200,12 +211,18 @@ void call_staement_fall_through() throws Exception {
var reached = new AtomicBoolean();
MockSqlProcessor sql = new MockSqlProcessor(true) {
@Override
public ResultSet execute(String statement, Region region) {
public PreparedStatementResult execute(String statement, Region region) {
if (!reached.compareAndSet(false, true)) {
fail();
}
assertEquals("CALL proc()", statement);
return null;
var er = new ExecuteResult() {
@Override
public Map<CounterType, Long> getCounters() {
return Map.of();
}
};
return new PreparedStatementResult(er);
}
};
MockResultProcessor rs = new MockResultProcessor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.tsurugidb.sql.proto.SqlRequest;
import com.tsurugidb.sql.proto.SqlRequest.Placeholder;
import com.tsurugidb.sql.proto.SqlResponse;
import com.tsurugidb.tsubakuro.sql.ExecuteResult;
import com.tsurugidb.tsubakuro.sql.PreparedStatement;
import com.tsurugidb.tsubakuro.sql.ResultSet;
import com.tsurugidb.tsubakuro.sql.SqlClient;
Expand Down Expand Up @@ -151,7 +152,7 @@ void execute_wo_result() throws Exception {
PreparedStatement ps = createPreparedStatement(false);
Transaction tx = new Transaction() {
@Override
public FutureResponse<Void> executeStatement(
public FutureResponse<ExecuteResult> executeStatement(
PreparedStatement statement,
Collection<? extends SqlRequest.Parameter> parameters) throws IOException {
if (!reached.compareAndSet(false, true)) {
Expand All @@ -175,7 +176,8 @@ public FutureResponse<PreparedStatement> prepare(
};
try (var sql = new BasicSqlProcessor(client)) {
sql.startTransaction(SqlRequest.TransactionOption.getDefaultInstance());
try (var rs = sql.execute("", new Region(0, 0, 0, 0))) {
try (var result = sql.execute("", new Region(0, 0, 0, 0))) {
var rs = result.getResultSet();
assertNull(rs);
}
}
Expand Down Expand Up @@ -214,7 +216,8 @@ public FutureResponse<PreparedStatement> prepare(
};
try (var sql = new BasicSqlProcessor(client)) {
sql.startTransaction(SqlRequest.TransactionOption.getDefaultInstance());
try (var rs = sql.execute("", new Region(0, 0, 0, 0))) {
try (var result = sql.execute("", new Region(0, 0, 0, 0))) {
var rs = result.getResultSet();
assertSame(r, rs);
}
}
Expand Down

0 comments on commit 52a1916

Please sign in to comment.