2018-08-13 22:24:44 +02:00
|
|
|
// mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
|
2022-02-23 13:30:21 +01:00
|
|
|
// Copyright (C) 2022 Tulir Asokan
|
2018-08-13 22:24:44 +02:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2022-02-23 13:30:21 +01:00
|
|
|
"fmt"
|
|
|
|
"time"
|
2018-08-26 21:53:13 +02:00
|
|
|
|
2021-10-26 16:01:10 +02:00
|
|
|
"github.com/lib/pq"
|
2019-03-06 22:57:38 +01:00
|
|
|
_ "github.com/mattn/go-sqlite3"
|
2022-03-16 00:15:13 +01:00
|
|
|
"go.mau.fi/whatsmeow/store/sqlstore"
|
2019-01-11 20:17:31 +01:00
|
|
|
log "maunium.net/go/maulogger/v2"
|
2019-05-16 19:14:32 +02:00
|
|
|
|
2022-02-23 13:30:21 +01:00
|
|
|
"maunium.net/go/mautrix-whatsapp/config"
|
2019-05-16 19:14:32 +02:00
|
|
|
"maunium.net/go/mautrix-whatsapp/database/upgrades"
|
2018-08-13 22:24:44 +02:00
|
|
|
)
|
|
|
|
|
2021-10-26 16:01:10 +02:00
|
|
|
func init() {
|
|
|
|
sqlstore.PostgresArrayWrapper = pq.Array
|
|
|
|
}
|
|
|
|
|
2018-08-13 22:24:44 +02:00
|
|
|
type Database struct {
|
|
|
|
*sql.DB
|
2019-08-25 15:29:35 +02:00
|
|
|
log log.Logger
|
|
|
|
dialect string
|
2018-08-13 22:24:44 +02:00
|
|
|
|
2022-03-05 20:22:31 +01:00
|
|
|
User *UserQuery
|
|
|
|
Portal *PortalQuery
|
|
|
|
Puppet *PuppetQuery
|
|
|
|
Message *MessageQuery
|
|
|
|
Reaction *ReactionQuery
|
2022-01-07 13:32:00 +01:00
|
|
|
|
|
|
|
DisappearingMessage *DisappearingMessageQuery
|
2018-08-13 22:24:44 +02:00
|
|
|
}
|
|
|
|
|
2022-02-23 13:30:21 +01:00
|
|
|
func New(cfg config.DatabaseConfig, baseLog log.Logger) (*Database, error) {
|
|
|
|
conn, err := sql.Open(cfg.Type, cfg.URI)
|
2018-08-13 22:24:44 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
db := &Database{
|
2019-08-25 15:29:35 +02:00
|
|
|
DB: conn,
|
2021-01-25 20:01:54 +01:00
|
|
|
log: baseLog.Sub("Database"),
|
2022-02-23 13:30:21 +01:00
|
|
|
dialect: cfg.Type,
|
2018-08-13 22:24:44 +02:00
|
|
|
}
|
|
|
|
db.User = &UserQuery{
|
|
|
|
db: db,
|
2018-08-16 18:20:07 +02:00
|
|
|
log: db.log.Sub("User"),
|
2018-08-13 22:24:44 +02:00
|
|
|
}
|
2018-08-16 14:59:18 +02:00
|
|
|
db.Portal = &PortalQuery{
|
|
|
|
db: db,
|
2018-08-16 18:20:07 +02:00
|
|
|
log: db.log.Sub("Portal"),
|
2018-08-16 14:59:18 +02:00
|
|
|
}
|
|
|
|
db.Puppet = &PuppetQuery{
|
|
|
|
db: db,
|
2018-08-16 18:20:07 +02:00
|
|
|
log: db.log.Sub("Puppet"),
|
2018-08-16 14:59:18 +02:00
|
|
|
}
|
2018-08-23 00:12:26 +02:00
|
|
|
db.Message = &MessageQuery{
|
|
|
|
db: db,
|
|
|
|
log: db.log.Sub("Message"),
|
|
|
|
}
|
2022-03-05 20:22:31 +01:00
|
|
|
db.Reaction = &ReactionQuery{
|
|
|
|
db: db,
|
|
|
|
log: db.log.Sub("Reaction"),
|
|
|
|
}
|
2022-01-07 13:32:00 +01:00
|
|
|
db.DisappearingMessage = &DisappearingMessageQuery{
|
|
|
|
db: db,
|
|
|
|
log: db.log.Sub("DisappearingMessage"),
|
|
|
|
}
|
2022-02-23 13:30:21 +01:00
|
|
|
|
|
|
|
db.SetMaxOpenConns(cfg.MaxOpenConns)
|
|
|
|
db.SetMaxIdleConns(cfg.MaxIdleConns)
|
|
|
|
if len(cfg.ConnMaxIdleTime) > 0 {
|
|
|
|
maxIdleTimeDuration, err := time.ParseDuration(cfg.ConnMaxIdleTime)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to parse max_conn_idle_time: %w", err)
|
|
|
|
}
|
|
|
|
db.SetConnMaxIdleTime(maxIdleTimeDuration)
|
|
|
|
}
|
|
|
|
if len(cfg.ConnMaxLifetime) > 0 {
|
|
|
|
maxLifetimeDuration, err := time.ParseDuration(cfg.ConnMaxLifetime)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to parse max_conn_idle_time: %w", err)
|
|
|
|
}
|
|
|
|
db.SetConnMaxLifetime(maxLifetimeDuration)
|
|
|
|
}
|
2018-08-13 22:24:44 +02:00
|
|
|
return db, nil
|
|
|
|
}
|
|
|
|
|
2019-08-25 15:29:35 +02:00
|
|
|
func (db *Database) Init() error {
|
|
|
|
return upgrades.Run(db.log.Sub("Upgrade"), db.dialect, db.DB)
|
2018-08-16 18:20:07 +02:00
|
|
|
}
|
|
|
|
|
2018-08-13 22:24:44 +02:00
|
|
|
type Scannable interface {
|
|
|
|
Scan(...interface{}) error
|
|
|
|
}
|