# マルチセッションの動作テスト。
# 2 read write transaction + 2 read only transaction, 
# read write transactionのすること = スキーマ操作 + DB操作 , 
# 共通のisolation level = no transaction。
# 1つ目のr/o transactionは常にT1のみのselect, 
# 2つ目のr/o transactionは常にT1とT2をjoinしたselect, 
# 3つ目のr/o transactionは常にbackupを行う。 
Begin;
BeginTimeSpan;
Initialize;
EndTimeSpan;
# テスト用のDBを作る
InitializeSession "";
Command "create database TESTDB";
TerminateSession;

InitializeSession "TESTDB";

# Tableの作成
Command  "create table T1(C1 int, C2 nchar(32))";
Command  "create table T2(C1 int, C2 nchar(32))";
Command  "create index I1_2 on T1(C2)";
Command  "create fulltext index I1_F0 on T1(C2)";
# タプルを準備する
Command  "insert into T1 (C1, C2) values (?, ?)" [1, "1"];
Command  "insert into T1 (C1, C2) values (?, ?)" [2, "2"];
Command  "insert into T1 (C1, C2) values (?, ?)" [3, "3"];
Command  "insert into T1 (C1, C2) values (?, ?)" [4, "4"];
Command  "insert into T1 (C1, C2) values (?, ?)" [5, "5"];
Command  "insert into T1 (C1, C2) values (?, ?)" [6, "6"];
Command  "insert into T1 (C1, C2) values (?, ?)" [7, "7"];
Command  "insert into T1 (C1, C2) values (?, ?)" [8, "8"];
Command  "insert into T1 (C1, C2) values (?, ?)" [9, "9"];
Command  "insert into T1 (C1, C2) values (?, ?)" [10, "10"];
Command  "insert into T1 (C1, C2) values (?, ?)" [11, "11"];
Command  "insert into T1 (C1, C2) values (?, ?)" [12, "12"];

# 複数のスレッドをここで同時実行
CreateThread "Thread1";
CreateThread "Thread2";
InitializeSession 3 "TESTDB";
CreateThread "Thread3";
InitializeSession 4 "TESTDB";
CreateThread "Thread4";
JoinThread "Thread3";
TerminateSession 3;
JoinThread "Thread4";
TerminateSession 4;
JoinThread "Thread1";
JoinThread "Thread2";

# Tableの内容の最終確認
Command  "select * from T1 order by C1";
Command  "select * from T2 order by C1";
# Tableの後始末
#Command  "drop table T1";
#Command  "drop table T2";
TerminateSession;

# DBの後始末
#InitializeSession "";
#Command "drop database TESTDB";
#TerminateSession;
# (障害回復を試すためTerminateしない)
End;

Thread1
{
InitializeSession 1 "TESTDB";
Command 1 "start transaction read only, using snapshot";
# 1..12についてlike検索を行う
Command 1 "select C2 from T1 where C2 like ? order by C2" ["%12%"];
Command 1 "select C2 from T1 where C2 like ? order by C2" ["%11%"];
Command 1 "select C2 from T1 where C2 like ? order by C2" ["%10%"];
Command 1 "select C2 from T1 where C2 like ? order by C2" ["%9%"];
Command 1 "select C2 from T1 where C2 like ? order by C2" ["%8%"];
Command 1 "select C2 from T1 where C2 like ? order by C2" ["%7%"];
Command 1 "select C2 from T1 where C2 like ? order by C2" ["%6%"];
Command 1 "select C2 from T1 where C2 like ? order by C2" ["%5%"];
Command 1 "select C2 from T1 where C2 like ? order by C2" ["%4%"];
Command 1 "select C2 from T1 where C2 like ? order by C2" ["%3%"];
Command 1 "select C2 from T1 where C2 like ? order by C2" ["%2%"];
Command 1 "select C2 from T1 where C2 like ? order by C2" ["%1%"];
Command 1 "commit";
TerminateSession 1;
}

