You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.8 KiB

3 years ago
  1. package request
  2. import (
  3. "fmt"
  4. "autocode/config"
  5. )
  6. type InitDB struct {
  7. DBType string `json:"dbType"` // 数据库类型
  8. Host string `json:"host"` // 服务器地址
  9. Port string `json:"port"` // 数据库连接端口
  10. UserName string `json:"userName" binding:"required"` // 数据库用户名
  11. Password string `json:"password"` // 数据库密码
  12. DBName string `json:"dbName" binding:"required"` // 数据库名
  13. }
  14. // MysqlEmptyDsn msyql 空数据库 建库链接
  15. // Author SliverHorn
  16. func (i *InitDB) MysqlEmptyDsn() string {
  17. if i.Host == "" {
  18. i.Host = "127.0.0.1"
  19. }
  20. if i.Port == "" {
  21. i.Port = "3306"
  22. }
  23. return fmt.Sprintf("%s:%s@tcp(%s:%s)/", i.UserName, i.Password, i.Host, i.Port)
  24. }
  25. // PgsqlEmptyDsn pgsql 空数据库 建库链接
  26. // Author SliverHorn
  27. func (i *InitDB) PgsqlEmptyDsn() string {
  28. if i.Host == "" {
  29. i.Host = "127.0.0.1"
  30. }
  31. if i.Port == "" {
  32. i.Port = "3306"
  33. }
  34. return "host=" + i.Host + " user=" + i.UserName + " password=" + i.Password + " port=" + i.Port + " " + "sslmode=disable TimeZone=Asia/Shanghai"
  35. }
  36. // ToMysqlConfig 转换 config.Mysql
  37. // Author [SliverHorn](https://github.com/SliverHorn)
  38. func (i *InitDB) ToMysqlConfig() config.Mysql {
  39. return config.Mysql{
  40. Path: i.Host,
  41. Port: i.Port,
  42. Dbname: i.DBName,
  43. Username: i.UserName,
  44. Password: i.Password,
  45. Config: "charset=utf8mb4&parseTime=True&loc=Local",
  46. }
  47. }
  48. // ToPgsqlConfig 转换 config.Pgsql
  49. // Author [SliverHorn](https://github.com/SliverHorn)
  50. func (i *InitDB) ToPgsqlConfig() config.Pgsql {
  51. return config.Pgsql{
  52. Path: i.Host,
  53. Port: i.Port,
  54. Dbname: i.DBName,
  55. Username: i.UserName,
  56. Password: i.Password,
  57. Config: "sslmode=disable TimeZone=Asia/Shanghai",
  58. }
  59. }