Matrix/utils/database/databaseConnector.go

165 lines
2.7 KiB
Go
Raw Normal View History

2020-09-30 08:28:49 +00:00
package database
2020-09-28 20:37:02 +00:00
import (
"database/sql"
2020-10-01 15:45:57 +00:00
"fmt"
2020-09-28 20:37:02 +00:00
_ "github.com/mattn/go-sqlite3"
)
var DB *sql.DB
func InitDB(filepath string) (err error) {
DB, err = sql.Open("sqlite3", filepath)
2020-09-28 20:37:02 +00:00
if err != nil {
panic(err)
}
if DB == nil {
2020-09-29 07:44:35 +00:00
panic("DB couldn't be initialized")
2020-09-28 20:37:02 +00:00
}
2020-09-29 07:44:35 +00:00
handleError(initDeviceTable())
handleError(initKeyTable())
handleError(initEventTable())
handleError(initRoomTable())
handleError(initTransactionTable())
handleError(initUserTable())
return
}
func initDeviceTable() (err error) {
statement, err := DB.Prepare(`CREATE TABLE IF NOT EXISTS device (
id TEXT PRIMARY KEY,
2020-10-11 21:11:30 +00:00
name TEXT,
accessToken TEXT,
userId TEXT
)`)
if err != nil {
return
}
statement.Exec()
return
}
func initKeyTable() (err error) {
statement, err := DB.Prepare(`CREATE TABLE IF NOT EXISTS key (
id TEXT PRIMARY KEY,
type TEXT,
key TEXT,
deviceId TEXT
)`)
if err != nil {
return
}
statement.Exec()
2020-10-17 10:07:39 +00:00
return
}
func initEventTable() (err error) {
statement, err := DB.Prepare(`CREATE TABLE IF NOT EXISTS event (
id TEXT PRIMARY KEY,
roomId TEXT,
txnId TEXT,
2020-10-11 21:11:30 +00:00
sender TEXT,
origin TEXT,
timestamp INTEGER,
eventType TEXT,
2020-10-11 21:11:30 +00:00
stateKey TEXT,
content TEXT,
depth INTEGER,
hash TEXT,
signature TEXT
)`)
if err != nil {
return
}
statement.Exec()
statement, err = DB.Prepare(`CREATE TABLE IF NOT EXISTS parent (
eventId TEXT,
parentId TEXT,
PRIMARY KEY (eventId, parentId)
)`)
if err != nil {
return
}
statement.Exec()
statement, err = DB.Prepare(`CREATE TABLE IF NOT EXISTS authEvent (
eventId TEXT,
authEventId TEXT,
PRIMARY KEY (eventId, authEventId)
)`)
if err != nil {
return
}
statement.Exec()
2020-10-17 10:07:39 +00:00
return
}
func initRoomTable() (err error) {
statement, err := DB.Prepare(`CREATE TABLE IF NOT EXISTS room (
id TEXT PRIMARY KEY,
2020-10-11 21:11:30 +00:00
version TEXT,
visibility TEXT,
name TEXT,
topic TEXT,
isDirect INT,
federated INT
)`)
if err != nil {
return
}
statement.Exec()
statement, err = DB.Prepare(`CREATE TABLE IF NOT EXISTS roomMember (
userId TEXT,
roomId TEXT,
2020-10-11 21:11:30 +00:00
server TEXT,
PRIMARY KEY (userId, roomId)
)`)
if err != nil {
return
}
statement.Exec()
2020-10-17 10:07:39 +00:00
return
}
func initTransactionTable() (err error) {
statement, err := DB.Prepare(`CREATE TABLE IF NOT EXISTS txn (
id TEXT PRIMARY KEY,
origin TEXT,
timestamp INTEGER
)`)
if err != nil {
return
}
statement.Exec()
return
}
func initUserTable() (err error) {
statement, err := DB.Prepare(`CREATE TABLE IF NOT EXISTS user (
id TEXT PRIMARY KEY,
name TEXT,
password TEXT
)`)
if err != nil {
return
}
statement.Exec()
2020-10-01 15:45:57 +00:00
return
2020-09-28 20:37:02 +00:00
}
2020-09-29 07:44:35 +00:00
2020-09-30 08:28:49 +00:00
func handleError(err error) {
if err != nil {
2020-10-01 15:45:57 +00:00
panic(fmt.Sprintf("Could not execute Database Query: %s", err))
2020-09-30 08:28:49 +00:00
}
}