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