-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
127 lines (95 loc) · 2.76 KB
/
util.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
package GormSQLBuilder
//package main
import (
"errors"
"fmt"
"reflect"
"strings"
"time"
)
type PersonDemo struct {
Id string `json:"id" gorm:"column:id;primary_key"` //uuid
User_id string `json:"guser_id" gorm:"column:guser_id" gensql:"notnull"` //uuid
Age int `json:"age" gorm:"column:age" gensql:"notnull"` //int
Weight float32 `json:"weight" gorm:"column:weight" gensql:"notnull"` //float32
Create_time time.Time `json:"create_time" gorm:"column:create_time" gensql:"notnull"` //timestamp without time zone
}
func (PersonDemo) TableName() string {
return "Info.PersonDemo"
}
func test_main() {
var person PersonDemo
sql, _ := CreateSQL(person)
fmt.Println(sql)
}
func FindColumn(s string) string {
ss := strings.Split(s, ";")
for _, v := range ss {
kv := strings.Split(v, ":")
if len(kv) >= 2 {
if kv[0] == "column" {
return kv[1]
}
}
}
return ""
}
func GetTableName(data interface{}) string {
object := reflect.ValueOf(data)
v := object.MethodByName("TableName")
if v.Kind() == reflect.Func {
return fmt.Sprintf("%v", v.Call([]reflect.Value{})[0])
} else {
s := fmt.Sprintf("%T", data)
ss := strings.Split(s, ".")
return ss[len(ss)-1]
}
}
func CreateSQL(data interface{}) (string, error) {
dataValue := reflect.ValueOf(data)
//fmt.Println("dataValue:", dataValue)
t := reflect.TypeOf(data)
if dataValue.Kind() != reflect.Struct {
return "", errors.New("Input is not a Struct.")
}
columns := ""
values := ""
for i := 0; i < t.NumField(); i++ {
k := dataValue.Type().Field(i)
value := dataValue.Field(i).Interface()
var valueStr string
switch value.(type) {
case *string:
if value.(*string) == nil {
valueStr = `null`
} else {
valueStr = fmt.Sprintf(`'%s'`, *value.(*string))
}
case string:
valueStr = fmt.Sprintf(`'%v'`, value)
case time.Time:
valueStr = fmt.Sprintf(`'%s'`, value.(time.Time).Format("2006-01-02 15:04:05"))
default:
valueStr = fmt.Sprintf("%v", value)
}
column := FindColumn(k.Tag.Get("gorm"))
if column !="" {
flag := k.Tag.Get("gensql")
//fmt.Println(column, flag)
if flag == "notnull" {
if valueStr == `''` || valueStr == `null` || valueStr == `<nil>` || valueStr == "0" || valueStr == `'0001-01-01 00:00:00 +0000 UTC'` || valueStr == `'0001-01-01 00:00:00'` {
} else {
values = values + valueStr + ","
columns = columns + `"` + column + `",`
}
} else {
values = values + valueStr + ","
columns = columns + `"` + column + `",`
}
}
}
tablename := GetTableName(data)
sql := fmt.Sprintf(`INSERT INTO %s (%s) VALUES (%s)`, tablename, columns[:len(columns)-1], values[:len(values)-1])
//fmt.Println(sql)
return sql, nil
}