common.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package dao
  2. import (
  3. "fmt"
  4. _ "github.com/go-sql-driver/mysql"
  5. "github.com/jinzhu/gorm"
  6. "iot_manager_service/config"
  7. "net/url"
  8. )
  9. var GDb *gorm.DB
  10. func InitDB() {
  11. cfg := config.Instance()
  12. dsn := cfg.Database.User + ":" + cfg.Database.Password + "@tcp(" + cfg.Database.Host + ":" + cfg.Database.Port + ")/" + cfg.Database.Name + "?charset=utf8&parseTime=True" + "&loc=" + url.QueryEscape(cfg.Database.Timezone)
  13. db0, err := gorm.Open("mysql", dsn)
  14. if err != nil {
  15. panic(err)
  16. } else {
  17. GDb = db0
  18. GDb.DB().SetMaxOpenConns(32)
  19. GDb.DB().SetMaxIdleConns(5)
  20. GDb.LogMode(false)
  21. err = GDb.AutoMigrate(
  22. &LampPoleGroup{},
  23. &LampPole{},
  24. &Gateway{},
  25. &GatewayRelation{},
  26. &Transformer{},
  27. &LightControl{},
  28. &Garbage{},
  29. &GarbageWayGroup{},
  30. &OnDemandGroup{},
  31. &OnDemandSensor{},
  32. &Zigbee{},
  33. &CameraDevice{},
  34. &InfoBoard{},
  35. &Alarm{},
  36. &CaptureUint{},
  37. &CheckPoint{},
  38. &AlarmTerminal{},
  39. &Bridge{},
  40. &BridgeSensor{},
  41. ).Error
  42. if err != nil {
  43. panic(fmt.Sprintf("AutoMigrate err : %v", err))
  44. }
  45. }
  46. }