Skip to content

Commit

Permalink
feat: TsurugiSqlResult: Added setEnableCheckResultOnClose()
Browse files Browse the repository at this point in the history
  • Loading branch information
hishidama committed Aug 23, 2024
1 parent 7f12ee3 commit 187b428
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -583,8 +583,10 @@ public void close(long timeoutNanos) throws IOException, InterruptedException, T

Throwable occurred = null;
try {
if (this.checkResultOnClose) {
getLowResultSet();
if (enableCheckResultOnClose()) {
if (this.checkResultOnClose) {
getLowResultSet();
}
}
callEndEvent();
} catch (Throwable e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,28 @@
*/
public abstract class TsurugiSqlResult implements IceaxeTimeoutCloseable {

private static boolean defaultEnableCheckResultOnClose = true; // TODO デフォルトをfalseにしたい

/**
* set default enable check result on close.
*
* @param enabled {@code true}: check result on close
* @since X.X.X
*/
public static void setDefaultEnableCheckResultOnClose(boolean enabled) {
defaultEnableCheckResultOnClose = enabled;
}

/**
* get default enable check result on close.
*
* @return {@code true}: check result on close
* @since X.X.X
*/
public static boolean getDefaultEnableCheckResultOnClose() {
return defaultEnableCheckResultOnClose;
}

private final int iceaxeSqlExecuteId;
private final TsurugiTransaction ownerTransaction;
private final TsurugiSql sqlStatement;
Expand All @@ -27,6 +49,8 @@ public abstract class TsurugiSqlResult implements IceaxeTimeoutCloseable {
/** close timeout. */
protected final IceaxeTimeout closeTimeout;

private Boolean enableCheckResultOnClose = null;

/**
* Creates a new instance.
* <p>
Expand Down Expand Up @@ -133,4 +157,28 @@ public TsurugiTransactionException fillToTsurugiException(TsurugiTransactionExce
public void close() throws IOException, InterruptedException, TsurugiTransactionException {
ownerTransaction.removeChild(this);
}

/**
* set enable check result on close.
*
* @param enabled {@code true}: check result on close
* @since X.X.X
*/
public void setEnableCheckResultOnClose(boolean enabled) {
this.enableCheckResultOnClose = enabled;
}

/**
* get enable check result on close.
*
* @return {@code true}: check result on close
* @since X.X.X
*/
protected boolean enableCheckResultOnClose() {
Boolean enabled = this.enableCheckResultOnClose;
if (enabled != null) {
return enabled.booleanValue();
}
return getDefaultEnableCheckResultOnClose();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,10 @@ public void close(long timeoutNanos) throws IOException, InterruptedException, T

Throwable occurred = null;
try {
if (this.checkResultOnClose) {
checkLowResult();
if (enableCheckResultOnClose()) {
if (this.checkResultOnClose) {
checkLowResult();
}
}
} catch (Throwable e) {
occurred = e;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.tsurugidb.iceaxe.sql.result;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import com.tsurugidb.iceaxe.session.TgSessionOption;
import com.tsurugidb.iceaxe.session.TsurugiSession;
import com.tsurugidb.iceaxe.test.TestTsurugiSession;
import com.tsurugidb.iceaxe.transaction.TsurugiTransaction;
import com.tsurugidb.iceaxe.transaction.option.TgTxOption;

class TsurugiSqlResultTest {

private static boolean backupDefaultEnableCheckResultOnClose;

@BeforeAll
static void beforeAll() {
backupDefaultEnableCheckResultOnClose = TsurugiSqlResult.getDefaultEnableCheckResultOnClose();
}

@AfterEach
void afterEach() {
TsurugiSqlResult.setDefaultEnableCheckResultOnClose(backupDefaultEnableCheckResultOnClose);
}

static class TestSqlResult extends TsurugiSqlResult {

public static TestSqlResult create() {
var sessionOption = TgSessionOption.of();
var session = new TestTsurugiSession(sessionOption);
var transaction = new TsurugiTransaction(session, TgTxOption.ofOCC());
return new TestSqlResult(transaction);
}

private TsurugiSession session;

public TestSqlResult(TsurugiTransaction transaction) {
super(0, transaction, null, null, null, null);
this.session = transaction.getSession();
}

@Override
public void close(long timeoutNanos) throws Exception {
try (var c = session) {
// close only
}
}
}

@Test
void enableCheckResultOnClose() throws Exception {
try (var result = TestSqlResult.create()) {
assertEquals(backupDefaultEnableCheckResultOnClose, result.enableCheckResultOnClose());

TsurugiSqlResult.setDefaultEnableCheckResultOnClose(false);
assertFalse(result.enableCheckResultOnClose());
TsurugiSqlResult.setDefaultEnableCheckResultOnClose(true);
assertTrue(result.enableCheckResultOnClose());

TsurugiSqlResult.setDefaultEnableCheckResultOnClose(false);
result.setEnableCheckResultOnClose(true);
assertTrue(result.enableCheckResultOnClose());

TsurugiSqlResult.setDefaultEnableCheckResultOnClose(true);
result.setEnableCheckResultOnClose(false);
assertFalse(result.enableCheckResultOnClose());
}
}
}

0 comments on commit 187b428

Please sign in to comment.