/
repl.go
58 lines (52 loc) · 1.08 KB
/
repl.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
package main
import (
"bufio"
"fmt"
"io"
"os"
"strings"
)
const (
ExitCommand = ".exit"
ExitSuccess = 0
ExitFailure = 1
)
// repl provides a repl for the db users to input statements
func repl(stdin io.Reader, table *Table) error {
for {
printPrompt()
s := readInput(stdin)
if strings.HasPrefix(s, ".") {
if err := handleMetaCommand(s, table); err != nil {
return err
}
}
stmnt, err := prepareStatement(s)
if err != nil {
return err
}
if err := executeStatement(stmnt, table); err != nil {
return err
}
}
}
// printPrompt prints a cmd prompt to users
func printPrompt() {
fmt.Printf("db > ")
}
// readInput reads std input and trimspaces around the input
func readInput(stdin io.Reader) string {
reader := bufio.NewReader(stdin)
s, _ := reader.ReadString('\n')
return strings.TrimSpace(s)
}
// handleMetaCommand executes meta commands
func handleMetaCommand(input string, table *Table) error {
if input == ExitCommand {
table.dbClose()
os.Exit(ExitSuccess)
return nil
} else {
return fmt.Errorf("unrecognized command %s \n ", input)
}
}