-
Notifications
You must be signed in to change notification settings - Fork 1
/
gormgen.go
58 lines (48 loc) 路 1.14 KB
/
gormgen.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
// go:build ignore
package main
import (
"log"
"strings"
"gorm.io/gen"
"gorm.io/gorm"
"gorm.io/rawsql"
)
func main() {
db, err := gorm.Open(rawsql.New(rawsql.Config{FilePath: []string{"../po/sql/table.sql"}}))
if err != nil {
log.Fatal(err)
}
g := gen.NewGenerator(gen.Config{
OutPath: "../dao",
OutFile: "",
ModelPkgPath: "po",
WithUnitTest: false,
FieldNullable: true,
FieldCoverable: true,
FieldSignable: true,
FieldWithIndexTag: true,
FieldWithTypeTag: true,
Mode: gen.WithQueryInterface,
})
g.UseDB(db)
g.WithDataTypeMap(map[string]func(gorm.ColumnType) (dataType string){
// bool mapping
"tinyint": func(columnType gorm.ColumnType) (dataType string) {
ct, _ := columnType.ColumnType()
if strings.HasPrefix(ct, "tinyint(1)") {
return "bool"
}
return "byte"
},
})
tables := g.GenerateAllTable()
g.ApplyBasic(tables...)
g.ApplyInterface(func(IBase) {}, tables...)
g.Execute()
}
type IBase interface {
// SELECT * FROM @@table WHERE id=@id
FindByID(id int64) (*gen.T, error)
// DELETE FROM @@table WHERE id=@id
DeleteByID(id int64) error
}