mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-07 15:03:09 -06:00
Move room sql to a separate file
This commit is contained in:
parent
999bb82992
commit
00748e2098
|
|
@ -0,0 +1,57 @@
|
||||||
|
package storage
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"github.com/matrix-org/dendrite/roomserver/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
const roomsSchema = `
|
||||||
|
CREATE SEQUENCE IF NOT EXISTS room_nid_seq;
|
||||||
|
CREATE TABLE IF NOT EXISTS rooms (
|
||||||
|
-- Local numeric ID for the room.
|
||||||
|
room_nid BIGINT PRIMARY KEY DEFAULT nextval('room_nid_seq'),
|
||||||
|
-- Textual ID for the room.
|
||||||
|
room_id TEXT NOT NULL CONSTRAINT room_id_unique UNIQUE
|
||||||
|
);
|
||||||
|
`
|
||||||
|
|
||||||
|
// Same as insertEventTypeNIDSQL
|
||||||
|
const insertRoomNIDSQL = "" +
|
||||||
|
"INSERT INTO rooms (room_id) VALUES ($1)" +
|
||||||
|
" ON CONFLICT ON CONSTRAINT room_id_unique" +
|
||||||
|
" DO UPDATE SET room_id = $1" +
|
||||||
|
" RETURNING (room_nid)"
|
||||||
|
|
||||||
|
const selectRoomNIDSQL = "" +
|
||||||
|
"SELECT room_nid FROM rooms WHERE room_id = $1"
|
||||||
|
|
||||||
|
type roomStatements struct {
|
||||||
|
insertRoomNIDStmt *sql.Stmt
|
||||||
|
selectRoomNIDStmt *sql.Stmt
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *roomStatements) prepare(db *sql.DB) (err error) {
|
||||||
|
_, err = db.Exec(roomsSchema)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if s.insertRoomNIDStmt, err = db.Prepare(insertRoomNIDSQL); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if s.selectRoomNIDStmt, err = db.Prepare(selectRoomNIDSQL); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *roomStatements) insertRoomNID(roomID string) (types.RoomNID, error) {
|
||||||
|
var roomNID int64
|
||||||
|
err := s.insertRoomNIDStmt.QueryRow(roomID).Scan(&roomNID)
|
||||||
|
return types.RoomNID(roomNID), err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *roomStatements) selectRoomNID(roomID string) (types.RoomNID, error) {
|
||||||
|
var roomNID int64
|
||||||
|
err := s.selectRoomNIDStmt.QueryRow(roomID).Scan(&roomNID)
|
||||||
|
return types.RoomNID(roomNID), err
|
||||||
|
}
|
||||||
|
|
@ -11,8 +11,7 @@ type statements struct {
|
||||||
partitionOffsetStatements
|
partitionOffsetStatements
|
||||||
eventTypeStatements
|
eventTypeStatements
|
||||||
eventStateKeyStatements
|
eventStateKeyStatements
|
||||||
insertRoomNIDStmt *sql.Stmt
|
roomStatements
|
||||||
selectRoomNIDStmt *sql.Stmt
|
|
||||||
insertEventStmt *sql.Stmt
|
insertEventStmt *sql.Stmt
|
||||||
bulkSelectStateEventByIDStmt *sql.Stmt
|
bulkSelectStateEventByIDStmt *sql.Stmt
|
||||||
bulkSelectStateAtEventByIDStmt *sql.Stmt
|
bulkSelectStateAtEventByIDStmt *sql.Stmt
|
||||||
|
|
@ -41,7 +40,7 @@ func (s *statements) prepare(db *sql.DB) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = s.prepareRooms(db); err != nil {
|
if err = s.roomStatements.prepare(db); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -56,52 +55,6 @@ func (s *statements) prepare(db *sql.DB) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *statements) prepareRooms(db *sql.DB) (err error) {
|
|
||||||
_, err = db.Exec(roomsSchema)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if s.insertRoomNIDStmt, err = db.Prepare(insertRoomNIDSQL); err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if s.selectRoomNIDStmt, err = db.Prepare(selectRoomNIDSQL); err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
const roomsSchema = `
|
|
||||||
CREATE SEQUENCE IF NOT EXISTS room_nid_seq;
|
|
||||||
CREATE TABLE IF NOT EXISTS rooms (
|
|
||||||
-- Local numeric ID for the room.
|
|
||||||
room_nid BIGINT PRIMARY KEY DEFAULT nextval('room_nid_seq'),
|
|
||||||
-- Textual ID for the room.
|
|
||||||
room_id TEXT NOT NULL CONSTRAINT room_id_unique UNIQUE
|
|
||||||
);
|
|
||||||
`
|
|
||||||
|
|
||||||
// Same as insertEventTypeNIDSQL
|
|
||||||
const insertRoomNIDSQL = "" +
|
|
||||||
"INSERT INTO rooms (room_id) VALUES ($1)" +
|
|
||||||
" ON CONFLICT ON CONSTRAINT room_id_unique" +
|
|
||||||
" DO UPDATE SET room_id = $1" +
|
|
||||||
" RETURNING (room_nid)"
|
|
||||||
|
|
||||||
const selectRoomNIDSQL = "" +
|
|
||||||
"SELECT room_nid FROM rooms WHERE room_id = $1"
|
|
||||||
|
|
||||||
func (s *statements) insertRoomNID(roomID string) (types.RoomNID, error) {
|
|
||||||
var roomNID int64
|
|
||||||
err := s.insertRoomNIDStmt.QueryRow(roomID).Scan(&roomNID)
|
|
||||||
return types.RoomNID(roomNID), err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *statements) selectRoomNID(roomID string) (types.RoomNID, error) {
|
|
||||||
var roomNID int64
|
|
||||||
err := s.selectRoomNIDStmt.QueryRow(roomID).Scan(&roomNID)
|
|
||||||
return types.RoomNID(roomNID), err
|
|
||||||
}
|
|
||||||
|
|
||||||
const eventsSchema = `
|
const eventsSchema = `
|
||||||
-- The events table holds metadata for each event, the actual JSON is stored
|
-- The events table holds metadata for each event, the actual JSON is stored
|
||||||
-- separately to keep the size of the rows small.
|
-- separately to keep the size of the rows small.
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue