command.go 3.3 KB

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