commit 00d07920729ce9d7866fe0a7f4c10e7d34d4223b Author: kanade <3136520963@qq.com> Date: Sat May 8 11:42:35 2021 +0800 init diff --git a/create.go b/create.go new file mode 100644 index 0000000..57e8dcd --- /dev/null +++ b/create.go @@ -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[:]) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..83756b3 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module git.oa00.com/go/makemodel + +go 1.16 + +require gorm.io/gorm v1.21.9 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..638fa25 --- /dev/null +++ b/go.sum @@ -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=