mirror of
https://github.com/hoernschen/dendrite.git
synced 2025-08-02 06:12:45 +00:00
Convert transactions/rooms table to share more code (#1063)
* Convert rooms table * Convert transactions table * Convert rooms table and factor out lots of functions * I think you'll be needing this..
This commit is contained in:
parent
803af87dc4
commit
19aa44ecae
12 changed files with 409 additions and 600 deletions
|
@ -22,6 +22,7 @@ import (
|
|||
|
||||
"github.com/lib/pq"
|
||||
"github.com/matrix-org/dendrite/internal"
|
||||
"github.com/matrix-org/dendrite/roomserver/storage/tables"
|
||||
"github.com/matrix-org/dendrite/roomserver/types"
|
||||
"github.com/matrix-org/gomatrixserverlib"
|
||||
)
|
||||
|
@ -82,12 +83,13 @@ type roomStatements struct {
|
|||
selectRoomVersionForRoomNIDStmt *sql.Stmt
|
||||
}
|
||||
|
||||
func (s *roomStatements) prepare(db *sql.DB) (err error) {
|
||||
_, err = db.Exec(roomsSchema)
|
||||
func NewPostgresRoomsTable(db *sql.DB) (tables.Rooms, error) {
|
||||
s := &roomStatements{}
|
||||
_, err := db.Exec(roomsSchema)
|
||||
if err != nil {
|
||||
return
|
||||
return nil, err
|
||||
}
|
||||
return statementList{
|
||||
return s, statementList{
|
||||
{&s.insertRoomNIDStmt, insertRoomNIDSQL},
|
||||
{&s.selectRoomNIDStmt, selectRoomNIDSQL},
|
||||
{&s.selectLatestEventNIDsStmt, selectLatestEventNIDsSQL},
|
||||
|
@ -98,7 +100,7 @@ func (s *roomStatements) prepare(db *sql.DB) (err error) {
|
|||
}.prepare(db)
|
||||
}
|
||||
|
||||
func (s *roomStatements) insertRoomNID(
|
||||
func (s *roomStatements) InsertRoomNID(
|
||||
ctx context.Context, txn *sql.Tx,
|
||||
roomID string, roomVersion gomatrixserverlib.RoomVersion,
|
||||
) (types.RoomNID, error) {
|
||||
|
@ -108,7 +110,7 @@ func (s *roomStatements) insertRoomNID(
|
|||
return types.RoomNID(roomNID), err
|
||||
}
|
||||
|
||||
func (s *roomStatements) selectRoomNID(
|
||||
func (s *roomStatements) SelectRoomNID(
|
||||
ctx context.Context, txn *sql.Tx, roomID string,
|
||||
) (types.RoomNID, error) {
|
||||
var roomNID int64
|
||||
|
@ -117,8 +119,8 @@ func (s *roomStatements) selectRoomNID(
|
|||
return types.RoomNID(roomNID), err
|
||||
}
|
||||
|
||||
func (s *roomStatements) selectLatestEventNIDs(
|
||||
ctx context.Context, roomNID types.RoomNID,
|
||||
func (s *roomStatements) SelectLatestEventNIDs(
|
||||
ctx context.Context, txn *sql.Tx, roomNID types.RoomNID,
|
||||
) ([]types.EventNID, types.StateSnapshotNID, error) {
|
||||
var nids pq.Int64Array
|
||||
var stateSnapshotNID int64
|
||||
|
@ -134,7 +136,7 @@ func (s *roomStatements) selectLatestEventNIDs(
|
|||
return eventNIDs, types.StateSnapshotNID(stateSnapshotNID), nil
|
||||
}
|
||||
|
||||
func (s *roomStatements) selectLatestEventsNIDsForUpdate(
|
||||
func (s *roomStatements) SelectLatestEventsNIDsForUpdate(
|
||||
ctx context.Context, txn *sql.Tx, roomNID types.RoomNID,
|
||||
) ([]types.EventNID, types.EventNID, types.StateSnapshotNID, error) {
|
||||
var nids pq.Int64Array
|
||||
|
@ -152,7 +154,7 @@ func (s *roomStatements) selectLatestEventsNIDsForUpdate(
|
|||
return eventNIDs, types.EventNID(lastEventSentNID), types.StateSnapshotNID(stateSnapshotNID), nil
|
||||
}
|
||||
|
||||
func (s *roomStatements) updateLatestEventNIDs(
|
||||
func (s *roomStatements) UpdateLatestEventNIDs(
|
||||
ctx context.Context,
|
||||
txn *sql.Tx,
|
||||
roomNID types.RoomNID,
|
||||
|
@ -171,7 +173,7 @@ func (s *roomStatements) updateLatestEventNIDs(
|
|||
return err
|
||||
}
|
||||
|
||||
func (s *roomStatements) selectRoomVersionForRoomID(
|
||||
func (s *roomStatements) SelectRoomVersionForRoomID(
|
||||
ctx context.Context, txn *sql.Tx, roomID string,
|
||||
) (gomatrixserverlib.RoomVersion, error) {
|
||||
var roomVersion gomatrixserverlib.RoomVersion
|
||||
|
@ -183,12 +185,11 @@ func (s *roomStatements) selectRoomVersionForRoomID(
|
|||
return roomVersion, err
|
||||
}
|
||||
|
||||
func (s *roomStatements) selectRoomVersionForRoomNID(
|
||||
ctx context.Context, txn *sql.Tx, roomNID types.RoomNID,
|
||||
func (s *roomStatements) SelectRoomVersionForRoomNID(
|
||||
ctx context.Context, roomNID types.RoomNID,
|
||||
) (gomatrixserverlib.RoomVersion, error) {
|
||||
var roomVersion gomatrixserverlib.RoomVersion
|
||||
stmt := internal.TxStmt(txn, s.selectRoomVersionForRoomNIDStmt)
|
||||
err := stmt.QueryRowContext(ctx, roomNID).Scan(&roomVersion)
|
||||
err := s.selectRoomVersionForRoomNIDStmt.QueryRowContext(ctx, roomNID).Scan(&roomVersion)
|
||||
if err == sql.ErrNoRows {
|
||||
return roomVersion, errors.New("room not found")
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue