-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtable_descriptor.go
executable file
·152 lines (136 loc) · 4.02 KB
/
table_descriptor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package dsc
import (
"fmt"
"reflect"
"sync"
"github.com/viant/toolbox"
"strings"
)
type commonTableDescriptorRegistry struct {
sync.RWMutex
manager Manager
registry map[string]*TableDescriptor
}
func (r *commonTableDescriptorRegistry) Has(table string) bool {
r.RLock()
defer r.RUnlock()
_, found := r.registry[table]
return found
}
func (r *commonTableDescriptorRegistry) getDescriptor(table string) *TableDescriptor {
dbConfig := r.manager.Config()
dialect := GetDatastoreDialect(dbConfig.DriverName)
datastore, _ := dialect.GetCurrentDatastore(r.manager)
key := dialect.GetKeyName(r.manager, datastore, table)
isAutoincrement := dialect.IsAutoincrement(r.manager, datastore, table)
descriptor := &TableDescriptor{
Table: table,
Autoincrement: isAutoincrement,
PkColumns: []string{},
Columns: []string{},
}
if key != "" {
descriptor.PkColumns = strings.Split(key, ",")
}
columns, _ := dialect.GetColumns(r.manager, datastore, table)
for _, column := range columns {
descriptor.Columns = append(descriptor.Columns, column.Name())
}
return descriptor
}
func (r *commonTableDescriptorRegistry) Get(table string) *TableDescriptor {
r.RLock()
if descriptor, found := r.registry[table]; found {
r.RUnlock()
return descriptor
}
r.RUnlock()
var result = r.getDescriptor(table)
_ = r.Register(result)
return result
}
func (r *commonTableDescriptorRegistry) Register(descriptor *TableDescriptor) error {
if descriptor.Table == "" {
return fmt.Errorf("table name was not set %v", descriptor)
}
for i, column := range descriptor.Columns {
if column == "" {
return fmt.Errorf("columns[%d] was empty %v %v", i, descriptor.Table, descriptor.Columns)
}
}
for i, column := range descriptor.PkColumns {
if column == "" {
return fmt.Errorf("pkColumns[%d] was empty %v %v", i, descriptor.Table, descriptor.Columns)
}
}
r.Lock()
defer r.Unlock()
r.registry[descriptor.Table] = descriptor
return nil
}
func (r *commonTableDescriptorRegistry) Tables() []string {
r.RLock()
defer r.RUnlock()
var result = make([]string, 0)
for key := range r.registry {
result = append(result, key)
}
return result
}
func newTableDescriptorRegistry() *commonTableDescriptorRegistry {
return &commonTableDescriptorRegistry{registry: make(map[string]*TableDescriptor)}
}
//newTableDescriptorRegistry returns a new newTableDescriptorRegistry
func NewTableDescriptorRegistry() TableDescriptorRegistry {
return newTableDescriptorRegistry()
}
//HasSchema check if table desciptor has defined schema.
func (d *TableDescriptor) HasSchema() bool {
return len(d.SchemaURL) > 0 || d.Schema != nil
}
//NewTableDescriptor creates a new table descriptor for passed in instance, it can use the following tags:"column", "dateLayout","dateFormat", "autoincrement", "primaryKey", "sequence", "transient"
func NewTableDescriptor(table string, instance interface{}) (*TableDescriptor, error) {
targetType := toolbox.DiscoverTypeByKind(instance, reflect.Struct)
var autoincrement bool
var pkColumns = make([]string, 0)
var columns = make([]string, 0)
columnToFieldMap := toolbox.NewFieldSettingByKey(targetType, "column")
for key := range columnToFieldMap {
mapping, _ := columnToFieldMap[key]
column, ok := mapping["column"]
if !ok {
column = mapping["fieldName"]
}
if _, ok := mapping["autoincrement"]; ok {
pkColumns = append(pkColumns, column)
autoincrement = true
continue
}
}
for key := range columnToFieldMap {
mapping, _ := columnToFieldMap[key]
column, ok := mapping["column"]
if !ok {
column = mapping["fieldName"]
}
columns = append(columns, column)
if _, ok := mapping["primaryKey"]; ok {
if !toolbox.HasSliceAnyElements(pkColumns, column) {
pkColumns = append(pkColumns, column)
}
continue
}
if key == "id" {
if !toolbox.HasSliceAnyElements(pkColumns, column) {
pkColumns = append(pkColumns, column)
}
continue
}
}
return &TableDescriptor{
Table: table,
Autoincrement: autoincrement,
Columns: columns,
PkColumns: pkColumns,
}, nil
}