mysql.go
5.22 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package utildto
import (
"fmt"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"gorm.io/gorm/migrator"
"strings"
)
const MYSQL_TABLE_TEMPLATE = "all_table_template" // 表名模板
func ConnectMySQL(dsn string) (db *gorm.DB, err error) {
return gorm.Open(mysql.Open(dsn), &gorm.Config{})
}
func InitTable(db *gorm.DB, value any, tableName string) {
m := db.Migrator().(mysql.Migrator).Migrator
queryTx, execTx := m.GetQueryAndExecTx()
if HasTable(queryTx.Migrator().(mysql.Migrator).Migrator, value, tableName) {
fmt.Println(tableName + " exist, skip!")
} else {
err := CreateTable(execTx, value, tableName)
if err != nil {
fmt.Println("err", err)
}
}
}
func HasTable(m migrator.Migrator, value interface{}, tableName string) bool {
var count int64
m.RunWithValue(value, func(stmt *gorm.Statement) error {
currentDatabase := m.DB.Migrator().CurrentDatabase()
return m.DB.Raw("SELECT count(*) FROM information_schema.tables WHERE table_schema = ? AND table_name = ? AND table_type = ?", currentDatabase, tableName, "BASE TABLE").Row().Scan(&count)
})
return count > 0
}
func CreateTable(execTx *gorm.DB, value any, tableName string) error {
m := execTx.Migrator().(mysql.Migrator).Migrator
tx := m.DB.Session(&gorm.Session{})
if err := m.RunWithValue(value, func(stmt *gorm.Statement) (err error) {
var (
createTableSQL = "CREATE TABLE ? ("
values = []interface{}{clause.Table{Name: tableName}}
hasPrimaryKeyInDataType bool
)
for _, dbName := range stmt.Schema.DBNames {
field := stmt.Schema.FieldsByDBName[dbName]
if !field.IgnoreMigration {
createTableSQL += "? ?"
hasPrimaryKeyInDataType = hasPrimaryKeyInDataType || strings.Contains(strings.ToUpper(m.DataTypeOf(field)), "PRIMARY KEY")
values = append(values, clause.Column{Name: dbName}, m.DB.Migrator().FullDataTypeOf(field))
createTableSQL += ","
}
}
if !hasPrimaryKeyInDataType && len(stmt.Schema.PrimaryFields) > 0 {
createTableSQL += "PRIMARY KEY ?,"
primaryKeys := make([]interface{}, 0, len(stmt.Schema.PrimaryFields))
for _, field := range stmt.Schema.PrimaryFields {
primaryKeys = append(primaryKeys, clause.Column{Name: field.DBName})
}
values = append(values, primaryKeys)
}
for _, idx := range stmt.Schema.ParseIndexes() {
if m.CreateIndexAfterCreateTable {
defer func(value interface{}, name string) {
if err == nil {
err = tx.Migrator().CreateIndex(value, name)
}
}(value, idx.Name)
} else {
if idx.Class != "" {
createTableSQL += idx.Class + " "
}
createTableSQL += "INDEX ? ?"
if idx.Comment != "" {
createTableSQL += fmt.Sprintf(" COMMENT '%s'", idx.Comment)
}
if idx.Option != "" {
createTableSQL += " " + idx.Option
}
createTableSQL += ","
values = append(values, clause.Column{Name: idx.Name}, tx.Migrator().(migrator.BuildIndexOptionsInterface).BuildIndexOptions(idx.Fields, stmt))
}
}
if !m.DB.DisableForeignKeyConstraintWhenMigrating && !m.DB.IgnoreRelationshipsWhenMigrating {
for _, rel := range stmt.Schema.Relationships.Relations {
if rel.Field.IgnoreMigration {
continue
}
if constraint := rel.ParseConstraint(); constraint != nil {
if constraint.Schema == stmt.Schema {
sql, vars := constraint.Build()
createTableSQL += sql + ","
values = append(values, vars...)
}
}
}
}
for _, uni := range stmt.Schema.ParseUniqueConstraints() {
createTableSQL += "CONSTRAINT ? UNIQUE (?),"
values = append(values, clause.Column{Name: uni.Name}, clause.Expr{SQL: stmt.Quote(uni.Field.DBName)})
}
for _, chk := range stmt.Schema.ParseCheckConstraints() {
createTableSQL += "CONSTRAINT ? CHECK (?),"
values = append(values, clause.Column{Name: chk.Name}, clause.Expr{SQL: chk.Constraint})
}
createTableSQL = strings.TrimSuffix(createTableSQL, ",")
createTableSQL += ")"
if tableOption, ok := m.DB.Get("gorm:table_options"); ok {
createTableSQL += fmt.Sprint(tableOption)
}
err = tx.Exec(createTableSQL, values...).Error
return err
}); err != nil {
return err
}
return nil
}
// Insert 插入
func Insert(db *gorm.DB, value any, tableName string) *gorm.DB {
stmt := db.Session(&gorm.Session{DryRun: true}).Create(value).Statement
stmtSQL := stmt.SQL.String()
sql := strings.Replace(stmtSQL, MYSQL_TABLE_TEMPLATE, tableName, -1)
return db.Exec(sql, stmt.Vars...)
}
// Update 更新
func Update(db *gorm.DB, value any, tableName string) *gorm.DB {
stmt := db.Session(&gorm.Session{DryRun: true}).Updates(value).Statement
stmtSQL := stmt.SQL.String()
sql := strings.Replace(stmtSQL, MYSQL_TABLE_TEMPLATE, tableName, -1)
return db.Exec(sql, stmt.Vars...)
}
// Save 保存
func Save(db *gorm.DB, value any, tableName string) *gorm.DB {
stmt := db.Session(&gorm.Session{DryRun: true}).Save(value).Statement
stmtSQL := stmt.SQL.String()
sql := strings.Replace(stmtSQL, MYSQL_TABLE_TEMPLATE, tableName, -1)
return db.Exec(sql, stmt.Vars...)
}
func First(db *gorm.DB, value any, tableName string) *gorm.DB {
stmt := db.Session(&gorm.Session{DryRun: true}).First(value).Statement
stmtSQL := stmt.SQL.String()
sql := strings.Replace(stmtSQL, MYSQL_TABLE_TEMPLATE, tableName, -1)
return db.Raw(sql, stmt.Vars...).Scan(value)
}