| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
package sqltypes |
| 19 |
|
| 20 |
import ( |
| 21 |
"encoding/base64" |
| 22 |
"encoding/json" |
| 23 |
"fmt" |
| 24 |
"strconv" |
| 25 |
|
| 26 |
"github.com/xwb1989/sqlparser/dependency/bytes2" |
| 27 |
"github.com/xwb1989/sqlparser/dependency/hack" |
| 28 |
|
| 29 |
"github.com/xwb1989/sqlparser/dependency/querypb" |
| 30 |
) |
| 31 |
|
| 32 |
var ( |
| 33 |
|
| 34 |
NULL = Value{} |
| 35 |
|
| 36 |
|
| 37 |
DontEscape = byte(255) |
| 38 |
|
| 39 |
nullstr = []byte("null") |
| 40 |
) |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
type BinWriter interface { |
| 47 |
Write([]byte) (int, error) |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
type Value struct { |
| 54 |
typ querypb.Type |
| 55 |
val []byte |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
func NewValue(typ querypb.Type, val []byte) (v Value, err error) { |
| 61 |
switch { |
| 62 |
case IsSigned(typ): |
| 63 |
if _, err := strconv.ParseInt(string(val), 0, 64); err != nil { |
| 64 |
return NULL, err |
| 65 |
} |
| 66 |
return MakeTrusted(typ, val), nil |
| 67 |
case IsUnsigned(typ): |
| 68 |
if _, err := strconv.ParseUint(string(val), 0, 64); err != nil { |
| 69 |
return NULL, err |
| 70 |
} |
| 71 |
return MakeTrusted(typ, val), nil |
| 72 |
case IsFloat(typ) || typ == Decimal: |
| 73 |
if _, err := strconv.ParseFloat(string(val), 64); err != nil { |
| 74 |
return NULL, err |
| 75 |
} |
| 76 |
return MakeTrusted(typ, val), nil |
| 77 |
case IsQuoted(typ) || typ == Null: |
| 78 |
return MakeTrusted(typ, val), nil |
| 79 |
} |
| 80 |
|
| 81 |
return NULL, fmt.Errorf("invalid type specified for MakeValue: %v", typ) |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
func MakeTrusted(typ querypb.Type, val []byte) Value { |
| 92 |
if typ == Null { |
| 93 |
return NULL |
| 94 |
} |
| 95 |
return Value{typ: typ, val: val} |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
func NewInt64(v int64) Value { |
| 100 |
return MakeTrusted(Int64, strconv.AppendInt(nil, v, 10)) |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
func NewInt32(v int32) Value { |
| 105 |
return MakeTrusted(Int32, strconv.AppendInt(nil, int64(v), 10)) |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
func NewUint64(v uint64) Value { |
| 110 |
return MakeTrusted(Uint64, strconv.AppendUint(nil, v, 10)) |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
func NewFloat64(v float64) Value { |
| 115 |
return MakeTrusted(Float64, strconv.AppendFloat(nil, v, 'g', -1, 64)) |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
func NewVarChar(v string) Value { |
| 120 |
return MakeTrusted(VarChar, []byte(v)) |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
func NewVarBinary(v string) Value { |
| 126 |
return MakeTrusted(VarBinary, []byte(v)) |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
func NewIntegral(val string) (n Value, err error) { |
| 132 |
signed, err := strconv.ParseInt(val, 0, 64) |
| 133 |
if err == nil { |
| 134 |
return MakeTrusted(Int64, strconv.AppendInt(nil, signed, 10)), nil |
| 135 |
} |
| 136 |
unsigned, err := strconv.ParseUint(val, 0, 64) |
| 137 |
if err != nil { |
| 138 |
return Value{}, err |
| 139 |
} |
| 140 |
return MakeTrusted(Uint64, strconv.AppendUint(nil, unsigned, 10)), nil |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
func InterfaceToValue(goval interface{}) (Value, error) { |
| 149 |
switch goval := goval.(type) { |
| 150 |
case nil: |
| 151 |
return NULL, nil |
| 152 |
case []byte: |
| 153 |
return MakeTrusted(VarBinary, goval), nil |
| 154 |
case int64: |
| 155 |
return NewInt64(goval), nil |
| 156 |
case uint64: |
| 157 |
return NewUint64(goval), nil |
| 158 |
case float64: |
| 159 |
return NewFloat64(goval), nil |
| 160 |
case string: |
| 161 |
return NewVarChar(goval), nil |
| 162 |
default: |
| 163 |
return NULL, fmt.Errorf("unexpected type %T: %v", goval, goval) |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
func (v Value) Type() querypb.Type { |
| 169 |
return v.typ |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
func (v Value) Raw() []byte { |
| 175 |
return v.val |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
func (v Value) ToBytes() []byte { |
| 183 |
if v.typ == Expression { |
| 184 |
return nil |
| 185 |
} |
| 186 |
return v.val |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
func (v Value) Len() int { |
| 191 |
return len(v.val) |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
func (v Value) ToString() string { |
| 197 |
if v.typ == Expression { |
| 198 |
return "" |
| 199 |
} |
| 200 |
return hack.String(v.val) |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
func (v Value) String() string { |
| 205 |
if v.typ == Null { |
| 206 |
return "NULL" |
| 207 |
} |
| 208 |
if v.IsQuoted() { |
| 209 |
return fmt.Sprintf("%v(%q)", v.typ, v.val) |
| 210 |
} |
| 211 |
return fmt.Sprintf("%v(%s)", v.typ, v.val) |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
func (v Value) EncodeSQL(b BinWriter) { |
| 216 |
switch { |
| 217 |
case v.typ == Null: |
| 218 |
b.Write(nullstr) |
| 219 |
case v.IsQuoted(): |
| 220 |
encodeBytesSQL(v.val, b) |
| 221 |
default: |
| 222 |
b.Write(v.val) |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
func (v Value) EncodeASCII(b BinWriter) { |
| 228 |
switch { |
| 229 |
case v.typ == Null: |
| 230 |
b.Write(nullstr) |
| 231 |
case v.IsQuoted(): |
| 232 |
encodeBytesASCII(v.val, b) |
| 233 |
default: |
| 234 |
b.Write(v.val) |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
func (v Value) IsNull() bool { |
| 240 |
return v.typ == Null |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
func (v Value) IsIntegral() bool { |
| 245 |
return IsIntegral(v.typ) |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
func (v Value) IsSigned() bool { |
| 250 |
return IsSigned(v.typ) |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
func (v Value) IsUnsigned() bool { |
| 255 |
return IsUnsigned(v.typ) |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
func (v Value) IsFloat() bool { |
| 260 |
return IsFloat(v.typ) |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
func (v Value) IsQuoted() bool { |
| 265 |
return IsQuoted(v.typ) |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
func (v Value) IsText() bool { |
| 270 |
return IsText(v.typ) |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
func (v Value) IsBinary() bool { |
| 275 |
return IsBinary(v.typ) |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
func (v Value) MarshalJSON() ([]byte, error) { |
| 281 |
switch { |
| 282 |
case v.IsQuoted(): |
| 283 |
return json.Marshal(v.ToString()) |
| 284 |
case v.typ == Null: |
| 285 |
return nullstr, nil |
| 286 |
} |
| 287 |
return v.val, nil |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
func (v *Value) UnmarshalJSON(b []byte) error { |
| 293 |
if len(b) == 0 { |
| 294 |
return fmt.Errorf("error unmarshaling empty bytes") |
| 295 |
} |
| 296 |
var val interface{} |
| 297 |
var err error |
| 298 |
switch b[0] { |
| 299 |
case '-': |
| 300 |
var ival int64 |
| 301 |
err = json.Unmarshal(b, &ival) |
| 302 |
val = ival |
| 303 |
case '"': |
| 304 |
var bval []byte |
| 305 |
err = json.Unmarshal(b, &bval) |
| 306 |
val = bval |
| 307 |
case 'n': |
| 308 |
err = json.Unmarshal(b, &val) |
| 309 |
default: |
| 310 |
var uval uint64 |
| 311 |
err = json.Unmarshal(b, &uval) |
| 312 |
val = uval |
| 313 |
} |
| 314 |
if err != nil { |
| 315 |
return err |
| 316 |
} |
| 317 |
*v, err = InterfaceToValue(val) |
| 318 |
return err |
| 319 |
} |
| 320 |
|
| 321 |
func encodeBytesSQL(val []byte, b BinWriter) { |
| 322 |
buf := &bytes2.Buffer{} |
| 323 |
buf.WriteByte('\'') |
| 324 |
for _, ch := range val { |
| 325 |
if encodedChar := SQLEncodeMap[ch]; encodedChar == DontEscape { |
| 326 |
buf.WriteByte(ch) |
| 327 |
} else { |
| 328 |
buf.WriteByte('\\') |
| 329 |
buf.WriteByte(encodedChar) |
| 330 |
} |
| 331 |
} |
| 332 |
buf.WriteByte('\'') |
| 333 |
b.Write(buf.Bytes()) |
| 334 |
} |
| 335 |
|
| 336 |
func encodeBytesASCII(val []byte, b BinWriter) { |
| 337 |
buf := &bytes2.Buffer{} |
| 338 |
buf.WriteByte('\'') |
| 339 |
encoder := base64.NewEncoder(base64.StdEncoding, buf) |
| 340 |
encoder.Write(val) |
| 341 |
encoder.Close() |
| 342 |
buf.WriteByte('\'') |
| 343 |
b.Write(buf.Bytes()) |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
var SQLEncodeMap [256]byte |
| 349 |
|
| 350 |
|
| 351 |
var SQLDecodeMap [256]byte |
| 352 |
|
| 353 |
var encodeRef = map[byte]byte{ |
| 354 |
'\x00': '0', |
| 355 |
'\'': '\'', |
| 356 |
'"': '"', |
| 357 |
'\b': 'b', |
| 358 |
'\n': 'n', |
| 359 |
'\r': 'r', |
| 360 |
'\t': 't', |
| 361 |
26: 'Z', |
| 362 |
'\\': '\\', |
| 363 |
} |
| 364 |
|
| 365 |
func init() { |
| 366 |
for i := range SQLEncodeMap { |
| 367 |
SQLEncodeMap[i] = DontEscape |
| 368 |
SQLDecodeMap[i] = DontEscape |
| 369 |
} |
| 370 |
for i := range SQLEncodeMap { |
| 371 |
if to, ok := encodeRef[byte(i)]; ok { |
| 372 |
SQLEncodeMap[byte(i)] = to |
| 373 |
SQLDecodeMap[to] = byte(i) |
| 374 |
} |
| 375 |
} |
| 376 |
} |