2021-08-25 13:34:33 +00:00
|
|
|
package schema
|
2021-08-12 19:03:24 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
type Tables struct {
|
|
|
|
dialect Dialect
|
|
|
|
tables sync.Map
|
2021-08-12 19:03:24 +00:00
|
|
|
|
|
|
|
mu sync.RWMutex
|
2024-04-15 10:01:20 +00:00
|
|
|
seen map[reflect.Type]*Table
|
2021-08-12 19:03:24 +00:00
|
|
|
inProgress map[reflect.Type]*tableInProgress
|
|
|
|
}
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
func NewTables(dialect Dialect) *Tables {
|
|
|
|
return &Tables{
|
|
|
|
dialect: dialect,
|
2024-04-15 10:01:20 +00:00
|
|
|
seen: make(map[reflect.Type]*Table),
|
2021-08-12 19:03:24 +00:00
|
|
|
inProgress: make(map[reflect.Type]*tableInProgress),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
func (t *Tables) Register(models ...interface{}) {
|
|
|
|
for _, model := range models {
|
|
|
|
_ = t.Get(reflect.TypeOf(model).Elem())
|
2021-08-12 19:03:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
func (t *Tables) Get(typ reflect.Type) *Table {
|
|
|
|
return t.table(typ, false)
|
|
|
|
}
|
|
|
|
|
2024-04-15 10:01:20 +00:00
|
|
|
func (t *Tables) InProgress(typ reflect.Type) *Table {
|
2021-08-25 13:34:33 +00:00
|
|
|
return t.table(typ, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Tables) table(typ reflect.Type, allowInProgress bool) *Table {
|
2021-09-08 19:05:26 +00:00
|
|
|
typ = indirectType(typ)
|
2021-08-12 19:03:24 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.mu.Lock()
|
|
|
|
|
|
|
|
if v, ok := t.tables.Load(typ); ok {
|
|
|
|
t.mu.Unlock()
|
|
|
|
return v.(*Table)
|
|
|
|
}
|
|
|
|
|
|
|
|
var table *Table
|
|
|
|
|
|
|
|
inProgress := t.inProgress[typ]
|
|
|
|
if inProgress == nil {
|
2024-04-15 10:01:20 +00:00
|
|
|
table = newTable(t.dialect, typ, t.seen, false)
|
2021-08-12 19:03:24 +00:00
|
|
|
inProgress = newTableInProgress(table)
|
|
|
|
t.inProgress[typ] = inProgress
|
|
|
|
} else {
|
|
|
|
table = inProgress.table
|
|
|
|
}
|
|
|
|
|
|
|
|
t.mu.Unlock()
|
|
|
|
|
|
|
|
if allowInProgress {
|
|
|
|
return table
|
|
|
|
}
|
|
|
|
|
2024-04-15 10:01:20 +00:00
|
|
|
if !inProgress.init() {
|
2021-11-13 11:29:08 +00:00
|
|
|
return table
|
2021-08-12 19:03:24 +00:00
|
|
|
}
|
|
|
|
|
2021-11-13 11:29:08 +00:00
|
|
|
t.mu.Lock()
|
|
|
|
delete(t.inProgress, typ)
|
|
|
|
t.tables.Store(typ, table)
|
|
|
|
t.mu.Unlock()
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
t.dialect.OnTable(table)
|
|
|
|
|
|
|
|
for _, field := range table.FieldMap {
|
|
|
|
if field.UserSQLType == "" {
|
|
|
|
field.UserSQLType = field.DiscoveredSQLType
|
|
|
|
}
|
|
|
|
if field.CreateTableSQLType == "" {
|
|
|
|
field.CreateTableSQLType = field.UserSQLType
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-12 19:03:24 +00:00
|
|
|
return table
|
|
|
|
}
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
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
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
return found
|
2021-08-12 19:03:24 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
func (t *Tables) ByName(name string) *Table {
|
2021-08-12 19:03:24 +00:00
|
|
|
var found *Table
|
|
|
|
t.tables.Range(func(key, value interface{}) bool {
|
|
|
|
t := value.(*Table)
|
2021-08-25 13:34:33 +00:00
|
|
|
if t.Name == name {
|
2021-08-12 19:03:24 +00:00
|
|
|
found = t
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
return found
|
|
|
|
}
|
2024-04-15 10:01:20 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|