mautrix-whatsapp/database/database.go

90 lines
1.9 KiB
Go
Raw Normal View History

// mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
// Copyright (C) 2019 Tulir Asokan
//
// 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"
2018-08-26 21:53:13 +02:00
2019-03-06 10:59:52 +01:00
_ "github.com/lib/pq"
2019-03-06 22:57:38 +01:00
_ "github.com/mattn/go-sqlite3"
2019-01-11 20:17:31 +01:00
log "maunium.net/go/maulogger/v2"
)
type Database struct {
*sql.DB
2018-08-16 18:20:07 +02:00
log log.Logger
User *UserQuery
Portal *PortalQuery
Puppet *PuppetQuery
Message *MessageQuery
}
2019-03-06 18:29:15 +01:00
func New(dbType string, uri string) (*Database, error) {
conn, err := sql.Open(dbType, uri)
if err != nil {
return nil, err
}
db := &Database{
DB: conn,
2018-08-16 18:20:07 +02:00
log: log.Sub("Database"),
}
db.User = &UserQuery{
db: db,
2018-08-16 18:20:07 +02:00
log: db.log.Sub("User"),
}
db.Portal = &PortalQuery{
db: db,
2018-08-16 18:20:07 +02:00
log: db.log.Sub("Portal"),
}
db.Puppet = &PuppetQuery{
db: db,
2018-08-16 18:20:07 +02:00
log: db.log.Sub("Puppet"),
}
db.Message = &MessageQuery{
db: db,
log: db.log.Sub("Message"),
}
return db, nil
}
2019-03-06 22:22:12 +01:00
func (db *Database) CreateTables(dbType string) error {
err := db.User.CreateTable(dbType)
if err != nil {
return err
}
2019-03-06 22:22:12 +01:00
err = db.Portal.CreateTable(dbType)
if err != nil {
return err
}
2019-03-06 22:22:12 +01:00
err = db.Puppet.CreateTable(dbType)
if err != nil {
return err
}
2019-03-06 22:22:12 +01:00
err = db.Message.CreateTable(dbType)
if err != nil {
return err
}
return nil
2018-08-16 18:20:07 +02:00
}
type Scannable interface {
Scan(...interface{}) error
}