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