command.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package command
  2. import (
  3. "errors"
  4. "io/ioutil"
  5. "path/filepath"
  6. "strings"
  7. "git.i2edu.net/i2/go-zero/core/logx"
  8. "git.i2edu.net/i2/go-zero/core/stores/sqlx"
  9. "git.i2edu.net/i2/go-zero/tools/goctl/config"
  10. "git.i2edu.net/i2/go-zero/tools/goctl/model/sql/gen"
  11. "git.i2edu.net/i2/go-zero/tools/goctl/model/sql/model"
  12. "git.i2edu.net/i2/go-zero/tools/goctl/model/sql/util"
  13. "git.i2edu.net/i2/go-zero/tools/goctl/util/console"
  14. "github.com/go-sql-driver/mysql"
  15. "github.com/urfave/cli"
  16. )
  17. const (
  18. flagSrc = "src"
  19. flagDir = "dir"
  20. flagCache = "cache"
  21. flagIdea = "idea"
  22. flagURL = "url"
  23. flagTable = "table"
  24. flagStyle = "style"
  25. )
  26. var errNotMatched = errors.New("sql not matched")
  27. // MysqlDDL generates model code from ddl
  28. func MysqlDDL(ctx *cli.Context) error {
  29. src := ctx.String(flagSrc)
  30. dir := ctx.String(flagDir)
  31. cache := ctx.Bool(flagCache)
  32. idea := ctx.Bool(flagIdea)
  33. style := ctx.String(flagStyle)
  34. cfg, err := config.NewConfig(style)
  35. if err != nil {
  36. return err
  37. }
  38. return fromDDl(src, dir, cfg, cache, idea)
  39. }
  40. // MyDataSource generates model code from datasource
  41. func MyDataSource(ctx *cli.Context) error {
  42. url := strings.TrimSpace(ctx.String(flagURL))
  43. dir := strings.TrimSpace(ctx.String(flagDir))
  44. cache := ctx.Bool(flagCache)
  45. idea := ctx.Bool(flagIdea)
  46. style := ctx.String(flagStyle)
  47. pattern := strings.TrimSpace(ctx.String(flagTable))
  48. cfg, err := config.NewConfig(style)
  49. if err != nil {
  50. return err
  51. }
  52. return fromDataSource(url, pattern, dir, cfg, cache, idea)
  53. }
  54. func fromDDl(src, dir string, cfg *config.Config, cache, idea bool) error {
  55. log := console.NewConsole(idea)
  56. src = strings.TrimSpace(src)
  57. if len(src) == 0 {
  58. return errors.New("expected path or path globbing patterns, but nothing found")
  59. }
  60. files, err := util.MatchFiles(src)
  61. if err != nil {
  62. return err
  63. }
  64. if len(files) == 0 {
  65. return errNotMatched
  66. }
  67. var source []string
  68. for _, file := range files {
  69. data, err := ioutil.ReadFile(file)
  70. if err != nil {
  71. return err
  72. }
  73. source = append(source, string(data))
  74. }
  75. generator, err := gen.NewDefaultGenerator(dir, cfg, gen.WithConsoleOption(log))
  76. if err != nil {
  77. return err
  78. }
  79. return generator.StartFromDDL(strings.Join(source, "\n"), cache)
  80. }
  81. func fromDataSource(url, pattern, dir string, cfg *config.Config, cache, idea bool) error {
  82. log := console.NewConsole(idea)
  83. if len(url) == 0 {
  84. log.Error("%v", "expected data source of mysql, but nothing found")
  85. return nil
  86. }
  87. if len(pattern) == 0 {
  88. log.Error("%v", "expected table or table globbing patterns, but nothing found")
  89. return nil
  90. }
  91. dsn, err := mysql.ParseDSN(url)
  92. if err != nil {
  93. return err
  94. }
  95. logx.Disable()
  96. databaseSource := strings.TrimSuffix(url, "/"+dsn.DBName) + "/information_schema"
  97. db := sqlx.NewMysql(databaseSource)
  98. im := model.NewInformationSchemaModel(db)
  99. tables, err := im.GetAllTables(dsn.DBName)
  100. if err != nil {
  101. return err
  102. }
  103. matchTables := make(map[string]*model.Table)
  104. for _, item := range tables {
  105. match, err := filepath.Match(pattern, item)
  106. if err != nil {
  107. return err
  108. }
  109. if !match {
  110. continue
  111. }
  112. columnData, err := im.FindColumns(dsn.DBName, item)
  113. if err != nil {
  114. return err
  115. }
  116. table, err := columnData.Convert()
  117. if err != nil {
  118. return err
  119. }
  120. matchTables[item] = table
  121. }
  122. if len(matchTables) == 0 {
  123. return errors.New("no tables matched")
  124. }
  125. generator, err := gen.NewDefaultGenerator(dir, cfg, gen.WithConsoleOption(log))
  126. if err != nil {
  127. return err
  128. }
  129. return generator.StartFromInformationSchema(matchTables, cache)
  130. }