-
Notifications
You must be signed in to change notification settings - Fork 0
/
ddl.go
66 lines (61 loc) · 1.41 KB
/
ddl.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
package skt
import (
"reflect"
"strings"
)
// DDLStruct is DDL struct
type DDLStruct struct {
StructName string
CreateString string
DropString string
}
// GetDDL get create string
func GetDDL(i interface{}) DDLStruct {
var ds DDLStruct
re := reflect.TypeOf(i).Elem()
ds.StructName = strings.ToLower(re.Name())
var rf []string
for n := 0; n < re.NumField(); n++ {
tag := re.Field(n).Tag
// 判断是否有核心结构,有则加入核心所有字段
if re.Field(n).Name == "CK" {
recore := reflect.TypeOf(&CK{}).Elem()
for n := 0; n < recore.NumField(); n++ {
tagcore := recore.Field(n).Tag
rf = append(rf, strings.ToLower(recore.Field(n).Name)+" "+tagcore.Get("db"))
}
continue
}
rf = append(rf, strings.ToLower(re.Field(n).Name)+" "+tag.Get("db"))
}
str := strings.Join(rf, ",")
ds.CreateString = "CREATE TABLE " + ds.StructName + "(" + str + ")"
ds.DropString = "DROP TABLE " + ds.StructName
return ds
}
// CreateTable create table
func (db *DB) CreateTable(ss ...interface{}) (int64, error) {
var i int64
for _, s := range ss {
gd := GetDDL(s)
_, err := db.Exec(gd.CreateString)
if err != nil {
return i, err
}
i++
}
return i, nil
}
// DropTable drop table
func (db *DB) DropTable(ss ...interface{}) (int64, error) {
var i int64
for _, s := range ss {
gd := GetDDL(s)
_, err := db.Exec(gd.DropString)
if err != nil {
return i, err
}
i++
}
return i, nil
}