فهرست منبع

perf(storage): use scalar comparison for single-element slices in query builder

Frédéric Guillot 1 هفته پیش
والد
کامیت
26d9195d21
1فایلهای تغییر یافته به همراه11 افزوده شده و 3 حذف شده
  1. 11 3
      internal/storage/entry_query_builder.go

+ 11 - 3
internal/storage/entry_query_builder.go

@@ -107,8 +107,13 @@ func (e *EntryQueryBuilder) AfterEntryID(entryID int64) *EntryQueryBuilder {
 
 // WithEntryIDs filter by entry IDs.
 func (e *EntryQueryBuilder) WithEntryIDs(entryIDs []int64) *EntryQueryBuilder {
-	e.conditions = append(e.conditions, fmt.Sprintf("e.id = ANY($%d)", len(e.args)+1))
-	e.args = append(e.args, pq.Int64Array(entryIDs))
+	if len(entryIDs) == 1 {
+		e.conditions = append(e.conditions, fmt.Sprintf("e.id = $%d", len(e.args)+1))
+		e.args = append(e.args, entryIDs[0])
+	} else if len(entryIDs) > 1 {
+		e.conditions = append(e.conditions, fmt.Sprintf("e.id = ANY($%d)", len(e.args)+1))
+		e.args = append(e.args, pq.Int64Array(entryIDs))
+	}
 	return e
 }
 
@@ -150,7 +155,10 @@ func (e *EntryQueryBuilder) WithStatus(status string) *EntryQueryBuilder {
 
 // WithStatuses filter by a list of entry statuses.
 func (e *EntryQueryBuilder) WithStatuses(statuses []string) *EntryQueryBuilder {
-	if len(statuses) > 0 {
+	if len(statuses) == 1 {
+		e.conditions = append(e.conditions, fmt.Sprintf("e.status = $%d", len(e.args)+1))
+		e.args = append(e.args, statuses[0])
+	} else if len(statuses) > 1 {
 		e.conditions = append(e.conditions, fmt.Sprintf("e.status = ANY($%d)", len(e.args)+1))
 		e.args = append(e.args, pq.StringArray(statuses))
 	}