connection.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package database
  2. import (
  3. "fmt"
  4. "gorm.io/gorm"
  5. "gorm.io/driver/mysql"
  6. log "gogs.veloe.link/toomanysugar/GoEthemineTelegramBot/internal/logger"
  7. "gogs.veloe.link/toomanysugar/GoEthemineTelegramBot/cmd/settings"
  8. )
  9. type ConnectionProperties struct {
  10. User string
  11. Pass string
  12. Host string
  13. Db string
  14. }
  15. type Connection struct {
  16. GormDb *gorm.DB
  17. DbOpenStr string
  18. ConnProperties ConnectionProperties
  19. }
  20. func (c *Connection) Init() {
  21. c.ConnProperties.User = settings.DbUser()
  22. c.ConnProperties.Pass = settings.DbPassword()
  23. c.ConnProperties.Host = settings.DbHost()
  24. c.ConnProperties.Db = settings.DbName()
  25. c.DbOpenStr = fmt.Sprintf("%s:%s@tcp(%s)/%s",
  26. c.ConnProperties.User, c.ConnProperties.Pass, c.ConnProperties.Host, c.ConnProperties.Db)
  27. log.LogInfo.Println("Connecting with:")
  28. log.LogInfo.Println(c.DbOpenStr)
  29. gormDb, err := gorm.Open(mysql.Open(c.DbOpenStr), &gorm.Config{})
  30. if err != nil {
  31. log.LogError.Println("failed to connect database")
  32. log.LogError.Println(err)
  33. } else {
  34. log.LogInfo.Println("Connected to database using gorm")
  35. }
  36. c.GormDb = gormDb
  37. }
  38. func (c *Connection) LatestWorkerRecord(worker string) (lastWorkerRecord Worker, err error) {
  39. result := c.GormDb.
  40. Where("worker = ?", worker).
  41. Order("time desc").
  42. Limit(1).
  43. Find(&lastWorkerRecord)
  44. return lastWorkerRecord, result.Error
  45. }
  46. func (c *Connection) LatestMinerRecord(wallet string) (lastMinerRecord Miner, err error) {
  47. result := c.GormDb.
  48. Where("wallet = ?", wallet).
  49. Order("time desc").
  50. Limit(1).
  51. Find(&lastMinerRecord)
  52. return lastMinerRecord, result.Error
  53. }
  54. func (c *Connection) ChatsWithWallet(wallet string) (ChatIds []int64, err error) {
  55. result := c.GormDb.Model(&User{}).
  56. Where("wallet = ?", wallet).
  57. Select("DISTINCT(chat_id)").
  58. Scan(&ChatIds)
  59. return ChatIds, result.Error
  60. }
  61. func (c *Connection) IsWorkerRowExists(worker string, time int64) (exists bool, err error) {
  62. var rowsCount int64
  63. result := c.GormDb.Model(&Worker{}).
  64. Where("worker = ? and time = ?", worker, time).
  65. Count(&rowsCount)
  66. if result.Error != nil {
  67. return false , result.Error
  68. }
  69. if rowsCount != 0 {
  70. return true, nil
  71. }
  72. return false, nil
  73. }
  74. // List of unique, not empty wallets in user table
  75. func (c *Connection) UniqueWallets() (wallets []string, err error) {
  76. result := c.GormDb.Model(&User{}).
  77. Not("wallet = ?", "").
  78. Select("DISTINCT(wallet)").
  79. Scan(&wallets)
  80. return wallets, result.Error
  81. }