feat: split code for repl

This commit is contained in:
2026-07-19 23:33:02 +05:30
parent d97308b4f1
commit 5550c92c23
3 changed files with 50 additions and 39 deletions
+14 -37
View File
@@ -1,11 +1,11 @@
package main package main
import ( import (
"bufio"
"fmt" "fmt"
"io"
"os" "os"
"strings" "strings"
. "godb/internals/repl"
) )
type metaCommandResults int type metaCommandResults int
@@ -30,10 +30,6 @@ const (
statementInsert statementInsert
) )
type inputBuffer struct {
buffer string
}
type row struct { type row struct {
id uint id uint
username string username string
@@ -45,9 +41,9 @@ 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!")
os.Exit(0) os.Exit(0)
} else { } else {
@@ -56,9 +52,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.HasPrefix(inputBuffer.buffer, "insert") { 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) 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())
@@ -69,7 +65,7 @@ func prepareStatements(inputBuffer *inputBuffer, statement *statement) prepareRe
statement.stype = statementInsert statement.stype = statementInsert
return prepareSuccess return prepareSuccess
} }
if strings.HasPrefix(inputBuffer.buffer, "select") { if strings.HasPrefix(inputBuffer.Buffer, "select") {
statement.stype = statementSelect statement.stype = statementSelect
return prepareSuccess 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() { func main() {
var stdin = bufio.NewReader(os.Stdin) inputBuffer := NewInputBuffer()
inputBuffer := createInputBuffer()
statement := statement{0, row{0, "", ""}} statement := statement{0, row{0, "", ""}}
for { for {
printConsole() PrintConsole()
inputBuffer = readConsole(inputBuffer, stdin) inputBuffer.ReadConsole()
if strings.HasPrefix(inputBuffer.buffer, ".") { if strings.HasPrefix(inputBuffer.Buffer, ".") {
switch doMetaCommands(inputBuffer) { switch doMetaCommands(inputBuffer) {
case metaCommandSuccess: case metaCommandSuccess:
continue continue
case metaCommandUnrecognised: case metaCommandUnrecognised:
fmt.Printf("unrecognised command %v\n", inputBuffer.buffer) fmt.Printf("unrecognised comma%v\n", inputBuffer.Buffer)
continue continue
} }
} }
@@ -124,7 +101,7 @@ func main() {
case prepareSuccess: case prepareSuccess:
//todo //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
} }
+2 -2
View File
@@ -1,3 +1,3 @@
module godb/main module godb
go 1.23.2 go 1.26.3
+34
View File
@@ -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)
}