SQL Expressions: Add JSON support (#103157)
- Support bi-directional mapping of frame JSON fields and GMS (go-mysql-server) columns - Permit GMS json functions Co-authored-by: Kyle Brandt <kyle@grafana.com>
This commit is contained in:
co-authored by
Kyle Brandt
parent
6754781d7b
commit
af08a9fae2
@@ -3,6 +3,7 @@
|
||||
package sql
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -92,6 +93,8 @@ func MySQLColToFieldType(col *mysql.Column) (data.FieldType, error) {
|
||||
fT = data.FieldTypeTime
|
||||
case types.Boolean:
|
||||
fT = data.FieldTypeBool
|
||||
case types.JSON:
|
||||
fT = data.FieldTypeJSON
|
||||
default:
|
||||
switch {
|
||||
case types.IsDecimal(col.Type):
|
||||
@@ -159,8 +162,21 @@ func fieldValFromRowVal(fieldType data.FieldType, val interface{}) (interface{},
|
||||
case data.FieldTypeBool, data.FieldTypeNullableBool:
|
||||
return parseBoolFromInt8(val, nullable)
|
||||
|
||||
case data.FieldTypeJSON, data.FieldTypeNullableJSON:
|
||||
switch v := val.(type) {
|
||||
case types.JSONDocument:
|
||||
raw := json.RawMessage(v.String())
|
||||
if nullable {
|
||||
return &raw, nil
|
||||
}
|
||||
return raw, nil
|
||||
|
||||
default:
|
||||
return nil, fmt.Errorf("JSON field does not support val %v of type %T", val, val)
|
||||
}
|
||||
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported field type %s for val %v", fieldType, val)
|
||||
return nil, fmt.Errorf("unsupported field type %s for val %v of type %T", fieldType, val, val)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user