database.go 2.6 KB

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