sys_init.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package request
  2. import (
  3. "fmt"
  4. "github.com/flipped-aurora/gin-vue-admin/server/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"` // 数据库用户名
  11. Password string `json:"password"` // 数据库密码
  12. DBName string `json:"dbName" binding:"required"` // 数据库名
  13. DBPath string `json:"dbPath"` // sqlite数据库文件路径
  14. }
  15. // MysqlEmptyDsn msyql 空数据库 建库链接
  16. // Author SliverHorn
  17. func (i *InitDB) MysqlEmptyDsn() string {
  18. if i.Host == "" {
  19. i.Host = "127.0.0.1"
  20. }
  21. if i.Port == "" {
  22. i.Port = "3306"
  23. }
  24. return fmt.Sprintf("%s:%s@tcp(%s:%s)/", i.UserName, i.Password, i.Host, i.Port)
  25. }
  26. // PgsqlEmptyDsn pgsql 空数据库 建库链接
  27. // Author SliverHorn
  28. func (i *InitDB) PgsqlEmptyDsn() string {
  29. if i.Host == "" {
  30. i.Host = "127.0.0.1"
  31. }
  32. if i.Port == "" {
  33. i.Port = "5432"
  34. }
  35. return "host=" + i.Host + " user=" + i.UserName + " password=" + i.Password + " port=" + i.Port + " dbname=" + "postgres" + " " + "sslmode=disable TimeZone=Asia/Shanghai"
  36. }
  37. // SqliteEmptyDsn sqlite 空数据库 建库链接
  38. // Author Kafumio
  39. func (i *InitDB) SqliteEmptyDsn() string {
  40. return i.DBPath + "\\" + i.DBName + ".db"
  41. }
  42. // ToMysqlConfig 转换 config.Mysql
  43. // Author [SliverHorn](https://github.com/SliverHorn)
  44. func (i *InitDB) ToMysqlConfig() config.Mysql {
  45. return config.Mysql{
  46. GeneralDB: config.GeneralDB{
  47. Path: i.Host,
  48. Port: i.Port,
  49. Dbname: i.DBName,
  50. Username: i.UserName,
  51. Password: i.Password,
  52. MaxIdleConns: 10,
  53. MaxOpenConns: 100,
  54. LogMode: "error",
  55. Config: "charset=utf8mb4&parseTime=True&loc=Local",
  56. },
  57. }
  58. }
  59. // ToPgsqlConfig 转换 config.Pgsql
  60. // Author [SliverHorn](https://github.com/SliverHorn)
  61. func (i *InitDB) ToPgsqlConfig() config.Pgsql {
  62. return config.Pgsql{
  63. GeneralDB: config.GeneralDB{
  64. Path: i.Host,
  65. Port: i.Port,
  66. Dbname: i.DBName,
  67. Username: i.UserName,
  68. Password: i.Password,
  69. MaxIdleConns: 10,
  70. MaxOpenConns: 100,
  71. LogMode: "error",
  72. Config: "sslmode=disable TimeZone=Asia/Shanghai",
  73. },
  74. }
  75. }
  76. // ToSqliteConfig 转换 config.Sqlite
  77. // Author [Kafumio](https://github.com/Kafumio)
  78. func (i *InitDB) ToSqliteConfig() config.Sqlite {
  79. return config.Sqlite{
  80. GeneralDB: config.GeneralDB{
  81. Path: i.DBPath,
  82. Port: i.Port,
  83. Dbname: i.DBName,
  84. Username: i.UserName,
  85. Password: i.Password,
  86. MaxIdleConns: 10,
  87. MaxOpenConns: 100,
  88. LogMode: "error",
  89. Config: "",
  90. },
  91. }
  92. }