command.go 3.6 KB

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