-
Notifications
You must be signed in to change notification settings - Fork 0
/
orm.go
72 lines (60 loc) · 1.95 KB
/
orm.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
package ormx
import (
"context"
"fmt"
"github.com/cloudfly/flagx"
"github.com/cloudfly/ormx/cache"
)
var (
tableNamePrefix = flagx.NewString("database.table.prefix", "", "the table name's common prefix")
structTagName = "db"
namespaceColumnName = flagx.NewString("database.table.namespace.column", "namespace", "the column name used to represent row's namespace")
primaryKey = flagx.NewString("database.table.primarykey", "id", "the primary id column name")
)
// Init the ormx, setting the sqlx.DB getter and common table name prefix
func Init(ctx context.Context, provider DBProvider) error {
if err := Connect(ctx); err != nil {
return err
}
if provider != nil {
p = provider
}
return cache.Init()
}
// SetStructTagName set the tag name in Go Struct Tag, in which specify the ormx options, default is 'db'
func SetStructTagName(name string) {
structTagName = name
}
// SetPrimaryKey set the primary column name, default is 'id'
func SetPrimaryKey(name string) {
if name != "" {
*primaryKey = name
}
}
// SetNamespaceColumnName set the common namespace colunm name, default is 'namespace';
//
// ormx will auto inject namespace where condition into sql.// Set to empty string disable this feature
func SetNamespaceColumnName(name string) {
*namespaceColumnName = name
}
type masterCtxKey struct{}
func isFromMaster(ctx context.Context) bool {
return fmt.Sprintf("%v", ctx.Value(masterCtxKey{})) == "true"
}
// FromMaster force ormx execute sql on master instance when called by this context
func FromMaster(ctx context.Context) context.Context {
return context.WithValue(ctx, masterCtxKey{}, "true")
}
// FromMaster force ormx execute sql on slave instance when called by this context
func FromSlave(ctx context.Context) context.Context {
return context.WithValue(ctx, masterCtxKey{}, "false")
}
func convertValueByDBType(v any, tag string) any {
switch tag {
case "timestamp":
if t, ok := Any2Time(v); ok {
return t
}
}
return v
}