Move all SQL queries to constants

This commit is contained in:
Tulir Asokan 2023-09-05 01:46:56 +03:00
parent 95c0cfa983
commit 58fa6fdedb
6 changed files with 96 additions and 61 deletions

View file

@ -63,9 +63,24 @@ const (
SELECT conv_id, conv_receiver, id, mxid, mx_room, sender, timestamp, status FROM message SELECT conv_id, conv_receiver, id, mxid, mx_room, sender, timestamp, status FROM message
WHERE mxid=$1 WHERE mxid=$1
` `
deleteAllInChat = ` deleteAllMessagesInChatQuery = `
DELETE FROM message WHERE conv_id=$1 AND conv_receiver=$2 DELETE FROM message WHERE conv_id=$1 AND conv_receiver=$2
` `
insertMessageQuery = `
INSERT INTO message (conv_id, conv_receiver, id, mxid, mx_room, sender, timestamp, status)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
`
massInsertMessageQueryPrefix = `
INSERT INTO message (conv_id, conv_receiver, id, mxid, mx_room, sender, timestamp, status)
VALUES
`
updateMessageQuery = `
UPDATE message
SET conv_id=$1, mxid=$4, mx_room=$5, sender=$6, timestamp=$7, status=$8
WHERE conv_receiver=$2 AND id=$3
`
updateMessageStatusQuery = "UPDATE message SET status=$1, timestamp=$2 WHERE conv_receiver=$3 AND id=$4"
deleteMessageQuery = "DELETE FROM message WHERE conv_id=$1 AND conv_receiver=$2 AND id=$3"
) )
func (mq *MessageQuery) GetByID(ctx context.Context, receiver int, messageID string) (*Message, error) { func (mq *MessageQuery) GetByID(ctx context.Context, receiver int, messageID string) (*Message, error) {
@ -85,7 +100,7 @@ func (mq *MessageQuery) GetLastInChatWithMXID(ctx context.Context, chat Key) (*M
} }
func (mq *MessageQuery) DeleteAllInChat(ctx context.Context, chat Key) error { func (mq *MessageQuery) DeleteAllInChat(ctx context.Context, chat Key) error {
_, err := mq.db.Conn(ctx).ExecContext(ctx, deleteAllInChat, chat.ID, chat.Receiver) _, err := mq.db.Conn(ctx).ExecContext(ctx, deleteAllMessagesInChatQuery, chat.ID, chat.Receiver)
return err return err
} }
@ -147,10 +162,7 @@ func (msg *Message) sqlVariables() []any {
} }
func (msg *Message) Insert(ctx context.Context) error { func (msg *Message) Insert(ctx context.Context) error {
_, err := msg.db.Conn(ctx).ExecContext(ctx, ` _, err := msg.db.Conn(ctx).ExecContext(ctx, insertMessageQuery, msg.sqlVariables()...)
INSERT INTO message (conv_id, conv_receiver, id, mxid, mx_room, sender, timestamp, status)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
`, msg.sqlVariables()...)
return err return err
} }
@ -173,30 +185,23 @@ func (mq *MessageQuery) MassInsert(ctx context.Context, messages []*Message) err
params[baseIndex+4] = dbutil.JSON{Data: &msg.Status} params[baseIndex+4] = dbutil.JSON{Data: &msg.Status}
placeholders[i] = fmt.Sprintf(valueStringFormat, baseIndex+1, baseIndex+2, baseIndex+3, baseIndex+4, baseIndex+5) placeholders[i] = fmt.Sprintf(valueStringFormat, baseIndex+1, baseIndex+2, baseIndex+3, baseIndex+4, baseIndex+5)
} }
query := ` query := massInsertMessageQueryPrefix + strings.Join(placeholders, ",")
INSERT INTO message (conv_id, conv_receiver, id, mxid, mx_room, sender, timestamp, status)
VALUES
` + strings.Join(placeholders, ",")
_, err := mq.db.Conn(ctx).ExecContext(ctx, query, params...) _, err := mq.db.Conn(ctx).ExecContext(ctx, query, params...)
return err return err
} }
func (msg *Message) Update(ctx context.Context) error { func (msg *Message) Update(ctx context.Context) error {
_, err := msg.db.Conn(ctx).ExecContext(ctx, ` _, err := msg.db.Conn(ctx).ExecContext(ctx, updateMessageQuery, msg.sqlVariables()...)
UPDATE message
SET conv_id=$1, mxid=$4, mx_room=$5, sender=$6, timestamp=$7, status=$8
WHERE conv_receiver=$2 AND id=$3
`, msg.sqlVariables()...)
return err return err
} }
func (msg *Message) UpdateStatus(ctx context.Context) error { func (msg *Message) UpdateStatus(ctx context.Context) error {
_, err := msg.db.Conn(ctx).ExecContext(ctx, "UPDATE message SET status=$1, timestamp=$2 WHERE conv_receiver=$3 AND id=$4", dbutil.JSON{Data: &msg.Status}, msg.Timestamp.UnixMicro(), msg.Chat.Receiver, msg.ID) _, err := msg.db.Conn(ctx).ExecContext(ctx, updateMessageStatusQuery, dbutil.JSON{Data: &msg.Status}, msg.Timestamp.UnixMicro(), msg.Chat.Receiver, msg.ID)
return err return err
} }
func (msg *Message) Delete(ctx context.Context) error { func (msg *Message) Delete(ctx context.Context) error {
_, err := msg.db.Conn(ctx).ExecContext(ctx, "DELETE FROM message WHERE conv_id=$1 AND conv_receiver=$2 AND id=$3", msg.Chat.ID, msg.Chat.Receiver, msg.ID) _, err := msg.db.Conn(ctx).ExecContext(ctx, deleteMessageQuery, msg.Chat.ID, msg.Chat.Receiver, msg.ID)
return err return err
} }

