-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec.go
More file actions
64 lines (54 loc) · 1.12 KB
/
exec.go
File metadata and controls
64 lines (54 loc) · 1.12 KB
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
64
package main
import (
"database/sql"
"fmt"
"log"
"net/http"
"time"
"github.com/gorilla/mux"
)
func HandleExec(w http.ResponseWriter, r *http.Request, info *DbInfo) {
qr := &QueryResult{}
n := time.Now()
dbn := mux.Vars(r)["db"]
query := mux.Vars(r)["query"]
log.Printf("[DATABASE] %q [QUERY] %q\n", dbn, query)
db, s, e := open(dbn, info)
if e != nil {
log.Println(e)
w.WriteHeader(s)
fmt.Fprintln(w, e)
return
}
s = qr.fetchExec(db, query)
e = WriteToJSON(w, s, qr)
if e != nil {
log.Println(e)
}
log.Printf("(Exec rendered in %v)\n", time.Now().Sub(n))
}
func (qr *QueryResult) fetchExec(db *sql.DB, query string) int {
rs, e := db.Exec(query)
if e != nil {
log.Println(e)
qr.Error = fmt.Sprintf("%s", e)
return http.StatusBadRequest
}
li, e := rs.LastInsertId()
if e != nil {
log.Println(e)
qr.Error = fmt.Sprintf("%s", e)
return http.StatusBadRequest
}
ra, e := rs.RowsAffected()
if e != nil {
log.Println(e)
qr.Error = fmt.Sprintf("%s", e)
return http.StatusBadRequest
}
qr.Infos = map[string]int64{
"lastInsertId": li,
"rowsAffected": ra,
}
return http.StatusOK
}