command.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package command
  2. import (
  3. "errors"
  4. "io/ioutil"
  5. "path/filepath"
  6. "strings"
  7. "github.com/go-sql-driver/mysql"
  8. "github.com/tal-tech/go-zero/core/logx"
  9. "github.com/tal-tech/go-zero/core/stores/sqlx"
  10. "github.com/tal-tech/go-zero/tools/goctl/model/sql/gen"
  11. "github.com/tal-tech/go-zero/tools/goctl/model/sql/model"
  12. "github.com/tal-tech/go-zero/tools/goctl/model/sql/util"
  13. "github.com/tal-tech/go-zero/tools/goctl/util/console"
  14. "github.com/urfave/cli"
  15. )
  16. const (
  17. flagSrc = "src"
  18. flagDir = "dir"
  19. flagCache = "cache"
  20. flagIdea = "idea"
  21. flagUrl = "url"
  22. flagTable = "table"
  23. )
  24. func MysqlDDL(ctx *cli.Context) error {
  25. src := ctx.String(flagSrc)
  26. dir := ctx.String(flagDir)
  27. cache := ctx.Bool(flagCache)
  28. idea := ctx.Bool(flagIdea)
  29. log := console.NewConsole(idea)
  30. src = strings.TrimSpace(src)
  31. if len(src) == 0 {
  32. return errors.New("expected path or path globbing patterns, but nothing found")
  33. }
  34. files, err := util.MatchFiles(src)
  35. if err != nil {
  36. return err
  37. }
  38. var source []string
  39. for _, file := range files {
  40. data, err := ioutil.ReadFile(file)
  41. if err != nil {
  42. return err
  43. }
  44. source = append(source, string(data))
  45. }
  46. generator := gen.NewDefaultGenerator(strings.Join(source, "\n"), dir, gen.WithConsoleOption(log))
  47. err = generator.Start(cache)
  48. if err != nil {
  49. log.Error("%v", err)
  50. }
  51. return nil
  52. }
  53. func MyDataSource(ctx *cli.Context) error {
  54. url := strings.TrimSpace(ctx.String(flagUrl))
  55. dir := strings.TrimSpace(ctx.String(flagDir))
  56. cache := ctx.Bool(flagCache)
  57. idea := ctx.Bool(flagIdea)
  58. pattern := strings.TrimSpace(ctx.String(flagTable))
  59. log := console.NewConsole(idea)
  60. if len(url) == 0 {
  61. log.Error("%v", "expected data source of mysql, but nothing found")
  62. return nil
  63. }
  64. if len(pattern) == 0 {
  65. log.Error("%v", "expected table or table globbing patterns, but nothing found")
  66. return nil
  67. }
  68. cfg, err := mysql.ParseDSN(url)
  69. if err != nil {
  70. return err
  71. }
  72. logx.Disable()
  73. conn := sqlx.NewMysql(url)
  74. databaseSource := strings.TrimSuffix(url, "/"+cfg.DBName) + "/information_schema"
  75. db := sqlx.NewMysql(databaseSource)
  76. m := model.NewDDLModel(conn)
  77. im := model.NewInformationSchemaModel(db)
  78. tables, err := im.GetAllTables(cfg.DBName)
  79. if err != nil {
  80. return err
  81. }
  82. var matchTables []string
  83. for _, item := range tables {
  84. match, err := filepath.Match(pattern, item)
  85. if err != nil {
  86. return err
  87. }
  88. if !match {
  89. continue
  90. }
  91. matchTables = append(matchTables, item)
  92. }
  93. if len(matchTables) == 0 {
  94. return errors.New("no tables matched")
  95. }
  96. ddl, err := m.ShowDDL(matchTables...)
  97. if err != nil {
  98. log.Error("%v", err)
  99. return nil
  100. }
  101. generator := gen.NewDefaultGenerator(strings.Join(ddl, "\n"), dir, gen.WithConsoleOption(log))
  102. err = generator.Start(cache)
  103. if err != nil {
  104. log.Error("%v", err)
  105. }
  106. return nil
  107. }