113 lines
2.3 KiB
Go
113 lines
2.3 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
|
|
. "godb/internals/repl"
|
|
)
|
|
|
|
type metaCommandResults int
|
|
|
|
const (
|
|
metaCommandSuccess metaCommandResults = iota
|
|
metaCommandUnrecognised
|
|
)
|
|
|
|
type prepareResults int
|
|
|
|
const (
|
|
prepareSuccess prepareResults = iota
|
|
prepareUnrecognised
|
|
prepareSyntaxError
|
|
)
|
|
|
|
type statementType int
|
|
|
|
const (
|
|
statementSelect statementType = iota
|
|
statementInsert
|
|
)
|
|
|
|
type row struct {
|
|
id uint
|
|
username string
|
|
email string
|
|
}
|
|
|
|
type statement struct {
|
|
stype statementType
|
|
row row
|
|
}
|
|
|
|
func doMetaCommands(inputBuffer *InputBuffer) metaCommandResults {
|
|
metacommand := metaCommandUnrecognised
|
|
if inputBuffer.Buffer == ".exit" {
|
|
fmt.Println("bye!")
|
|
os.Exit(0)
|
|
} else {
|
|
metacommand = metaCommandUnrecognised
|
|
}
|
|
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)
|
|
if err != nil {
|
|
fmt.Println("error parsing insert statement")
|
|
fmt.Println(err.Error())
|
|
}
|
|
if argsAssigned > 3 {
|
|
return prepareSyntaxError
|
|
}
|
|
statement.stype = statementInsert
|
|
return prepareSuccess
|
|
}
|
|
if strings.HasPrefix(inputBuffer.Buffer, "select") {
|
|
statement.stype = statementSelect
|
|
return prepareSuccess
|
|
}
|
|
return prepareUnrecognised
|
|
}
|
|
|
|
func executeStatement(statement *statement) {
|
|
switch statement.stype {
|
|
case statementInsert:
|
|
fmt.Println("This is where the logic of insert statement should go")
|
|
case statementSelect:
|
|
fmt.Println("This is where the logic of select statement should go")
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
inputBuffer := NewInputBuffer()
|
|
statement := statement{0, row{0, "", ""}}
|
|
for {
|
|
PrintConsole()
|
|
inputBuffer.ReadConsole()
|
|
if strings.HasPrefix(inputBuffer.Buffer, ".") {
|
|
switch doMetaCommands(inputBuffer) {
|
|
case metaCommandSuccess:
|
|
continue
|
|
case metaCommandUnrecognised:
|
|
fmt.Printf("unrecognised comma%v\n", inputBuffer.Buffer)
|
|
continue
|
|
}
|
|
}
|
|
|
|
switch prepareStatements(inputBuffer, &statement) {
|
|
case prepareSuccess:
|
|
//todo
|
|
case prepareUnrecognised:
|
|
fmt.Printf("Unrecognised keyword at the start of '%s'.\n", inputBuffer.Buffer)
|
|
continue
|
|
}
|
|
|
|
executeStatement(&statement)
|
|
fmt.Println("Executed")
|
|
|
|
}
|
|
}
|