-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
67 lines (61 loc) · 1.44 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
package main
/*
#include <stdio.h>
#include <stdlib.h>
*/
import "C"
import (
"fmt"
"os"
)
func main() {
args := os.Args
if len(args) < 2 {
fmt.Println("Must supply a database filename")
os.Exit(0)
}
filename := args[1]
table := dbOpen(filename)
inputBuffer := newInputBuffer()
for {
printPrompt()
err := readInput(inputBuffer)
if err != nil {
fmt.Println("readInput failed, err = ", err)
os.Exit(0)
}
if inputBuffer.buffer[0] == '.' {
switch doMetaCommand(inputBuffer, table) {
case META_COMMAND_EXIT:
fmt.Println("exit command!")
return // 直接退出main
case META_COMMAND_UNRECOGNIZED_COMMAND:
fmt.Println(fmt.Sprintf("Unrecognized command %s", inputBuffer.buffer))
continue
}
}
statement := Statement{}
switch prepareStatement(inputBuffer, &statement) {
case PREPARE_SUCCESS:
break
case PREPARE_NEGATIVE_ID:
fmt.Println("Syntax error. Could not parse negative id")
case PREPARE_STRING_TOO_LONG:
fmt.Println("Syntax error. userName or email is too long")
case PREPARE_SYNTAX_ERROR:
fmt.Println("Syntax error. Could not parse statement.")
continue
case PREPARE_UNRECOGNIZED_STATEMENT:
fmt.Println(fmt.Sprintf("Unrecognized keyword at start of %s.", inputBuffer.buffer))
continue
}
switch executeStatement(&statement, table) {
case EXECUTE_SUCCESS:
fmt.Println("Executed.")
break
case EXECUTE_TABLE_FULL:
fmt.Println("table full.")
break
}
}
}