commit
00d0792072
@ -0,0 +1,105 @@
|
||||
package makemodel
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gorm.io/gorm"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Column struct {
|
||||
ColumnName string `gorm:"column:COLUMN_NAME"`
|
||||
DataType string `gorm:"column:DATA_TYPE"`
|
||||
ColumnKey string `gorm:"column:COLUMN_KEY"`
|
||||
ColumnComment string `gorm:"column:COLUMN_COMMENT"`
|
||||
}
|
||||
|
||||
// Create @Title 创建model
|
||||
func Create(db *gorm.DB, table string, path ...string) error {
|
||||
var columns []Column
|
||||
sql := fmt.Sprintf("select * from information_schema.COLUMNS where table_name ='%s'", db.NamingStrategy.TableName(table))
|
||||
db.Raw(sql).Scan(&columns)
|
||||
tableName := camelString(table)
|
||||
|
||||
strContent := "type " + tableName + " struct {\n"
|
||||
imports := map[string]bool{}
|
||||
for _, column := range columns {
|
||||
columnType := ""
|
||||
switch column.DataType {
|
||||
case "varchar", "text":
|
||||
columnType = "string"
|
||||
case "int", "tinyint":
|
||||
columnType = "uint"
|
||||
case "decimal":
|
||||
columnType = "decimal.Decimal"
|
||||
imports["github.com/shopspring/decimal"] = true
|
||||
case "datetime":
|
||||
if column.ColumnName == "deleted_at" {
|
||||
columnType = "gorm.DeletedAt"
|
||||
imports["gorm.io/gorm"] = true
|
||||
} else {
|
||||
columnType = "time.Time"
|
||||
imports["time"] = true
|
||||
}
|
||||
}
|
||||
pri := ""
|
||||
if column.ColumnKey == "PRI" {
|
||||
pri = "`gorm:\"primarykey\"`"
|
||||
}
|
||||
comment := ""
|
||||
if column.ColumnComment != "" {
|
||||
comment = "// " + column.ColumnComment
|
||||
}
|
||||
strContent += fmt.Sprintf("\t%s %s %s%s\n", camelString(column.ColumnName), columnType, pri, comment)
|
||||
}
|
||||
strContent += "}"
|
||||
headerStr := "package model\n\nimport(\n"
|
||||
for key, _ := range imports {
|
||||
headerStr += fmt.Sprintf("\t\"%s\"\n", key)
|
||||
}
|
||||
headerStr += ")\n\n"
|
||||
strContent = headerStr + strContent
|
||||
|
||||
fileName := strings.ToLower(tableName[0:1]) + tableName[1:]
|
||||
dir := ""
|
||||
if len(path) > 0 {
|
||||
dir = path[0]
|
||||
os.MkdirAll(dir, 0777)
|
||||
}
|
||||
f, err := os.OpenFile(filepath.Join(dir, fileName+".go"), os.O_CREATE|os.O_RDWR, 0644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
_, err = f.WriteString(strContent)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// camel string, xx_yy to XxYy
|
||||
func camelString(s string) string {
|
||||
data := make([]byte, 0, len(s))
|
||||
j := false
|
||||
k := false
|
||||
num := len(s) - 1
|
||||
for i := 0; i <= num; i++ {
|
||||
d := s[i]
|
||||
if k == false && d >= 'A' && d <= 'Z' {
|
||||
k = true
|
||||
}
|
||||
if d >= 'a' && d <= 'z' && (j || k == false) {
|
||||
d = d - 32
|
||||
j = false
|
||||
k = true
|
||||
}
|
||||
if k && d == '_' && num > i && s[i+1] >= 'a' && s[i+1] <= 'z' {
|
||||
j = true
|
||||
continue
|
||||
}
|
||||
data = append(data, d)
|
||||
}
|
||||
return string(data[:])
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
module git.oa00.com/go/makemodel
|
||||
|
||||
go 1.16
|
||||
|
||||
require gorm.io/gorm v1.21.9
|
@ -0,0 +1,6 @@
|
||||
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
|
||||
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
|
||||
github.com/jinzhu/now v1.1.2 h1:eVKgfIdy9b6zbWBMgFpfDPoAMifwSZagU9HmEU6zgiI=
|
||||
github.com/jinzhu/now v1.1.2/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
|
||||
gorm.io/gorm v1.21.9 h1:INieZtn4P2Pw6xPJ8MzT0G4WUOsHq3RhfuDF1M6GW0E=
|
||||
gorm.io/gorm v1.21.9/go.mod h1:F+OptMscr0P2F2qU97WT1WimdH9GaQPoDW7AYd5i2Y0=
|
Loading…
Reference in new issue