core.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package orm
  2. import (
  3. "fmt"
  4. "gitea.ckfah.com/go-script/logger"
  5. "github.com/go-xorm/xorm"
  6. "os"
  7. "strings"
  8. "sync"
  9. "text/template"
  10. )
  11. type DbType string
  12. // type
  13. const (
  14. Mysql = DbType("mysql")
  15. )
  16. // template name
  17. const (
  18. daoTemplateName = "daoTemplateName"
  19. dtoTemplateName = "dtoTemplateName"
  20. modelTemplateName = "modelTemplateName"
  21. )
  22. // other
  23. const (
  24. sep = string(os.PathSeparator)
  25. fileSuffix = ".go"
  26. )
  27. type Template interface {
  28. Run(template *template.Template) ([]byte, error)
  29. }
  30. type DbMeta interface {
  31. GetTables() ([]string, error)
  32. }
  33. type Config struct {
  34. wg sync.WaitGroup
  35. daoTemplate *template.Template
  36. modelTemplate *template.Template
  37. dtoTemplate *template.Template
  38. // db
  39. DbType DbType
  40. DbName string
  41. DbPort int
  42. DbHost string
  43. DbUserName string
  44. DbPassword string
  45. DbCharset string
  46. // cnn
  47. engine *xorm.Engine
  48. // save
  49. SaveFile string
  50. // table
  51. TableNames []string
  52. // template
  53. GeneratorModel bool
  54. GeneratorDao bool
  55. GeneratorDto bool
  56. DaoPackageName string
  57. ModelPackageName string
  58. DtoPackageName string
  59. Tags []string
  60. }
  61. func FillDNS(userName string, password string, host string, port int, dbName string, charset string) string {
  62. return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=True&loc=Local",
  63. userName,
  64. password,
  65. host,
  66. port,
  67. dbName,
  68. charset,
  69. )
  70. }
  71. func GetDbType(str string) DbType {
  72. lower := strings.ToLower(str)
  73. switch DbType(lower) {
  74. case Mysql:
  75. return Mysql
  76. default:
  77. logger.FatalF("not support type")
  78. return Mysql
  79. }
  80. }