
Begin;
Initialize;
InitializeSession "";
Command "create database TESTDB";
TerminateSession;

InitializeSession "TESTDB";

######################################################################

Command  "create table T(f int, g int generated by default as identity, h int)";

######################################################################


######################################################################
Command "drop table T";
Command "create table T(f int, g int generated by default as identity (INCREMENT BY 7 MAXVALUE 29 MINVALUE 3 CYCLE), h int)";

#g に値を指定しないで数件の挿入を数回繰り返す

Command "insert T(f, h) values (1, 10), (2, 20), (3, 30)";
Command "insert T(f, h) values (4, 40), (5, 50), (6, 60)";

#g の値が MIN から順番に割り当てられて、MAXに到達したらMINVALUEに戻っていることを確認する

Command "select * from T";
#⇒ 期待結果:
#   {1, 3, 10}
#   {2, 10, 20}
#   {3, 17, 30}
#   {4, 24, 40}
#   {5, 3, 50}
#   {6, 10, 60}


######################################################################


######################################################################
#表を作り直す

Command "drop table T";
Command "create table T(f int, g int generated by default as identity (INCREMENT BY -7 MAXVALUE -3 MINVALUE -27 NO CYCLE), h int)";

#g に値を指定しないで数件の挿入を数回繰り返す

Command "insert T(f, h) values (1, 10), (2, 20), (3, 30)";
Command "insert T(f, h) values (4, 40), (5, 50), (6, 60)";
#⇒ エラー(Sequence limit reached)

#g の値が MAX から順番に割り当てられて、エラーになったINSERT文は反映されていないことを確認する

Command "select * from T";
#⇒ 期待結果:
#   {1, -3, 10}
#   {2, -10, 20}
#   {3, -17, 30}


######################################################################


######################################################################

#表を作り直す

Command "drop table T";
Command "create table T(f int, g int generated by default as identity (INCREMENT BY 37 MAXVALUE 19 MINVALUE 3 CYCLE GET MAX), h int)";

#g に値を指定しないで数件の挿入を数回繰り返す

Command "insert T(f, h) values (1, 10), (2, 20), (3, 30)";
Command "insert T(f, h) values (4, 40), (5, 50), (6, 60)";

#g の値がすべて MIN になっていることを確認する

Command "select * from T";
#⇒ 期待結果:
#   {1, 3, 10}
#   {2, 3, 20}
#   {3, 3, 30}
#   {4, 3, 40}
#   {5, 3, 50}
#   {6, 3, 60}

#さらに g に範囲外の値を指定して数件の挿入を数回繰り返す

Command "insert T values (7, 700, 70), (8, 800, 80), (9, 900, 90)";
Command "insert T values (10, 1000, 100), (11, 1100, 110), (12, 1200, 120)";

#g の値が指定した値になっていることを確認する

Command "select * from T where f >= 7";
#⇒ 期待結果:
#   {7, 100, 70}
#   {8, 200, 80}
#   {9, 300, 90}
#   {10, 400, 100}
#   {11, 500, 110}
#   {12, 600, 120}

#もう一度 g に値を指定しないで数件の挿入を数回繰り返す

Command "insert T(f, h) values (100, 1000), (200, 2000), (300, 3000)";
Command "insert T(f, h) values (400, 4000), (500, 5000), (600, 6000)";

#g の値がすべて MIN になっていることを確認する

Command "select * from T where f >= 100";
#⇒ 期待結果:
#   {100, 3, 1000}
#   {200, 3, 2000}
#   {300, 3, 3000}
#   {400, 3, 4000}
#   {500, 3, 5000}
#   {600, 3, 6000}

######################################################################


######################################################################

#表を作り直す

Command "drop table T";
Command "create table T(f int, g int generated by default as identity (START WITH 5 MAXVALUE 8 MINVALUE -3 CYCLE), h int)";

#g に値を指定しないで数件の挿入を数回繰り返す(GENERATEが2周以上するように)

Command "insert T(f, h) values (1, 10), (2, 20), (3, 30)";
Command "insert T(f, h) values (4, 40), (5, 50), (6, 60)";
Command "insert T(f, h) values (7, 70), (8, 80), (9, 90)";
Command "insert T(f, h) values (10, 100), (11, 110), (12, 120)";
Command "insert T(f, h) values (13, 130), (14, 140), (15, 150)";
Command "insert T(f, h) values (16, 160), (17, 170), (18, 180)";

#g の値が 5 から始まって MAX に到達したら MIN から振られなおしていることを確認する

Command "select * from T";
#⇒ 期待結果:
#   {1, 5, 10}
#   {2, 6, 20}
#   {3, 7, 30}
#   {4, 8, 40}
#   {5, -3 50}
#   {6, -2, 60}
#   {7, -1, 70}
#   {8, 0, 80}
#   {9, 1, 90}
#   {10, 2, 100}
#   {11, 3 110}
#   {12, 4, 120}
#   {13, 5, 130}
#   {14, 6, 140}
#   {15, 7, 150}
#   {16, 8, 160}
#   {17, -3, 170}
#   {18, -2, 180}

######################################################################


######################################################################

Command "drop table T";
Command "create table T(f int, g int generated by default as identity (START WITH 5 INCREMENT BY 11 MAXVALUE -3 MINVALUE 5), h int)";
#⇒ エラー(SQLSyntaxError)
######################################################################


