database.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
  2. // Copyright (C) 2022 Tulir Asokan
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package database
  17. import (
  18. "database/sql"
  19. "fmt"
  20. "time"
  21. "github.com/lib/pq"
  22. _ "github.com/mattn/go-sqlite3"
  23. "go.mau.fi/whatsmeow/store/sqlstore"
  24. log "maunium.net/go/maulogger/v2"
  25. "maunium.net/go/mautrix-whatsapp/config"
  26. "maunium.net/go/mautrix-whatsapp/database/upgrades"
  27. )
  28. func init() {
  29. sqlstore.PostgresArrayWrapper = pq.Array
  30. }
  31. type Database struct {
  32. *sql.DB
  33. log log.Logger
  34. dialect string
  35. User *UserQuery
  36. Portal *PortalQuery
  37. Puppet *PuppetQuery
  38. Message *MessageQuery
  39. Reaction *ReactionQuery
  40. DisappearingMessage *DisappearingMessageQuery
  41. BackfillQuery *BackfillQuery
  42. HistorySyncQuery *HistorySyncQuery
  43. }
  44. func New(cfg config.DatabaseConfig, baseLog log.Logger) (*Database, error) {
  45. conn, err := sql.Open(cfg.Type, cfg.URI)
  46. if err != nil {
  47. return nil, err
  48. }
  49. db := &Database{
  50. DB: conn,
  51. log: baseLog.Sub("Database"),
  52. dialect: cfg.Type,
  53. }
  54. db.User = &UserQuery{
  55. db: db,
  56. log: db.log.Sub("User"),
  57. }
  58. db.Portal = &PortalQuery{
  59. db: db,
  60. log: db.log.Sub("Portal"),
  61. }
  62. db.Puppet = &PuppetQuery{
  63. db: db,
  64. log: db.log.Sub("Puppet"),
  65. }
  66. db.Message = &MessageQuery{
  67. db: db,
  68. log: db.log.Sub("Message"),
  69. }
  70. db.Reaction = &ReactionQuery{
  71. db: db,
  72. log: db.log.Sub("Reaction"),
  73. }
  74. db.DisappearingMessage = &DisappearingMessageQuery{
  75. db: db,
  76. log: db.log.Sub("DisappearingMessage"),
  77. }
  78. db.BackfillQuery = &BackfillQuery{
  79. db: db,
  80. log: db.log.Sub("Backfill"),
  81. }
  82. db.HistorySyncQuery = &HistorySyncQuery{
  83. db: db,
  84. log: db.log.Sub("HistorySync"),
  85. }
  86. db.SetMaxOpenConns(cfg.MaxOpenConns)
  87. db.SetMaxIdleConns(cfg.MaxIdleConns)
  88. if len(cfg.ConnMaxIdleTime) > 0 {
  89. maxIdleTimeDuration, err := time.ParseDuration(cfg.ConnMaxIdleTime)
  90. if err != nil {
  91. return nil, fmt.Errorf("failed to parse max_conn_idle_time: %w", err)
  92. }
  93. db.SetConnMaxIdleTime(maxIdleTimeDuration)
  94. }
  95. if len(cfg.ConnMaxLifetime) > 0 {
  96. maxLifetimeDuration, err := time.ParseDuration(cfg.ConnMaxLifetime)
  97. if err != nil {
  98. return nil, fmt.Errorf("failed to parse max_conn_idle_time: %w", err)
  99. }
  100. db.SetConnMaxLifetime(maxLifetimeDuration)
  101. }
  102. return db, nil
  103. }
  104. func (db *Database) Init() error {
  105. return upgrades.Run(db.log.Sub("Upgrade"), db.dialect, db.DB)
  106. }
  107. type Scannable interface {
  108. Scan(...interface{}) error
  109. }