command.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. package command
  2. import (
  3. "errors"
  4. "path/filepath"
  5. "strings"
  6. "github.com/go-sql-driver/mysql"
  7. "github.com/tal-tech/go-zero/core/logx"
  8. "github.com/tal-tech/go-zero/core/stores/postgres"
  9. "github.com/tal-tech/go-zero/core/stores/sqlx"
  10. "github.com/tal-tech/go-zero/tools/goctl/config"
  11. "github.com/tal-tech/go-zero/tools/goctl/model/sql/gen"
  12. "github.com/tal-tech/go-zero/tools/goctl/model/sql/model"
  13. "github.com/tal-tech/go-zero/tools/goctl/model/sql/util"
  14. file "github.com/tal-tech/go-zero/tools/goctl/util"
  15. "github.com/tal-tech/go-zero/tools/goctl/util/console"
  16. "github.com/urfave/cli"
  17. )
  18. const (
  19. flagSrc = "src"
  20. flagDir = "dir"
  21. flagCache = "cache"
  22. flagIdea = "idea"
  23. flagURL = "url"
  24. flagTable = "table"
  25. flagStyle = "style"
  26. flagDatabase = "database"
  27. flagSchema = "schema"
  28. flagHome = "home"
  29. )
  30. var errNotMatched = errors.New("sql not matched")
  31. // MysqlDDL generates model code from ddl
  32. func MysqlDDL(ctx *cli.Context) error {
  33. src := ctx.String(flagSrc)
  34. dir := ctx.String(flagDir)
  35. cache := ctx.Bool(flagCache)
  36. idea := ctx.Bool(flagIdea)
  37. style := ctx.String(flagStyle)
  38. database := ctx.String(flagDatabase)
  39. home := ctx.String(flagHome)
  40. if len(home) > 0 {
  41. file.RegisterGoctlHome(home)
  42. }
  43. cfg, err := config.NewConfig(style)
  44. if err != nil {
  45. return err
  46. }
  47. return fromDDL(src, dir, cfg, cache, idea, database)
  48. }
  49. // MySqlDataSource generates model code from datasource
  50. func MySqlDataSource(ctx *cli.Context) error {
  51. url := strings.TrimSpace(ctx.String(flagURL))
  52. dir := strings.TrimSpace(ctx.String(flagDir))
  53. cache := ctx.Bool(flagCache)
  54. idea := ctx.Bool(flagIdea)
  55. style := ctx.String(flagStyle)
  56. home := ctx.String("home")
  57. if len(home) > 0 {
  58. file.RegisterGoctlHome(home)
  59. }
  60. pattern := strings.TrimSpace(ctx.String(flagTable))
  61. cfg, err := config.NewConfig(style)
  62. if err != nil {
  63. return err
  64. }
  65. return fromMysqlDataSource(url, pattern, dir, cfg, cache, idea)
  66. }
  67. // PostgreSqlDataSource generates model code from datasource
  68. func PostgreSqlDataSource(ctx *cli.Context) error {
  69. url := strings.TrimSpace(ctx.String(flagURL))
  70. dir := strings.TrimSpace(ctx.String(flagDir))
  71. cache := ctx.Bool(flagCache)
  72. idea := ctx.Bool(flagIdea)
  73. style := ctx.String(flagStyle)
  74. schema := ctx.String(flagSchema)
  75. home := ctx.String("home")
  76. if len(home) > 0 {
  77. file.RegisterGoctlHome(home)
  78. }
  79. if len(schema) == 0 {
  80. schema = "public"
  81. }
  82. pattern := strings.TrimSpace(ctx.String(flagTable))
  83. cfg, err := config.NewConfig(style)
  84. if err != nil {
  85. return err
  86. }
  87. return fromPostgreSqlDataSource(url, pattern, dir, schema, cfg, cache, idea)
  88. }
  89. func fromDDL(src, dir string, cfg *config.Config, cache, idea bool, database string) error {
  90. log := console.NewConsole(idea)
  91. src = strings.TrimSpace(src)
  92. if len(src) == 0 {
  93. return errors.New("expected path or path globbing patterns, but nothing found")
  94. }
  95. files, err := util.MatchFiles(src)
  96. if err != nil {
  97. return err
  98. }
  99. if len(files) == 0 {
  100. return errNotMatched
  101. }
  102. generator, err := gen.NewDefaultGenerator(dir, cfg, gen.WithConsoleOption(log))
  103. if err != nil {
  104. return err
  105. }
  106. for _, file := range files {
  107. err = generator.StartFromDDL(file, cache, database)
  108. if err != nil {
  109. return err
  110. }
  111. }
  112. return nil
  113. }
  114. func fromMysqlDataSource(url, pattern, dir string, cfg *config.Config, cache, idea bool) error {
  115. log := console.NewConsole(idea)
  116. if len(url) == 0 {
  117. log.Error("%v", "expected data source of mysql, but nothing found")
  118. return nil
  119. }
  120. if len(pattern) == 0 {
  121. log.Error("%v", "expected table or table globbing patterns, but nothing found")
  122. return nil
  123. }
  124. dsn, err := mysql.ParseDSN(url)
  125. if err != nil {
  126. return err
  127. }
  128. logx.Disable()
  129. databaseSource := strings.TrimSuffix(url, "/"+dsn.DBName) + "/information_schema"
  130. db := sqlx.NewMysql(databaseSource)
  131. im := model.NewInformationSchemaModel(db)
  132. tables, err := im.GetAllTables(dsn.DBName)
  133. if err != nil {
  134. return err
  135. }
  136. matchTables := make(map[string]*model.Table)
  137. for _, item := range tables {
  138. match, err := filepath.Match(pattern, item)
  139. if err != nil {
  140. return err
  141. }
  142. if !match {
  143. continue
  144. }
  145. columnData, err := im.FindColumns(dsn.DBName, item)
  146. if err != nil {
  147. return err
  148. }
  149. table, err := columnData.Convert()
  150. if err != nil {
  151. return err
  152. }
  153. matchTables[item] = table
  154. }
  155. if len(matchTables) == 0 {
  156. return errors.New("no tables matched")
  157. }
  158. generator, err := gen.NewDefaultGenerator(dir, cfg, gen.WithConsoleOption(log))
  159. if err != nil {
  160. return err
  161. }
  162. return generator.StartFromInformationSchema(matchTables, cache)
  163. }
  164. func fromPostgreSqlDataSource(url, pattern, dir, schema string, cfg *config.Config, cache, idea bool) error {
  165. log := console.NewConsole(idea)
  166. if len(url) == 0 {
  167. log.Error("%v", "expected data source of postgresql, but nothing found")
  168. return nil
  169. }
  170. if len(pattern) == 0 {
  171. log.Error("%v", "expected table or table globbing patterns, but nothing found")
  172. return nil
  173. }
  174. db := postgres.New(url)
  175. im := model.NewPostgreSqlModel(db)
  176. tables, err := im.GetAllTables(schema)
  177. if err != nil {
  178. return err
  179. }
  180. matchTables := make(map[string]*model.Table)
  181. for _, item := range tables {
  182. match, err := filepath.Match(pattern, item)
  183. if err != nil {
  184. return err
  185. }
  186. if !match {
  187. continue
  188. }
  189. columnData, err := im.FindColumns(schema, item)
  190. if err != nil {
  191. return err
  192. }
  193. table, err := columnData.Convert()
  194. if err != nil {
  195. return err
  196. }
  197. matchTables[item] = table
  198. }
  199. if len(matchTables) == 0 {
  200. return errors.New("no tables matched")
  201. }
  202. generator, err := gen.NewDefaultGenerator(dir, cfg, gen.WithConsoleOption(log), gen.WithPostgreSql())
  203. if err != nil {
  204. return err
  205. }
  206. return generator.StartFromInformationSchema(matchTables, cache)
  207. }