87 lines
1.7 KiB
Go
87 lines
1.7 KiB
Go
package vm
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"fmt"
|
||
|
|
"os"
|
||
|
|
"strings"
|
||
|
|
|
||
|
|
. "godb/internals/buffer"
|
||
|
|
)
|
||
|
|
|
||
|
|
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 NewStatement() *Statement {
|
||
|
|
return &Statement{0, row{0, "", ""}}
|
||
|
|
}
|
||
|
|
|
||
|
|
func DoMetaCommands(inputBuffer *InputBuffer) MetaCommandResults {
|
||
|
|
metacommand := MetaCommandUnrecognised
|
||
|
|
if inputBuffer.Buffer == ".exit" {
|
||
|
|
fmt.Println("bye!")
|
||
|
|
os.Exit(0)
|
||
|
|
} else {
|
||
|
|
metacommand = MetaCommandUnrecognised
|
||
|
|
}
|
||
|
|
return metacommand
|
||
|
|
}
|
||
|
|
|
||
|
|
func (statement *Statement) PrepareStatements(inputBuffer *InputBuffer) 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 (statement *Statement) ExecuteStatement() {
|
||
|
|
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")
|
||
|
|
}
|
||
|
|
}
|