-
Notifications
You must be signed in to change notification settings - Fork 2
/
result.go
237 lines (224 loc) · 6.79 KB
/
result.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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package nebulaorm
import (
"fmt"
"github.com/haysons/nebulaorm/internal/utils"
"github.com/haysons/nebulaorm/resolver"
"github.com/haysons/nebulaorm/statement"
nebula "github.com/vesoft-inc/nebula-go/v3"
"reflect"
)
// NGQL get this generated statement does not actually execute the statement
func (db *DB) NGQL() (string, error) {
tx := db.getInstance()
return tx.Statement.NGQL()
}
// RawResult exec the statement and return the result of nebula-go directly
func (db *DB) RawResult() (*nebula.ResultSet, error) {
nGQL, err := db.NGQL()
if err != nil {
return nil, err
}
return db.sessionPool.Execute(nGQL)
}
// Exec the statement, but don't care about the result as long as it is used for insert, update, delete operations
func (db *DB) Exec() error {
nGQL, err := db.NGQL()
if err != nil {
return err
}
res, err := db.sessionPool.Execute(nGQL)
if err != nil {
return err
}
if !res.IsSucceed() {
return fmt.Errorf("nebulaorm: result is not succeed, err code: %d, msg: %s", res.GetErrorCode(), res.GetErrorMsg())
}
return nil
}
// Find exec the statement and assign the returned result to the dest variable
func (db *DB) Find(dest interface{}) error {
rawRes, err := db.RawResult()
if err != nil {
return err
}
return Scan(rawRes, dest)
}
// FindCol parse one column of the result, it is used to easily get the value of a field
func (db *DB) FindCol(col string, dest interface{}) error {
rawRes, err := db.RawResult()
if err != nil {
return err
}
return Pluck(rawRes, col, dest)
}
// Take get a single test result, if no limit is specified, limit 1 will be added automatically,
// if the final return value is empty, will return ErrRecordNotFound
func (db *DB) Take(dest interface{}) error {
tx := db.getInstance()
lastPart := tx.Statement.LastPart()
if lastPart.GetType() != statement.PartTypeLimit {
tx.Statement.Limit(1)
}
nGQL, err := tx.Statement.NGQL()
if err != nil {
return err
}
rawRes, err := db.sessionPool.Execute(nGQL)
if err != nil {
return err
}
return scan(rawRes, dest, true)
}
// TakeCol parse one column of the result, it is used to easily get the value of a field
// if the final return value is empty, will return ErrRecordNotFound
func (db *DB) TakeCol(col string, dest interface{}) error {
tx := db.getInstance()
lastPart := tx.Statement.LastPart()
if lastPart.GetType() != statement.PartTypeLimit {
tx.Statement.Limit(1)
}
nGQL, err := tx.Statement.NGQL()
if err != nil {
return err
}
rawRes, err := db.sessionPool.Execute(nGQL)
if err != nil {
return err
}
return pluck(rawRes, col, dest, true)
}
// Scan assign the results to the target variable
func Scan(rawRes *nebula.ResultSet, dest interface{}) error {
return scan(rawRes, dest, false)
}
func scan(rawRes *nebula.ResultSet, dest interface{}, raiseNotFound bool) error {
if !rawRes.IsSucceed() {
return fmt.Errorf("nebulaorm: result is not succeed, err code: %d, msg: %s", rawRes.GetErrorCode(), rawRes.GetErrorMsg())
}
if rawRes.GetRowSize() == 0 {
if raiseNotFound {
return ErrRecordNotFound
} else {
return nil
}
}
switch v := dest.(type) {
case *map[string]interface{}:
if *v == nil {
*v = make(map[string]interface{})
}
record, err := rawRes.GetRowValuesByIndex(0)
if err != nil {
return err
}
return scanIntoMap(record, rawRes.GetColNames(), *v)
case map[string]interface{}:
record, err := rawRes.GetRowValuesByIndex(0)
if err != nil {
return err
}
return scanIntoMap(record, rawRes.GetColNames(), v)
case *[]map[string]interface{}:
for i := 0; i < rawRes.GetRowSize(); i++ {
record, err := rawRes.GetRowValuesByIndex(i)
if err != nil {
return err
}
value := make(map[string]interface{}, len(rawRes.GetColNames()))
if err = scanIntoMap(record, rawRes.GetColNames(), value); err != nil {
return err
}
*v = append(*v, value)
}
return nil
default:
destValue := reflect.ValueOf(dest)
if destValue.Kind() != reflect.Ptr {
return fmt.Errorf("nebulaorm: %w, scan dest should be pointer to struct, slice or array", ErrInvalidValue)
}
destValue = utils.PtrValue(destValue)
if !destValue.IsValid() {
return fmt.Errorf("nebulaorm: %w, scan dest should be pointer to struct, slice or array", ErrInvalidValue)
}
rv := resolver.NewResolver()
switch destValue.Kind() {
case reflect.Slice, reflect.Array:
return utils.SliceSetElem(destValue, rawRes.GetRowSize(), func(i int, elem reflect.Value) (bool, error) {
if i >= rawRes.GetRowSize() {
return false, nil
}
record, _ := rawRes.GetRowValuesByIndex(i)
if err := rv.ScanRecord(record, rawRes.GetColNames(), elem); err != nil {
return false, err
}
return true, nil
})
case reflect.Struct:
colNames := rawRes.GetColNames()
record, err := rawRes.GetRowValuesByIndex(0)
if err != nil {
return err
}
return rv.ScanRecord(record, colNames, destValue)
default:
return fmt.Errorf("nebulaorm: %w, scan dest should be pointer to struct, slice or array", ErrInvalidValue)
}
}
}
// scanIntoMap scan a row into map
func scanIntoMap(record *nebula.Record, colNames []string, dest map[string]interface{}) error {
for _, colName := range colNames {
colValue, err := record.GetValueByColName(colName)
if err != nil {
return err
}
dest[colName], err = resolver.GetValueIface(colValue)
if err != nil {
return err
}
}
return nil
}
// Pluck assign one of the fields of the return value into dest
func Pluck(rawRes *nebula.ResultSet, col string, dest interface{}) error {
return pluck(rawRes, col, dest, false)
}
func pluck(rawRes *nebula.ResultSet, col string, dest interface{}, raiseNotFound bool) error {
if !rawRes.IsSucceed() {
return fmt.Errorf("nebulaorm: result is not succeed, err code: %d, msg: %s", rawRes.GetErrorCode(), rawRes.GetErrorMsg())
}
if rawRes.GetRowSize() == 0 {
if raiseNotFound {
return ErrRecordNotFound
} else {
return nil
}
}
values, err := rawRes.GetValuesByColName(col)
if err != nil {
return fmt.Errorf("nebulaorm: get values by col name failed: %w", err)
}
destValue := reflect.ValueOf(dest)
if destValue.Kind() != reflect.Ptr && destValue.Kind() != reflect.Map {
return fmt.Errorf("nebulaorm: %w, dest must be able to assign, such as pointers for each type or map", ErrInvalidValue)
}
destValue = utils.PtrValue(destValue)
if !destValue.IsValid() {
return fmt.Errorf("nebulaorm: %w, dest must be able to assign, such as pointers for each type or map", ErrInvalidValue)
}
rv := resolver.NewResolver()
switch destValue.Kind() {
case reflect.Slice, reflect.Array:
return utils.SliceSetElem(destValue, rawRes.GetRowSize(), func(i int, elem reflect.Value) (bool, error) {
if i >= len(values) {
return false, nil
}
if err := rv.ScanValue(values[i], elem); err != nil {
return false, err
}
return true, nil
})
default:
return rv.ScanValue(values[0], destValue)
}
}