2021-08-25 16:34:33 +03:00
|
|
|
package schema
|
2021-08-12 22:03:24 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
type tableInProgress struct {
|
|
|
|
table *Table
|
|
|
|
|
|
|
|
init1Once sync.Once
|
|
|
|
init2Once sync.Once
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTableInProgress(table *Table) *tableInProgress {
|
|
|
|
return &tableInProgress{
|
|
|
|
table: table,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (inp *tableInProgress) init1() bool {
|
|
|
|
var inited bool
|
|
|
|
inp.init1Once.Do(func() {
|
|
|
|
inp.table.init1()
|
|
|
|
inited = true
|
|
|
|
})
|
|
|
|
return inited
|
|
|
|
}
|
|
|
|
|
|
|
|
func (inp *tableInProgress) init2() bool {
|
|
|
|
var inited bool
|
|
|
|
inp.init2Once.Do(func() {
|
|
|
|
inp.table.init2()
|
|
|
|
inited = true
|
|
|
|
})
|
|
|
|
return inited
|
|
|
|
}
|
|
|
|
|
2021-08-25 16:34:33 +03:00
|
|
|
type Tables struct {
|
|
|
|
dialect Dialect
|
|
|
|
tables sync.Map
|
2021-08-12 22:03:24 +03:00
|
|
|
|
|
|
|
mu sync.RWMutex
|
|
|
|
inProgress map[reflect.Type]*tableInProgress
|
|
|
|
}
|
|
|
|
|
2021-08-25 16:34:33 +03:00
|
|
|
func NewTables(dialect Dialect) *Tables {
|
|
|
|
return &Tables{
|
|
|
|
dialect: dialect,
|
2021-08-12 22:03:24 +03:00
|
|
|
inProgress: make(map[reflect.Type]*tableInProgress),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-25 16:34:33 +03:00
|
|
|
func (t *Tables) Register(models ...interface{}) {
|
|
|
|
for _, model := range models {
|
|
|
|
_ = t.Get(reflect.TypeOf(model).Elem())
|
2021-08-12 22:03:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-25 16:34:33 +03:00
|
|
|
func (t *Tables) Get(typ reflect.Type) *Table {
|
|
|
|
return t.table(typ, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Tables) Ref(typ reflect.Type) *Table {
|
|
|
|
return t.table(typ, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Tables) table(typ reflect.Type, allowInProgress bool) *Table {
|
2021-09-08 22:05:26 +03:00
|
|
|
typ = indirectType(typ)
|
2021-08-12 22:03:24 +03: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 {
|
2021-08-25 16:34:33 +03:00
|
|
|
table = newTable(t.dialect, typ)
|
2021-08-12 22:03:24 +03:00
|
|
|
inProgress = newTableInProgress(table)
|
|
|
|
t.inProgress[typ] = inProgress
|
|
|
|
} else {
|
|
|
|
table = inProgress.table
|
|
|
|
}
|
|
|
|
|
|
|
|
t.mu.Unlock()
|
|
|
|
|
|
|
|
inProgress.init1()
|
|
|
|
if allowInProgress {
|
|
|
|
return table
|
|
|
|
}
|
|
|
|
|
2021-11-13 14:29:08 +03:00
|
|
|
if !inProgress.init2() {
|
|
|
|
return table
|
2021-08-12 22:03:24 +03:00
|
|
|
}
|
|
|
|
|
2021-11-13 14:29:08 +03:00
|
|
|
t.mu.Lock()
|
|
|
|
delete(t.inProgress, typ)
|
|
|
|
t.tables.Store(typ, table)
|
|
|
|
t.mu.Unlock()
|
|
|
|
|
2021-08-25 16:34:33 +03: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 22:03:24 +03:00
|
|
|
return table
|
|
|
|
}
|
|
|
|
|
2021-08-25 16:34:33 +03: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 22:03:24 +03:00
|
|
|
}
|
|
|
|
|
2021-08-25 16:34:33 +03:00
|
|
|
func (t *Tables) ByName(name string) *Table {
|
2021-08-12 22:03:24 +03:00
|
|
|
var found *Table
|
|
|
|
t.tables.Range(func(key, value interface{}) bool {
|
|
|
|
t := value.(*Table)
|
2021-08-25 16:34:33 +03:00
|
|
|
if t.Name == name {
|
2021-08-12 22:03:24 +03:00
|
|
|
found = t
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
return found
|
|
|
|
}
|