| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package sqltypes |
| 18 |
|
| 19 |
import ( |
| 20 |
"errors" |
| 21 |
"fmt" |
| 22 |
"reflect" |
| 23 |
"strconv" |
| 24 |
|
| 25 |
"github.com/xwb1989/sqlparser/dependency/querypb" |
| 26 |
) |
| 27 |
|
| 28 |
|
| 29 |
var NullBindVariable = &querypb.BindVariable{Type: querypb.Type_NULL_TYPE} |
| 30 |
|
| 31 |
|
| 32 |
func ValueToProto(v Value) *querypb.Value { |
| 33 |
return &querypb.Value{Type: v.typ, Value: v.val} |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
func ProtoToValue(v *querypb.Value) Value { |
| 38 |
return MakeTrusted(v.Type, v.Value) |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
func BuildBindVariables(in map[string]interface{}) (map[string]*querypb.BindVariable, error) { |
| 43 |
if len(in) == 0 { |
| 44 |
return nil, nil |
| 45 |
} |
| 46 |
|
| 47 |
out := make(map[string]*querypb.BindVariable, len(in)) |
| 48 |
for k, v := range in { |
| 49 |
bv, err := BuildBindVariable(v) |
| 50 |
if err != nil { |
| 51 |
return nil, fmt.Errorf("%s: %v", k, err) |
| 52 |
} |
| 53 |
out[k] = bv |
| 54 |
} |
| 55 |
return out, nil |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
func Int32BindVariable(v int32) *querypb.BindVariable { |
| 60 |
return ValueBindVariable(NewInt32(v)) |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
func Int64BindVariable(v int64) *querypb.BindVariable { |
| 65 |
return ValueBindVariable(NewInt64(v)) |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
func Uint64BindVariable(v uint64) *querypb.BindVariable { |
| 70 |
return ValueBindVariable(NewUint64(v)) |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
func Float64BindVariable(v float64) *querypb.BindVariable { |
| 75 |
return ValueBindVariable(NewFloat64(v)) |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
func StringBindVariable(v string) *querypb.BindVariable { |
| 80 |
return ValueBindVariable(NewVarChar(v)) |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
func BytesBindVariable(v []byte) *querypb.BindVariable { |
| 85 |
return &querypb.BindVariable{Type: VarBinary, Value: v} |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
func ValueBindVariable(v Value) *querypb.BindVariable { |
| 90 |
return &querypb.BindVariable{Type: v.typ, Value: v.val} |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
func BuildBindVariable(v interface{}) (*querypb.BindVariable, error) { |
| 95 |
switch v := v.(type) { |
| 96 |
case string: |
| 97 |
return StringBindVariable(v), nil |
| 98 |
case []byte: |
| 99 |
return BytesBindVariable(v), nil |
| 100 |
case int: |
| 101 |
return &querypb.BindVariable{ |
| 102 |
Type: querypb.Type_INT64, |
| 103 |
Value: strconv.AppendInt(nil, int64(v), 10), |
| 104 |
}, nil |
| 105 |
case int64: |
| 106 |
return Int64BindVariable(v), nil |
| 107 |
case uint64: |
| 108 |
return Uint64BindVariable(v), nil |
| 109 |
case float64: |
| 110 |
return Float64BindVariable(v), nil |
| 111 |
case nil: |
| 112 |
return NullBindVariable, nil |
| 113 |
case Value: |
| 114 |
return ValueBindVariable(v), nil |
| 115 |
case *querypb.BindVariable: |
| 116 |
return v, nil |
| 117 |
case []interface{}: |
| 118 |
bv := &querypb.BindVariable{ |
| 119 |
Type: querypb.Type_TUPLE, |
| 120 |
Values: make([]*querypb.Value, len(v)), |
| 121 |
} |
| 122 |
values := make([]querypb.Value, len(v)) |
| 123 |
for i, lv := range v { |
| 124 |
lbv, err := BuildBindVariable(lv) |
| 125 |
if err != nil { |
| 126 |
return nil, err |
| 127 |
} |
| 128 |
values[i].Type = lbv.Type |
| 129 |
values[i].Value = lbv.Value |
| 130 |
bv.Values[i] = &values[i] |
| 131 |
} |
| 132 |
return bv, nil |
| 133 |
case []string: |
| 134 |
bv := &querypb.BindVariable{ |
| 135 |
Type: querypb.Type_TUPLE, |
| 136 |
Values: make([]*querypb.Value, len(v)), |
| 137 |
} |
| 138 |
values := make([]querypb.Value, len(v)) |
| 139 |
for i, lv := range v { |
| 140 |
values[i].Type = querypb.Type_VARCHAR |
| 141 |
values[i].Value = []byte(lv) |
| 142 |
bv.Values[i] = &values[i] |
| 143 |
} |
| 144 |
return bv, nil |
| 145 |
case [][]byte: |
| 146 |
bv := &querypb.BindVariable{ |
| 147 |
Type: querypb.Type_TUPLE, |
| 148 |
Values: make([]*querypb.Value, len(v)), |
| 149 |
} |
| 150 |
values := make([]querypb.Value, len(v)) |
| 151 |
for i, lv := range v { |
| 152 |
values[i].Type = querypb.Type_VARBINARY |
| 153 |
values[i].Value = lv |
| 154 |
bv.Values[i] = &values[i] |
| 155 |
} |
| 156 |
return bv, nil |
| 157 |
case []int: |
| 158 |
bv := &querypb.BindVariable{ |
| 159 |
Type: querypb.Type_TUPLE, |
| 160 |
Values: make([]*querypb.Value, len(v)), |
| 161 |
} |
| 162 |
values := make([]querypb.Value, len(v)) |
| 163 |
for i, lv := range v { |
| 164 |
values[i].Type = querypb.Type_INT64 |
| 165 |
values[i].Value = strconv.AppendInt(nil, int64(lv), 10) |
| 166 |
bv.Values[i] = &values[i] |
| 167 |
} |
| 168 |
return bv, nil |
| 169 |
case []int64: |
| 170 |
bv := &querypb.BindVariable{ |
| 171 |
Type: querypb.Type_TUPLE, |
| 172 |
Values: make([]*querypb.Value, len(v)), |
| 173 |
} |
| 174 |
values := make([]querypb.Value, len(v)) |
| 175 |
for i, lv := range v { |
| 176 |
values[i].Type = querypb.Type_INT64 |
| 177 |
values[i].Value = strconv.AppendInt(nil, lv, 10) |
| 178 |
bv.Values[i] = &values[i] |
| 179 |
} |
| 180 |
return bv, nil |
| 181 |
case []uint64: |
| 182 |
bv := &querypb.BindVariable{ |
| 183 |
Type: querypb.Type_TUPLE, |
| 184 |
Values: make([]*querypb.Value, len(v)), |
| 185 |
} |
| 186 |
values := make([]querypb.Value, len(v)) |
| 187 |
for i, lv := range v { |
| 188 |
values[i].Type = querypb.Type_UINT64 |
| 189 |
values[i].Value = strconv.AppendUint(nil, lv, 10) |
| 190 |
bv.Values[i] = &values[i] |
| 191 |
} |
| 192 |
return bv, nil |
| 193 |
case []float64: |
| 194 |
bv := &querypb.BindVariable{ |
| 195 |
Type: querypb.Type_TUPLE, |
| 196 |
Values: make([]*querypb.Value, len(v)), |
| 197 |
} |
| 198 |
values := make([]querypb.Value, len(v)) |
| 199 |
for i, lv := range v { |
| 200 |
values[i].Type = querypb.Type_FLOAT64 |
| 201 |
values[i].Value = strconv.AppendFloat(nil, lv, 'g', -1, 64) |
| 202 |
bv.Values[i] = &values[i] |
| 203 |
} |
| 204 |
return bv, nil |
| 205 |
} |
| 206 |
return nil, fmt.Errorf("type %T not supported as bind var: %v", v, v) |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
func ValidateBindVariables(bv map[string]*querypb.BindVariable) error { |
| 211 |
for k, v := range bv { |
| 212 |
if err := ValidateBindVariable(v); err != nil { |
| 213 |
return fmt.Errorf("%s: %v", k, err) |
| 214 |
} |
| 215 |
} |
| 216 |
return nil |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
func ValidateBindVariable(bv *querypb.BindVariable) error { |
| 222 |
if bv == nil { |
| 223 |
return errors.New("bind variable is nil") |
| 224 |
} |
| 225 |
|
| 226 |
if bv.Type == querypb.Type_TUPLE { |
| 227 |
if len(bv.Values) == 0 { |
| 228 |
return errors.New("empty tuple is not allowed") |
| 229 |
} |
| 230 |
for _, val := range bv.Values { |
| 231 |
if val.Type == querypb.Type_TUPLE { |
| 232 |
return errors.New("tuple not allowed inside another tuple") |
| 233 |
} |
| 234 |
if err := ValidateBindVariable(&querypb.BindVariable{Type: val.Type, Value: val.Value}); err != nil { |
| 235 |
return err |
| 236 |
} |
| 237 |
} |
| 238 |
return nil |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
_, err := NewValue(bv.Type, bv.Value) |
| 243 |
return err |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
func BindVariableToValue(bv *querypb.BindVariable) (Value, error) { |
| 248 |
if bv.Type == querypb.Type_TUPLE { |
| 249 |
return NULL, errors.New("cannot convert a TUPLE bind var into a value") |
| 250 |
} |
| 251 |
return MakeTrusted(bv.Type, bv.Value), nil |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
func BindVariablesEqual(x, y map[string]*querypb.BindVariable) bool { |
| 256 |
return reflect.DeepEqual(&querypb.BoundQuery{BindVariables: x}, &querypb.BoundQuery{BindVariables: y}) |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
func CopyBindVariables(bindVariables map[string]*querypb.BindVariable) map[string]*querypb.BindVariable { |
| 261 |
result := make(map[string]*querypb.BindVariable, len(bindVariables)) |
| 262 |
for key, value := range bindVariables { |
| 263 |
result[key] = value |
| 264 |
} |
| 265 |
return result |
| 266 |
} |