修复一些序列化的细节
This commit is contained in:
118
generator/basic.go
Normal file
118
generator/basic.go
Normal file
@@ -0,0 +1,118 @@
|
||||
package main
|
||||
|
||||
const (
|
||||
_ int = iota
|
||||
LongVarBinary
|
||||
LongVarChar
|
||||
GeometryCollection
|
||||
GeomCollection
|
||||
LineString
|
||||
MultiLineString
|
||||
MultiPoint
|
||||
MultiPolygon
|
||||
Point
|
||||
Polygon
|
||||
Json
|
||||
Geometry
|
||||
Enum
|
||||
Set
|
||||
Bit
|
||||
Time
|
||||
Timestamp
|
||||
DateTime
|
||||
Binary
|
||||
VarBinary
|
||||
Blob
|
||||
Year
|
||||
Decimal
|
||||
Dec
|
||||
Fixed
|
||||
Numeric
|
||||
Float
|
||||
Float4
|
||||
Float8
|
||||
Double
|
||||
Real
|
||||
TinyInt
|
||||
SmallInt
|
||||
MediumInt
|
||||
Int
|
||||
Integer
|
||||
BigInt
|
||||
MiddleInt
|
||||
Int1
|
||||
Int2
|
||||
Int3
|
||||
Int4
|
||||
Int8
|
||||
Date
|
||||
TinyBlob
|
||||
MediumBlob
|
||||
LongBlob
|
||||
Bool
|
||||
Boolean
|
||||
Serial
|
||||
NVarChar
|
||||
NChar
|
||||
Char
|
||||
Character
|
||||
VarChar
|
||||
TinyText
|
||||
Text
|
||||
MediumText
|
||||
LongText
|
||||
)
|
||||
|
||||
var SQLTypeToGoTypeMap = map[int]string{
|
||||
LongVarBinary: "[]byte",
|
||||
Binary: "[]byte",
|
||||
VarBinary: "[]byte",
|
||||
Blob: "[]byte",
|
||||
TinyBlob: "[]byte",
|
||||
MediumBlob: "[]byte",
|
||||
LongBlob: "[]byte",
|
||||
|
||||
LongVarChar: "*string",
|
||||
NVarChar: "*string",
|
||||
NChar: "*string",
|
||||
Char: "*string",
|
||||
Character: "*string",
|
||||
VarChar: "*string",
|
||||
TinyText: "*string",
|
||||
Text: "*string",
|
||||
MediumText: "*string",
|
||||
LongText: "*string",
|
||||
|
||||
Time: "*time.Time",
|
||||
Timestamp: "*time.Time",
|
||||
DateTime: "*time.Time",
|
||||
Date: "*time.Time",
|
||||
|
||||
Year: "*int64",
|
||||
TinyInt: "*int64",
|
||||
SmallInt: "*int64",
|
||||
MediumInt: "*int64",
|
||||
Int: "*int64",
|
||||
Integer: "*int64",
|
||||
BigInt: "*int64",
|
||||
MiddleInt: "*int64",
|
||||
Int1: "*int64",
|
||||
Int2: "*int64",
|
||||
Int3: "*int64",
|
||||
Int4: "*int64",
|
||||
Int8: "*int64",
|
||||
Serial: "*int64",
|
||||
|
||||
Decimal: "*float64",
|
||||
Dec: "*float64",
|
||||
Fixed: "*float64",
|
||||
Numeric: "*float64",
|
||||
Float: "*float64",
|
||||
Float4: "*float64",
|
||||
Float8: "*float64",
|
||||
Double: "*float64",
|
||||
Real: "*float64",
|
||||
|
||||
Bool: "*bool",
|
||||
Boolean: "*bool",
|
||||
}
|
||||
@@ -27,158 +27,55 @@ func toPascalCase(s string) string {
|
||||
return strings.Join(words, "")
|
||||
}
|
||||
|
||||
const (
|
||||
_ int = iota
|
||||
LongVarBinary
|
||||
LongVarChar
|
||||
GeometryCollection
|
||||
GeomCollection
|
||||
LineString
|
||||
MultiLineString
|
||||
MultiPoint
|
||||
MultiPolygon
|
||||
Point
|
||||
Polygon
|
||||
Json
|
||||
Geometry
|
||||
Enum
|
||||
Set
|
||||
Bit
|
||||
Time
|
||||
Timestamp
|
||||
DateTime
|
||||
Binary
|
||||
VarBinary
|
||||
Blob
|
||||
Year
|
||||
Decimal
|
||||
Dec
|
||||
Fixed
|
||||
Numeric
|
||||
Float
|
||||
Float4
|
||||
Float8
|
||||
Double
|
||||
Real
|
||||
TinyInt
|
||||
SmallInt
|
||||
MediumInt
|
||||
Int
|
||||
Integer
|
||||
BigInt
|
||||
MiddleInt
|
||||
Int1
|
||||
Int2
|
||||
Int3
|
||||
Int4
|
||||
Int8
|
||||
Date
|
||||
TinyBlob
|
||||
MediumBlob
|
||||
LongBlob
|
||||
Bool
|
||||
Boolean
|
||||
Serial
|
||||
NVarChar
|
||||
NChar
|
||||
Char
|
||||
Character
|
||||
VarChar
|
||||
TinyText
|
||||
Text
|
||||
MediumText
|
||||
LongText
|
||||
)
|
||||
|
||||
var SQLTypeToGoTypeMap = map[int]string{
|
||||
LongVarBinary: "[]byte",
|
||||
Binary: "[]byte",
|
||||
VarBinary: "[]byte",
|
||||
Blob: "[]byte",
|
||||
TinyBlob: "[]byte",
|
||||
MediumBlob: "[]byte",
|
||||
LongBlob: "[]byte",
|
||||
|
||||
LongVarChar: "*string",
|
||||
NVarChar: "*string",
|
||||
NChar: "*string",
|
||||
Char: "*string",
|
||||
Character: "*string",
|
||||
VarChar: "*string",
|
||||
TinyText: "*string",
|
||||
Text: "*string",
|
||||
MediumText: "*string",
|
||||
LongText: "*string",
|
||||
|
||||
Time: "*time.Time",
|
||||
Timestamp: "*time.Time",
|
||||
DateTime: "*time.Time",
|
||||
Date: "*time.Time",
|
||||
|
||||
Year: "*int64",
|
||||
TinyInt: "*int64",
|
||||
SmallInt: "*int64",
|
||||
MediumInt: "*int64",
|
||||
Int: "*int64",
|
||||
Integer: "*int64",
|
||||
BigInt: "*int64",
|
||||
MiddleInt: "*int64",
|
||||
Int1: "*int64",
|
||||
Int2: "*int64",
|
||||
Int3: "*int64",
|
||||
Int4: "*int64",
|
||||
Int8: "*int64",
|
||||
Serial: "*int64",
|
||||
|
||||
Decimal: "*float64",
|
||||
Dec: "*float64",
|
||||
Fixed: "*float64",
|
||||
Numeric: "*float64",
|
||||
Float: "*float64",
|
||||
Float4: "*float64",
|
||||
Float8: "*float64",
|
||||
Double: "*float64",
|
||||
Real: "*float64",
|
||||
|
||||
Bool: "*bool",
|
||||
Boolean: "*bool",
|
||||
}
|
||||
|
||||
func main() {
|
||||
var name string
|
||||
flag.StringVar(&name, "name", "", "输入需要序列化的ddl文件名, 不需要后缀.ddl")
|
||||
flag.Parse()
|
||||
|
||||
p, err := filepath.Abs(fmt.Sprintf("%s/%s.sql", targerDir, name))
|
||||
if name != "" {
|
||||
name = fmt.Sprintf("%s/%s.sql", targerDir, name)
|
||||
GenFromPath(name)
|
||||
} else {
|
||||
matches, err := filepath.Glob(fmt.Sprintf("%s/*.sql", targerDir))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
for _, pth := range matches {
|
||||
GenFromPath(pth)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func GenFromPath(pth string) {
|
||||
p, err := filepath.Abs(pth)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
ddlf, err := os.Open(p)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
ddlfilestr, err := ioutil.ReadAll(ddlf)
|
||||
ddlfilestr, err := ioutil.ReadFile(pth)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
// PRIMARY KEY (`guest_id`) USING BTREE
|
||||
re := regexp.MustCompile("PRIMARY\\s+KEY\\s+\\(\\s*`([^`]+)`\\s*\\)|`([^`]+)` [^\n]+PRIMARY\\s+KEY\\s+")
|
||||
|
||||
matches := re.FindStringSubmatch(string(ddlfilestr))
|
||||
PrimaryStr := ""
|
||||
if len(matches) > 0 {
|
||||
PrimaryStr = matches[1]
|
||||
}
|
||||
|
||||
// 匹配到主键定义
|
||||
var importstr = "import (\"gorm.io/gorm\"\n"
|
||||
|
||||
// 匹配到主键定义
|
||||
parser.NewParser()
|
||||
result, err := parser.NewParser().From(p)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fcontent := "package model\nimport \"gorm.io/gorm\"\n"
|
||||
fcontent := "package model\n"
|
||||
|
||||
for _, table := range result {
|
||||
structstr := "type %s struct {%s\n}\n"
|
||||
@@ -189,6 +86,9 @@ func main() {
|
||||
for _, col := range table.Columns {
|
||||
fieldName := toPascalCase(col.Name)
|
||||
typeName := SQLTypeToGoTypeMap[col.DataType.Type()]
|
||||
if typeName == "*time.Time" {
|
||||
importstr += "\"time\"\n"
|
||||
}
|
||||
tagstr := "`gorm:"
|
||||
if col.Name == PrimaryStr {
|
||||
tagstr += "\"primary_key\""
|
||||
@@ -204,6 +104,7 @@ func main() {
|
||||
|
||||
}
|
||||
|
||||
fcontent += importstr + ")\n"
|
||||
fcontent += fmt.Sprintf(structstr, tableName, fieldstr)
|
||||
modelstr := fmt.Sprintf(`type %sModel struct {db *gorm.DB}`, tableName)
|
||||
fcontent += modelstr
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -9,7 +8,7 @@ func TestMain(t *testing.T) {
|
||||
// args := []string{"-name", "fs_guest"}
|
||||
targerDir = "../" + targerDir
|
||||
genDir = "../" + genDir
|
||||
os.Args = []string{"cmd", "-name=fs_guest"}
|
||||
// os.Args = []string{"cmd", "-name=fs_guest"}
|
||||
|
||||
main()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user