fix(api): bound mysql page sizes
This commit is contained in:
@@ -13,8 +13,10 @@ type SQLQuery struct {
|
||||
CountArgs []any
|
||||
}
|
||||
|
||||
const maxSQLPageSize = 10_000
|
||||
|
||||
func buildVehicleListSQL(query url.Values) SQLQuery {
|
||||
limit := parsePositive(query.Get("limit"), 20)
|
||||
limit := parseSQLPageSize(query.Get("limit"), 20)
|
||||
offset := parsePositive(query.Get("offset"), 0)
|
||||
canonicalSourceCount := strconv.Itoa(len(canonicalVehicleProtocols))
|
||||
args := []any{}
|
||||
@@ -65,7 +67,7 @@ func buildVehicleListSQL(query url.Values) SQLQuery {
|
||||
}
|
||||
|
||||
func buildVehicleCoverageSQL(query url.Values) SQLQuery {
|
||||
limit := parsePositive(query.Get("limit"), 20)
|
||||
limit := parseSQLPageSize(query.Get("limit"), 20)
|
||||
offset := parsePositive(query.Get("offset"), 0)
|
||||
canonicalSourceCount := strconv.Itoa(len(canonicalVehicleProtocols))
|
||||
args := []any{}
|
||||
@@ -288,7 +290,7 @@ func archiveMissingFieldPredicate(field string) string {
|
||||
}
|
||||
|
||||
func buildRealtimeLocationSQL(query url.Values) SQLQuery {
|
||||
limit := parsePositive(query.Get("limit"), 20)
|
||||
limit := parseSQLPageSize(query.Get("limit"), 20)
|
||||
offset := parsePositive(query.Get("offset"), 0)
|
||||
args := []any{}
|
||||
where := []string{"1 = 1"}
|
||||
@@ -316,7 +318,7 @@ func buildRealtimeLocationSQL(query url.Values) SQLQuery {
|
||||
}
|
||||
|
||||
func buildVehicleRealtimeSQL(query url.Values) SQLQuery {
|
||||
limit := parsePositive(query.Get("limit"), 20)
|
||||
limit := parseSQLPageSize(query.Get("limit"), 20)
|
||||
offset := parsePositive(query.Get("offset"), 0)
|
||||
canonicalSourceCount := strconv.Itoa(len(canonicalVehicleProtocols))
|
||||
args := []any{}
|
||||
@@ -415,7 +417,7 @@ func buildVehicleRealtimeSQL(query url.Values) SQLQuery {
|
||||
}
|
||||
|
||||
func buildDailyMileageSQL(query url.Values) SQLQuery {
|
||||
limit := parsePositive(query.Get("limit"), 20)
|
||||
limit := parseSQLPageSize(query.Get("limit"), 20)
|
||||
offset := parsePositive(query.Get("offset"), 0)
|
||||
args := []any{}
|
||||
where := []string{"1 = 1"}
|
||||
@@ -671,11 +673,22 @@ func appendVINListFilter(where []string, args []any, column string, raw string)
|
||||
}
|
||||
|
||||
func buildLimitOffset(query url.Values) (int, int) {
|
||||
return parsePositive(query.Get("limit"), 20), parsePositive(query.Get("offset"), 0)
|
||||
return parseSQLPageSize(query.Get("limit"), 20), parsePositive(query.Get("offset"), 0)
|
||||
}
|
||||
|
||||
func parseLimitOffset(rawLimit, rawOffset string) (int, int) {
|
||||
return parsePositive(rawLimit, 20), parsePositive(rawOffset, 0)
|
||||
return parseSQLPageSize(rawLimit, 20), parsePositive(rawOffset, 0)
|
||||
}
|
||||
|
||||
func parseSQLPageSize(raw string, fallback int) int {
|
||||
value, err := strconv.Atoi(raw)
|
||||
if err != nil || value <= 0 {
|
||||
return fallback
|
||||
}
|
||||
if value > maxSQLPageSize {
|
||||
return maxSQLPageSize
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func mustInt(value string) int {
|
||||
|
||||
Reference in New Issue
Block a user