Skip to content

Commit

Permalink
Database back-end 'read_db' action.
Browse files Browse the repository at this point in the history
  • Loading branch information
nthnn committed Dec 31, 2023
1 parent 093b1ab commit 2b38c1b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
47 changes: 46 additions & 1 deletion backend/database/callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ func getByNameCallback(apiKey string, args []string) func(*sql.DB) {
query.Scan(&mode, &content)
}

proc.ShowResult("[\"" + mode + "\", \"" + content + "\"]")
if strings.Contains(mode, "r") {
proc.ShowResult("[\"" + mode + "\", \"" + content + "\"]")
} else {
proc.ShowResult("[\"" + mode + "\"]")
}

query.Close()
}
}
Expand Down Expand Up @@ -179,6 +184,46 @@ func getDbModeCallback(apiKey string, args []string) func(*sql.DB) {
}
}

func readDbCallback(apiKey string, args []string) func(*sql.DB) {
return func(d *sql.DB) {
name := args[2]
query, err := d.Query("SELECT mode, content FROM " + apiKey + "_database WHERE name=\"" + name + "\"")

if err != nil {
proc.ShowFailedResponse("Internal error occured.")
query.Close()

return
}

count := 0
mode := ""
content := ""

for query.Next() {
query.Scan(&mode, &content)
count++
}

if count != 1 {
proc.ShowFailedResponse("Cannot resolve database name.")
query.Close()

return
}

if !strings.Contains(mode, "r") {
proc.ShowFailedResponse("Read operation denied.")
query.Close()

return
}

proc.ShowResult("\"" + content + "\"")
query.Close()
}
}

func writeDbCallback(apiKey string, args []string) func(*sql.DB) {
return func(d *sql.DB) {
name := args[2]
Expand Down
4 changes: 4 additions & 0 deletions backend/database/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ func main() {
failOnUmatchedArgSize(4, args)
callback = writeDbCallback(apiKey, args)

case "read_db":
failOnUmatchedArgSize(3, args)
callback = readDbCallback(apiKey, args)

case "delete_db":
failOnUmatchedArgSize(3, args)
callback = deleteDbCallback(apiKey, args)
Expand Down

0 comments on commit 2b38c1b

Please sign in to comment.