Skip to content

Commit c02d00e

Browse files
committed
Refactor query
The generated sql queries are only concatenated at the end has_ban is now ANDed with pid/fc queries instead of ORed
1 parent 9770d21 commit c02d00e

File tree

1 file changed

+25
-24
lines changed

1 file changed

+25
-24
lines changed

api/query.go

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"net/http"
77
"regexp"
8+
"strings"
89
"wwfc/database"
910
"wwfc/logging"
1011

@@ -48,7 +49,7 @@ var (
4849
numOnlyRegex = regexp.MustCompile("^[0-9]+$")
4950
)
5051

51-
const QUERYBASE = `SELECT profile_id FROM users WHERE`
52+
const QUERYBASE = `SELECT profile_id FROM users WHERE `
5253

5354
func HandleQuery(req any, _ bool, _ *http.Request) (any, int, error) {
5455
_req := req.(QueryRequest)
@@ -137,46 +138,43 @@ func validateOptions(req QueryRequest) error {
137138
}
138139

139140
func makeQuery(req QueryRequest) string {
140-
query := QUERYBASE
141+
queries := []string{}
141142

142143
if req.IP != "" {
143-
query += fmt.Sprintf(" last_ip_address = '%s' AND", req.IP)
144+
queries = append(queries, fmt.Sprintf("last_ip_address = '%s'", req.IP))
144145
}
145146

146147
if req.DeviceID != 0 {
147-
query += fmt.Sprintf(" %d = ANY(ng_device_id) AND", req.DeviceID)
148+
queries = append(queries, fmt.Sprintf("%d = ANY(ng_device_id)", req.DeviceID))
148149
}
149150

150151
if req.Csnum != "" {
151-
query += fmt.Sprintf(" '%s' = ANY(csnum) AND", req.Csnum)
152+
queries = append(queries, fmt.Sprintf("'%s' = ANY(csnum)", req.Csnum))
152153
}
153154

154155
if req.DiscordID != "" {
155-
query += fmt.Sprintf(" discord_id = '%s' AND", req.DiscordID)
156+
queries = append(queries, fmt.Sprintf("discord_id = '%s'", req.DiscordID))
156157
}
157158

158159
if req.UserID != 0 {
159-
query += fmt.Sprintf(" user_id = %d AND", req.UserID)
160+
queries = append(queries, fmt.Sprintf("user_id = %d", req.UserID))
160161
}
161162

162163
switch req.HasBan {
163164
case 1:
164-
query += " has_ban = false AND"
165+
queries = append(queries, "has_ban = false")
165166
case 2:
166-
query += " has_ban = true AND"
167+
queries = append(queries, "has_ban = true")
167168
}
168169

169-
query = query[0 : len(query)-4]
170-
query += ";"
171-
172-
return query
170+
return QUERYBASE + strings.Join(queries, " AND ") + ";"
173171
}
174172

175173
func makeQueryFromUser(user database.User, rrr QueryRequest) string {
176-
query := QUERYBASE
174+
queries := []string{}
177175

178176
if user.LastIPAddress != "" {
179-
query += fmt.Sprintf(" last_ip_address = '%s' OR", user.LastIPAddress)
177+
queries = append(queries, fmt.Sprintf("last_ip_address = '%s'", user.LastIPAddress))
180178
}
181179

182180
deviceIDs := []string{}
@@ -189,26 +187,29 @@ func makeQueryFromUser(user database.User, rrr QueryRequest) string {
189187
}
190188

191189
if len(deviceIDs) > 0 {
192-
query += fmt.Sprintf(" %s && ng_device_id OR", fmtPSQLArray(deviceIDs))
190+
queries = append(queries, fmt.Sprintf("%s && ng_device_id", fmtPSQLArray(deviceIDs)))
193191
}
194192

195193
if len(user.Csnum) > 0 {
196-
query += fmt.Sprintf(" %s && csnum OR", fmtPSQLArray(user.Csnum))
194+
queries = append(queries, fmt.Sprintf("%s && csnum", fmtPSQLArray(user.Csnum)))
197195
}
198196

199-
query += fmt.Sprintf(" user_id = %d OR", user.UserId)
197+
queries = append(queries, fmt.Sprintf("user_id = %d", user.UserId))
198+
199+
if user.DiscordID != "" {
200+
queries = append(queries, fmt.Sprintf("discord_id = '%s'", user.DiscordID))
201+
}
202+
203+
query := strings.Join(queries, " OR ")
200204

201205
switch rrr.HasBan {
202206
case 1:
203-
query += " has_ban = false OR"
207+
query = fmt.Sprintf("(%s) AND has_ban = true", query)
204208
case 2:
205-
query += " has_ban = true OR"
209+
query = fmt.Sprintf("(%s) AND has_ban = true", query)
206210
}
207211

208-
query = query[0 : len(query)-3]
209-
query += ";"
210-
211-
return query
212+
return QUERYBASE + query + ";"
212213
}
213214

214215
func fmtPSQLArray[S any](arr []S) string {

0 commit comments

Comments
 (0)