refactor(database)!: decouple SQLs from backend logic and cache stmts

This decouples SQLs from handler logic, makes it easier for auditing and
caching. It also enables the possibility to switch or support multiple
database backends.
This commit is contained in:
oxalica 2024-09-21 07:28:41 -04:00
parent b955d32099
commit fafd2de2e3
11 changed files with 769 additions and 669 deletions

View file

@ -17,12 +17,6 @@ CREATE TABLE IF NOT EXISTS `user_act_key` (
PRIMARY KEY (`uid`, `act_key`)
) STRICT, WITHOUT ROWID;
CREATE VIEW IF NOT EXISTS `valid_user_act_key` AS
SELECT `act_key`, `user`.*
FROM `user_act_key`
JOIN `user` USING (`uid`)
WHERE unixepoch() < `expire_time`;
-- The highest bit of `rid` will be set for peer chat room.
-- So simply comparing it against 0 can filter them out.
CREATE TABLE IF NOT EXISTS `room` (
@ -43,6 +37,10 @@ CREATE UNIQUE INDEX IF NOT EXISTS `ix_peer_chat` ON `room`
(`peer1`, `peer2`)
WHERE `rid` < 0;
-- RoomAttrs::PUBLIC_READABLE
CREATE INDEX IF NOT EXISTS `ix_public_room` ON `room` (`rid`)
WHERE `attrs` & 1 != 0;
CREATE TABLE IF NOT EXISTS `room_member` (
`rid` INTEGER NOT NULL REFERENCES `room` ON DELETE CASCADE,
`uid` INTEGER NOT NULL REFERENCES `user` ON DELETE RESTRICT,
@ -68,3 +66,11 @@ CREATE TABLE IF NOT EXISTS `msg` (
) STRICT;
CREATE INDEX IF NOT EXISTS `room_latest_msg` ON `msg` (`rid` ASC, `cid` DESC);
-- Temporary views.
CREATE TEMP VIEW `valid_user_act_key` AS
SELECT `act_key`, `user`.*
FROM `user_act_key`
JOIN `user` USING (`uid`)
WHERE unixepoch() < `expire_time`;