-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.go
142 lines (119 loc) · 3.58 KB
/
engine.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
package orm
import (
"context"
"reflect"
"strings"
"time"
)
const DefaultPoolCode = "default"
type EngineRegistry interface {
EntitySchema(entity any) EntitySchema
DBPools() map[string]DB
LocalCachePools() map[string]LocalCache
RedisPools() map[string]RedisCache
Entities() []EntitySchema
Option(key string) any
Enums() map[string][]string
getDefaultQueryLogger() LogHandler
getDBTables() map[string]map[string]bool
}
type EngineSetter interface {
SetOption(key string, value any)
}
type Engine interface {
NewContext(parent context.Context) Context
DB(code string) DB
LocalCache(code string) LocalCache
Redis(code string) RedisCache
Registry() EngineRegistry
Option(key string) any
}
type engineRegistryImplementation struct {
engine *engineImplementation
entities map[string]reflect.Type
entitySchemaList []EntitySchema
entitySchemas map[reflect.Type]*entitySchema
entitySchemasQuickMap map[reflect.Type]*entitySchema
entityLogSchemas map[reflect.Type]*entitySchema
defaultQueryLogger *defaultLogLogger
dbTables map[string]map[string]bool
options map[string]any
enums map[string][]string
asyncConsumerBlockTime time.Duration
}
type engineImplementation struct {
registry *engineRegistryImplementation
localCacheServers map[string]LocalCache
dbServers map[string]DB
redisServers map[string]RedisCache
options map[string]any
pluginFlush []PluginInterfaceEntityFlush
asyncTemporaryIsQueueRunning bool
}
func (e *engineImplementation) NewContext(context context.Context) Context {
return &ormImplementation{context: context, engine: e}
}
func (e *engineImplementation) Registry() EngineRegistry {
return e.registry
}
func (e *engineImplementation) Option(key string) any {
return e.options[key]
}
func (e *engineImplementation) SetOption(key string, value any) {
e.options[key] = value
}
func (e *engineImplementation) DB(code string) DB {
return e.dbServers[code]
}
func (e *engineImplementation) LocalCache(code string) LocalCache {
return e.localCacheServers[code]
}
func (e *engineImplementation) Redis(code string) RedisCache {
return e.redisServers[code]
}
func (er *engineRegistryImplementation) RedisPools() map[string]RedisCache {
return er.engine.redisServers
}
func (er *engineRegistryImplementation) LocalCachePools() map[string]LocalCache {
return er.engine.localCacheServers
}
func (er *engineRegistryImplementation) DBPools() map[string]DB {
return er.engine.dbServers
}
func (er *engineRegistryImplementation) EntitySchema(entity any) EntitySchema {
switch entity.(type) {
case reflect.Type:
return er.entitySchemasQuickMap[entity.(reflect.Type)]
case string:
name := entity.(string)
if strings.HasPrefix(name, "*") {
name = name[1:]
}
t, has := er.entities[name]
if !has {
return nil
}
return er.entitySchemas[t]
default:
t := reflect.TypeOf(entity)
if t.Kind() == reflect.Ptr {
return er.entitySchemas[t.Elem()]
}
return er.entitySchemas[t]
}
}
func (er *engineRegistryImplementation) getDBTables() map[string]map[string]bool {
return er.dbTables
}
func (er *engineRegistryImplementation) Entities() []EntitySchema {
return er.entitySchemaList
}
func (er *engineRegistryImplementation) Option(key string) any {
return er.options[key]
}
func (er *engineRegistryImplementation) Enums() map[string][]string {
return er.enums
}
func (er *engineRegistryImplementation) getDefaultQueryLogger() LogHandler {
return er.defaultQueryLogger
}