This repository was archived by the owner on Jul 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsql.go
More file actions
48 lines (39 loc) · 1.33 KB
/
sql.go
File metadata and controls
48 lines (39 loc) · 1.33 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
package controllers
import (
"fmt"
"net/http"
"github.com/gorilla/mux"
"github.com/prest/config"
)
// ExecuteScriptQuery is a function to execute and return result of script query
func ExecuteScriptQuery(rq *http.Request, queriesPath string, script string) ([]byte, error) {
config.PrestConf.Adapter.SetDatabase(config.PrestConf.PGDatabase)
sqlPath, err := config.PrestConf.Adapter.GetScript(rq.Method, queriesPath, script)
if err != nil {
err = fmt.Errorf("could not get script %s/%s, %+v", queriesPath, script, err)
return nil, err
}
sql, values, err := config.PrestConf.Adapter.ParseScript(sqlPath, rq.URL.Query())
if err != nil {
err = fmt.Errorf("could not parse script %s/%s, %+v", queriesPath, script, err)
return nil, err
}
sc := config.PrestConf.Adapter.ExecuteScripts(rq.Method, sql, values)
if sc.Err() != nil {
err = fmt.Errorf("could not execute sql %+v, %s", sc.Err(), sql)
return nil, err
}
return sc.Bytes(), nil
}
// ExecuteFromScripts is a controller to peform SQL in scripts created by users
func ExecuteFromScripts(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
queriesPath := vars["queriesLocation"]
script := vars["script"]
result, err := ExecuteScriptQuery(r, queriesPath, script)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
w.Write(result)
}