2019-08-20 15:38:40 +00:00
|
|
|
package sqlite
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql/driver"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2023-05-19 15:31:45 +00:00
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"math/big"
|
2019-08-20 15:38:40 +00:00
|
|
|
"reflect"
|
|
|
|
)
|
|
|
|
|
|
|
|
// JSONBlob type for marshaling/unmarshaling inner type to json.
|
|
|
|
type JSONBlob struct {
|
2023-05-19 15:31:45 +00:00
|
|
|
Data interface{}
|
|
|
|
Valid bool
|
2019-08-20 15:38:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Scan implements interface.
|
|
|
|
func (blob *JSONBlob) Scan(value interface{}) error {
|
|
|
|
dataVal := reflect.ValueOf(blob.Data)
|
2023-05-19 15:31:45 +00:00
|
|
|
blob.Valid = false
|
2019-08-20 15:38:40 +00:00
|
|
|
if value == nil || dataVal.Kind() == reflect.Ptr && dataVal.IsNil() {
|
|
|
|
return nil
|
|
|
|
}
|
2023-05-19 15:31:45 +00:00
|
|
|
|
|
|
|
var bytes []byte
|
|
|
|
ok := true
|
|
|
|
switch v := value.(type) {
|
|
|
|
case []byte:
|
|
|
|
bytes, ok = value.([]byte)
|
|
|
|
case string:
|
|
|
|
bytes = []byte(v)
|
|
|
|
default:
|
|
|
|
ok = false
|
|
|
|
}
|
2019-08-20 15:38:40 +00:00
|
|
|
if !ok {
|
2023-05-19 15:31:45 +00:00
|
|
|
return errors.New("not a byte slice or string")
|
2019-08-20 15:38:40 +00:00
|
|
|
}
|
|
|
|
if len(bytes) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
err := json.Unmarshal(bytes, blob.Data)
|
2023-05-19 15:31:45 +00:00
|
|
|
blob.Valid = err == nil
|
2019-08-20 15:38:40 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value implements interface.
|
|
|
|
func (blob *JSONBlob) Value() (driver.Value, error) {
|
|
|
|
dataVal := reflect.ValueOf(blob.Data)
|
2022-03-23 18:47:00 +00:00
|
|
|
if (blob.Data == nil) || (dataVal.Kind() == reflect.Ptr && dataVal.IsNil()) {
|
2019-08-20 15:38:40 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
2022-03-23 18:47:00 +00:00
|
|
|
|
|
|
|
switch dataVal.Kind() {
|
|
|
|
case reflect.Slice, reflect.Array, reflect.Map:
|
|
|
|
if dataVal.Len() == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-20 15:38:40 +00:00
|
|
|
return json.Marshal(blob.Data)
|
|
|
|
}
|
2023-05-19 15:31:45 +00:00
|
|
|
|
|
|
|
func BigIntToClampedInt64(val *big.Int) *int64 {
|
|
|
|
if val == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var v int64
|
|
|
|
if val.IsInt64() {
|
|
|
|
v = val.Int64()
|
|
|
|
} else {
|
|
|
|
v = math.MaxInt64
|
|
|
|
}
|
|
|
|
return &v
|
|
|
|
}
|
|
|
|
|
|
|
|
// BigIntToPadded128BitsStr converts a big.Int to a string, padding it with 0 to account for 128 bits size
|
|
|
|
// Returns nil if input val is nil
|
|
|
|
// This should work to sort and compare big.Ints values in SQLite
|
|
|
|
func BigIntToPadded128BitsStr(val *big.Int) *string {
|
|
|
|
if val == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
hexStr := val.Text(16)
|
|
|
|
res := new(string)
|
|
|
|
*res = fmt.Sprintf("%032s", hexStr)
|
|
|
|
return res
|
|
|
|
}
|