mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-10 16:33:11 -06:00
Add database functions to interact with aliases
This commit is contained in:
parent
6d073dcf9f
commit
fd740350ca
|
|
@ -0,0 +1,95 @@
|
|||
// Copyright 2017 Vector Creations Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package storage
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
const roomAliasesSchema = `
|
||||
-- Stores room aliases and room IDs they refer to
|
||||
CREATE TABLE IF NOT EXISTS room_aliases (
|
||||
-- Alias of the room
|
||||
alias TEXT NOT NULL PRIMARY KEY,
|
||||
-- Room ID the alias refers to
|
||||
room_id TEXT NOT NULL
|
||||
);
|
||||
`
|
||||
|
||||
const insertRoomAliasSQL = "" +
|
||||
"INSERT INTO room_aliases (alias, room_id) VALUES ($1, $2)"
|
||||
|
||||
const selectRoomIDFromAliasSQL = "" +
|
||||
"SELECT room_id FROM room_aliases WHERE alias = $1"
|
||||
|
||||
const selectAliasesFromRoomIDSQL = "" +
|
||||
"SELECT alias FROM room_aliases WHERE room_id = $1"
|
||||
|
||||
const deleteRoomAliasSQL = "" +
|
||||
"DELETE FROM room_aliases WHERE alias = $1"
|
||||
|
||||
type roomAliasesStatements struct {
|
||||
insertRoomAliasStmt *sql.Stmt
|
||||
selectRoomIDFromAliasStmt *sql.Stmt
|
||||
selectAliasesFromRoomIDStmt *sql.Stmt
|
||||
deleteRoomAliasStmt *sql.Stmt
|
||||
}
|
||||
|
||||
func (s *roomAliasesStatements) prepare(db *sql.DB) (err error) {
|
||||
_, err = db.Exec(roomAliasesSchema)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return statementList{
|
||||
{&s.insertRoomAliasStmt, insertRoomAliasSQL},
|
||||
{&s.selectRoomIDFromAliasStmt, selectRoomIDFromAliasSQL},
|
||||
{&s.selectAliasesFromRoomIDStmt, selectAliasesFromRoomIDSQL},
|
||||
{&s.deleteRoomAliasStmt, deleteRoomAliasSQL},
|
||||
}.prepare(db)
|
||||
}
|
||||
|
||||
func (s *roomAliasesStatements) insertRoomAlias(alias string, roomID string) (err error) {
|
||||
_, err = s.insertRoomAliasStmt.Exec(alias, roomID)
|
||||
return
|
||||
}
|
||||
|
||||
func (s *roomAliasesStatements) selectRoomIDFromAlias(alias string) (roomID string, err error) {
|
||||
err = s.selectRoomIDFromAliasStmt.QueryRow(alias).Scan(&roomID)
|
||||
return
|
||||
}
|
||||
|
||||
func (s *roomAliasesStatements) selectAliasesFromRoomID(roomID string) (aliases []string, err error) {
|
||||
aliases = []string{}
|
||||
rows, err := s.selectRoomIDFromAliasStmt.Query(roomID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for rows.Next() {
|
||||
var alias string
|
||||
if err = rows.Scan(&alias); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
aliases = append(aliases, alias)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (s *roomAliasesStatements) deleteRoomAlias(alias string) (err error) {
|
||||
_, err = s.deleteRoomAliasStmt.Exec(alias)
|
||||
return
|
||||
}
|
||||
|
|
@ -27,6 +27,7 @@ type statements struct {
|
|||
stateSnapshotStatements
|
||||
stateBlockStatements
|
||||
previousEventStatements
|
||||
roomAliasesStatements
|
||||
}
|
||||
|
||||
func (s *statements) prepare(db *sql.DB) error {
|
||||
|
|
@ -64,5 +65,9 @@ func (s *statements) prepare(db *sql.DB) error {
|
|||
return err
|
||||
}
|
||||
|
||||
if err = s.roomAliasesStatements.prepare(db); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -355,6 +355,26 @@ func (d *Database) LatestEventIDs(roomNID types.RoomNID) ([]gomatrixserverlib.Ev
|
|||
return references, currentStateSnapshotNID, depth, nil
|
||||
}
|
||||
|
||||
// SetRoomAlias implements query.RoomserverQueryAPIDB
|
||||
func (d *Database) SetRoomAlias(alias string, roomID string) error {
|
||||
return d.statements.insertRoomAlias(alias, roomID)
|
||||
}
|
||||
|
||||
// GetRoomIDFromAlias implements query.RoomserverQueryAPIDB
|
||||
func (d *Database) GetRoomIDFromAlias(alias string) (string, error) {
|
||||
return d.statements.selectRoomIDFromAlias(alias)
|
||||
}
|
||||
|
||||
// GetAliasesFromRoomID implements query.RoomserverQueryAPIDB
|
||||
func (d *Database) GetAliasesFromRoomID(roomID string) ([]string, error) {
|
||||
return d.statements.selectAliasesFromRoomID(roomID)
|
||||
}
|
||||
|
||||
// RemoveRoomAlias implements query.RoomserverQueryAPIDB
|
||||
func (d *Database) RemoveRoomAlias(alias string) error {
|
||||
return d.statements.deleteRoomAlias(alias)
|
||||
}
|
||||
|
||||
// StateEntriesForTuples implements state.RoomStateDatabase
|
||||
func (d *Database) StateEntriesForTuples(
|
||||
stateBlockNIDs []types.StateBlockNID, stateKeyTuples []types.StateKeyTuple,
|
||||
|
|
|
|||
Loading…
Reference in a new issue