From 187b428d4c208d4670cebe47daa2600ed9f3165d Mon Sep 17 00:00:00 2001 From: Hishida Masato Date: Fri, 23 Aug 2024 12:21:10 +0900 Subject: [PATCH] feat: TsurugiSqlResult: Added setEnableCheckResultOnClose() --- .../iceaxe/sql/result/TsurugiQueryResult.java | 6 +- .../iceaxe/sql/result/TsurugiSqlResult.java | 48 ++++++++++++ .../sql/result/TsurugiStatementResult.java | 6 +- .../sql/result/TsurugiSqlResultTest.java | 74 +++++++++++++++++++ 4 files changed, 130 insertions(+), 4 deletions(-) create mode 100755 modules/iceaxe-core/src/test/java/com/tsurugidb/iceaxe/sql/result/TsurugiSqlResultTest.java diff --git a/modules/iceaxe-core/src/main/java/com/tsurugidb/iceaxe/sql/result/TsurugiQueryResult.java b/modules/iceaxe-core/src/main/java/com/tsurugidb/iceaxe/sql/result/TsurugiQueryResult.java index 60db805..8ca55ab 100755 --- a/modules/iceaxe-core/src/main/java/com/tsurugidb/iceaxe/sql/result/TsurugiQueryResult.java +++ b/modules/iceaxe-core/src/main/java/com/tsurugidb/iceaxe/sql/result/TsurugiQueryResult.java @@ -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) { diff --git a/modules/iceaxe-core/src/main/java/com/tsurugidb/iceaxe/sql/result/TsurugiSqlResult.java b/modules/iceaxe-core/src/main/java/com/tsurugidb/iceaxe/sql/result/TsurugiSqlResult.java index 9fdd74a..2249db6 100755 --- a/modules/iceaxe-core/src/main/java/com/tsurugidb/iceaxe/sql/result/TsurugiSqlResult.java +++ b/modules/iceaxe-core/src/main/java/com/tsurugidb/iceaxe/sql/result/TsurugiSqlResult.java @@ -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; @@ -27,6 +49,8 @@ public abstract class TsurugiSqlResult implements IceaxeTimeoutCloseable { /** close timeout. */ protected final IceaxeTimeout closeTimeout; + private Boolean enableCheckResultOnClose = null; + /** * Creates a new instance. *

@@ -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(); + } } diff --git a/modules/iceaxe-core/src/main/java/com/tsurugidb/iceaxe/sql/result/TsurugiStatementResult.java b/modules/iceaxe-core/src/main/java/com/tsurugidb/iceaxe/sql/result/TsurugiStatementResult.java index 901205f..6e4f1f7 100755 --- a/modules/iceaxe-core/src/main/java/com/tsurugidb/iceaxe/sql/result/TsurugiStatementResult.java +++ b/modules/iceaxe-core/src/main/java/com/tsurugidb/iceaxe/sql/result/TsurugiStatementResult.java @@ -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; diff --git a/modules/iceaxe-core/src/test/java/com/tsurugidb/iceaxe/sql/result/TsurugiSqlResultTest.java b/modules/iceaxe-core/src/test/java/com/tsurugidb/iceaxe/sql/result/TsurugiSqlResultTest.java new file mode 100755 index 0000000..9908d56 --- /dev/null +++ b/modules/iceaxe-core/src/test/java/com/tsurugidb/iceaxe/sql/result/TsurugiSqlResultTest.java @@ -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()); + } + } +}