Guest User

Untitled

a guest
Jan 11th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. var (
  2. db *gorm.DB
  3. Config DBConfigurator
  4. getDBOnce sync.Once
  5. )
  6.  
  7. func dbConnection() string {
  8. usernameAndPassword := Config.GetUsername()
  9. if Config.GetPassword() != "" {
  10. usernameAndPassword += ":" + Config.GetPassword()
  11. }
  12. if util.IsDevAppServer() {
  13. return fmt.Sprintf("%v@unix(%v)/%v?charset=utf8mb4&parseTime=True", usernameAndPassword, Config.GetHost(), Config.GetSchema())
  14.  
  15. } else {
  16. //this is live setup
  17. return fmt.Sprintf("%v@cloudsql(%v:%v)/%v?charset=utf8mb4&parseTime=True", usernameAndPassword, Config.GetProjectId(), Config.GetInstanceName(), Config.GetSchema())
  18. }
  19. }
  20.  
  21. func DB() *gorm.DB {
  22. getDBOnce.Do(func() {
  23. var err error
  24. db, err = gorm.Open("mysql", dbConnection())
  25. if err != nil {
  26. panic("error opening DB " + err.Error())
  27. }
  28. err = db.DB().Ping()
  29. if err != nil {
  30. panic("unable to ping DB " + err.Error())
  31. }
  32. })
  33. return db
  34. }
  35.  
  36. database.DB().Where("username = ?", "sp").First(&user)
  37. database.DB().Where("username = ?", "sp").Find(&user)
Add Comment
Please, Sign In to add comment