Merging into main #1
+27
-41
@@ -9,23 +9,29 @@ import (
|
||||
)
|
||||
|
||||
type metaCommandResults int
|
||||
type prepareResults int
|
||||
type statementType int
|
||||
|
||||
const (
|
||||
metaCommandSuccess metaCommandResults = iota
|
||||
metaCommandUnrecognised
|
||||
)
|
||||
|
||||
type prepareResults int
|
||||
|
||||
const (
|
||||
prepareSuccess prepareResults = iota
|
||||
prepareUnrecognised
|
||||
prepareSyntaxError
|
||||
)
|
||||
|
||||
type statementType int
|
||||
|
||||
const (
|
||||
statementSelect statementType = iota
|
||||
statementInsert
|
||||
)
|
||||
|
||||
type inputBuffer struct {
|
||||
buffer string
|
||||
bufferLength int
|
||||
inputLength int
|
||||
}
|
||||
|
||||
type row struct {
|
||||
@@ -39,7 +45,7 @@ type statement struct {
|
||||
row row
|
||||
}
|
||||
|
||||
func doMetaCommands(inputBuffer inputBuffer) metaCommandResults {
|
||||
func doMetaCommands(inputBuffer *inputBuffer) metaCommandResults {
|
||||
metacommand := metaCommandUnrecognised
|
||||
if inputBuffer.buffer == ".exit" {
|
||||
fmt.Println("bye!")
|
||||
@@ -50,9 +56,9 @@ func doMetaCommands(inputBuffer inputBuffer) metaCommandResults {
|
||||
return metacommand
|
||||
}
|
||||
|
||||
func prepareStatements(inputBuffer inputBuffer, statement *statement) prepareResults {
|
||||
if strings.Split(inputBuffer.buffer, " ")[0] == "insert" {
|
||||
argsAssigned, err := fmt.Sscanf(inputBuffer.buffer, "insert %v %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())
|
||||
@@ -63,7 +69,7 @@ func prepareStatements(inputBuffer inputBuffer, statement *statement) prepareRes
|
||||
statement.stype = statementInsert
|
||||
return prepareSuccess
|
||||
}
|
||||
if strings.Split(inputBuffer.buffer, " ")[0] == "select" {
|
||||
if strings.HasPrefix(inputBuffer.buffer, "select") {
|
||||
statement.stype = statementSelect
|
||||
return prepareSuccess
|
||||
}
|
||||
@@ -74,57 +80,37 @@ func executeStatement(statement *statement) {
|
||||
switch statement.stype {
|
||||
case statementInsert:
|
||||
fmt.Println("This is where the logic of insert statement should go")
|
||||
break
|
||||
case statementSelect:
|
||||
fmt.Println("This is where the logic of select statement should go")
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
func createInputBuffer() inputBuffer {
|
||||
newInputBuffer := inputBuffer{"", 0, 0}
|
||||
return newInputBuffer
|
||||
func createInputBuffer() *inputBuffer {
|
||||
return &inputBuffer{""}
|
||||
}
|
||||
|
||||
func printConsole() {
|
||||
fmt.Print("godb > ")
|
||||
}
|
||||
|
||||
func readConsole(inputBuffer inputBuffer) inputBuffer {
|
||||
var stdin *bufio.Reader
|
||||
var line []rune
|
||||
stdin = bufio.NewReader(os.Stdin)
|
||||
for {
|
||||
c, _, err := stdin.ReadRune()
|
||||
if err == io.EOF || c == '\n' {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Println("Error reading input")
|
||||
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)
|
||||
}
|
||||
line = append(line, c)
|
||||
}
|
||||
inputBuffer.buffer = string(line)
|
||||
bytesRead := len(inputBuffer.buffer)
|
||||
if bytesRead <= 0 {
|
||||
fmt.Println("Error reading input")
|
||||
os.Exit(1)
|
||||
}
|
||||
inputBuffer.bufferLength = len(inputBuffer.buffer)
|
||||
inputBuffer.inputLength = bytesRead - 1
|
||||
|
||||
inputBuffer.buffer = strings.TrimSpace(line)
|
||||
return inputBuffer
|
||||
}
|
||||
|
||||
func main() {
|
||||
var stdin = bufio.NewReader(os.Stdin)
|
||||
inputBuffer := createInputBuffer()
|
||||
statement := statement{0, row{0, "", ""}}
|
||||
condition := true
|
||||
for condition {
|
||||
for {
|
||||
printConsole()
|
||||
inputBuffer = readConsole(inputBuffer)
|
||||
if strings.Split(inputBuffer.buffer, "")[0] == "." {
|
||||
inputBuffer = readConsole(inputBuffer, stdin)
|
||||
if strings.HasPrefix(inputBuffer.buffer, ".") {
|
||||
switch doMetaCommands(inputBuffer) {
|
||||
case metaCommandSuccess:
|
||||
continue
|
||||
@@ -136,7 +122,7 @@ func main() {
|
||||
|
||||
switch prepareStatements(inputBuffer, &statement) {
|
||||
case prepareSuccess:
|
||||
break
|
||||
//todo
|
||||
case prepareUnrecognised:
|
||||
fmt.Printf("Unrecognised keyword at the start of '%s'.\n", inputBuffer.buffer)
|
||||
continue
|
||||
|
||||
Reference in New Issue
Block a user