Database UI

/app/controller/clib/database.go (6.3 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package clib

import (
"fmt"
"strconv"

"github.com/pkg/errors"
"github.com/valyala/fasthttp"

"{{{ .Package }}}/app"
"{{{ .Package }}}/app/controller"
"{{{ .Package }}}/app/controller/cutil"
"{{{ .Package }}}/app/lib/database"
"{{{ .Package }}}/app/util"
"{{{ .Package }}}/views/vdatabase"
)

const KeyAnalyze = "analyze"

func DatabaseList(rc *fasthttp.RequestCtx) {
controller.Act("database.list", rc, func(as *app.State, ps *cutil.PageState) (string, error) {
keys := database.RegistryKeys()
if len(keys) == 1 {
return "/admin/database/" + keys[0], nil
}
svcs := make(map[string]*database.Service, len(keys))
for _, key := range keys {
svc, err := database.RegistryGet(key)
if err != nil {
return "", errors.Wrapf(err, "no database found with key [%s]", key)
}
svcs[key] = svc
}
return controller.Render(rc, as, &vdatabase.List{Keys: keys, Services: svcs}, ps, "admin", "Database")
})
}

func DatabaseDetail(rc *fasthttp.RequestCtx) {
controller.Act("database.detail", rc, func(as *app.State, ps *cutil.PageState) (string, error) {
svc, err := getDatabaseService(rc)
if err != nil {
return "", err
}
return controller.Render(rc, as, &vdatabase.Detail{Mode: "", Svc: svc}, ps, "admin", "Database||/admin/database", svc.Key)
})
}

func DatabaseAction(rc *fasthttp.RequestCtx) {
controller.Act("database.action", rc, func(as *app.State, ps *cutil.PageState) (string, error) {
svc, err := getDatabaseService(rc)
if err != nil {
return "", err
}
act, err := cutil.RCRequiredString(rc, "act", true)
if err != nil {
return "", err
}
bc := []string{"admin", "Database||/admin/database", fmt.Sprintf("%s||/admin/database/%s", svc.Key, svc.Key), act}
switch act {
case "enable":
_ = svc.EnableTracing(string(rc.URI().QueryArgs().Peek("tracing")), ps.Logger)
return "/admin/database/" + svc.Key + "/recent", nil
case "recent":
if idxStr := string(rc.URI().QueryArgs().Peek("idx")); idxStr != "" {
idx, _ := strconv.ParseInt(idxStr, 10, 32)
st := database.GetDebugStatement(svc.Key, int(idx))
if st != nil {
return controller.Render(rc, as, &vdatabase.Statement{Statement: st}, ps, bc...)
}
}
recent := database.GetDebugStatements(svc.Key)
return controller.Render(rc, as, &vdatabase.Detail{Mode: "recent", Svc: svc, Recent: recent}, ps, bc...)
case "tables":
sizes, dberr := svc.Sizes(ps.Context, ps.Logger)
if dberr != nil {
return "", errors.Wrapf(dberr, "unable to calculate sizes for database [%s]", svc.Key)
}
return controller.Render(rc, as, &vdatabase.Detail{Mode: "tables", Svc: svc, Sizes: sizes}, ps, bc...)
case KeyAnalyze:
t := util.TimerStart()
var tmp []any
err = svc.Select(ps.Context, &tmp, KeyAnalyze, nil, ps.Logger)
if err != nil {
return "", err
}
msg := fmt.Sprintf("Analyzed database in [%s]", util.MicrosToMillis(t.End()))
return controller.FlashAndRedir(true, msg, "/admin/database/"+svc.Key+"/tables", rc, ps){{{ if .DatabaseUISQLEditor }}}
case "sql":
return controller.Render(rc, as, &vdatabase.Detail{Mode: "sql", Svc: svc, SQL: "select 1;"}, ps, bc...){{{ end }}}
default:
return "", errors.Errorf("invalid database action [%s]", act)
}
})
}

func DatabaseTableView(rc *fasthttp.RequestCtx) {
controller.Act("database.sql.run", rc, func(as *app.State, ps *cutil.PageState) (string, error) {
prms := ps.Params.Get("table", []string{"*"}, ps.Logger).Sanitize("table")
svc, err := getDatabaseService(rc)
if err != nil {
return "", err
}
schema, _ := cutil.RCRequiredString(rc, "schema", true)
table, _ := cutil.RCRequiredString(rc, "table", true)

tbl := fmt.Sprintf("%q", table)
if schema != "default" {
tbl = fmt.Sprintf("%q.%q", schema, table)
}

q := database.SQLSelect("*", tbl, "", prms.OrderByString(), prms.Limit, prms.Offset, as.DB.Placeholder())
res, err := svc.QueryRows(ps.Context, q, nil, ps.Logger)
ps.Data = res
bc := []string{"admin", "Database||/admin/database", fmt.Sprintf("%s||/admin/database/%s", svc.Key, svc.Key), "Tables"}
return controller.Render(rc, as, &vdatabase.Results{Svc: svc, Schema: schema, Table: table, Results: res, Params: prms, Error: err}, ps, bc...)
})
}{{{ if .DatabaseUISQLEditor }}}

func DatabaseSQLRun(rc *fasthttp.RequestCtx) {
controller.Act("database.sql.run", rc, func(as *app.State, ps *cutil.PageState) (string, error) {
svc, err := getDatabaseService(rc)
if err != nil {
return "", err
}
f := rc.PostArgs()
sql := string(f.Peek("sql"))
c := string(f.Peek("commit"))
commit := c == util.BoolTrue
action := string(f.Peek("action"))
if action == KeyAnalyze {
sql = "explain analyze " + sql
}

tx, err := svc.StartTransaction(ps.Logger)
if err != nil {
return "", errors.Wrap(err, "unable to start transaction")
}
defer func() { _ = tx.Rollback() }()

var columns []string
results := [][]any{}

timer := util.TimerStart()
result, err := svc.Query(ps.Context, sql, tx, ps.Logger)
if err != nil {
return "", err
}
defer func() { _ = result.Close() }()

elapsed := timer.End()

if result != nil {
for result.Next() {
if columns == nil {
columns, _ = result.Columns()
}
row, e := result.SliceScan()
if e != nil {
return "", errors.Wrap(e, "unable to read row")
}
results = append(results, row)
}
}{{{ if .DatabaseUIReadOnly }}}
_ = tx.Rollback(){{{ else }}}
if commit {
err = tx.Commit()
if err != nil {
return "", errors.Wrap(err, "unable to commit transaction")
}
} else {
_ = tx.Rollback()
}{{{ end }}}

ps.Title = "SQL Results"
ps.Data = results
page := &vdatabase.Detail{Mode: "sql", Svc: svc, SQL: sql, Columns: columns, Results: results, Timing: elapsed, Commit: commit}
return controller.Render(rc, as, page, ps, "admin", "Database||/admin/database", svc.Key+"||/admin/database/"+svc.Key, "Results")
})
}{{{ end }}}

func getDatabaseService(rc *fasthttp.RequestCtx) (*database.Service, error) {
key, err := cutil.RCRequiredString(rc, "key", true)
if err != nil {
return nil, err
}
svc, err := database.RegistryGet(key)
if err != nil {
return nil, errors.Wrapf(err, "no database found with key [%s]", key)
}
return svc, nil
}