mysql.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package dao
  2. import (
  3. "context"
  4. "errors"
  5. "go-common/app/service/main/antispam/conf"
  6. "go-common/library/database/sql"
  7. "go-common/library/log"
  8. )
  9. const (
  10. // AreaReply .
  11. AreaReply int = iota + 1
  12. // AreaIMessage .
  13. AreaIMessage
  14. // AreaLiveDM .
  15. AreaLiveDM
  16. // AreaMainSiteDM .
  17. AreaMainSiteDM
  18. )
  19. const (
  20. // StateDefault .
  21. StateDefault int = iota
  22. // StateDeleted .
  23. StateDeleted
  24. )
  25. var (
  26. // ErrPingDao .
  27. ErrPingDao = errors.New("Ping dao error")
  28. // ErrResourceNotExist .
  29. ErrResourceNotExist = errors.New("Resource Not Exist")
  30. // ErrParams .
  31. ErrParams = errors.New("wrong params")
  32. )
  33. // GetTotalCounts .
  34. func GetTotalCounts(ctx context.Context, q Querier, selectCountsSQL string) (int64, error) {
  35. var totalCounts int64
  36. if err := q.QueryRow(ctx, selectCountsSQL).Scan(&totalCounts); err != nil {
  37. log.Error("Error: %v, sql: %s", err, selectCountsSQL)
  38. return 0, err
  39. }
  40. log.Info("GetTotalCounts query sql: %s", selectCountsSQL)
  41. return totalCounts, nil
  42. }
  43. // PingMySQL .
  44. func PingMySQL(ctx context.Context) error {
  45. if db != nil {
  46. if err := db.Ping(ctx); err != nil {
  47. log.Error("%v", err)
  48. return err
  49. }
  50. }
  51. return nil
  52. }
  53. // Close .
  54. func Close() {
  55. if db != nil {
  56. db.Close()
  57. }
  58. }
  59. // Init .
  60. func Init(conf *conf.Config) (ok bool) {
  61. if db == nil {
  62. db = sql.NewMySQL(conf.MySQL.AntiSpam)
  63. }
  64. return db != nil
  65. }
  66. var db *sql.DB