forked from MirrorHub/mautrix-whatsapp
Use upserts on SQLite too
This commit is contained in:
parent
8168d0efff
commit
658b37f81f
1 changed files with 13 additions and 36 deletions
|
@ -19,7 +19,6 @@ package database
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
log "maunium.net/go/maulogger/v2"
|
log "maunium.net/go/maulogger/v2"
|
||||||
|
@ -60,14 +59,7 @@ func (store *SQLStateStore) IsRegistered(userID id.UserID) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (store *SQLStateStore) MarkRegistered(userID id.UserID) {
|
func (store *SQLStateStore) MarkRegistered(userID id.UserID) {
|
||||||
var err error
|
_, err := store.db.Exec("INSERT INTO mx_registrations (user_id) VALUES ($1) ON CONFLICT (user_id) DO NOTHING", userID)
|
||||||
if store.db.dialect == "postgres" {
|
|
||||||
_, err = store.db.Exec("INSERT INTO mx_registrations (user_id) VALUES ($1) ON CONFLICT (user_id) DO NOTHING", userID)
|
|
||||||
} else if store.db.dialect == "sqlite3" {
|
|
||||||
_, err = store.db.Exec("INSERT OR REPLACE INTO mx_registrations (user_id) VALUES ($1)", userID)
|
|
||||||
} else {
|
|
||||||
err = fmt.Errorf("unsupported dialect %s", store.db.dialect)
|
|
||||||
}
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
store.log.Warnfln("Failed to mark %s as registered: %v", userID, err)
|
store.log.Warnfln("Failed to mark %s as registered: %v", userID, err)
|
||||||
}
|
}
|
||||||
|
@ -161,31 +153,20 @@ func (store *SQLStateStore) IsMembership(roomID id.RoomID, userID id.UserID, all
|
||||||
}
|
}
|
||||||
|
|
||||||
func (store *SQLStateStore) SetMembership(roomID id.RoomID, userID id.UserID, membership event.Membership) {
|
func (store *SQLStateStore) SetMembership(roomID id.RoomID, userID id.UserID, membership event.Membership) {
|
||||||
var err error
|
_, err := store.db.Exec(`
|
||||||
if store.db.dialect == "postgres" {
|
INSERT INTO mx_user_profile (room_id, user_id, membership) VALUES ($1, $2, $3)
|
||||||
_, err = store.db.Exec(`INSERT INTO mx_user_profile (room_id, user_id, membership) VALUES ($1, $2, $3)
|
ON CONFLICT (room_id, user_id) DO UPDATE SET membership=excluded.membership
|
||||||
ON CONFLICT (room_id, user_id) DO UPDATE SET membership=$3`, roomID, userID, membership)
|
`, roomID, userID, membership)
|
||||||
} else if store.db.dialect == "sqlite3" {
|
|
||||||
_, err = store.db.Exec("INSERT OR REPLACE INTO mx_user_profile (room_id, user_id, membership) VALUES ($1, $2, $3)", roomID, userID, membership)
|
|
||||||
} else {
|
|
||||||
err = fmt.Errorf("unsupported dialect %s", store.db.dialect)
|
|
||||||
}
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
store.log.Warnfln("Failed to set membership of %s in %s to %s: %v", userID, roomID, membership, err)
|
store.log.Warnfln("Failed to set membership of %s in %s to %s: %v", userID, roomID, membership, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (store *SQLStateStore) SetMember(roomID id.RoomID, userID id.UserID, member *event.MemberEventContent) {
|
func (store *SQLStateStore) SetMember(roomID id.RoomID, userID id.UserID, member *event.MemberEventContent) {
|
||||||
var err error
|
_, err := store.db.Exec(`
|
||||||
if store.db.dialect == "postgres" {
|
INSERT INTO mx_user_profile (room_id, user_id, membership, displayname, avatar_url) VALUES ($1, $2, $3, $4, $5)
|
||||||
_, err = store.db.Exec(`INSERT INTO mx_user_profile (room_id, user_id, membership, displayname, avatar_url) VALUES ($1, $2, $3, $4, $5)
|
ON CONFLICT (room_id, user_id) DO UPDATE SET membership=excluded.membership, displayname=excluded.displayname, avatar_url=excluded.avatar_url
|
||||||
ON CONFLICT (room_id, user_id) DO UPDATE SET membership=$3, displayname=$4, avatar_url=$5`, roomID, userID, member.Membership, member.Displayname, member.AvatarURL)
|
`, roomID, userID, member.Membership, member.Displayname, member.AvatarURL)
|
||||||
} else if store.db.dialect == "sqlite3" {
|
|
||||||
_, err = store.db.Exec("INSERT OR REPLACE INTO mx_user_profile (room_id, user_id, membership, displayname, avatar_url) VALUES ($1, $2, $3, $4, $5)",
|
|
||||||
roomID, userID, member.Membership, member.Displayname, member.AvatarURL)
|
|
||||||
} else {
|
|
||||||
err = fmt.Errorf("unsupported dialect %s", store.db.dialect)
|
|
||||||
}
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
store.log.Warnfln("Failed to set membership of %s in %s to %s: %v", userID, roomID, member, err)
|
store.log.Warnfln("Failed to set membership of %s in %s to %s: %v", userID, roomID, member, err)
|
||||||
}
|
}
|
||||||
|
@ -197,14 +178,10 @@ func (store *SQLStateStore) SetPowerLevels(roomID id.RoomID, levels *event.Power
|
||||||
store.log.Errorfln("Failed to marshal power levels of %s: %v", roomID, err)
|
store.log.Errorfln("Failed to marshal power levels of %s: %v", roomID, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if store.db.dialect == "postgres" {
|
_, err = store.db.Exec(`
|
||||||
_, err = store.db.Exec(`INSERT INTO mx_room_state (room_id, power_levels) VALUES ($1, $2)
|
INSERT INTO mx_room_state (room_id, power_levels) VALUES ($1, $2)
|
||||||
ON CONFLICT (room_id) DO UPDATE SET power_levels=$2`, roomID, levelsBytes)
|
ON CONFLICT (room_id) DO UPDATE SET power_levels=excluded.power_levels
|
||||||
} else if store.db.dialect == "sqlite3" {
|
`, roomID, levelsBytes)
|
||||||
_, err = store.db.Exec("INSERT OR REPLACE INTO mx_room_state (room_id, power_levels) VALUES ($1, $2)", roomID, levelsBytes)
|
|
||||||
} else {
|
|
||||||
err = fmt.Errorf("unsupported dialect %s", store.db.dialect)
|
|
||||||
}
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
store.log.Warnfln("Failed to store power levels of %s: %v", roomID, err)
|
store.log.Warnfln("Failed to store power levels of %s: %v", roomID, err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue