/
call.go
154 lines (125 loc) · 3.32 KB
/
call.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
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
package db
import (
"fmt"
"cuelang.org/go/cue"
"github.com/hofstadter-io/cuetils/flow/context"
)
func init() {
context.Register("db.Call", NewCall)
}
type Call struct {}
func NewCall(val cue.Value) (context.Runner, error) {
return &Call{}, nil
}
func (T *Call) Run(ctx *context.Context) (interface{}, error) {
// todo, check failure modes, fill, not return error?
// (in all tasks)
// does a failed stmt fail the proper way, especially when transaction?
v := ctx.Value
out, err := handleQuery(ctx, v)
if err != nil {
return nil, err
}
ctx.CUELock.Lock()
defer ctx.CUELock.Unlock()
return v.FillPath(cue.ParsePath("results"), out), nil
}
func handleQuery(ctx *context.Context, val cue.Value) (interface{}, error) {
var (
query cue.Value
callType string
dbname string
qs string
iargs []interface{}
err error
)
ferr := func () error {
ctx.CUELock.Lock()
defer func() {
ctx.CUELock.Unlock()
}()
query = val.LookupPath(cue.ParsePath("query"))
if query.Exists() && query.Err() == nil {
callType = "query"
}
if callType == "" {
query = val.LookupPath(cue.ParsePath("exec"))
if query.Exists() && query.Err() == nil {
callType = "exec"
}
}
if callType == "" {
query = val.LookupPath(cue.ParsePath("stmts"))
if query.Exists() && query.Err() == nil {
callType = "stmts"
}
}
var args []string
av := val.LookupPath(cue.ParsePath("args"))
if av.Exists() {
err = av.Decode(&args)
if err != nil {
return fmt.Errorf("while decoding 'args' at %v", err)
}
}
// args for the database call
for _, a := range args {
iargs = append(iargs, a)
}
conn := val.LookupPath(cue.ParsePath("conn"))
if !conn.Exists() {
return fmt.Errorf("field 'conn' is required on db.Query at %q", val.Path())
}
// what type of database is this?
// we shouldn't look here, migrate to a lookup table
iter, err := conn.Fields()
if err != nil {
return fmt.Errorf("in field 'conn' at %v", err)
}
for iter.Next() {
sel := iter.Selector().String()
switch sel {
case "sqlite":
dbname, err = iter.Value().String()
if err != nil {
return err
}
if callType != "stmts" {
qs, err = query.String()
if err != nil {
return fmt.Errorf("in field 'query' at %v", err)
}
}
}
}
return nil
}()
if ferr != nil {
return nil, ferr
}
switch callType {
case "query":
rows, err := handleSQLiteQuery(dbname, qs, iargs)
if err != nil {
return nil, fmt.Errorf("error during query %v", err)
}
jstr, err := scanRowToJson(rows)
if err != nil {
return nil, fmt.Errorf("error during scan %v", err)
}
return val.Context().CompileBytes(jstr), nil
case "exec":
out, err := handleSQLiteExec(dbname, qs, iargs)
if err != nil {
return nil, fmt.Errorf("error during exec %v", err)
}
return out, nil
case "stmts":
out, err := handleSQLiteStmts(dbname, query, iargs)
if err != nil {
return nil, fmt.Errorf("error during query %v", err)
}
return out, nil
}
return "", fmt.Errorf("no supported conn types found in db.Query %q", val.Path())
}