View file

@ -43,24 +43,42 @@ func (pq *PortalQuery) getDB() *Database {
return pq.db return pq.db
} }
const (
getAllPortalsQuery = "SELECT id, receiver, self_user, other_user, type, mxid, name, name_set, encrypted, in_space FROM portal"
getAllPortalsForUserQuery = getAllPortalsQuery + " WHERE receiver=$1"
getPortalByKeyQuery = getAllPortalsQuery + " WHERE id=$1 AND receiver=$2"
getPortalByOtherUserQuery = getAllPortalsQuery + " WHERE other_user=$1 AND receiver=$2"
getPortalByMXIDQuery = getAllPortalsQuery + " WHERE mxid=$1"
insertPortalQuery = `
INSERT INTO portal (id, receiver, self_user, other_user, type, mxid, name, name_set, encrypted, in_space)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
`
updatePortalQuery = `
UPDATE portal
SET self_user=$3, other_user=$4, type=$5, mxid=$6, name=$7, name_set=$8, encrypted=$9, in_space=$10
WHERE id=$1 AND receiver=$2
`
deletePortalQuery = "DELETE FROM portal WHERE id=$1 AND receiver=$2"
)
func (pq *PortalQuery) GetAll(ctx context.Context) ([]*Portal, error) { func (pq *PortalQuery) GetAll(ctx context.Context) ([]*Portal, error) {
return getAll[*Portal](pq, ctx, "SELECT id, receiver, self_user, other_user, type, mxid, name, name_set, encrypted, in_space FROM portal") return getAll[*Portal](pq, ctx, getAllPortalsQuery)
} }
func (pq *PortalQuery) GetAllForUser(ctx context.Context, receiver int) ([]*Portal, error) { func (pq *PortalQuery) GetAllForUser(ctx context.Context, receiver int) ([]*Portal, error) {
return getAll[*Portal](pq, ctx, "SELECT id, receiver, self_user, other_user, type, mxid, name, name_set, encrypted, in_space FROM portal WHERE receiver=$1", receiver) return getAll[*Portal](pq, ctx, getAllPortalsForUserQuery, receiver)
} }
func (pq *PortalQuery) GetByKey(ctx context.Context, key Key) (*Portal, error) { func (pq *PortalQuery) GetByKey(ctx context.Context, key Key) (*Portal, error) {
return get[*Portal](pq, ctx, "SELECT id, receiver, self_user, other_user, type, mxid, name, name_set, encrypted, in_space FROM portal WHERE id=$1 AND receiver=$2", key.ID, key.Receiver) return get[*Portal](pq, ctx, getPortalByKeyQuery, key.ID, key.Receiver)
} }
func (pq *PortalQuery) GetByOtherUser(ctx context.Context, key Key) (*Portal, error) { func (pq *PortalQuery) GetByOtherUser(ctx context.Context, key Key) (*Portal, error) {
return get[*Portal](pq, ctx, "SELECT id, receiver, self_user, other_user, type, mxid, name, name_set, encrypted, in_space FROM portal WHERE other_user=$1 AND receiver=$2", key.ID, key.Receiver) return get[*Portal](pq, ctx, getPortalByOtherUserQuery, key.ID, key.Receiver)
} }
func (pq *PortalQuery) GetByMXID(ctx context.Context, mxid id.RoomID) (*Portal, error) { func (pq *PortalQuery) GetByMXID(ctx context.Context, mxid id.RoomID) (*Portal, error) {
return get[*Portal](pq, ctx, "SELECT id, receiver, self_user, other_user, type, mxid, name, name_set, encrypted, in_space FROM portal WHERE mxid=$1", mxid) return get[*Portal](pq, ctx, getPortalByMXIDQuery, mxid)
} }
type Key struct { type Key struct {
@ -125,23 +143,16 @@ func (portal *Portal) sqlVariables() []any {
} }
func (portal *Portal) Insert(ctx context.Context) error { func (portal *Portal) Insert(ctx context.Context) error {
_, err := portal.db.Conn(ctx).ExecContext(ctx, ` _, err := portal.db.Conn(ctx).ExecContext(ctx, insertPortalQuery, portal.sqlVariables()...)
INSERT INTO portal (id, receiver, self_user, other_user, type, mxid, name, name_set, encrypted, in_space)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
`, portal.sqlVariables()...)
return err return err
} }
func (portal *Portal) Update(ctx context.Context) error { func (portal *Portal) Update(ctx context.Context) error {
_, err := portal.db.Conn(ctx).ExecContext(ctx, ` _, err := portal.db.Conn(ctx).ExecContext(ctx, updatePortalQuery, portal.sqlVariables()...)
UPDATE portal
SET self_user=$3, other_user=$4, type=$5, mxid=$6, name=$7, name_set=$8, encrypted=$9, in_space=$10
WHERE id=$1 AND receiver=$2
`, portal.sqlVariables()...)
return err return err
} }
func (portal *Portal) Delete(ctx context.Context) error { func (portal *Portal) Delete(ctx context.Context) error {
_, err := portal.db.Conn(ctx).ExecContext(ctx, "DELETE FROM portal WHERE id=$1 AND receiver=$2", portal.ID, portal.Receiver) _, err := portal.db.Conn(ctx).ExecContext(ctx, deletePortalQuery, portal.ID, portal.Receiver)
return err return err
} }

View file

@ -36,21 +36,31 @@ func (pq *PuppetQuery) New() *Puppet {
} }
} }
const (
deleteAllPuppetsForUserQuery = "DELETE FROM puppet WHERE receiver=$1"
getPuppetQuery = "SELECT id, receiver, phone, contact_id, name, name_set, avatar_hash, avatar_mxc, avatar_set, avatar_update_ts, contact_info_set FROM puppet WHERE id=$1 AND receiver=$2"
insertPuppetQuery = `
INSERT INTO puppet (id, receiver, phone, contact_id, name, name_set, avatar_hash, avatar_mxc, avatar_set, avatar_update_ts, contact_info_set)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
`
updatePuppetQuery = `
UPDATE puppet
SET phone=$3, contact_id=$4, name=$5, name_set=$6, avatar_hash=$7, avatar_mxc=$8, avatar_set=$9, avatar_update_ts=$10, contact_info_set=$11
WHERE id=$1 AND receiver=$2
`
)
func (pq *PuppetQuery) getDB() *Database { func (pq *PuppetQuery) getDB() *Database {
return pq.db return pq.db
} }
func (pq *PuppetQuery) GetAll(ctx context.Context) ([]*Puppet, error) {
return getAll[*Puppet](pq, ctx, "SELECT id, receiver, phone, contact_id, name, name_set, avatar_hash, avatar_mxc, avatar_set, avatar_update_ts, contact_info_set FROM puppet")
}
func (pq *PuppetQuery) DeleteAllForUser(ctx context.Context, userID int) error { func (pq *PuppetQuery) DeleteAllForUser(ctx context.Context, userID int) error {
_, err := pq.db.Conn(ctx).ExecContext(ctx, "DELETE FROM puppet WHERE receiver=$1", userID) _, err := pq.db.Conn(ctx).ExecContext(ctx, deleteAllPuppetsForUserQuery, userID)
return err return err
} }
func (pq *PuppetQuery) Get(ctx context.Context, key Key) (*Puppet, error) { func (pq *PuppetQuery) Get(ctx context.Context, key Key) (*Puppet, error) {
return get[*Puppet](pq, ctx, "SELECT id, receiver, phone, contact_id, name, name_set, avatar_hash, avatar_mxc, avatar_set, avatar_update_ts, contact_info_set FROM puppet WHERE id=$1 AND receiver=$2", key.ID, key.Receiver) return get[*Puppet](pq, ctx, getPuppetQuery, key.ID, key.Receiver)
} }
type Puppet struct { type Puppet struct {
@ -89,18 +99,11 @@ func (puppet *Puppet) sqlVariables() []any {
} }
func (puppet *Puppet) Insert(ctx context.Context) error { func (puppet *Puppet) Insert(ctx context.Context) error {
_, err := puppet.db.Conn(ctx).ExecContext(ctx, ` _, err := puppet.db.Conn(ctx).ExecContext(ctx, insertPuppetQuery, puppet.sqlVariables()...)
INSERT INTO puppet (id, receiver, phone, contact_id, name, name_set, avatar_hash, avatar_mxc, avatar_set, avatar_update_ts, contact_info_set)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
`, puppet.sqlVariables()...)
return err return err
} }
func (puppet *Puppet) Update(ctx context.Context) error { func (puppet *Puppet) Update(ctx context.Context) error {
_, err := puppet.db.Conn(ctx).ExecContext(ctx, ` _, err := puppet.db.Conn(ctx).ExecContext(ctx, updatePuppetQuery, puppet.sqlVariables()...)
UPDATE puppet
SET phone=$3, contact_id=$4, name=$5, name_set=$6, avatar_hash=$7, avatar_mxc=$8, avatar_set=$9, avatar_update_ts=$10, contact_info_set=$11
WHERE id=$1 AND receiver=$2
`, puppet.sqlVariables()...)
return err return err
} }

View file

@ -58,12 +58,13 @@ const (
SELECT conv_id, conv_receiver, msg_id, sender, reaction, mxid FROM reaction SELECT conv_id, conv_receiver, msg_id, sender, reaction, mxid FROM reaction
WHERE conv_id=$1 AND conv_receiver=$2 AND msg_id=$3 WHERE conv_id=$1 AND conv_receiver=$2 AND msg_id=$3
` `
insertReaction = ` insertReactionQuery = `
INSERT INTO reaction (conv_id, conv_receiver, msg_id, sender, reaction, mxid) INSERT INTO reaction (conv_id, conv_receiver, msg_id, sender, reaction, mxid)
VALUES ($1, $2, $3, $4, $5, $6) VALUES ($1, $2, $3, $4, $5, $6)
ON CONFLICT (conv_receiver, msg_id, sender) ON CONFLICT (conv_receiver, msg_id, sender)
DO UPDATE SET reaction=excluded.reaction, mxid=excluded.mxid DO UPDATE SET reaction=excluded.reaction, mxid=excluded.mxid
` `
deleteReactionQuery = "DELETE FROM reaction WHERE conv_id=$1 AND conv_receiver=$2 AND msg_id=$3 AND sender=$4"
) )
func (rq *ReactionQuery) GetByID(ctx context.Context, receiver int, messageID, sender string) (*Reaction, error) { func (rq *ReactionQuery) GetByID(ctx context.Context, receiver int, messageID, sender string) (*Reaction, error) {
@ -104,7 +105,7 @@ func (r *Reaction) Scan(row dbutil.Scannable) (*Reaction, error) {
} }
func (r *Reaction) Insert(ctx context.Context) error { func (r *Reaction) Insert(ctx context.Context) error {
_, err := r.db.Conn(ctx).ExecContext(ctx, insertReaction, r.Chat.ID, r.Chat.Receiver, r.MessageID, r.Sender, r.Reaction, r.MXID) _, err := r.db.Conn(ctx).ExecContext(ctx, insertReactionQuery, r.Chat.ID, r.Chat.Receiver, r.MessageID, r.Sender, r.Reaction, r.MXID)
return err return err
} }
@ -125,12 +126,12 @@ func (rq *ReactionQuery) MassInsert(ctx context.Context, reactions []*Reaction)
params[baseIndex+3] = msg.MXID params[baseIndex+3] = msg.MXID
placeholders[i] = fmt.Sprintf(valueStringFormat, baseIndex+1, baseIndex+2, baseIndex+3, baseIndex+4) placeholders[i] = fmt.Sprintf(valueStringFormat, baseIndex+1, baseIndex+2, baseIndex+3, baseIndex+4)
} }
query := strings.Replace(insertReaction, "($1, $2, $3, $4, $5, $6)", strings.Join(placeholders, ","), 1) query := strings.Replace(insertReactionQuery, "($1, $2, $3, $4, $5, $6)", strings.Join(placeholders, ","), 1)
_, err := rq.db.Conn(ctx).ExecContext(ctx, query, params...) _, err := rq.db.Conn(ctx).ExecContext(ctx, query, params...)
return err return err
} }
func (r *Reaction) Delete(ctx context.Context) error { func (r *Reaction) Delete(ctx context.Context) error {
_, err := r.db.Conn(ctx).ExecContext(ctx, "DELETE FROM reaction WHERE conv_id=$1 AND conv_receiver=$2 AND msg_id=$3 AND sender=$4", r.Chat.ID, r.Chat.Receiver, r.MessageID, r.Sender) _, err := r.db.Conn(ctx).ExecContext(ctx, deleteReactionQuery, r.Chat.ID, r.Chat.Receiver, r.MessageID, r.Sender)
return err return err
} }

