-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
100 lines (83 loc) · 1.91 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
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
// ------------- INPUT AND OUTPUT --------------
func printPrompt() {
fmt.Printf("db > ")
}
type InputBuffer struct {
buffer string
inputLength int
}
func newInputBuffer() *InputBuffer {
inputBuffer := &InputBuffer{}
inputBuffer.buffer = ""
inputBuffer.inputLength = 0
return inputBuffer
}
func readInput(buffer *InputBuffer) {
// reading input
reader := bufio.NewReader(os.Stdin)
input, err := reader.ReadString('\n')
if err != nil {
fmt.Println("Error reading input")
os.Exit(1)
}
// ignore trailing newline
input = strings.TrimSpace(strings.TrimSuffix(input, "\n"))
buffer.buffer = input
buffer.inputLength = len(input)
}
func main() {
if len(os.Args) < 2 {
fmt.Println("Must supply a database filename.")
os.Exit(1)
}
filename := os.Args[1]
inputBuffer := newInputBuffer()
table := DB_OPEN(filename)
//table := newTable()
for {
printPrompt()
readInput(inputBuffer)
if inputBuffer.buffer[0] == '.' {
switch DoMetaCommand(inputBuffer, table) {
case META_COMMAND_SUCCESS:
continue
case META_COMMAND_UNRECOGNIZED_COMMAND:
fmt.Printf("Unrecognized command '%s'.\n", inputBuffer.buffer)
continue
}
}
statement := &Statement{}
switch PrepareStatement(inputBuffer, statement) {
case PREPARE_SUCCESS:
break
case PREPARE_SYNTAX_ERROR:
fmt.Println("Syntax error could not parse statement.")
continue
case PREPARE_UNRECOGNIZED_STATEMENT:
fmt.Printf("Unrecognized keyword at start of '%s'.\n", inputBuffer.buffer)
continue
case PREPARE_NEGATIVE_ID:
fmt.Println("Parsing Error: Negative ID passed")
continue
default:
continue
}
switch executeStatement(statement, table) {
case EXECUTE_SUCCESS:
fmt.Println("Executed")
break
case EXECUTE_TABLE_FULL:
fmt.Println("Table is full!")
break
case EXECUTE_FAIL:
fmt.Println("Executing the command failed.")
}
}
}