summaryrefslogtreecommitdiff
path: root/vendor/github.com/uptrace/bun/schema
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/uptrace/bun/schema')
-rw-r--r--vendor/github.com/uptrace/bun/schema/append_value.go29
-rw-r--r--vendor/github.com/uptrace/bun/schema/dialect.go2
-rw-r--r--vendor/github.com/uptrace/bun/schema/reflect.go3
-rw-r--r--vendor/github.com/uptrace/bun/schema/relation.go26
-rw-r--r--vendor/github.com/uptrace/bun/schema/scan.go14
-rw-r--r--vendor/github.com/uptrace/bun/schema/table.go86
-rw-r--r--vendor/github.com/uptrace/bun/schema/tables.go104
-rw-r--r--vendor/github.com/uptrace/bun/schema/zerochecker.go2
8 files changed, 111 insertions, 155 deletions
diff --git a/vendor/github.com/uptrace/bun/schema/append_value.go b/vendor/github.com/uptrace/bun/schema/append_value.go
index 9f0782e0f..a67b41e38 100644
--- a/vendor/github.com/uptrace/bun/schema/append_value.go
+++ b/vendor/github.com/uptrace/bun/schema/append_value.go
@@ -7,9 +7,9 @@ import (
"reflect"
"strconv"
"strings"
- "sync"
"time"
+ "github.com/puzpuzpuz/xsync/v3"
"github.com/uptrace/bun/dialect"
"github.com/uptrace/bun/dialect/sqltype"
"github.com/uptrace/bun/extra/bunjson"
@@ -51,7 +51,7 @@ var appenders = []AppenderFunc{
reflect.UnsafePointer: nil,
}
-var appenderMap sync.Map
+var appenderCache = xsync.NewMapOf[reflect.Type, AppenderFunc]()
func FieldAppender(dialect Dialect, field *Field) AppenderFunc {
if field.Tag.HasOption("msgpack") {
@@ -67,7 +67,7 @@ func FieldAppender(dialect Dialect, field *Field) AppenderFunc {
}
if fieldType.Kind() != reflect.Ptr {
- if reflect.PtrTo(fieldType).Implements(driverValuerType) {
+ if reflect.PointerTo(fieldType).Implements(driverValuerType) {
return addrAppender(appendDriverValue)
}
}
@@ -79,14 +79,14 @@ func FieldAppender(dialect Dialect, field *Field) AppenderFunc {
}
func Appender(dialect Dialect, typ reflect.Type) AppenderFunc {
- if v, ok := appenderMap.Load(typ); ok {
- return v.(AppenderFunc)
+ if v, ok := appenderCache.Load(typ); ok {
+ return v
}
fn := appender(dialect, typ)
- if v, ok := appenderMap.LoadOrStore(typ, fn); ok {
- return v.(AppenderFunc)
+ if v, ok := appenderCache.LoadOrStore(typ, fn); ok {
+ return v
}
return fn
}
@@ -99,10 +99,10 @@ func appender(dialect Dialect, typ reflect.Type) AppenderFunc {
return appendTimeValue
case timePtrType:
return PtrAppender(appendTimeValue)
- case ipType:
- return appendIPValue
case ipNetType:
return appendIPNetValue
+ case ipType, netipPrefixType, netipAddrType:
+ return appendStringer
case jsonRawMessageType:
return appendJSONRawMessageValue
}
@@ -123,7 +123,7 @@ func appender(dialect Dialect, typ reflect.Type) AppenderFunc {
}
if kind != reflect.Ptr {
- ptr := reflect.PtrTo(typ)
+ ptr := reflect.PointerTo(typ)
if ptr.Implements(queryAppenderType) {
return addrAppender(appendQueryAppenderValue)
}
@@ -247,16 +247,15 @@ func appendTimeValue(fmter Formatter, b []byte, v reflect.Value) []byte {
return fmter.Dialect().AppendTime(b, tm)
}
-func appendIPValue(fmter Formatter, b []byte, v reflect.Value) []byte {
- ip := v.Interface().(net.IP)
- return fmter.Dialect().AppendString(b, ip.String())
-}
-
func appendIPNetValue(fmter Formatter, b []byte, v reflect.Value) []byte {
ipnet := v.Interface().(net.IPNet)
return fmter.Dialect().AppendString(b, ipnet.String())
}
+func appendStringer(fmter Formatter, b []byte, v reflect.Value) []byte {
+ return fmter.Dialect().AppendString(b, v.Interface().(fmt.Stringer).String())
+}
+
func appendJSONRawMessageValue(fmter Formatter, b []byte, v reflect.Value) []byte {
bytes := v.Bytes()
if bytes == nil {
diff --git a/vendor/github.com/uptrace/bun/schema/dialect.go b/vendor/github.com/uptrace/bun/schema/dialect.go
index 8814313f7..330293444 100644
--- a/vendor/github.com/uptrace/bun/schema/dialect.go
+++ b/vendor/github.com/uptrace/bun/schema/dialect.go
@@ -118,7 +118,7 @@ func (BaseDialect) AppendJSON(b, jsonb []byte) []byte {
case '\000':
continue
case '\\':
- if p.SkipBytes([]byte("u0000")) {
+ if p.CutPrefix([]byte("u0000")) {
b = append(b, `\\u0000`...)
} else {
b = append(b, '\\')
diff --git a/vendor/github.com/uptrace/bun/schema/reflect.go b/vendor/github.com/uptrace/bun/schema/reflect.go
index 89be8eeb6..75980b102 100644
--- a/vendor/github.com/uptrace/bun/schema/reflect.go
+++ b/vendor/github.com/uptrace/bun/schema/reflect.go
@@ -4,6 +4,7 @@ import (
"database/sql/driver"
"encoding/json"
"net"
+ "net/netip"
"reflect"
"time"
)
@@ -14,6 +15,8 @@ var (
timeType = timePtrType.Elem()
ipType = reflect.TypeOf((*net.IP)(nil)).Elem()
ipNetType = reflect.TypeOf((*net.IPNet)(nil)).Elem()
+ netipPrefixType = reflect.TypeOf((*netip.Prefix)(nil)).Elem()
+ netipAddrType = reflect.TypeOf((*netip.Addr)(nil)).Elem()
jsonRawMessageType = reflect.TypeOf((*json.RawMessage)(nil)).Elem()
driverValuerType = reflect.TypeOf((*driver.Valuer)(nil)).Elem()
diff --git a/vendor/github.com/uptrace/bun/schema/relation.go b/vendor/github.com/uptrace/bun/schema/relation.go
index 9eb74f7e9..f653cd7a3 100644
--- a/vendor/github.com/uptrace/bun/schema/relation.go
+++ b/vendor/github.com/uptrace/bun/schema/relation.go
@@ -13,21 +13,25 @@ const (
)
type Relation struct {
- Type int
- Field *Field
- JoinTable *Table
- BaseFields []*Field
- JoinFields []*Field
- OnUpdate string
- OnDelete string
- Condition []string
+ // Base and Join can be explained with this query:
+ //
+ // SELECT * FROM base_table JOIN join_table
+
+ Type int
+ Field *Field
+ JoinTable *Table
+ BasePKs []*Field
+ JoinPKs []*Field
+ OnUpdate string
+ OnDelete string
+ Condition []string
PolymorphicField *Field
PolymorphicValue string
- M2MTable *Table
- M2MBaseFields []*Field
- M2MJoinFields []*Field
+ M2MTable *Table
+ M2MBasePKs []*Field
+ M2MJoinPKs []*Field
}
// References returns true if the table to which the Relation belongs needs to declare a foreign key constraint to create the relation.
diff --git a/vendor/github.com/uptrace/bun/schema/scan.go b/vendor/github.com/uptrace/bun/schema/scan.go
index 96b31caf3..4da160daf 100644
--- a/vendor/github.com/uptrace/bun/schema/scan.go
+++ b/vendor/github.com/uptrace/bun/schema/scan.go
@@ -8,9 +8,9 @@ import (
"reflect"
"strconv"
"strings"
- "sync"
"time"
+ "github.com/puzpuzpuz/xsync/v3"
"github.com/vmihailenco/msgpack/v5"
"github.com/uptrace/bun/dialect/sqltype"
@@ -53,7 +53,7 @@ func init() {
}
}
-var scannerMap sync.Map
+var scannerCache = xsync.NewMapOf[reflect.Type, ScannerFunc]()
func FieldScanner(dialect Dialect, field *Field) ScannerFunc {
if field.Tag.HasOption("msgpack") {
@@ -72,14 +72,14 @@ func FieldScanner(dialect Dialect, field *Field) ScannerFunc {
}
func Scanner(typ reflect.Type) ScannerFunc {
- if v, ok := scannerMap.Load(typ); ok {
- return v.(ScannerFunc)
+ if v, ok := scannerCache.Load(typ); ok {
+ return v
}
fn := scanner(typ)
- if v, ok := scannerMap.LoadOrStore(typ, fn); ok {
- return v.(ScannerFunc)
+ if v, ok := scannerCache.LoadOrStore(typ, fn); ok {
+ return v
}
return fn
}
@@ -111,7 +111,7 @@ func scanner(typ reflect.Type) ScannerFunc {
}
if kind != reflect.Ptr {
- ptr := reflect.PtrTo(typ)
+ ptr := reflect.PointerTo(typ)
if ptr.Implements(scannerType) {
return addrScanner(scanScanner)
}
diff --git a/vendor/github.com/uptrace/bun/schema/table.go b/vendor/github.com/uptrace/bun/schema/table.go
index 0a23156a2..c8e71e38f 100644
--- a/vendor/github.com/uptrace/bun/schema/table.go
+++ b/vendor/github.com/uptrace/bun/schema/table.go
@@ -74,16 +74,7 @@ type structField struct {
Table *Table
}
-func newTable(
- dialect Dialect, typ reflect.Type, seen map[reflect.Type]*Table, canAddr bool,
-) *Table {
- if table, ok := seen[typ]; ok {
- return table
- }
-
- table := new(Table)
- seen[typ] = table
-
+func (table *Table) init(dialect Dialect, typ reflect.Type, canAddr bool) {
table.dialect = dialect
table.Type = typ
table.ZeroValue = reflect.New(table.Type).Elem()
@@ -97,7 +88,7 @@ func newTable(
table.Fields = make([]*Field, 0, typ.NumField())
table.FieldMap = make(map[string]*Field, typ.NumField())
- table.processFields(typ, seen, canAddr)
+ table.processFields(typ, canAddr)
hooks := []struct {
typ reflect.Type
@@ -109,28 +100,15 @@ func newTable(
{afterScanRowHookType, afterScanRowHookFlag},
}
- typ = reflect.PtrTo(table.Type)
+ typ = reflect.PointerTo(table.Type)
for _, hook := range hooks {
if typ.Implements(hook.typ) {
table.flags = table.flags.Set(hook.flag)
}
}
-
- return table
-}
-
-func (t *Table) init() {
- for _, field := range t.relFields {
- t.processRelation(field)
- }
- t.relFields = nil
}
-func (t *Table) processFields(
- typ reflect.Type,
- seen map[reflect.Type]*Table,
- canAddr bool,
-) {
+func (t *Table) processFields(typ reflect.Type, canAddr bool) {
type embeddedField struct {
prefix string
index []int
@@ -172,7 +150,7 @@ func (t *Table) processFields(
continue
}
- subtable := newTable(t.dialect, sfType, seen, canAddr)
+ subtable := t.dialect.Tables().InProgress(sfType)
for _, subfield := range subtable.allFields {
embedded = append(embedded, embeddedField{
@@ -206,7 +184,7 @@ func (t *Table) processFields(
t.TypeName, sf.Name, fieldType.Kind()))
}
- subtable := newTable(t.dialect, fieldType, seen, canAddr)
+ subtable := t.dialect.Tables().InProgress(fieldType)
for _, subfield := range subtable.allFields {
embedded = append(embedded, embeddedField{
prefix: prefix,
@@ -229,7 +207,7 @@ func (t *Table) processFields(
}
t.StructMap[field.Name] = &structField{
Index: field.Index,
- Table: newTable(t.dialect, field.IndirectType, seen, canAddr),
+ Table: t.dialect.Tables().InProgress(field.IndirectType),
}
}
}
@@ -423,6 +401,10 @@ func (t *Table) newField(sf reflect.StructField, tag tagparser.Tag) *Field {
sqlName = tag.Name
}
+ if s, ok := tag.Option("column"); ok {
+ sqlName = s
+ }
+
for name := range tag.Options {
if !isKnownFieldOption(name) {
internal.Warn.Printf("%s.%s has unknown tag option: %q", t.TypeName, sf.Name, name)
@@ -490,6 +472,13 @@ func (t *Table) newField(sf reflect.StructField, tag tagparser.Tag) *Field {
//---------------------------------------------------------------------------------------
+func (t *Table) initRelations() {
+ for _, field := range t.relFields {
+ t.processRelation(field)
+ }
+ t.relFields = nil
+}
+
func (t *Table) processRelation(field *Field) {
if rel, ok := field.Tag.Option("rel"); ok {
t.initRelation(field, rel)
@@ -577,7 +566,7 @@ func (t *Table) belongsToRelation(field *Field) *Relation {
joinColumn := joinColumns[i]
if f := t.FieldMap[baseColumn]; f != nil {
- rel.BaseFields = append(rel.BaseFields, f)
+ rel.BasePKs = append(rel.BasePKs, f)
} else {
panic(fmt.Errorf(
"bun: %s belongs-to %s: %s must have column %s",
@@ -586,7 +575,7 @@ func (t *Table) belongsToRelation(field *Field) *Relation {
}
if f := joinTable.FieldMap[joinColumn]; f != nil {
- rel.JoinFields = append(rel.JoinFields, f)
+ rel.JoinPKs = append(rel.JoinPKs, f)
} else {
panic(fmt.Errorf(
"bun: %s belongs-to %s: %s must have column %s",
@@ -597,17 +586,17 @@ func (t *Table) belongsToRelation(field *Field) *Relation {
return rel
}
- rel.JoinFields = joinTable.PKs
+ rel.JoinPKs = joinTable.PKs
fkPrefix := internal.Underscore(field.GoName) + "_"
for _, joinPK := range joinTable.PKs {
fkName := fkPrefix + joinPK.Name
if fk := t.FieldMap[fkName]; fk != nil {
- rel.BaseFields = append(rel.BaseFields, fk)
+ rel.BasePKs = append(rel.BasePKs, fk)
continue
}
if fk := t.FieldMap[joinPK.Name]; fk != nil {
- rel.BaseFields = append(rel.BaseFields, fk)
+ rel.BasePKs = append(rel.BasePKs, fk)
continue
}
@@ -640,7 +629,7 @@ func (t *Table) hasOneRelation(field *Field) *Relation {
baseColumns, joinColumns := parseRelationJoin(join)
for i, baseColumn := range baseColumns {
if f := t.FieldMap[baseColumn]; f != nil {
- rel.BaseFields = append(rel.BaseFields, f)
+ rel.BasePKs = append(rel.BasePKs, f)
} else {
panic(fmt.Errorf(
"bun: %s has-one %s: %s must have column %s",
@@ -650,7 +639,7 @@ func (t *Table) hasOneRelation(field *Field) *Relation {
joinColumn := joinColumns[i]
if f := joinTable.FieldMap[joinColumn]; f != nil {
- rel.JoinFields = append(rel.JoinFields, f)
+ rel.JoinPKs = append(rel.JoinPKs, f)
} else {
panic(fmt.Errorf(
"bun: %s has-one %s: %s must have column %s",
@@ -661,17 +650,17 @@ func (t *Table) hasOneRelation(field *Field) *Relation {
return rel
}
- rel.BaseFields = t.PKs
+ rel.BasePKs = t.PKs
fkPrefix := internal.Underscore(t.ModelName) + "_"
for _, pk := range t.PKs {
fkName := fkPrefix + pk.Name
if f := joinTable.FieldMap[fkName]; f != nil {
- rel.JoinFields = append(rel.JoinFields, f)
+ rel.JoinPKs = append(rel.JoinPKs, f)
continue
}
if f := joinTable.FieldMap[pk.Name]; f != nil {
- rel.JoinFields = append(rel.JoinFields, f)
+ rel.JoinPKs = append(rel.JoinPKs, f)
continue
}
@@ -720,7 +709,7 @@ func (t *Table) hasManyRelation(field *Field) *Relation {
}
if f := t.FieldMap[baseColumn]; f != nil {
- rel.BaseFields = append(rel.BaseFields, f)
+ rel.BasePKs = append(rel.BasePKs, f)
} else {
panic(fmt.Errorf(
"bun: %s has-many %s: %s must have column %s",
@@ -729,7 +718,7 @@ func (t *Table) hasManyRelation(field *Field) *Relation {
}
if f := joinTable.FieldMap[joinColumn]; f != nil {
- rel.JoinFields = append(rel.JoinFields, f)
+ rel.JoinPKs = append(rel.JoinPKs, f)
} else {
panic(fmt.Errorf(
"bun: %s has-many %s: %s must have column %s",
@@ -738,7 +727,7 @@ func (t *Table) hasManyRelation(field *Field) *Relation {
}
}
} else {
- rel.BaseFields = t.PKs
+ rel.BasePKs = t.PKs
fkPrefix := internal.Underscore(t.ModelName) + "_"
if isPolymorphic {
polymorphicColumn = fkPrefix + "type"
@@ -747,12 +736,12 @@ func (t *Table) hasManyRelation(field *Field) *Relation {
for _, pk := range t.PKs {
joinColumn := fkPrefix + pk.Name
if fk := joinTable.FieldMap[joinColumn]; fk != nil {
- rel.JoinFields = append(rel.JoinFields, fk)
+ rel.JoinPKs = append(rel.JoinPKs, fk)
continue
}
if fk := joinTable.FieldMap[pk.Name]; fk != nil {
- rel.JoinFields = append(rel.JoinFields, fk)
+ rel.JoinPKs = append(rel.JoinPKs, fk)
continue
}
@@ -852,12 +841,12 @@ func (t *Table) m2mRelation(field *Field) *Relation {
}
leftRel := m2mTable.belongsToRelation(leftField)
- rel.BaseFields = leftRel.JoinFields
- rel.M2MBaseFields = leftRel.BaseFields
+ rel.BasePKs = leftRel.JoinPKs
+ rel.M2MBasePKs = leftRel.BasePKs
rightRel := m2mTable.belongsToRelation(rightField)
- rel.JoinFields = rightRel.JoinFields
- rel.M2MJoinFields = rightRel.BaseFields
+ rel.JoinPKs = rightRel.JoinPKs
+ rel.M2MJoinPKs = rightRel.BasePKs
return rel
}
@@ -918,6 +907,7 @@ func isKnownFieldOption(name string) bool {
"array",
"hstore",
"composite",
+ "multirange",
"json_use_number",
"msgpack",
"notnull",
diff --git a/vendor/github.com/uptrace/bun/schema/tables.go b/vendor/github.com/uptrace/bun/schema/tables.go
index 19aff8606..985093421 100644
--- a/vendor/github.com/uptrace/bun/schema/tables.go
+++ b/vendor/github.com/uptrace/bun/schema/tables.go
@@ -4,22 +4,24 @@ import (
"fmt"
"reflect"
"sync"
+
+ "github.com/puzpuzpuz/xsync/v3"
)
type Tables struct {
dialect Dialect
- tables sync.Map
- mu sync.RWMutex
- seen map[reflect.Type]*Table
- inProgress map[reflect.Type]*tableInProgress
+ mu sync.Mutex
+ tables *xsync.MapOf[reflect.Type, *Table]
+
+ inProgress map[reflect.Type]*Table
}
func NewTables(dialect Dialect) *Tables {
return &Tables{
dialect: dialect,
- seen: make(map[reflect.Type]*Table),
- inProgress: make(map[reflect.Type]*tableInProgress),
+ tables: xsync.NewMapOf[reflect.Type, *Table](),
+ inProgress: make(map[reflect.Type]*Table),
}
}
@@ -30,58 +32,26 @@ func (t *Tables) Register(models ...interface{}) {
}
func (t *Tables) Get(typ reflect.Type) *Table {
- return t.table(typ, false)
-}
-
-func (t *Tables) InProgress(typ reflect.Type) *Table {
- return t.table(typ, true)
-}
-
-func (t *Tables) table(typ reflect.Type, allowInProgress bool) *Table {
typ = indirectType(typ)
if typ.Kind() != reflect.Struct {
panic(fmt.Errorf("got %s, wanted %s", typ.Kind(), reflect.Struct))
}
if v, ok := t.tables.Load(typ); ok {
- return v.(*Table)
+ return v
}
t.mu.Lock()
+ defer t.mu.Unlock()
if v, ok := t.tables.Load(typ); ok {
- t.mu.Unlock()
- return v.(*Table)
- }
-
- var table *Table
-
- inProgress := t.inProgress[typ]
- if inProgress == nil {
- table = newTable(t.dialect, typ, t.seen, false)
- inProgress = newTableInProgress(table)
- t.inProgress[typ] = inProgress
- } else {
- table = inProgress.table
+ return v
}
- t.mu.Unlock()
-
- if allowInProgress {
- return table
- }
-
- if !inProgress.init() {
- return table
- }
-
- t.mu.Lock()
- delete(t.inProgress, typ)
- t.tables.Store(typ, table)
- t.mu.Unlock()
+ table := t.InProgress(typ)
+ table.initRelations()
t.dialect.OnTable(table)
-
for _, field := range table.FieldMap {
if field.UserSQLType == "" {
field.UserSQLType = field.DiscoveredSQLType
@@ -91,15 +61,27 @@ func (t *Tables) table(typ reflect.Type, allowInProgress bool) *Table {
}
}
+ t.tables.Store(typ, table)
+ return table
+}
+
+func (t *Tables) InProgress(typ reflect.Type) *Table {
+ if table, ok := t.inProgress[typ]; ok {
+ return table
+ }
+
+ table := new(Table)
+ t.inProgress[typ] = table
+ table.init(t.dialect, typ, false)
+
return table
}
func (t *Tables) ByModel(name string) *Table {
var found *Table
- t.tables.Range(func(key, value interface{}) bool {
- t := value.(*Table)
- if t.TypeName == name {
- found = t
+ t.tables.Range(func(typ reflect.Type, table *Table) bool {
+ if table.TypeName == name {
+ found = table
return false
}
return true
@@ -109,34 +91,12 @@ func (t *Tables) ByModel(name string) *Table {
func (t *Tables) ByName(name string) *Table {
var found *Table
- t.tables.Range(func(key, value interface{}) bool {
- t := value.(*Table)
- if t.Name == name {
- found = t
+ t.tables.Range(func(typ reflect.Type, table *Table) bool {
+ if table.Name == name {
+ found = table
return false
}
return true
})
return found
}
-
-type tableInProgress struct {
- table *Table
-
- initOnce sync.Once
-}
-
-func newTableInProgress(table *Table) *tableInProgress {
- return &tableInProgress{
- table: table,
- }
-}
-
-func (inp *tableInProgress) init() bool {
- var inited bool
- inp.initOnce.Do(func() {
- inp.table.init()
- inited = true
- })
- return inited
-}
diff --git a/vendor/github.com/uptrace/bun/schema/zerochecker.go b/vendor/github.com/uptrace/bun/schema/zerochecker.go
index f24e51d30..7c1f088c1 100644
--- a/vendor/github.com/uptrace/bun/schema/zerochecker.go
+++ b/vendor/github.com/uptrace/bun/schema/zerochecker.go
@@ -60,7 +60,7 @@ func zeroChecker(typ reflect.Type) IsZeroerFunc {
kind := typ.Kind()
if kind != reflect.Ptr {
- ptr := reflect.PtrTo(typ)
+ ptr := reflect.PointerTo(typ)
if ptr.Implements(isZeroerType) {
return addrChecker(isZeroInterface)
}