/
transaction_queryresult.go
71 lines (57 loc) · 1.58 KB
/
transaction_queryresult.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
package gocb
import (
"encoding/json"
)
// TransactionQueryResult allows access to the results of a query.
type TransactionQueryResult struct {
results []json.RawMessage
idx int
rowBytes json.RawMessage
metadata *QueryMetaData
endpoint string
}
func newTransactionQueryResult(results []json.RawMessage, meta *QueryMetaData, endpoint string) *TransactionQueryResult {
return &TransactionQueryResult{
results: results,
metadata: meta,
endpoint: endpoint,
}
}
// Next assigns the next result from the results into the value pointer, returning whether the read was successful.
func (r *TransactionQueryResult) Next() bool {
if r.idx >= len(r.results) {
return false
}
r.rowBytes = r.results[r.idx]
r.idx++
return true
}
// Row returns the contents of the current row
func (r *TransactionQueryResult) Row(valuePtr interface{}) error {
if r.rowBytes == nil {
return ErrNoResult
}
if bytesPtr, ok := valuePtr.(*json.RawMessage); ok {
*bytesPtr = r.rowBytes
return nil
}
return json.Unmarshal(r.rowBytes, valuePtr)
}
// One assigns the first value from the results into the value pointer.
func (r *TransactionQueryResult) One(valuePtr interface{}) error {
// Prime the row
if !r.Next() {
return ErrNoResult
}
err := r.Row(valuePtr)
if err != nil {
return err
}
return nil
}
// MetaData returns any meta-data that was available from this query. Note that
// the meta-data will only be available once the object has been closed (either
// implicitly or explicitly).
func (r *TransactionQueryResult) MetaData() (*QueryMetaData, error) {
return r.metadata, nil
}