-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathnamed_result.go
172 lines (149 loc) · 4.72 KB
/
named_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
/*
Copyright 2019 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package sqltypes
import (
"errors"
querypb "vitess.io/vitess/go/vt/proto/query"
)
var (
// ErrNoSuchField indicates a search for a value by an unknown field/column name
ErrNoSuchField = errors.New("No such field in RowNamedValues")
)
// RowNamedValues contains a row's values as a map based on Field (aka table column) name
type RowNamedValues map[string]Value
// ToString returns the named field as string
func (r RowNamedValues) ToString(fieldName string) (string, error) {
if v, ok := r[fieldName]; ok {
return v.ToString(), nil
}
return "", ErrNoSuchField
}
// AsString returns the named field as string, or default value if nonexistent/error
func (r RowNamedValues) AsString(fieldName string, def string) string {
if v, err := r.ToString(fieldName); err == nil {
return v
}
return def
}
// ToInt64 returns the named field as int64
func (r RowNamedValues) ToInt64(fieldName string) (int64, error) {
if v, ok := r[fieldName]; ok {
return v.ToInt64()
}
return 0, ErrNoSuchField
}
// AsInt64 returns the named field as int64, or default value if nonexistent/error
func (r RowNamedValues) AsInt64(fieldName string, def int64) int64 {
if v, err := r.ToInt64(fieldName); err == nil {
return v
}
return def
}
// ToUint64 returns the named field as uint64
func (r RowNamedValues) ToUint64(fieldName string) (uint64, error) {
if v, ok := r[fieldName]; ok {
return v.ToUint64()
}
return 0, ErrNoSuchField
}
// AsUint64 returns the named field as uint64, or default value if nonexistent/error
func (r RowNamedValues) AsUint64(fieldName string, def uint64) uint64 {
if v, err := r.ToUint64(fieldName); err == nil {
return v
}
return def
}
// AsFloat64 returns the named field as float64, or default value if nonexistent/error
func (r RowNamedValues) AsFloat64(fieldName string, def float64) float64 {
if v, err := r.ToFloat64(fieldName); err == nil {
return v
}
return def
}
// ToFloat64 returns the named field as float64
func (r RowNamedValues) ToFloat64(fieldName string) (float64, error) {
if v, ok := r[fieldName]; ok {
return v.ToFloat64()
}
return 0, ErrNoSuchField
}
// ToBool returns the named field as bool
func (r RowNamedValues) ToBool(fieldName string) (bool, error) {
if v, ok := r[fieldName]; ok {
return v.ToBool()
}
return false, ErrNoSuchField
}
// AsBool returns the named field as bool, or default value if nonexistent/error
func (r RowNamedValues) AsBool(fieldName string, def bool) bool {
if v, err := r.ToBool(fieldName); err == nil {
return v
}
return def
}
// ToBytes returns the named field as a byte array
func (r RowNamedValues) ToBytes(fieldName string) ([]byte, error) {
if v, ok := r[fieldName]; ok {
return v.ToBytes()
}
return nil, ErrNoSuchField
}
// AsBytes returns the named field as a byte array, or default value if nonexistent/error
func (r RowNamedValues) AsBytes(fieldName string, def []byte) []byte {
if v, err := r.ToBytes(fieldName); err == nil {
return v
}
return def
}
// NamedResult represents a query result with named values as opposed to ordinal values.
type NamedResult struct {
Fields []*querypb.Field `json:"fields"`
RowsAffected uint64 `json:"rows_affected"`
InsertID uint64 `json:"insert_id"`
Rows []RowNamedValues `json:"rows"`
}
// ToNamedResult converts a Result struct into a new NamedResult struct
func ToNamedResult(result *Result) (r *NamedResult) {
if result == nil {
return r
}
r = &NamedResult{
Fields: result.Fields,
RowsAffected: result.RowsAffected,
InsertID: result.InsertID,
}
columnOrdinals := make(map[int]string)
for i, field := range result.Fields {
columnOrdinals[i] = field.Name
}
r.Rows = make([]RowNamedValues, len(result.Rows))
for rowIndex, row := range result.Rows {
namedRow := make(RowNamedValues)
for i, value := range row {
namedRow[columnOrdinals[i]] = value
}
r.Rows[rowIndex] = namedRow
}
return r
}
// Row assumes this result has exactly one row, and returns it, or else returns nil.
// It is useful for queries like:
// - select count(*) from ...
// - select @@read_only
// - select UNIX_TIMESTAMP() from dual
func (r *NamedResult) Row() RowNamedValues {
if len(r.Rows) != 1 {
return nil
}
return r.Rows[0]
}