-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.go
More file actions
132 lines (118 loc) · 2.95 KB
/
util.go
File metadata and controls
132 lines (118 loc) · 2.95 KB
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
package qorm
import (
"reflect"
"regexp"
"runtime"
"strconv"
"strings"
jsoniter "github.com/json-iterator/go"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)
func QuoteTo(db *gorm.DB, str string) string {
builder := strings.Builder{}
db.Dialector.QuoteTo(&builder, str)
return builder.String()
}
func GetTableName(tx *gorm.DB) (tableName string) {
tableName = tx.Statement.Table
if tableName == "" {
tableName = tx.NamingStrategy.TableName(reflect.TypeOf(tx.Statement.Model).Elem().Name())
}
if tabler, ok := tx.Statement.Model.(schema.Tabler); ok {
tableName = tabler.TableName()
}
return
}
func getRelation(name string) (relation string) {
index := strings.LastIndex(name, ".")
if index != -1 {
relation = name[0:index]
}
return
}
func getInfoFromTag(tag, defaultFlag, defaultName string) (flag, name, relation string) {
flag = defaultFlag
name = defaultName
if tag != "" {
arr := strings.Split(tag, ";")
if len(arr) == 1 {
flag = arr[0]
}
if len(arr) == 2 {
flag = arr[0]
name = arr[1]
// 别名有可能是联表字段
relation = getRelation(name)
}
}
return
}
func getFlagAndNameFromTag(tag, defaultFlag, defaultName string) (flag, name string) {
flag = defaultFlag
name = defaultName
if tag != "" {
arr := strings.Split(tag, ";")
if len(arr) == 1 {
flag = arr[0]
}
if len(arr) == 2 {
flag = arr[0]
name = arr[1]
}
}
return
}
var qormSourceDir string
func init() {
_, file, _, _ := runtime.Caller(0)
// compatible solution to get gorm source directory with various operating systems
qormSourceDir = regexp.MustCompile(`util\.go`).ReplaceAllString(file, "")
}
func fileWithLineNum() string {
// the second caller usually from gorm internal, so set i start from 2
for i := 2; i < 15; i++ {
_, file, line, ok := runtime.Caller(i)
if ok && (!strings.HasPrefix(file, qormSourceDir) || strings.HasSuffix(file, "_test.go")) {
var funcName string
if p, _, _, ok := runtime.Caller(i - 1); ok {
arr := strings.Split(runtime.FuncForPC(p).Name(), ".")
funcName = arr[len(arr)-1]
}
return funcName + " " + file + ":" + strconv.FormatInt(int64(line), 10)
}
}
return ""
}
func printFileWithLineNum(db *DB) {
db.Config.Logger.Info(db.Statement.Context, "qorm."+fileWithLineNum())
}
func IsZeroOfUnderlyingType(x interface{}) bool {
return reflect.DeepEqual(x, reflect.Zero(reflect.TypeOf(x)).Interface())
}
func GetDeepType(typ reflect.Type) reflect.Type {
resKind := typ.Kind()
if resKind == reflect.Array || resKind == reflect.Slice || resKind == reflect.Ptr {
return GetDeepType(typ.Elem())
} else {
return typ
}
}
func GetDeepValue(value reflect.Value) reflect.Value {
resKind := value.Kind()
if resKind == reflect.Ptr {
return GetDeepValue(value.Elem())
} else {
return value
}
}
func ToStruct(param, pointer interface{}) error {
bytes, err := jsoniter.Marshal(¶m)
if err != nil {
return err
}
if err = jsoniter.Unmarshal(bytes, pointer); err != nil {
return err
}
return nil
}