diff --git a/src/github.com/matrix-org/dendrite/roomserver/storage/rooms_table.go b/src/github.com/matrix-org/dendrite/roomserver/storage/rooms_table.go new file mode 100644 index 000000000..982fd24c8 --- /dev/null +++ b/src/github.com/matrix-org/dendrite/roomserver/storage/rooms_table.go @@ -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 +} diff --git a/src/github.com/matrix-org/dendrite/roomserver/storage/sql.go b/src/github.com/matrix-org/dendrite/roomserver/storage/sql.go index 470148ecb..8c83e6639 100644 --- a/src/github.com/matrix-org/dendrite/roomserver/storage/sql.go +++ b/src/github.com/matrix-org/dendrite/roomserver/storage/sql.go @@ -11,8 +11,7 @@ type statements struct { partitionOffsetStatements eventTypeStatements eventStateKeyStatements - insertRoomNIDStmt *sql.Stmt - selectRoomNIDStmt *sql.Stmt + roomStatements insertEventStmt *sql.Stmt bulkSelectStateEventByIDStmt *sql.Stmt bulkSelectStateAtEventByIDStmt *sql.Stmt @@ -41,7 +40,7 @@ func (s *statements) prepare(db *sql.DB) error { return err } - if err = s.prepareRooms(db); err != nil { + if err = s.roomStatements.prepare(db); err != nil { return err } @@ -56,52 +55,6 @@ func (s *statements) prepare(db *sql.DB) error { 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 = ` -- The events table holds metadata for each event, the actual JSON is stored -- separately to keep the size of the rows small.