######################################################################

#表を作り直す

Command "create table T(f int, g int generated by default as identity (START WITH -12 MAXVALUE -9 MINVALUE -13 CYCLE GET MAX), h int)";

#g に値を指定しないで数件の挿入を数回繰り返す

Command "insert T(f, h) values (1, 10), (2, 20), (3, 30)";
Command "insert T(f, h) values (4, 40), (5, 50), (6, 60)";

#g の値が-12から始まってMAXに到達したらMINに戻っていることを確認する

Command "select * from T";
#⇒ 期待結果:
#   {1, -12, 10}
#   {2, -11, 20}
#   {3, -10, 30}
#   {4, -9, 40}
#   {5, -13, 50}
#   {6, -12, 60}

#さらに g に範囲外の値を指定して数件の挿入を数回繰り返す

Command "insert T values (7, 700, 70), (8, 800, 80), (9, 900, 90)";
Command "insert T values (10, 1000, 100), (11, 1100, 110), (12, 1200, 120)";

#g の値が指定した値になっていることを確認する

Command "select * from T where f >= 7";
#⇒ 期待結果:
#   {7, 100, 70}
#   {8, 200, 80}
#   {9, 300, 90}
#   {10, 400, 100}
#   {11, 500, 110}
#   {12, 600, 120}

#もう一度 g に値を指定しないで数件の挿入を数回繰り返す

Command "insert T(f, h) values (100, 1000), (200, 2000), (300, 3000)";
Command "insert T(f, h) values (400, 4000), (500, 5000), (600, 6000)";

#g の値が再びMINから振られていることを確認する

Command "select * from T where f >= 100";
#⇒ 期待結果:
#   {100, -13, 1000}
#   {200, -12, 2000}
#   {300, -11, 3000}
#   {400, -10, 4000}
#   {500, -9, 5000}
#   {600, -13, 6000}

######################################################################


######################################################################

#表を作り直す

Command "drop table T";
Command "create table T(f int, g int generated by default as identity (START WITH -5 INCREMENT BY 7 MAXVALUE 13 MINVALUE -13 GET MAX), h int)";

#g に値を指定しないで数件の挿入を数回繰り返す

Command "insert T(f, h) values (1, 10), (2, 20), (3, 30)";
Command "insert T(f, h) values (4, 40), (5, 50), (6, 60)";
#⇒ エラー

#g の値が-5から始まってMAXに到達したINSERT文が反映されていないことを確認する

Command "select * from T";
#⇒ 期待結果:
#   {1, -5, 10}
#   {2, 2, 20}
#   {3, 9, 30}

#さらに g に範囲外の値を指定して数件の挿入を数回繰り返す

Command "insert T values (7, 700, 70), (8, 800, 80), (9, 900, 90)";
Command "insert T values (10, 1000, 100), (11, 1100, 110), (12, 1200, 120)";

#g の値が指定した値になっていることを確認する

Command "select * from T where f >= 7";
#⇒ 期待結果:
#   {7, 100, 70}
#   {8, 200, 80}
#   {9, 300, 90}
#   {10, 400, 100}
#   {11, 500, 110}
#   {12, 600, 120}

######################################################################


######################################################################

Command "drop table T";
Command "create table T(f int, g int generated by default as identity (START WITH -5 INCREMENT BY -7 MINVALUE 3 CYCLE), h int)";
#⇒ エラー
######################################################################


######################################################################

#表を作り直す

Command "drop table T";
Command "create table T(f int, g int generated by default as identity (START WITH -5 INCREMENT BY 7 MAXVALUE 13 NO CYCLE), h int)";

#g に値を指定しないで数件の挿入を数回繰り返す

Command "insert T(f, h) values (1, 10), (2, 20), (3, 30)";
Command "insert T(f, h) values (4, 40), (5, 50), (6, 60)";
#⇒ エラー

#g の値が-5から始まってMAXに到達したINSERT文が反映されていないことを確認する

Command "select * from T";
#⇒ 期待結果:
#   {1, -5, 10}
#   {2, 2, 20}
#   {3, 9, 30}
######################################################################


######################################################################

#表を作り直す

Command "drop table T";
Command "create table T(f int, g int generated by default as identity (START WITH 541 MAXVALUE 13 MINVALUE 3 NO CYCLE), h int)";
#⇒ エラー
######################################################################


######################################################################

#表を作り直す

Command "drop table T";
Command "create table T(f int, g int generated by default as identity (START WITH 541 INCREMENT BY 7 MAXVALUE -4 CYCLE), h int)";
#⇒ エラー
######################################################################


######################################################################

#表を作り直す

Command "drop table T";
Command "create table T(f int, g int generated by default as identity (START WITH 541 INCREMENT BY -7 MAXVALUE 13 MINVALUE 3 GET MAX), h int)";
#⇒ エラー
######################################################################


######################################################################

#表を作り直す

Command "drop table T";
Command "create table T(f int, g int generated by default as identity (START WITH 541 INCREMENT BY 2147483651 MINVALUE -3 CYCLE), h int)";
#⇒ エラー
######################################################################


# tableの後始末
Command "drop table T";

TerminateSession;

InitializeSession "";
Command "drop database TESTDB";
TerminateSession;
Terminate;
End;