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