2020-06-30 10:37:21 +01:00
|
|
|
package sqlite3
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/currentstateserver/storage/shared"
|
2020-08-10 14:18:04 +01:00
|
|
|
"github.com/matrix-org/dendrite/internal/config"
|
2020-06-30 10:37:21 +01:00
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Database struct {
|
|
|
|
shared.Database
|
2020-08-21 10:42:08 +01:00
|
|
|
db *sql.DB
|
|
|
|
writer sqlutil.Writer
|
2020-06-30 10:37:21 +01:00
|
|
|
sqlutil.PartitionOffsetStatements
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDatabase creates a new sync server database
|
|
|
|
// nolint: gocyclo
|
2020-08-10 14:18:04 +01:00
|
|
|
func NewDatabase(dbProperties *config.DatabaseOptions) (*Database, error) {
|
2020-06-30 10:37:21 +01:00
|
|
|
var d Database
|
2020-08-10 14:18:04 +01:00
|
|
|
var err error
|
|
|
|
if d.db, err = sqlutil.Open(dbProperties); err != nil {
|
2020-06-30 10:37:21 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-08-21 10:42:08 +01:00
|
|
|
d.writer = sqlutil.NewExclusiveWriter()
|
|
|
|
if err = d.PartitionOffsetStatements.Prepare(d.db, d.writer, "currentstate"); err != nil {
|
2020-06-30 10:37:21 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
currRoomState, err := NewSqliteCurrentRoomStateTable(d.db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
d.Database = shared.Database{
|
|
|
|
DB: d.db,
|
2020-08-21 10:42:08 +01:00
|
|
|
Writer: d.writer,
|
2020-06-30 10:37:21 +01:00
|
|
|
CurrentRoomState: currRoomState,
|
|
|
|
}
|
|
|
|
return &d, nil
|
|
|
|
}
|