-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathselect.go
166 lines (130 loc) · 3.41 KB
/
select.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
155
156
157
158
159
160
161
162
163
164
165
166
package surrealhigh
import (
"errors"
"fmt"
"strings"
"github.com/surrealdb/surrealdb.go"
)
type SurrealDB interface {
Query(sql string, vars interface{}) (interface{}, error)
Update(what string, data interface{}) (interface{}, error)
Create(thing string, data interface{}) (interface{}, error)
}
type SurrealDriver interface {
Driver() SurrealDB
}
type defaultDriver struct {
db *surrealdb.DB
}
func (driver defaultDriver) Driver() SurrealDB {
return driver.db
}
func DefaultDriver(db *surrealdb.DB) SurrealDriver {
return defaultDriver{db}
}
type DBSelect[D Doc] interface {
// Do returns with the following errors; in chronological order:
// - type ErrDuplicateValuation
// - any error from surrealdb.go query driver
// - any error from surrealdb.go unmarshal
// - ErrNoResult
Do() ([]D, error)
}
type DBSelectAndUpdate[D Doc] interface {
// Do returns with the following errors
// - Any error from DBSelect.Do
// - ErrNoDoc
Do() (D, error)
}
func SelectOn[D Doc](q Select, db SurrealDriver) DBSelect[D] {
return DBSelect[D](dbSelect[D]{
query: q,
db: db,
})
}
func SelectAndUpdate[D DocWithID](q Select, update func(D) D, db SurrealDriver) DBSelectAndUpdate[D] {
return DBSelectAndUpdate[D](dbSelectUpdate[D]{
query: q,
update: update,
db: db,
})
}
type dbSelect[D Doc] struct {
query Select
db SurrealDriver
}
type DocWithID interface {
Doc
Id() Thing
}
type dbSelectUpdate[D DocWithID] struct {
query Select
update func(D) D
db SurrealDriver
}
type ErrDuplicateValuation struct {
vars []conditionAtomVar
}
func (err ErrDuplicateValuation) Error() string {
var couples []string
for _, v := range err.vars {
couples = append(couples, fmt.Sprintf("%s=%v", v.name, v.value))
}
return "found duplicate valuated condition atoms: " + strings.Join(couples, ", ")
}
var (
ErrNoResult = errors.New("surrealdb: unmarshal results: no `results`")
)
func (q dbSelect[D]) Do() ([]D, error) {
var vars map[string]interface{}
if q.query.where != nil {
vars = make(map[string]interface{})
var duplicates []conditionAtomVar
for _, v := range q.query.where.valuedVars() {
if _, ok := vars[v.name.Var()]; ok {
duplicates = append(duplicates, v)
}
vars[v.name.Var()] = v.value
}
if len(duplicates) > 0 {
return nil, ErrDuplicateValuation{duplicates}
}
}
data, err := q.db.Driver().Query(q.query.String(), vars)
if err != nil {
return nil, fmt.Errorf("surrealdb: %w", err)
}
var results []struct {
Results []D `json:"result"`
Status string `json:"status"`
}
// TODO see if there is a way to use SmartUnmarshal instead
if err := surrealdb.Unmarshal(data, &results); err != nil {
return nil, fmt.Errorf("surrealdb: unmarshal results: %w", err)
}
if len(results) == 0 {
return nil, ErrNoResult
}
if len(results[0].Results) == 0 {
return nil, ErrNoResult
}
return results[0].Results, nil
}
var ErrNoDoc = errors.New("no document matched")
func (u dbSelectUpdate[D]) Do() (D, error) {
q, db, update := u.query, u.db, u.update
docs, err := SelectOn[D](q, db).Do()
if err != nil {
var d D
return d, fmt.Errorf("select on %q: %w", d.Table(), err)
}
if len(docs) == 0 {
var d D
return d, fmt.Errorf("select on %q: %w", d.Table(), ErrNoDoc)
}
newDoc := update(docs[0])
if _, err := db.Driver().Update(docs[0].Id().String(), newDoc); err != nil {
return newDoc, fmt.Errorf("sdb: update %q: %w", docs[0].Id(), err)
}
return newDoc, nil
}