Database UI

/app/lib/database/explain.go (740 B)

 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
package database

import (
"context"
"strings"

"github.com/pkg/errors"

"{{{ .Package }}}/app/util"
)

const explainPrefix = "{{{ .ExplainPrefix }}}"

func (s *Service) Explain(ctx context.Context, q string, values []any, _ util.Logger) ([]util.ValueMap, error) {
q = strings.TrimSpace(q)
if !strings.HasPrefix(q, explainPrefix) {
q = explainPrefix + q
}
res, err := s.db.QueryxContext(ctx, q, values...)
if err != nil {
return nil, errors.Wrap(err, "invalid explain result")
}
defer func() { _ = res.Close() }()
var ret []util.ValueMap
for res.Next() {
x := util.ValueMap{}
err = res.MapScan(x)
if err != nil {
return nil, errors.Wrap(err, "can't read results")
}
ret = append(ret, x)
}

return ret, nil
}