Skip to content

Commit

Permalink
Database backend 'write_db' action.
Browse files Browse the repository at this point in the history
  • Loading branch information
nthnn committed Dec 31, 2023
1 parent c560388 commit 0c17837
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
50 changes: 50 additions & 0 deletions backend/database/callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,56 @@ func getDbModeCallback(apiKey string, args []string) func(*sql.DB) {
}
}

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

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

return
}

count := 0
mode := ""

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

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

return
}

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

return
}

query, err = d.Query("UPDATE " + apiKey +
"_database SET content=\"" + content +
"\" WHERE name=\"" + name + "\"")
if err != nil {
proc.ShowFailedResponse("Internal error occured.")
query.Close()

return
}

proc.ShowSuccessResponse()
query.Close()
}
}

func deleteDbCallback(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 @@ -41,6 +41,10 @@ func main() {
failOnUmatchedArgSize(3, args)
callback = getDbModeCallback(apiKey, args)

case "write_db":
failOnUmatchedArgSize(4, args)
callback = writeDbCallback(apiKey, args)

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

0 comments on commit 0c17837

Please sign in to comment.