You've already forked go-clickhouse
mirror of
https://github.com/uptrace/go-clickhouse.git
synced 2025-06-18 23:57:36 +02:00
.github
ch
bfloat16
chpool
chproto
chschema
append.go
append_value.go
block.go
column.go
column_gen.go
column_lowcard.go
column_nullable.go
column_safe_gen.go
column_unsafe_gen.go
enum.go
field.go
formatter.go
hooks.go
reflect.go
sqlfmt.go
table.go
tables.go
types.go
chtype
internal
testdata
bench_test.go
ch.go
config.go
db.go
db_test.go
hook.go
model.go
model_map.go
model_scan.go
model_slice_map.go
model_table_slice.go
model_table_struct.go
proto.go
query_base.go
query_insert.go
query_raw.go
query_select.go
query_table_create.go
query_table_drop.go
query_table_truncate.go
query_test.go
query_view_create.go
query_view_drop.go
reflect.go
chdebug
chmigrate
chotel
example
scripts
.prettierrc.yml
CHANGELOG.md
LICENSE
Makefile
README.md
go.mod
go.sum
package.json
version.go
52 lines
846 B
Go
52 lines
846 B
Go
![]() |
package chschema
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"reflect"
|
||
|
"sync"
|
||
|
)
|
||
|
|
||
|
var globalTables = newTablesMap()
|
||
|
|
||
|
func TableForType(typ reflect.Type) *Table {
|
||
|
return globalTables.Get(typ)
|
||
|
}
|
||
|
|
||
|
type tablesMap struct {
|
||
|
m sync.Map
|
||
|
}
|
||
|
|
||
|
func newTablesMap() *tablesMap {
|
||
|
return new(tablesMap)
|
||
|
}
|
||
|
|
||
|
func (t *tablesMap) Get(typ reflect.Type) *Table {
|
||
|
if typ.Kind() != reflect.Struct {
|
||
|
panic(fmt.Errorf("got %s, wanted %s", typ.Kind(), reflect.Struct))
|
||
|
}
|
||
|
|
||
|
if v, ok := t.m.Load(typ); ok {
|
||
|
return v.(*Table)
|
||
|
}
|
||
|
|
||
|
table := newTable(typ)
|
||
|
if v, loaded := t.m.LoadOrStore(typ, table); loaded {
|
||
|
return v.(*Table)
|
||
|
}
|
||
|
|
||
|
return table
|
||
|
}
|
||
|
|
||
|
func (t *tablesMap) getByName(name string) *Table {
|
||
|
var found *Table
|
||
|
t.m.Range(func(key, value any) bool {
|
||
|
t := value.(*Table)
|
||
|
if t.Name == name || t.ModelName == name {
|
||
|
found = t
|
||
|
return false
|
||
|
}
|
||
|
return true
|
||
|
})
|
||
|
return found
|
||
|
}
|