feat: split code for repl
This commit is contained in:
+14
-37
@@ -1,11 +1,11 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
. "godb/internals/repl"
|
||||
)
|
||||
|
||||
type metaCommandResults int
|
||||
@@ -30,10 +30,6 @@ const (
|
||||
statementInsert
|
||||
)
|
||||
|
||||
type inputBuffer struct {
|
||||
buffer string
|
||||
}
|
||||
|
||||
type row struct {
|
||||
id uint
|
||||
username string
|
||||
@@ -45,9 +41,9 @@ type statement struct {
|
||||
row row
|
||||
}
|
||||
|
||||
func doMetaCommands(inputBuffer *inputBuffer) metaCommandResults {
|
||||
func doMetaCommands(inputBuffer *InputBuffer) metaCommandResults {
|
||||
metacommand := metaCommandUnrecognised
|
||||
if inputBuffer.buffer == ".exit" {
|
||||
if inputBuffer.Buffer == ".exit" {
|
||||
fmt.Println("bye!")
|
||||
os.Exit(0)
|
||||
} else {
|
||||
@@ -56,9 +52,9 @@ func doMetaCommands(inputBuffer *inputBuffer) metaCommandResults {
|
||||
return metacommand
|
||||
}
|
||||
|
||||
func prepareStatements(inputBuffer *inputBuffer, statement *statement) prepareResults {
|
||||
if strings.HasPrefix(inputBuffer.buffer, "insert") {
|
||||
argsAssigned, err := fmt.Sscanf(inputBuffer.buffer, "insert %d %s %s", &statement.row.id, &statement.row.username, &statement.row.email)
|
||||
func prepareStatements(inputBuffer *InputBuffer, statement *statement) prepareResults {
|
||||
if strings.HasPrefix(inputBuffer.Buffer, "insert") {
|
||||
argsAssigned, err := fmt.Sscanf(inputBuffer.Buffer, "insert %d %s %s", &statement.row.id, &statement.row.username, &statement.row.email)
|
||||
if err != nil {
|
||||
fmt.Println("error parsing insert statement")
|
||||
fmt.Println(err.Error())
|
||||
@@ -69,7 +65,7 @@ func prepareStatements(inputBuffer *inputBuffer, statement *statement) prepareRe
|
||||
statement.stype = statementInsert
|
||||
return prepareSuccess
|
||||
}
|
||||
if strings.HasPrefix(inputBuffer.buffer, "select") {
|
||||
if strings.HasPrefix(inputBuffer.Buffer, "select") {
|
||||
statement.stype = statementSelect
|
||||
return prepareSuccess
|
||||
}
|
||||
@@ -85,37 +81,18 @@ func executeStatement(statement *statement) {
|
||||
}
|
||||
}
|
||||
|
||||
func createInputBuffer() *inputBuffer {
|
||||
return &inputBuffer{""}
|
||||
}
|
||||
|
||||
func printConsole() {
|
||||
fmt.Print("godb > ")
|
||||
}
|
||||
|
||||
func readConsole(inputBuffer *inputBuffer, stdin *bufio.Reader) *inputBuffer {
|
||||
line, err := stdin.ReadString('\n')
|
||||
if err != nil && err != io.EOF {
|
||||
fmt.Println("Error reading input", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
inputBuffer.buffer = strings.TrimSpace(line)
|
||||
return inputBuffer
|
||||
}
|
||||
|
||||
func main() {
|
||||
var stdin = bufio.NewReader(os.Stdin)
|
||||
inputBuffer := createInputBuffer()
|
||||
inputBuffer := NewInputBuffer()
|
||||
statement := statement{0, row{0, "", ""}}
|
||||
for {
|
||||
printConsole()
|
||||
inputBuffer = readConsole(inputBuffer, stdin)
|
||||
if strings.HasPrefix(inputBuffer.buffer, ".") {
|
||||
PrintConsole()
|
||||
inputBuffer.ReadConsole()
|
||||
if strings.HasPrefix(inputBuffer.Buffer, ".") {
|
||||
switch doMetaCommands(inputBuffer) {
|
||||
case metaCommandSuccess:
|
||||
continue
|
||||
case metaCommandUnrecognised:
|
||||
fmt.Printf("unrecognised command %v\n", inputBuffer.buffer)
|
||||
fmt.Printf("unrecognised comma%v\n", inputBuffer.Buffer)
|
||||
continue
|
||||
}
|
||||
}
|
||||
@@ -124,7 +101,7 @@ func main() {
|
||||
case prepareSuccess:
|
||||
//todo
|
||||
case prepareUnrecognised:
|
||||
fmt.Printf("Unrecognised keyword at the start of '%s'.\n", inputBuffer.buffer)
|
||||
fmt.Printf("Unrecognised keyword at the start of '%s'.\n", inputBuffer.Buffer)
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
package repl
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type InputBuffer struct {
|
||||
Buffer string
|
||||
Reader *bufio.Reader
|
||||
}
|
||||
|
||||
func NewInputBuffer() *InputBuffer {
|
||||
return &InputBuffer{
|
||||
Buffer: "",
|
||||
Reader: bufio.NewReader(os.Stdin),
|
||||
}
|
||||
}
|
||||
|
||||
func PrintConsole() {
|
||||
fmt.Print("godb > ")
|
||||
}
|
||||
|
||||
func (inputBuffer *InputBuffer) ReadConsole() {
|
||||
line, err := inputBuffer.Reader.ReadString('\n')
|
||||
if err != nil && err != io.EOF {
|
||||
fmt.Println("Error reading input", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
inputBuffer.Buffer = strings.TrimSpace(line)
|
||||
}
|
||||
Reference in New Issue
Block a user