database.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. }
  42. func New(cfg config.DatabaseConfig, baseLog log.Logger) (*Database, error) {
  43. conn, err := sql.Open(cfg.Type, cfg.URI)
  44. if err != nil {
  45. return nil, err
  46. }
  47. db := &Database{
  48. DB: conn,
  49. log: baseLog.Sub("Database"),
  50. dialect: cfg.Type,
  51. }
  52. db.User = &UserQuery{
  53. db: db,
  54. log: db.log.Sub("User"),
  55. }
  56. db.Portal = &PortalQuery{
  57. db: db,
  58. log: db.log.Sub("Portal"),
  59. }
  60. db.Puppet = &PuppetQuery{
  61. db: db,
  62. log: db.log.Sub("Puppet"),
  63. }
  64. db.Message = &MessageQuery{
  65. db: db,
  66. log: db.log.Sub("Message"),
  67. }
  68. db.Reaction = &ReactionQuery{
  69. db: db,
  70. log: db.log.Sub("Reaction"),
  71. }
  72. db.DisappearingMessage = &DisappearingMessageQuery{
  73. db: db,
  74. log: db.log.Sub("DisappearingMessage"),
  75. }
  76. db.SetMaxOpenConns(cfg.MaxOpenConns)
  77. db.SetMaxIdleConns(cfg.MaxIdleConns)
  78. if len(cfg.ConnMaxIdleTime) > 0 {
  79. maxIdleTimeDuration, err := time.ParseDuration(cfg.ConnMaxIdleTime)
  80. if err != nil {
  81. return nil, fmt.Errorf("failed to parse max_conn_idle_time: %w", err)
  82. }
  83. db.SetConnMaxIdleTime(maxIdleTimeDuration)
  84. }
  85. if len(cfg.ConnMaxLifetime) > 0 {
  86. maxLifetimeDuration, err := time.ParseDuration(cfg.ConnMaxLifetime)
  87. if err != nil {
  88. return nil, fmt.Errorf("failed to parse max_conn_idle_time: %w", err)
  89. }
  90. db.SetConnMaxLifetime(maxLifetimeDuration)
  91. }
  92. return db, nil
  93. }
  94. func (db *Database) Init() error {
  95. return upgrades.Run(db.log.Sub("Upgrade"), db.dialect, db.DB)
  96. }
  97. type Scannable interface {
  98. Scan(...interface{}) error
  99. }