sys_init.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package request
  2. import (
  3. "fmt"
  4. "lcfns/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. }