Thread2
{
InitializeSession 2 "TESTDB";
Command 2 "start transaction read only, using snapshot";
# T1とT2のjoinに対してselectを行う
Command 2 "select T1.C1, T2.C1 from T1, T2 where T2.C1=? or T1.C2 like ? order by T1.C1, T2.C1" [1, "%1%"];
Command 2 "select T1.C1, T2.C1 from T1, T2 where T2.C1=? or T1.C2 like ? order by T1.C1, T2.C1" [2, "%2%"];
Command 2 "select T1.C1, T2.C1 from T1, T2 where T2.C1=? or T1.C2 like ? order by T1.C1, T2.C1" [3, "%3%"];
Command 2 "select T1.C1, T2.C1 from T1, T2 where T2.C1=? or T1.C2 like ? order by T1.C1, T2.C1" [4, "%4%"];
Command 2 "select T1.C1, T2.C1 from T1, T2 where T2.C1=? or T1.C2 like ? order by T1.C1, T2.C1" [5, "%5%"];
Command 2 "select T1.C1, T2.C1 from T1, T2 where T2.C1=? or T1.C2 like ? order by T1.C1, T2.C1" [6, "%6%"];
Command 2 "select T1.C1, T2.C1 from T1, T2 where T2.C1=? or T1.C2 like ? order by T1.C1, T2.C1" [7, "%7%"];
Command 2 "select T1.C1, T2.C1 from T1, T2 where T2.C1=? or T1.C2 like ? order by T1.C1, T2.C1" [8, "%8%"];
Command 2 "select T1.C1, T2.C1 from T1, T2 where T2.C1=? or T1.C2 like ? order by T1.C1, T2.C1" [9, "%9%"];
Command 2 "select T1.C1, T2.C1 from T1, T2 where T2.C1=? or T1.C2 like ? order by T1.C1, T2.C1" [10, "%10%"];
Command 2 "select T1.C1, T2.C1 from T1, T2 where T2.C1=? or T1.C2 like ? order by T1.C1, T2.C1" [11, "%11%"];
Command 2 "select T1.C1, T2.C1 from T1, T2 where T2.C1=? or T1.C2 like ? order by T1.C1, T2.C1" [12, "%12%"];
Command 2 "commit";
TerminateSession 2;
}

# transaction内ではスキーマ操作/DB操作/mount/unmountができない
Thread3
{
# index, table, areaについて操作を行う
Command 3 "set transaction read write";
Command 3 "create area IDX_AREA 'd:\\dm\\idx_area'";
Command 3 "set transaction read write";
Command 3 "create index IX on T1(C1)";
Command 3 "set transaction read write";
Command 3 "create index I2_2 on T2(C2)";
Command 3 "set transaction read write";
Command 3 "create fulltext index I1_F on T1(C2)";
Command 3 "set transaction read write";
Command 3 "create fulltext index I2_F on T2(C2)";
Command 3 "set transaction read write";
Command 3 "alter index IX set area IDX_AREA";
Command 3 "set transaction read write";
Command 3 "alter index IX drop area";
Command 3 "set transaction read write";
Command 3 "alter table T1 set area index IDX_AREA";
Command 3 "set transaction read write";
Command 3 "alter table T1 drop area index";
Command 3 "set transaction read write";
Command 3 "drop index IX";
Command 3 "set transaction read write";
Command 3 "drop area IDX_AREA";
Command 3 "set transaction read write";
Command 3 "drop index I2_2";
Command 3 "set transaction read write";
Command 3 "drop index I2_F";
Command 3 "set transaction read write";
Command 3 "drop index I1_F";

Command 3 "set transaction read write";
Command 3 "create area area1 'd:\\dm\\areatest'";
Command 3 "set transaction read write";
Command 3 "alter table T1 set area area1";
Command 3 "set transaction read write";
Command 3 "alter area area1 modify 'd:\\dm\\alterareatest'";
Command 3 "set transaction read write";
Command 3 "alter table T1 drop area";
Command 3 "set transaction read write";
Command 3 "drop area area1";

Command 3 "set transaction read write";
Command 3 "create area TBL_AREA 'd:\\dm\\tbl_area'";
Command 3 "set transaction read write";
Command 3 "alter table T1 set area TBL_AREA";
Command 3 "set transaction read write";
Command 3 "alter table T1 drop area";
Command 3 "set transaction read write";
Command 3 "drop area TBL_AREA";
}

# transaction内ではスキーマ操作/DB操作/mount/unmountができない
Thread4
{
# DBやそのareaについて操作を行う
Command 4 "set transaction read write";
Command 4 "alter database TESTDB set path 'd:\\dm\\db_area'";
Command 4 "set transaction read write";
Command 4 "alter database TESTDB drop path ''";
Command 4 "set transaction read write";
Command 4 "alter database TESTDB set path system 'd:\\dm\\db_area'";
Command 4 "set transaction read write";
Command 4 "alter database TESTDB drop path system ''";
}

