diff --git a/data/view/genfunc/gen.base.go b/data/view/genfunc/gen.base.go new file mode 100644 index 0000000..b1689ca --- /dev/null +++ b/data/view/genfunc/gen.base.go @@ -0,0 +1,34 @@ +package genfunc + +import ( + "context" + "fmt" + + "github.com/jinzhu/gorm" +) + +type _BaseMgr struct { + *gorm.DB + ctx *context.Context +} + +// SetCtx set context +func (obj *_BaseMgr) SetCtx(c *context.Context) { + obj.ctx = c +} + +////////////////////////////////////////////logic + +type _ExampleMgr struct { + *_BaseMgr +} + +// ExampleMgr open func +func ExampleMgr(db *gorm.DB) *_ExampleMgr { + if db == nil { + panic(fmt.Errorf("ExampleMgr init need db")) + } + return &_ExampleMgr{_BaseMgr: &_BaseMgr{DB: db}} +} + +/////////////////////////////////////////////////// diff --git a/data/view/genfunc/genfunc.go b/data/view/genfunc/genfunc.go new file mode 100644 index 0000000..fbde9db --- /dev/null +++ b/data/view/genfunc/genfunc.go @@ -0,0 +1 @@ +package genfunc diff --git a/data/view/gtools/commont.go b/data/view/gtools/commont.go index 328425a..ab1e2d9 100644 --- a/data/view/gtools/commont.go +++ b/data/view/gtools/commont.go @@ -5,8 +5,8 @@ import ( "github.com/xxjwxc/gormt/data/view/model/genmysql" ) -// GetModel get model interface. 获取model接口 -func GetModel() model.IModel { +// GetMysqlModel get model interface. 获取model接口 +func GetMysqlModel() model.IModel { //now just support mysql return &genmysql.MySQLModel } diff --git a/data/view/gtools/gtools.go b/data/view/gtools/gtools.go index 71ce1d7..6f2a17e 100644 --- a/data/view/gtools/gtools.go +++ b/data/view/gtools/gtools.go @@ -19,20 +19,20 @@ func Execute() { // orm.Where("nickname = ?", "ticket_001").Find(&tt) // fmt.Println(tt) - modeldb := GetModel() + modeldb := GetMysqlModel() pkg := modeldb.GenModel() - pkg.PackageName = modeldb.GetPkgName() - str := model.Generate(pkg) + list := model.Generate(pkg) - path := config.GetOutDir() + "/" + modeldb.GetDbName() + ".go" - tools.WriteFile(path, - []string{str}, true) + for _, v := range list { + path := config.GetOutDir() + "/" + v.FileName + tools.WriteFile(path, []string{v.FileCtx}, true) - fmt.Println("formatting differs from goimport's:") - cmd, _ := exec.Command("goimports", "-l", "-w", path).Output() - fmt.Println(string(cmd)) + fmt.Println("formatting differs from goimport's:") + cmd, _ := exec.Command("goimports", "-l", "-w", path).Output() + fmt.Println(string(cmd)) - fmt.Println("formatting differs from gofmt's:") - cmd, _ = exec.Command("gofmt", "-l", "-w", path).Output() - fmt.Println(string(cmd)) + fmt.Println("formatting differs from gofmt's:") + cmd, _ = exec.Command("gofmt", "-l", "-w", path).Output() + fmt.Println(string(cmd)) + } } diff --git a/data/view/model/def.go b/data/view/model/def.go index 5e01149..98c1183 100644 --- a/data/view/model/def.go +++ b/data/view/model/def.go @@ -23,7 +23,8 @@ const ( // DBInfo database default info type DBInfo struct { - PackageName string + DbName string // database name + PackageName string // package name TabList []TabInfo // table list .表列表 } @@ -60,3 +61,9 @@ type BaseInfo struct { Name string // table name.表名 Notes string // table comment . 表注释 } + +// GenOutInfo generate file list. 生成的文件列表 +type GenOutInfo struct { + FileName string // output file name .输出文件名 + FileCtx string // output file context.输出文件内容 +} diff --git a/data/view/model/genmysql/genmysql.go b/data/view/model/genmysql/genmysql.go index 4b02d74..513c396 100644 --- a/data/view/model/genmysql/genmysql.go +++ b/data/view/model/genmysql/genmysql.go @@ -23,6 +23,8 @@ func (m *mysqlModel) GenModel() model.DBInfo { var dbInfo model.DBInfo getPackageInfo(orm, &dbInfo) + dbInfo.PackageName = m.GetPkgName() + dbInfo.DbName = m.GetDbName() return dbInfo } diff --git a/go.sum b/go.sum index e56a03b..f53f55d 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,6 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +cloud.google.com/go v0.37.4 h1:glPeL3BQJsbF6aIIYfZizMwc5LTYz250bDMjttbBGAU= cloud.google.com/go v0.37.4/go.mod h1:NHPJ89PdicEuT9hdPXMROBD91xc5uRDxsMtSB16k7hw= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo= @@ -21,10 +22,12 @@ github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3Ee github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/denisenkom/go-mssqldb v0.0.0-20190515213511-eb9f6a1743f3 h1:tkum0XDgfR0jcVVXuTsYv/erY2NnEDqwRojbxR1rBYA= github.com/denisenkom/go-mssqldb v0.0.0-20190515213511-eb9f6a1743f3/go.mod h1:zAg7JM8CkOJ43xKXIj7eRO9kmWm/TW578qo+oDO6tuM= github.com/eapache/go-resiliency v1.1.0/go.mod h1:kFI+JgMyC7bLPUVY133qvEBtVayf5mFgVsvEsIPBvNs= github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1:+020luEh2TKB4/GOp8oxxtq0Daoen/Cii55CzbTV6DU= github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFPTqq+I= +github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 h1:Yzb9+7DPaBjB8zlTR87/ElzFsnQfuHnVUVqpZZIcV5Y= github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5/go.mod h1:a2zkGnVExMxdzMo3M0Hi/3sEU+cWnZpSni0O6/Yb/P0= github.com/ezbuy/tgen v0.0.0-20180109020500-95ef13895032/go.mod h1:OeW1N0acAlRaGTlOG8jRZZUKEtyiGa0qvD+lWNWb9vs= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= @@ -63,6 +66,7 @@ github.com/jinzhu/gorm v1.9.11 h1:gaHGvE+UnWGlbWG4Y3FUwY1EcZ5n6S9WtqBA/uySMLE= github.com/jinzhu/gorm v1.9.11/go.mod h1:bu/pK8szGZ2puuErfU0RwyeNdsf3e6nCX/noXaVxkfw= 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.0.1 h1:HjfetcXq097iXP0uoPCdnM4Efp5/9MsM0/M+XOTeR3M= github.com/jinzhu/now v1.0.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= @@ -75,8 +79,10 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y= github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= +github.com/lib/pq v1.1.1 h1:sJZmqHoEaY7f+NPP8pgLB/WxulyR3fewgCM2qaSlBb4= github.com/lib/pq v1.1.1/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= +github.com/mattn/go-sqlite3 v1.11.0 h1:LDdKkqtYlom37fkvqs8rMPFKAMe8+SgjbwZ6ex1/A/Q= github.com/mattn/go-sqlite3 v1.11.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= @@ -122,6 +128,7 @@ go.opencensus.io v0.20.1/go.mod h1:6WKK9ahsWS3RSO+PY9ZHZUfv2irvY6gN279GOPZjmmk= golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c h1:Vj5n4GlwjmQteupaxJ9+0FNOmBrHfq7vN4btdGoDZgI= golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= diff --git a/gormt b/gormt new file mode 100755 index 0000000..81567e2 Binary files /dev/null and b/gormt differ