From e2ee74428ae604605ffb795a2588d2d6db9eab77 Mon Sep 17 00:00:00 2001 From: jvoisin Date: Mon, 18 Mar 2024 17:35:20 +0100 Subject: [PATCH] Minor concatenation-related simplifications in internal/storage/ Use plain strings concatenation instead of building an array and then joining it. --- internal/storage/batch.go | 8 +++----- internal/storage/entry_query_builder.go | 10 +++++----- internal/storage/feed_query_builder.go | 12 ++++++------ 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/internal/storage/batch.go b/internal/storage/batch.go index 107d480e..bf6a0d24 100644 --- a/internal/storage/batch.go +++ b/internal/storage/batch.go @@ -60,18 +60,16 @@ func (b *BatchBuilder) WithoutDisabledFeeds() *BatchBuilder { } func (b *BatchBuilder) FetchJobs() (jobs model.JobList, err error) { - var parts []string - parts = append(parts, `SELECT id, user_id FROM feeds`) + query := `SELECT id, user_id FROM feeds` if len(b.conditions) > 0 { - parts = append(parts, fmt.Sprintf("WHERE %s", strings.Join(b.conditions, " AND "))) + query += fmt.Sprintf(" WHERE %s", strings.Join(b.conditions, " AND ")) } if b.limit > 0 { - parts = append(parts, fmt.Sprintf("ORDER BY next_check_at ASC LIMIT %d", b.limit)) + query += fmt.Sprintf(" ORDER BY next_check_at ASC LIMIT %d", b.limit) } - query := strings.Join(parts, " ") rows, err := b.db.Query(query, b.args...) if err != nil { return nil, fmt.Errorf(`store: unable to fetch batch of jobs: %v`, err) diff --git a/internal/storage/entry_query_builder.go b/internal/storage/entry_query_builder.go index 70d04e25..680fbedb 100644 --- a/internal/storage/entry_query_builder.go +++ b/internal/storage/entry_query_builder.go @@ -439,21 +439,21 @@ func (e *EntryQueryBuilder) buildCondition() string { } func (e *EntryQueryBuilder) buildSorting() string { - var parts []string + var parts string if len(e.sortExpressions) > 0 { - parts = append(parts, fmt.Sprintf(`ORDER BY %s`, strings.Join(e.sortExpressions, ", "))) + parts += fmt.Sprintf(" ORDER BY %s", strings.Join(e.sortExpressions, ", ")) } if e.limit > 0 { - parts = append(parts, fmt.Sprintf(`LIMIT %d`, e.limit)) + parts += fmt.Sprintf(" LIMIT %d", e.limit) } if e.offset > 0 { - parts = append(parts, fmt.Sprintf(`OFFSET %d`, e.offset)) + parts += fmt.Sprintf(" OFFSET %d", e.offset) } - return strings.Join(parts, " ") + return parts } // NewEntryQueryBuilder returns a new EntryQueryBuilder. diff --git a/internal/storage/feed_query_builder.go b/internal/storage/feed_query_builder.go index bceaf4ed..e12107ec 100644 --- a/internal/storage/feed_query_builder.go +++ b/internal/storage/feed_query_builder.go @@ -91,25 +91,25 @@ func (f *FeedQueryBuilder) buildCounterCondition() string { } func (f *FeedQueryBuilder) buildSorting() string { - var parts []string + var parts string if len(f.sortExpressions) > 0 { - parts = append(parts, fmt.Sprintf(`ORDER BY %s`, strings.Join(f.sortExpressions, ", "))) + parts += fmt.Sprintf(" ORDER BY %s", strings.Join(f.sortExpressions, ", ")) } if len(parts) > 0 { - parts = append(parts, ", lower(f.title) ASC") + parts += ", lower(f.title) ASC" } if f.limit > 0 { - parts = append(parts, fmt.Sprintf(`LIMIT %d`, f.limit)) + parts += fmt.Sprintf(" LIMIT %d", f.limit) } if f.offset > 0 { - parts = append(parts, fmt.Sprintf(`OFFSET %d`, f.offset)) + parts += fmt.Sprintf(" OFFSET %d", f.offset) } - return strings.Join(parts, " ") + return parts } // GetFeed returns a single feed that match the condition.