2021-08-25 15:34:33 +02:00
|
|
|
package schema
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql/driver"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"reflect"
|
|
|
|
"strconv"
|
2021-09-23 11:13:28 +02:00
|
|
|
"strings"
|
2021-10-24 13:14:37 +02:00
|
|
|
"sync"
|
2021-08-25 15:34:33 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/uptrace/bun/dialect"
|
2021-09-23 11:13:28 +02:00
|
|
|
"github.com/uptrace/bun/dialect/sqltype"
|
2021-08-25 15:34:33 +02:00
|
|
|
"github.com/uptrace/bun/extra/bunjson"
|
|
|
|
"github.com/uptrace/bun/internal"
|
2021-10-24 13:14:37 +02:00
|
|
|
"github.com/vmihailenco/msgpack/v5"
|
2021-08-25 15:34:33 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
AppenderFunc func(fmter Formatter, b []byte, v reflect.Value) []byte
|
|
|
|
CustomAppender func(typ reflect.Type) AppenderFunc
|
|
|
|
)
|
|
|
|
|
|
|
|
var appenders = []AppenderFunc{
|
|
|
|
reflect.Bool: AppendBoolValue,
|
|
|
|
reflect.Int: AppendIntValue,
|
|
|
|
reflect.Int8: AppendIntValue,
|
|
|
|
reflect.Int16: AppendIntValue,
|
|
|
|
reflect.Int32: AppendIntValue,
|
|
|
|
reflect.Int64: AppendIntValue,
|
|
|
|
reflect.Uint: AppendUintValue,
|
|
|
|
reflect.Uint8: AppendUintValue,
|
|
|
|
reflect.Uint16: AppendUintValue,
|
2021-10-24 13:14:37 +02:00
|
|
|
reflect.Uint32: appendUint32Value,
|
|
|
|
reflect.Uint64: appendUint64Value,
|
2021-08-25 15:34:33 +02:00
|
|
|
reflect.Uintptr: nil,
|
|
|
|
reflect.Float32: AppendFloat32Value,
|
|
|
|
reflect.Float64: AppendFloat64Value,
|
|
|
|
reflect.Complex64: nil,
|
|
|
|
reflect.Complex128: nil,
|
|
|
|
reflect.Array: AppendJSONValue,
|
|
|
|
reflect.Chan: nil,
|
|
|
|
reflect.Func: nil,
|
|
|
|
reflect.Interface: nil,
|
|
|
|
reflect.Map: AppendJSONValue,
|
|
|
|
reflect.Ptr: nil,
|
|
|
|
reflect.Slice: AppendJSONValue,
|
|
|
|
reflect.String: AppendStringValue,
|
|
|
|
reflect.Struct: AppendJSONValue,
|
|
|
|
reflect.UnsafePointer: nil,
|
|
|
|
}
|
|
|
|
|
2021-10-24 13:14:37 +02:00
|
|
|
var appenderMap sync.Map
|
|
|
|
|
2021-09-23 11:13:28 +02:00
|
|
|
func FieldAppender(dialect Dialect, field *Field) AppenderFunc {
|
|
|
|
if field.Tag.HasOption("msgpack") {
|
|
|
|
return appendMsgpack
|
|
|
|
}
|
|
|
|
|
2022-04-24 12:26:22 +02:00
|
|
|
fieldType := field.StructField.Type
|
|
|
|
|
2021-09-23 11:13:28 +02:00
|
|
|
switch strings.ToUpper(field.UserSQLType) {
|
|
|
|
case sqltype.JSON, sqltype.JSONB:
|
2022-04-24 12:26:22 +02:00
|
|
|
if fieldType.Implements(driverValuerType) {
|
|
|
|
return appendDriverValue
|
|
|
|
}
|
|
|
|
|
|
|
|
if fieldType.Kind() != reflect.Ptr {
|
|
|
|
if reflect.PtrTo(fieldType).Implements(driverValuerType) {
|
|
|
|
return addrAppender(appendDriverValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-23 11:13:28 +02:00
|
|
|
return AppendJSONValue
|
|
|
|
}
|
|
|
|
|
2022-04-24 12:26:22 +02:00
|
|
|
return Appender(dialect, fieldType)
|
2021-10-24 13:14:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func Appender(dialect Dialect, typ reflect.Type) AppenderFunc {
|
|
|
|
if v, ok := appenderMap.Load(typ); ok {
|
|
|
|
return v.(AppenderFunc)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn := appender(dialect, typ)
|
|
|
|
|
|
|
|
if v, ok := appenderMap.LoadOrStore(typ, fn); ok {
|
|
|
|
return v.(AppenderFunc)
|
|
|
|
}
|
|
|
|
return fn
|
2021-09-23 11:13:28 +02:00
|
|
|
}
|
|
|
|
|
2021-10-24 13:14:37 +02:00
|
|
|
func appender(dialect Dialect, typ reflect.Type) AppenderFunc {
|
2021-08-25 15:34:33 +02:00
|
|
|
switch typ {
|
2021-09-08 21:05:26 +02:00
|
|
|
case bytesType:
|
|
|
|
return appendBytesValue
|
2021-08-25 15:34:33 +02:00
|
|
|
case timeType:
|
|
|
|
return appendTimeValue
|
2022-04-24 12:26:22 +02:00
|
|
|
case timePtrType:
|
|
|
|
return PtrAppender(appendTimeValue)
|
2021-08-25 15:34:33 +02:00
|
|
|
case ipType:
|
|
|
|
return appendIPValue
|
|
|
|
case ipNetType:
|
|
|
|
return appendIPNetValue
|
|
|
|
case jsonRawMessageType:
|
|
|
|
return appendJSONRawMessageValue
|
|
|
|
}
|
|
|
|
|
2022-04-24 12:26:22 +02:00
|
|
|
kind := typ.Kind()
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
if typ.Implements(queryAppenderType) {
|
2022-04-24 12:26:22 +02:00
|
|
|
if kind == reflect.Ptr {
|
|
|
|
return nilAwareAppender(appendQueryAppenderValue)
|
|
|
|
}
|
2021-08-25 15:34:33 +02:00
|
|
|
return appendQueryAppenderValue
|
|
|
|
}
|
|
|
|
if typ.Implements(driverValuerType) {
|
2022-04-24 12:26:22 +02:00
|
|
|
if kind == reflect.Ptr {
|
|
|
|
return nilAwareAppender(appendDriverValue)
|
|
|
|
}
|
2021-10-24 13:14:37 +02:00
|
|
|
return appendDriverValue
|
2021-08-25 15:34:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if kind != reflect.Ptr {
|
|
|
|
ptr := reflect.PtrTo(typ)
|
|
|
|
if ptr.Implements(queryAppenderType) {
|
2021-10-24 13:14:37 +02:00
|
|
|
return addrAppender(appendQueryAppenderValue)
|
2021-08-25 15:34:33 +02:00
|
|
|
}
|
|
|
|
if ptr.Implements(driverValuerType) {
|
2021-10-24 13:14:37 +02:00
|
|
|
return addrAppender(appendDriverValue)
|
2021-08-25 15:34:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch kind {
|
|
|
|
case reflect.Interface:
|
2021-10-24 13:14:37 +02:00
|
|
|
return ifaceAppenderFunc
|
2021-08-25 15:34:33 +02:00
|
|
|
case reflect.Ptr:
|
2022-04-24 12:26:22 +02:00
|
|
|
if typ.Implements(jsonMarshalerType) {
|
|
|
|
return nilAwareAppender(AppendJSONValue)
|
|
|
|
}
|
2021-10-24 13:14:37 +02:00
|
|
|
if fn := Appender(dialect, typ.Elem()); fn != nil {
|
2021-09-08 21:05:26 +02:00
|
|
|
return PtrAppender(fn)
|
|
|
|
}
|
2021-08-25 15:34:33 +02:00
|
|
|
case reflect.Slice:
|
|
|
|
if typ.Elem().Kind() == reflect.Uint8 {
|
|
|
|
return appendBytesValue
|
|
|
|
}
|
|
|
|
case reflect.Array:
|
|
|
|
if typ.Elem().Kind() == reflect.Uint8 {
|
|
|
|
return appendArrayBytesValue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return appenders[typ.Kind()]
|
|
|
|
}
|
|
|
|
|
2021-10-24 13:14:37 +02:00
|
|
|
func ifaceAppenderFunc(fmter Formatter, b []byte, v reflect.Value) []byte {
|
|
|
|
if v.IsNil() {
|
|
|
|
return dialect.AppendNull(b)
|
2021-08-25 15:34:33 +02:00
|
|
|
}
|
2021-10-24 13:14:37 +02:00
|
|
|
elem := v.Elem()
|
|
|
|
appender := Appender(fmter.Dialect(), elem.Type())
|
|
|
|
return appender(fmter, b, elem)
|
2021-08-25 15:34:33 +02:00
|
|
|
}
|
|
|
|
|
2022-04-24 12:26:22 +02:00
|
|
|
func nilAwareAppender(fn AppenderFunc) AppenderFunc {
|
|
|
|
return func(fmter Formatter, b []byte, v reflect.Value) []byte {
|
|
|
|
if v.IsNil() {
|
|
|
|
return dialect.AppendNull(b)
|
|
|
|
}
|
|
|
|
return fn(fmter, b, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-08 21:05:26 +02:00
|
|
|
func PtrAppender(fn AppenderFunc) AppenderFunc {
|
2021-08-25 15:34:33 +02:00
|
|
|
return func(fmter Formatter, b []byte, v reflect.Value) []byte {
|
|
|
|
if v.IsNil() {
|
|
|
|
return dialect.AppendNull(b)
|
|
|
|
}
|
2021-09-08 21:05:26 +02:00
|
|
|
return fn(fmter, b, v.Elem())
|
2021-08-25 15:34:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func AppendBoolValue(fmter Formatter, b []byte, v reflect.Value) []byte {
|
|
|
|
return dialect.AppendBool(b, v.Bool())
|
|
|
|
}
|
|
|
|
|
|
|
|
func AppendIntValue(fmter Formatter, b []byte, v reflect.Value) []byte {
|
|
|
|
return strconv.AppendInt(b, v.Int(), 10)
|
|
|
|
}
|
|
|
|
|
|
|
|
func AppendUintValue(fmter Formatter, b []byte, v reflect.Value) []byte {
|
|
|
|
return strconv.AppendUint(b, v.Uint(), 10)
|
|
|
|
}
|
|
|
|
|
2021-10-24 13:14:37 +02:00
|
|
|
func appendUint32Value(fmter Formatter, b []byte, v reflect.Value) []byte {
|
|
|
|
return fmter.Dialect().AppendUint32(b, uint32(v.Uint()))
|
|
|
|
}
|
|
|
|
|
|
|
|
func appendUint64Value(fmter Formatter, b []byte, v reflect.Value) []byte {
|
|
|
|
return fmter.Dialect().AppendUint64(b, v.Uint())
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
func AppendFloat32Value(fmter Formatter, b []byte, v reflect.Value) []byte {
|
|
|
|
return dialect.AppendFloat32(b, float32(v.Float()))
|
|
|
|
}
|
|
|
|
|
|
|
|
func AppendFloat64Value(fmter Formatter, b []byte, v reflect.Value) []byte {
|
|
|
|
return dialect.AppendFloat64(b, float64(v.Float()))
|
|
|
|
}
|
|
|
|
|
|
|
|
func appendBytesValue(fmter Formatter, b []byte, v reflect.Value) []byte {
|
2021-10-24 13:14:37 +02:00
|
|
|
return fmter.Dialect().AppendBytes(b, v.Bytes())
|
2021-08-25 15:34:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func appendArrayBytesValue(fmter Formatter, b []byte, v reflect.Value) []byte {
|
|
|
|
if v.CanAddr() {
|
2021-10-24 13:14:37 +02:00
|
|
|
return fmter.Dialect().AppendBytes(b, v.Slice(0, v.Len()).Bytes())
|
2021-08-25 15:34:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
tmp := make([]byte, v.Len())
|
|
|
|
reflect.Copy(reflect.ValueOf(tmp), v)
|
2021-10-24 13:14:37 +02:00
|
|
|
b = fmter.Dialect().AppendBytes(b, tmp)
|
2021-08-25 15:34:33 +02:00
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func AppendStringValue(fmter Formatter, b []byte, v reflect.Value) []byte {
|
2021-11-13 12:29:08 +01:00
|
|
|
return fmter.Dialect().AppendString(b, v.String())
|
2021-08-25 15:34:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func AppendJSONValue(fmter Formatter, b []byte, v reflect.Value) []byte {
|
|
|
|
bb, err := bunjson.Marshal(v.Interface())
|
|
|
|
if err != nil {
|
|
|
|
return dialect.AppendError(b, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(bb) > 0 && bb[len(bb)-1] == '\n' {
|
|
|
|
bb = bb[:len(bb)-1]
|
|
|
|
}
|
|
|
|
|
2021-10-24 13:14:37 +02:00
|
|
|
return fmter.Dialect().AppendJSON(b, bb)
|
2021-08-25 15:34:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func appendTimeValue(fmter Formatter, b []byte, v reflect.Value) []byte {
|
|
|
|
tm := v.Interface().(time.Time)
|
2021-10-24 13:14:37 +02:00
|
|
|
return fmter.Dialect().AppendTime(b, tm)
|
2021-08-25 15:34:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func appendIPValue(fmter Formatter, b []byte, v reflect.Value) []byte {
|
|
|
|
ip := v.Interface().(net.IP)
|
2021-11-13 12:29:08 +01:00
|
|
|
return fmter.Dialect().AppendString(b, ip.String())
|
2021-08-25 15:34:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func appendIPNetValue(fmter Formatter, b []byte, v reflect.Value) []byte {
|
|
|
|
ipnet := v.Interface().(net.IPNet)
|
2021-11-13 12:29:08 +01:00
|
|
|
return fmter.Dialect().AppendString(b, ipnet.String())
|
2021-08-25 15:34:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func appendJSONRawMessageValue(fmter Formatter, b []byte, v reflect.Value) []byte {
|
|
|
|
bytes := v.Bytes()
|
|
|
|
if bytes == nil {
|
|
|
|
return dialect.AppendNull(b)
|
|
|
|
}
|
2021-11-13 12:29:08 +01:00
|
|
|
return fmter.Dialect().AppendString(b, internal.String(bytes))
|
2021-08-25 15:34:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func appendQueryAppenderValue(fmter Formatter, b []byte, v reflect.Value) []byte {
|
|
|
|
return AppendQueryAppender(fmter, b, v.Interface().(QueryAppender))
|
|
|
|
}
|
|
|
|
|
2021-10-24 13:14:37 +02:00
|
|
|
func appendDriverValue(fmter Formatter, b []byte, v reflect.Value) []byte {
|
|
|
|
value, err := v.Interface().(driver.Valuer).Value()
|
2021-08-25 15:34:33 +02:00
|
|
|
if err != nil {
|
|
|
|
return dialect.AppendError(b, err)
|
|
|
|
}
|
2022-09-28 19:30:40 +02:00
|
|
|
if _, ok := value.(driver.Valuer); ok {
|
|
|
|
return dialect.AppendError(b, fmt.Errorf("driver.Valuer returns unsupported type %T", value))
|
|
|
|
}
|
2021-10-24 13:14:37 +02:00
|
|
|
return Append(fmter, b, value)
|
2021-08-25 15:34:33 +02:00
|
|
|
}
|
|
|
|
|
2021-10-24 13:14:37 +02:00
|
|
|
func addrAppender(fn AppenderFunc) AppenderFunc {
|
2021-08-25 15:34:33 +02:00
|
|
|
return func(fmter Formatter, b []byte, v reflect.Value) []byte {
|
|
|
|
if !v.CanAddr() {
|
|
|
|
err := fmt.Errorf("bun: Append(nonaddressable %T)", v.Interface())
|
|
|
|
return dialect.AppendError(b, err)
|
|
|
|
}
|
|
|
|
return fn(fmter, b, v.Addr())
|
|
|
|
}
|
|
|
|
}
|
2021-10-24 13:14:37 +02:00
|
|
|
|
|
|
|
func appendMsgpack(fmter Formatter, b []byte, v reflect.Value) []byte {
|
|
|
|
hexEnc := internal.NewHexEncoder(b)
|
|
|
|
|
|
|
|
enc := msgpack.GetEncoder()
|
|
|
|
defer msgpack.PutEncoder(enc)
|
|
|
|
|
|
|
|
enc.Reset(hexEnc)
|
|
|
|
if err := enc.EncodeValue(v); err != nil {
|
|
|
|
return dialect.AppendError(b, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := hexEnc.Close(); err != nil {
|
|
|
|
return dialect.AppendError(b, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return hexEnc.Bytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
func AppendQueryAppender(fmter Formatter, b []byte, app QueryAppender) []byte {
|
|
|
|
bb, err := app.AppendQuery(fmter, b)
|
|
|
|
if err != nil {
|
|
|
|
return dialect.AppendError(b, err)
|
|
|
|
}
|
|
|
|
return bb
|
|
|
|
}
|