feat: upated the codebase to be a bit more idiomatic

This commit is contained in:
2026-07-19 22:16:05 +05:30
parent df75480238
commit a522c63f47
+28 -42
View File
@@ -9,23 +9,29 @@ import (
) )
type metaCommandResults int type metaCommandResults int
type prepareResults int
type statementType int
const ( const (
metaCommandSuccess metaCommandResults = iota metaCommandSuccess metaCommandResults = iota
metaCommandUnrecognised metaCommandUnrecognised
)
type prepareResults int
const (
prepareSuccess prepareResults = iota prepareSuccess prepareResults = iota
prepareUnrecognised prepareUnrecognised
prepareSyntaxError prepareSyntaxError
)
type statementType int
const (
statementSelect statementType = iota statementSelect statementType = iota
statementInsert statementInsert
) )
type inputBuffer struct { type inputBuffer struct {
buffer string buffer string
bufferLength int
inputLength int
} }
type row struct { type row struct {
@@ -39,7 +45,7 @@ type statement struct {
row row row row
} }
func doMetaCommands(inputBuffer inputBuffer) metaCommandResults { func doMetaCommands(inputBuffer *inputBuffer) metaCommandResults {
metacommand := metaCommandUnrecognised metacommand := metaCommandUnrecognised
if inputBuffer.buffer == ".exit" { if inputBuffer.buffer == ".exit" {
fmt.Println("bye!") fmt.Println("bye!")
@@ -50,9 +56,9 @@ func doMetaCommands(inputBuffer inputBuffer) metaCommandResults {
return metacommand return metacommand
} }
func prepareStatements(inputBuffer inputBuffer, statement *statement) prepareResults { func prepareStatements(inputBuffer *inputBuffer, statement *statement) prepareResults {
if strings.Split(inputBuffer.buffer, " ")[0] == "insert" { if strings.HasPrefix(inputBuffer.buffer, "insert") {
argsAssigned, err := fmt.Sscanf(inputBuffer.buffer, "insert %v %s %s", &statement.row.id, &statement.row.username, &statement.row.email) argsAssigned, err := fmt.Sscanf(inputBuffer.buffer, "insert %d %s %s", &statement.row.id, &statement.row.username, &statement.row.email)
if err != nil { if err != nil {
fmt.Println("error parsing insert statement") fmt.Println("error parsing insert statement")
fmt.Println(err.Error()) fmt.Println(err.Error())
@@ -63,7 +69,7 @@ func prepareStatements(inputBuffer inputBuffer, statement *statement) prepareRes
statement.stype = statementInsert statement.stype = statementInsert
return prepareSuccess return prepareSuccess
} }
if strings.Split(inputBuffer.buffer, " ")[0] == "select" { if strings.HasPrefix(inputBuffer.buffer, "select") {
statement.stype = statementSelect statement.stype = statementSelect
return prepareSuccess return prepareSuccess
} }
@@ -74,57 +80,37 @@ func executeStatement(statement *statement) {
switch statement.stype { switch statement.stype {
case statementInsert: case statementInsert:
fmt.Println("This is where the logic of insert statement should go") fmt.Println("This is where the logic of insert statement should go")
break
case statementSelect: case statementSelect:
fmt.Println("This is where the logic of select statement should go") fmt.Println("This is where the logic of select statement should go")
break
} }
} }
func createInputBuffer() inputBuffer { func createInputBuffer() *inputBuffer {
newInputBuffer := inputBuffer{"", 0, 0} return &inputBuffer{""}
return newInputBuffer
} }
func printConsole() { func printConsole() {
fmt.Print("godb > ") fmt.Print("godb > ")
} }
func readConsole(inputBuffer inputBuffer) inputBuffer { func readConsole(inputBuffer *inputBuffer, stdin *bufio.Reader) *inputBuffer {
var stdin *bufio.Reader line, err := stdin.ReadString('\n')
var line []rune if err != nil && err != io.EOF {
stdin = bufio.NewReader(os.Stdin) fmt.Println("Error reading input", err)
for {
c, _, err := stdin.ReadRune()
if err == io.EOF || c == '\n' {
break
}
if err != nil {
fmt.Println("Error reading input")
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) os.Exit(1)
} }
inputBuffer.bufferLength = len(inputBuffer.buffer) inputBuffer.buffer = strings.TrimSpace(line)
inputBuffer.inputLength = bytesRead - 1
return inputBuffer return inputBuffer
} }
func main() { func main() {
var stdin = bufio.NewReader(os.Stdin)
inputBuffer := createInputBuffer() inputBuffer := createInputBuffer()
statement := statement{0, row{0, "", ""}} statement := statement{0, row{0, "", ""}}
condition := true for {
for condition {
printConsole() printConsole()
inputBuffer = readConsole(inputBuffer) inputBuffer = readConsole(inputBuffer, stdin)
if strings.Split(inputBuffer.buffer, "")[0] == "." { if strings.HasPrefix(inputBuffer.buffer, ".") {
switch doMetaCommands(inputBuffer) { switch doMetaCommands(inputBuffer) {
case metaCommandSuccess: case metaCommandSuccess:
continue continue
@@ -136,7 +122,7 @@ func main() {
switch prepareStatements(inputBuffer, &statement) { switch prepareStatements(inputBuffer, &statement) {
case prepareSuccess: case prepareSuccess:
break //todo
case prepareUnrecognised: 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 continue