-
Notifications
You must be signed in to change notification settings - Fork 0
/
activity.go
63 lines (55 loc) · 1.88 KB
/
activity.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 workspace
import (
"github.com/pkg/errors"
"github.com/valyala/fasthttp"
"admini.dev/admini/app"
"admini.dev/admini/app/action"
"admini.dev/admini/app/controller/cutil"
"admini.dev/admini/views/vaction"
)
func sourceActivity(req *cutil.WorkspaceRequest, act *action.Action, as *app.State) (*Result, error) {
a := act.Config.GetStringOpt(action.TypeActivity.Key)
switch a {
case "sql":
return sourceActivitySQL(req, act, as)
case "":
return ErrResult(req, act, errors.New("must provide activity in action config"))
default:
return ErrResult(req, act, errors.Errorf("invalid activity [%s] in action config", a))
}
}
func sourceActivitySQL(req *cutil.WorkspaceRequest, act *action.Action, as *app.State) (*Result, error) {
srcKey := act.Config.GetStringOpt(action.TypeSource.Key)
if srcKey == "" {
return ErrResult(req, act, errors.New("must provide source in action config"))
}
sql := act.Config.GetStringOpt("query")
if req.Req.Method == fasthttp.MethodPost {
frm, err := cutil.ParseForm(req.Req, req.ReqBody)
if err != nil {
return nil, errors.Wrap(err, "unable to parse form")
}
s := frm.GetStringOpt("sql")
if s != "" {
sql = s
}
}
if sql == "" {
page := &vaction.ActivitySQL{Req: req, Act: act, SQL: sql, Res: nil}
return NewResult("", nil, req, act, act, page), nil
}
src := req.Sources.Get(srcKey)
if src == nil {
return ErrResult(req, act, errors.Errorf("source [%s] is not in this project's configuration", srcKey))
}
ld, err := as.Services.Loaders.Get(src.Type, src.Key, src.Config)
if err != nil {
return ErrResult(req, act, errors.New("unable to create loader"))
}
r, err := ld.Query(req.Context, nil, sql)
if err != nil {
return ErrResult(req, act, errors.New("unable to execute query"))
}
page := &vaction.ActivitySQL{Req: req, Act: act, SQL: sql, Res: r}
return NewResult(act.Name()+" result", nil, req, act, r, page), nil
}