Skip to content

Commit

Permalink
refactor: fix example
Browse files Browse the repository at this point in the history
  • Loading branch information
hishidama committed Aug 15, 2024
1 parent 759c618 commit 997a467
Show file tree
Hide file tree
Showing 11 changed files with 274 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import com.tsurugidb.iceaxe.session.TgSessionOption;
import com.tsurugidb.iceaxe.session.TgSessionOption.TgTimeoutKey;
import com.tsurugidb.iceaxe.session.TgSessionShutdownType;
import com.tsurugidb.iceaxe.transaction.TgCommitType;

/**
Expand All @@ -13,6 +14,14 @@
*/
public class Example02SessionOption {

void label() {
var sessionOption = TgSessionOption.of();
sessionOption.setApplicationName("application name");
sessionOption.setLabel("session label");

// $TSURUGI_HOME/bin/tgctl session list --verbose
}

void timeout() {
var sessionOption = TgSessionOption.of();
sessionOption.setTimeout(TgTimeoutKey.DEFAULT, 1, TimeUnit.MINUTES);
Expand All @@ -23,4 +32,9 @@ void commitType() {
var sessionOption = TgSessionOption.of();
sessionOption.setCommitType(TgCommitType.DEFAULT);
}

void shutdownType() {
var sessionOption = TgSessionOption.of();
sessionOption.setCloseShutdownType(TgSessionShutdownType.GRACEFUL);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@ public class Example04TransactionManager {
*/
private static final TgTmSetting SETTING = TgTmSetting.of(OCC, LTX);

void main() throws IOException, InterruptedException {
public static void main(String... args) throws IOException, InterruptedException {
try (var session = Example02Session.createSession()) {
manager1(session);
manager2(session);
new Example04TransactionManager().main(session);
}
}

void main(TsurugiSession session) throws IOException, InterruptedException {
manager1(session);
manager2(session);
}

void manager1(TsurugiSession session) throws IOException, InterruptedException {
var tm = session.createTransactionManager(SETTING);
tm.execute(transaction -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,23 @@
*/
public class Example11Ddl {

void main() throws IOException, InterruptedException {
public static void main(String... args) throws IOException, InterruptedException {
try (var session = Example02Session.createSession()) {
// TsurugiTransactionManagerのexecuteDdlメソッドを使う場合、
// TgTmSettingが指定されていなかったら、暗黙にLTXとして実行する。
var tm = session.createTransactionManager();
createTable(tm);
dropTable(tm);
new Example11Ddl().main(session);
}
}

existsTable(session);
getTableMetadata(session);
void main(TsurugiSession session) throws IOException, InterruptedException {
// TsurugiTransactionManagerのexecuteDdlメソッドを使う場合、
// TgTmSettingが指定されていなかったら、暗黙にLTXとして実行する。
var tm = session.createTransactionManager();

dropAndCreateTable(session);
if (existsTable(session)) {
dropTable(tm);
}
createTable(tm);

getTableMetadata(session);
}

void createTable(TsurugiTransactionManager tm) throws IOException, InterruptedException {
Expand All @@ -42,12 +46,14 @@ void dropTable(TsurugiTransactionManager tm) throws IOException, InterruptedExce
tm.executeDdl(sql);
}

void existsTable(TsurugiSession session) throws IOException, InterruptedException {
boolean existsTable(TsurugiSession session) throws IOException, InterruptedException {
var metadataOpt = session.findTableMetadata("TEST");
if (metadataOpt.isPresent()) {
System.out.println("table exists");
return true;
} else {
System.out.println("table not exists");
return false;
}
}

Expand All @@ -62,7 +68,7 @@ void getTableMetadata(TsurugiSession session) throws IOException, InterruptedExc
}
}

void dropAndCreateTable(TsurugiSession session) throws IOException, InterruptedException {
public static void dropAndCreateTable(TsurugiSession session) throws IOException, InterruptedException {
var tm = session.createTransactionManager(TgTxOption.ofDDL());
tm.execute(transaction -> {
if (transaction.getSession().findTableMetadata("TEST").isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.RecursiveAction;

Expand All @@ -24,24 +25,32 @@
*/
public class Example21Insert {

void main() throws IOException, InterruptedException {
public static void main(String... args) throws IOException, InterruptedException {
try (var session = Example02Session.createSession()) {
var setting = TgTmSetting.of(TgTxOption.ofOCC(), TgTxOption.ofLTX("TEST"));
var tm = session.createTransactionManager(setting);

insert_executeStatement(session, tm);
insert_executeAndGetCount(session, tm);
insert_tm(session, tm);
insert_tm_sql(tm);

insertBindParameter(session, tm);
insertEntity(session, tm);
insertEntityMapping(session, tm);
insertForkJoin(session, tm, List.of(/* entities */));
Example11Ddl.dropAndCreateTable(session);

new Example21Insert().main(session);
}
}

void main(TsurugiSession session) throws IOException, InterruptedException {
var setting = TgTmSetting.of(TgTxOption.ofOCC(), TgTxOption.ofLTX("TEST"));
var tm = session.createTransactionManager(setting);

insert_executeStatement(session, tm);
insert_executeAndGetCount(session, tm);
insert_tm(session, tm);
insert_tm_sql(tm);

insertBindParameter(session, tm);
insertEntity(session, tm);
insertEntityMapping(session, tm);
insertForkJoin(session, tm);
}

void insert_executeStatement(TsurugiSession session, TsurugiTransactionManager tm) throws IOException, InterruptedException {
deleteAll(tm);

try (var ps = session.createStatement("insert into TEST values(123, 456, 'abc')")) {
int count = tm.execute(transaction -> {
try (var result = transaction.executeStatement(ps)) {
Expand All @@ -53,6 +62,8 @@ void insert_executeStatement(TsurugiSession session, TsurugiTransactionManager t
}

void insert_executeAndGetCount(TsurugiSession session, TsurugiTransactionManager tm) throws IOException, InterruptedException {
deleteAll(tm);

try (var ps = session.createStatement("insert into TEST values(123, 456, 'abc')")) {
int count = tm.execute(transaction -> {
return transaction.executeAndGetCount(ps);
Expand All @@ -62,18 +73,24 @@ void insert_executeAndGetCount(TsurugiSession session, TsurugiTransactionManager
}

void insert_tm(TsurugiSession session, TsurugiTransactionManager tm) throws IOException, InterruptedException {
deleteAll(tm);

try (var ps = session.createStatement("insert into TEST values(123, 456, 'abc')")) {
int count = tm.executeAndGetCount(ps);
System.out.println(count);
}
}

void insert_tm_sql(TsurugiTransactionManager tm) throws IOException, InterruptedException {
deleteAll(tm);

int count = tm.executeAndGetCount("insert into TEST values(123, 456, 'abc')");
System.out.println(count);
}

void insertBindParameter(TsurugiSession session, TsurugiTransactionManager tm) throws IOException, InterruptedException {
deleteAll(tm);

var foo = TgBindVariable.ofInt("foo");
var bar = TgBindVariable.ofLong("bar");
var zzz = TgBindVariable.ofString("zzz");
Expand Down Expand Up @@ -124,6 +141,8 @@ void insertBindParameter(TsurugiSession session, TsurugiTransactionManager tm) t
}

void insertEntity(TsurugiSession session, TsurugiTransactionManager tm) throws IOException, InterruptedException {
deleteAll(tm);

try (var ps = session.createStatement(TestEntity.INSERT_SQL, TgParameterMapping.of(TestEntity.VARIABLES, TestEntity::toParameter))) {
tm.execute(transaction -> {
// var entity = new TestEntity(123, 456L, "abc");
Expand All @@ -139,6 +158,8 @@ void insertEntity(TsurugiSession session, TsurugiTransactionManager tm) throws I
}

void insertEntityMapping(TsurugiSession session, TsurugiTransactionManager tm) throws IOException, InterruptedException {
deleteAll(tm);

var sql = "insert into TEST values(:foo, :bar, :zzz)";

TgParameterMapping<TestEntity> parameterMapping;
Expand Down Expand Up @@ -172,7 +193,15 @@ void insertEntityMapping(TsurugiSession session, TsurugiTransactionManager tm) t
}
}

void insertForkJoin(TsurugiSession session, TsurugiTransactionManager tm, List<TestEntity> entityList) throws IOException, InterruptedException {
void insertForkJoin(TsurugiSession session, TsurugiTransactionManager tm) throws IOException, InterruptedException {
deleteAll(tm);

var entityList = new ArrayList<TestEntity>();
for (int i = 0; i < 500; i++) {
var entity = new TestEntity(i, (long) i, Integer.toString(i));
entityList.add(entity);
}

try (var ps = session.createStatement(TestEntity.INSERT_SQL, TgParameterMapping.of(TestEntity.VARIABLES, TestEntity::toParameter))) {
tm.execute(transaction -> {
var task = new InsertTask(transaction, ps, entityList).fork();
Expand Down Expand Up @@ -217,4 +246,28 @@ protected void compute() {
}
}
}

private static void deleteAll(TsurugiTransactionManager tm) throws IOException, InterruptedException {
tm.executeAndGetCountDetail("delete from TEST");
}

public static void insert(TsurugiSession session) throws IOException, InterruptedException {
var tm = session.createTransactionManager(TgTxOption.ofLTX("TEST"));
tm.execute(transaction -> {
var foo = TgBindVariable.ofInt("foo");
var bar = TgBindVariable.ofLong("bar");
var zzz = TgBindVariable.ofString("zzz");
var variables = TgBindVariables.of(foo, bar, zzz);
var sql = "insert into TEST values(" + variables.getSqlNames() + ")";
var parameterMapping = TgParameterMapping.of(variables);

try (var ps = session.createStatement(sql, parameterMapping)) {
for (int i = 0; i < 5; i++) {
var parameter = TgBindParameters.of(foo.bind(i + 121), bar.bind(i * 10 + i), zzz.bind(String.valueOf((char) ('a' + i % 3))));
transaction.executeAndGetCountDetail(ps, parameter);
}
}
return;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,43 +20,50 @@
*/
public class Example31Select {

void main() throws IOException, InterruptedException {
public static void main(String... args) throws IOException, InterruptedException {
try (var session = Example02Session.createSession()) {
var setting = TgTmSetting.of(TgTxOption.ofOCC(), TgTxOption.ofRTX());
var tm = session.createTransactionManager(setting);
Example11Ddl.dropAndCreateTable(session);
Example21Insert.insert(session);

selectLoop(session, tm);
new Example31Select().main(session);
}
}

selectAsList_executeQuery(session, tm);
selectAsList_executeAndGetList(session, tm);
selectAsList_tm(session, tm);
selectAsList_tm_sql(tm);
void main(TsurugiSession session) throws IOException, InterruptedException {
var setting = TgTmSetting.of(TgTxOption.ofOCC(), TgTxOption.ofRTX());
var tm = session.createTransactionManager(setting);

selectAsEntityLoop(session, tm);
selectAsEntityList(session, tm);
selectLoop(session, tm);

selectByParameter1(session, tm);
selectByParameter1_singleVariable(session, tm);
selectByParameter2(session, tm);
selectByParameter2_bindVariable(session, tm);
selectByParameter2_asEntityList_executeQuery(session, tm);
selectByParameter2_asEntityList_executeAndGetList(session, tm);
selectByParameter2_asEntityList_tm(session, tm);
selectByParameter2_asEntityList_tm_sql(tm);
}
selectAsList_executeQuery(session, tm);
selectAsList_executeAndGetList(session, tm);
selectAsList_tm(session, tm);
selectAsList_tm_sql(tm);

selectAsEntityLoop(session, tm);
selectAsEntityList(session, tm);

selectByParameter1(session, tm);
selectByParameter1_singleVariable(session, tm);
selectByParameter2(session, tm);
selectByParameter2_bindVariable(session, tm);
selectByParameter2_asEntityList_executeQuery(session, tm);
selectByParameter2_asEntityList_executeAndGetList(session, tm);
selectByParameter2_asEntityList_tm(session, tm);
selectByParameter2_asEntityList_tm_sql(tm);
}

void selectLoop(TsurugiSession session, TsurugiTransactionManager tm) throws IOException, InterruptedException {
try (var ps = session.createQuery("select * from TEST")) {
tm.execute(transaction -> {
try (var result = transaction.executeQuery(ps)) {
List<String> nameList = result.getNameList();
System.out.println(nameList);
System.out.println("nameList=" + nameList);

for (TsurugiResultEntity entity : result) {
System.out.println(entity.getIntOrNull("FOO"));
System.out.println(entity.getLongOrNull("BAR"));
System.out.println(entity.getStringOrNull("ZZZ"));
System.out.print("FOO=" + entity.getIntOrNull("FOO"));
System.out.print(", BAR=" + entity.getLongOrNull("BAR"));
System.out.println(", ZZZ=" + entity.getStringOrNull("ZZZ"));
}
}
});
Expand Down Expand Up @@ -101,9 +108,9 @@ void selectAsEntityLoop(TsurugiSession session, TsurugiTransactionManager tm) th
tm.execute(transaction -> {
try (var result = transaction.executeQuery(ps)) {
for (TestEntity entity : result) {
System.out.println(entity.getFoo());
System.out.println(entity.getBar());
System.out.println(entity.getZzz());
System.out.print("FOO=" + entity.getFoo());
System.out.print(", BAR=" + entity.getBar());
System.out.println(", ZZZ=" + entity.getZzz());
}
}
});
Expand Down
Loading

0 comments on commit 997a467

Please sign in to comment.