fix(vote): fix bulk del sql issue

This commit is contained in:
2026-06-26 00:12:52 +02:00
parent 313d0d418c
commit c5204c6970
+8 -3
View File
@@ -3,6 +3,7 @@ package database
import (
"errors"
"fmt"
"log"
"strings"
"tomatentum.net/outfit-voting-abi26/internal/util"
@@ -16,7 +17,7 @@ const BULKINSERTVOTEENTRY string = "INSERT INTO vote(id, category, name, votes)
const SETVOTE string = "UPDATE vote SET votes = $1 WHERE id = $2"
const INCVOTE string = "UPDATE vote SET votes = votes + 1 WHERE id = $1"
const DELVOTEENTRY string = "DELETE FROM vote WHERE id = $1"
const BULKDELVOTEENTRY string = "DELETE FROM vote WHERE key IN (%s)"
const BULKDELVOTEENTRY string = "DELETE FROM vote WHERE id IN (%s)"
const GETVOTES string = "SELECT * FROM vote ORDER BY votes DESC"
const GETVOTEOPTIONS string = "SELECT id, category, name FROM vote"
const GETVOTE string = "SELECT * FROM vote WHERE id = $1"
@@ -120,12 +121,16 @@ func DelVoteEntry(id string) error {
func BulkDelVoteEntry(ids []string) error {
placeholders := make([]string, len(ids))
args := make([]any, len(ids))
for i := 0; i < len(ids); i++ {
placeholders = append(placeholders, fmt.Sprintf("$%d", i))
placeholders[i] = fmt.Sprintf("$%d", i+1)
args[i] = ids[i]
}
res, err := db.Exec(fmt.Sprintf(BULKDELVOTEENTRY, strings.Join(placeholders, ", ")), ids)
stmnt := fmt.Sprintf(BULKDELVOTEENTRY, strings.Join(placeholders, ", "))
log.Println(stmnt)
res, err := db.Exec(stmnt, args...)
if err != nil {
return err