-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
256 lines (214 loc) · 5.28 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package main
import (
"database/sql"
"fmt"
"log"
"os"
"os/exec"
"pg-isolation/isolation"
"pg-isolation/read_phenomena"
"runtime"
"strings"
"github.com/joho/godotenv"
_ "github.com/lib/pq"
)
var conn *sql.DB
var clear map[string]func()
func init() {
clear = make(map[string]func())
clear["linux"] = func() {
cmd := exec.Command("clear") //Linux
cmd.Stdout = os.Stdout
cmd.Run()
}
clear["windows"] = func() {
cmd := exec.Command("cmd", "/c", "cls") //Windows
cmd.Stdout = os.Stdout
cmd.Run()
}
clear["darwin"] = func() {
cmd := exec.Command("clear") //Mac
cmd.Stdout = os.Stdout
cmd.Run()
}
if _, ok := clear[runtime.GOOS]; !ok {
log.Fatal("Your platform is unsupported!")
}
}
func main() {
var err error
err = godotenv.Load()
if err != nil {
log.Fatal(err)
}
var topic Topic
var readPhenomena read_phenomena.ReadPhenomena
var isolationLevel isolation.Isolation
config := ParseConfig()
db := &DB{
Config: config,
}
conn, err = db.CreateConn()
if err != nil {
log.Fatal("[x] failed to connect to database", err)
}
MainLoop:
for {
Clear()
// choose topic
for {
topic, err = chooseTopic()
if err == nil && topic != TOPIC_UNSPECIFIED {
break
}
fmt.Println("\nChoose the right Topic option!")
}
if topic == TOPIC_READ_PHENOMENA {
for {
readPhenomena, err = chooseReadPhenomena()
if err == nil && readPhenomena != read_phenomena.READ_PHENOMENA_UNSPECIFIED {
break
}
fmt.Println("\nChoose the right Read Phenomena option!")
}
// repeatable read
if readPhenomena == read_phenomena.READ_PHENOMENA_NON_REPEATABLE_READ {
Clear()
nonRepeatableRead := read_phenomena.NonRepeatableRead{
DB: conn,
Migrate: db.Migrate,
}
err := nonRepeatableRead.Run()
if err != nil {
log.Fatal(err)
}
} else if readPhenomena == read_phenomena.READ_PHENOMENA_PHANTOM_READ {
Clear()
phantomRead := read_phenomena.PhantomRead{
DB: conn,
Migrate: db.Migrate,
}
err := phantomRead.Run()
if err != nil {
log.Fatal(err)
}
} else if readPhenomena == read_phenomena.READ_PHENOMENA_SERIALIZATION_ANOMALY {
Clear()
serializationAnomaly := read_phenomena.SerializationAnomaly{
DB: conn,
Migrate: db.Migrate,
}
err := serializationAnomaly.Run()
if err != nil {
log.Fatal(err)
}
}
} else if topic == TOPIC_PG_ISOLATION_LEVEL {
for {
isolationLevel, err = chooseIsolationLevel()
if err == nil && isolationLevel != isolation.ISOLATION_UNSPECIFIED {
break
}
fmt.Println("\nChoose the right Isolation Level option!")
}
// read committed
if isolationLevel == isolation.ISOLATION_READ_COMMITTED {
Clear()
readCommitted := isolation.RadCommitted{
DB: conn,
Migrate: db.Migrate,
}
err = readCommitted.Run()
if err != nil {
log.Fatal(err)
}
} else if isolationLevel == isolation.ISOLATION_REPEATABLE_READ {
Clear()
repeatableRead := isolation.RepeatableRead{
DB: conn,
Migrate: db.Migrate,
}
err = repeatableRead.Run()
if err != nil {
log.Fatal(err)
}
} else if isolationLevel == isolation.ISOLATION_SERIALIZABLE {
// TODO: Implement this
}
}
RunAgainLoop:
for {
var confirmMessage = "\nDo you want to see another example?"
fmt.Println(confirmMessage)
fmt.Println(strings.Repeat("-", len(confirmMessage)))
fmt.Println("[0] No")
fmt.Println("[1] Yes")
fmt.Print("\n-> ")
var runAgain int
_, err = fmt.Scan(&runAgain)
if err != nil {
log.Fatal(err)
}
switch runAgain {
case 0:
break MainLoop
case 1:
break RunAgainLoop
default:
fmt.Println("\nChoose the right option!")
}
}
}
}
func chooseTopic() (Topic, error) {
var msg1 = "\nChoose one of the topics below:"
fmt.Println(msg1)
fmt.Println(strings.Repeat("-", len(msg1)))
fmt.Println("[1] Read Phenomena")
fmt.Println("[2] PG Isolation Level")
fmt.Print("\n-> ")
var topic Topic
_, err := fmt.Scan(&topic)
if err != nil || !topic.Valid() {
return TOPIC_UNSPECIFIED, ErrInvalidInput
}
return topic, nil
}
func chooseReadPhenomena() (read_phenomena.ReadPhenomena, error) {
var msg1 = "\nChoose one of the Read Phenomena below:"
fmt.Println(msg1)
fmt.Println(strings.Repeat("-", len(msg1)))
fmt.Println("[1] Non Repeatable Read")
fmt.Println("[2] Phantom Read")
fmt.Println("[3] Serialization Anomaly")
fmt.Print("\n-> ")
var readPhenomena read_phenomena.ReadPhenomena
_, err := fmt.Scan(&readPhenomena)
if err != nil || !readPhenomena.Valid() {
return read_phenomena.READ_PHENOMENA_UNSPECIFIED, ErrInvalidInput
}
return readPhenomena, nil
}
func chooseIsolationLevel() (isolation.Isolation, error) {
var msg1 = "\nChoose one of the Isolation Level below:"
fmt.Println(msg1)
fmt.Println(strings.Repeat("-", len(msg1)))
fmt.Println("[1] Read Committed")
fmt.Println("[2] Repeatable Read")
fmt.Println("[3] Serializable")
fmt.Print("\n-> ")
var isolationLevel isolation.Isolation
_, err := fmt.Scan(&isolationLevel)
if err != nil || !isolationLevel.Valid() {
return isolation.ISOLATION_UNSPECIFIED, ErrInvalidInput
}
return isolationLevel, nil
}
func Clear() {
value, ok := clear[runtime.GOOS]
if ok {
value()
} else {
log.Fatal("Your platform is unsupported!")
}
}