2018-06-18 22:25:47 +00:00
|
|
|
// maubot - A plugin-based Matrix bot system written in Go.
|
2018-06-13 22:41:05 +00:00
|
|
|
// Copyright (C) 2018 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"
|
|
|
|
|
|
|
|
log "maunium.net/go/maulogger"
|
|
|
|
)
|
|
|
|
|
2018-06-18 22:25:47 +00:00
|
|
|
type Scannable interface {
|
|
|
|
Scan(...interface{}) error
|
|
|
|
}
|
|
|
|
|
2018-06-13 22:41:05 +00:00
|
|
|
type Database struct {
|
|
|
|
Type string `yaml:"type"`
|
|
|
|
Name string `yaml:"name"`
|
|
|
|
|
|
|
|
MatrixClient *MatrixClientStatic `yaml:"-"`
|
|
|
|
Plugin *PluginStatic `yaml:"-"`
|
2018-06-18 22:25:47 +00:00
|
|
|
CommandSpec *CommandSpecStatic `yaml:"-"`
|
2018-06-13 22:41:05 +00:00
|
|
|
|
|
|
|
sql *sql.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) Connect() (err error) {
|
|
|
|
db.sql, err = sql.Open(db.Type, db.Name)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
db.MatrixClient = &MatrixClientStatic{db: db, sql: db.sql}
|
|
|
|
db.Plugin = &PluginStatic{db: db, sql: db.sql}
|
2018-06-18 22:25:47 +00:00
|
|
|
db.CommandSpec = &CommandSpecStatic{db: db, sql: db.sql}
|
2018-06-13 22:41:05 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) CreateTables() {
|
|
|
|
log.Debugln("Creating database tables")
|
|
|
|
|
|
|
|
err := db.MatrixClient.CreateTable()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln("Failed to create matrix_client table:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = db.Plugin.CreateTable()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln("Failed to create plugin table:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) SQL() *sql.DB {
|
|
|
|
return db.sql
|
|
|
|
}
|