-
Notifications
You must be signed in to change notification settings - Fork 7
/
api.go
63 lines (54 loc) · 1.39 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
sql "github.com/krasun/gosqlparser"
)
func handler(db *Database) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
query, err := parseQuery(r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
log.Printf("executing query: %s\n", query)
result, err := executeQuery(db, query)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
fmt.Fprintf(w, "the query has been successfully executed: %v\n", result)
}
}
func parseQuery(requestBody io.ReadCloser) (sql.Statement, error) {
body, err := ioutil.ReadAll(requestBody)
if err != nil {
return nil, fmt.Errorf("failed to read request body: %w", err)
}
query, err := sql.Parse(string(body))
if err != nil {
return nil, fmt.Errorf("failed to parse body: %w", err)
}
return query, nil
}
func executeQuery(db *Database, q sql.Statement) (interface{}, error) {
switch query := q.(type) {
case *sql.CreateTable:
return nil, db.CreateTable(query)
case *sql.DropTable:
return nil, db.DropTable(query)
case *sql.Select:
return db.Select(query)
case *sql.Insert:
return db.Insert(query)
case *sql.Update:
return db.Update(query)
case *sql.Delete:
return db.Delete(query)
default:
return nil, fmt.Errorf("unsupported query type: %T", query)
}
}