common.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. &User{},
  23. &Dict{},
  24. &LampPoleGroup{},
  25. &LampPole{},
  26. &Gateway{},
  27. &GatewayRelation{},
  28. &Transformer{},
  29. &LightControl{},
  30. &Garbage{},
  31. &GarbageWayGroup{},
  32. &OnDemandGroup{},
  33. &OnDemandSensor{},
  34. &Zigbee{},
  35. &CameraDevice{},
  36. &InfoBoard{},
  37. &Alarm{},
  38. &CaptureUint{},
  39. &CheckPoint{},
  40. &AlarmTerminal{},
  41. &Bridge{},
  42. &BridgeSensor{},
  43. &IpBroadcast{},
  44. &ManholeCover{},
  45. ).Error
  46. if err != nil {
  47. panic(fmt.Sprintf("AutoMigrate err : %v", err))
  48. }
  49. }
  50. }