View file

@ -46,20 +46,39 @@ func (uq *UserQuery) getDB() *Database {
return uq.db return uq.db
} }
const (
getUserBaseQuery = `SELECT rowid, mxid, phone_id, session, self_participant_ids, sim_metadata, settings, management_room, space_room, access_token FROM "user"`
getAllUsersWithSessionQuery = getUserBaseQuery + " WHERE session IS NOT NULL"
getAllUsersWithDoublePuppetQuery = getUserBaseQuery + " WHERE access_token<>''"
getUserByRowIDQuery = getUserBaseQuery + " WHERE rowid=$1"
getUserByMXIDQuery = getUserBaseQuery + " WHERE mxid=$1"
insertUserQuery = `
INSERT INTO "user" (mxid, phone_id, session, self_participant_ids, sim_metadata, settings, management_room, space_room, access_token)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING rowid
`
updateUserQuery = `
UPDATE "user"
SET phone_id=$2, session=$3, self_participant_ids=$4, sim_metadata=$5, settings=$6,
management_room=$7, space_room=$8, access_token=$9
WHERE mxid=$1
`
)
func (uq *UserQuery) GetAllWithSession(ctx context.Context) ([]*User, error) { func (uq *UserQuery) GetAllWithSession(ctx context.Context) ([]*User, error) {
return getAll[*User](uq, ctx, `SELECT rowid, mxid, phone_id, session, self_participant_ids, sim_metadata, settings, management_room, space_room, access_token FROM "user" WHERE session IS NOT NULL`) return getAll[*User](uq, ctx, getAllUsersWithSessionQuery)
} }
func (uq *UserQuery) GetAllWithDoublePuppet(ctx context.Context) ([]*User, error) { func (uq *UserQuery) GetAllWithDoublePuppet(ctx context.Context) ([]*User, error) {
return getAll[*User](uq, ctx, `SELECT rowid, mxid, phone_id, session, self_participant_ids, sim_metadata, settings, management_room, space_room, access_token FROM "user" WHERE access_token<>''`) return getAll[*User](uq, ctx, getAllUsersWithDoublePuppetQuery)
} }
func (uq *UserQuery) GetByRowID(ctx context.Context, rowID int) (*User, error) { func (uq *UserQuery) GetByRowID(ctx context.Context, rowID int) (*User, error) {
return get[*User](uq, ctx, `SELECT rowid, mxid, phone_id, session, self_participant_ids, sim_metadata, settings, management_room, space_room, access_token FROM "user" WHERE rowid=$1`, rowID) return get[*User](uq, ctx, getUserByRowIDQuery, rowID)
} }
func (uq *UserQuery) GetByMXID(ctx context.Context, userID id.UserID) (*User, error) { func (uq *UserQuery) GetByMXID(ctx context.Context, userID id.UserID) (*User, error) {
return get[*User](uq, ctx, `SELECT rowid, mxid, phone_id, session, self_participant_ids, sim_metadata, settings, management_room, space_room, access_token FROM "user" WHERE mxid=$1`, userID) return get[*User](uq, ctx, getUserByMXIDQuery, userID)
} }
type Settings struct { type Settings struct {
@ -257,12 +276,12 @@ func (user *User) AddSelfParticipantID(ctx context.Context, id string) error {
func (user *User) Insert(ctx context.Context) error { func (user *User) Insert(ctx context.Context) error {
err := user.db.Conn(ctx). err := user.db.Conn(ctx).
QueryRowContext(ctx, `INSERT INTO "user" (mxid, phone_id, session, self_participant_ids, sim_metadata, settings, management_room, space_room, access_token) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING rowid`, user.sqlVariables()...). QueryRowContext(ctx, insertUserQuery, user.sqlVariables()...).
Scan(&user.RowID) Scan(&user.RowID)
return err return err
} }
func (user *User) Update(ctx context.Context) error { func (user *User) Update(ctx context.Context) error {
_, err := user.db.Conn(ctx).ExecContext(ctx, `UPDATE "user" SET phone_id=$2, session=$3, self_participant_ids=$4, sim_metadata=$5, settings=$6, management_room=$7, space_room=$8, access_token=$9 WHERE mxid=$1`, user.sqlVariables()...) _, err := user.db.Conn(ctx).ExecContext(ctx, updateUserQuery, user.sqlVariables()...)
return err return err
} }

View file

@ -120,10 +120,6 @@ func (puppet *Puppet) GetMXID() id.UserID {
return puppet.MXID return puppet.MXID
} }
func (br *GMBridge) GetAllPuppets() []*Puppet {
return br.loadManyPuppets(br.DB.Puppet.GetAll)
}
func (br *GMBridge) loadManyPuppets(query func(ctx context.Context) ([]*database.Puppet, error)) []*Puppet { func (br *GMBridge) loadManyPuppets(query func(ctx context.Context) ([]*database.Puppet, error)) []*Puppet {
br.puppetsLock.Lock() br.puppetsLock.Lock()
defer br.puppetsLock.Unlock() defer br.puppetsLock.Unlock()