Skip to content

Commit ca94f25

Browse files
authored
Merge pull request #1214 from uptrace/fix/add-comments-and-restore-check
fix: restore q.limit check
2 parents 868b17d + 07d32c1 commit ca94f25

File tree

8 files changed

+33
-19
lines changed

8 files changed

+33
-19
lines changed

query_delete.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,12 +384,13 @@ func (q *DeleteQuery) afterDeleteHook(ctx context.Context) error {
384384
return nil
385385
}
386386

387+
// String returns the generated SQL query string. The DeleteQuery instance must not be
388+
// modified during query generation to ensure multiple calls to String() return identical results.
387389
func (q *DeleteQuery) String() string {
388390
buf, err := q.AppendQuery(q.db.Formatter(), nil)
389391
if err != nil {
390392
panic(err)
391393
}
392-
393394
return string(buf)
394395
}
395396

query_insert.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,11 +694,12 @@ func (q *InsertQuery) tryLastInsertID(res sql.Result, dest []interface{}) error
694694
return nil
695695
}
696696

697+
// String returns the generated SQL query string. The InsertQuery instance must not be
698+
// modified during query generation to ensure multiple calls to String() return identical results.
697699
func (q *InsertQuery) String() string {
698700
buf, err := q.AppendQuery(q.db.Formatter(), nil)
699701
if err != nil {
700702
panic(err)
701703
}
702-
703704
return string(buf)
704705
}

query_merge.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,12 +281,13 @@ func (q *MergeQuery) scanOrExec(
281281
return res, nil
282282
}
283283

284+
// String returns the generated SQL query string. The MergeQuery instance must not be
285+
// modified during query generation to ensure multiple calls to String() return identical results.
284286
func (q *MergeQuery) String() string {
285287
buf, err := q.AppendQuery(q.db.Formatter(), nil)
286288
if err != nil {
287289
panic(err)
288290
}
289-
290291
return string(buf)
291292
}
292293

query_raw.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,12 @@ func (q *RawQuery) Operation() string {
9696
return "SELECT"
9797
}
9898

99+
// String returns the generated SQL query string. The RawQuery instance must not be
100+
// modified during query generation to ensure multiple calls to String() return identical results.
99101
func (q *RawQuery) String() string {
100102
buf, err := q.AppendQuery(q.db.Formatter(), nil)
101103
if err != nil {
102104
panic(err)
103105
}
104-
105106
return string(buf)
106107
}

query_select.go

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -979,20 +979,25 @@ func (q *SelectQuery) scanAndCountConcurrently(
979979
var mu sync.Mutex
980980
var firstErr error
981981

982+
// FIXME: clone should not be needed, because the query is not modified here
983+
// and should not be implicitly modified by the Bun lib.
982984
countQuery := q.Clone()
983985

984-
wg.Add(1)
985-
go func() {
986-
defer wg.Done()
987-
988-
if err := q.Scan(ctx, dest...); err != nil {
989-
mu.Lock()
990-
if firstErr == nil {
991-
firstErr = err
986+
// Don't scan results if the user explicitly set Limit(-1).
987+
if q.limit >= 0 {
988+
wg.Add(1)
989+
go func() {
990+
defer wg.Done()
991+
992+
if err := q.Scan(ctx, dest...); err != nil {
993+
mu.Lock()
994+
if firstErr == nil {
995+
firstErr = err
996+
}
997+
mu.Unlock()
992998
}
993-
mu.Unlock()
994-
}
995-
}()
999+
}()
1000+
}
9961001

9971002
wg.Add(1)
9981003
go func() {
@@ -1016,6 +1021,7 @@ func (q *SelectQuery) scanAndCountConcurrently(
10161021
func (q *SelectQuery) scanAndCountSeq(ctx context.Context, dest ...interface{}) (int, error) {
10171022
var firstErr error
10181023

1024+
// Don't scan results if the user explicitly set Limit(-1).
10191025
if q.limit >= 0 {
10201026
firstErr = q.Scan(ctx, dest...)
10211027
}
@@ -1086,12 +1092,13 @@ func (q *SelectQuery) whereExists(ctx context.Context) (bool, error) {
10861092
return n == 1, nil
10871093
}
10881094

1095+
// String returns the generated SQL query string. The SelectQuery instance must not be
1096+
// modified during query generation to ensure multiple calls to String() return identical results.
10891097
func (q *SelectQuery) String() string {
10901098
buf, err := q.AppendQuery(q.db.Formatter(), nil)
10911099
if err != nil {
10921100
panic(err)
10931101
}
1094-
10951102
return string(buf)
10961103
}
10971104

query_table_create.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,11 +408,12 @@ func (q *CreateTableQuery) afterCreateTableHook(ctx context.Context) error {
408408
return nil
409409
}
410410

411+
// String returns the generated SQL query string. The CreateTableQuery instance must not be
412+
// modified during query generation to ensure multiple calls to String() return identical results.
411413
func (q *CreateTableQuery) String() string {
412414
buf, err := q.AppendQuery(q.db.Formatter(), nil)
413415
if err != nil {
414416
panic(err)
415417
}
416-
417418
return string(buf)
418419
}

query_table_drop.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,12 @@ func (q *DropTableQuery) afterDropTableHook(ctx context.Context) error {
165165
return nil
166166
}
167167

168+
// String returns the generated SQL query string. The DropTableQuery instance must not be
169+
// modified during query generation to ensure multiple calls to String() return identical results.
168170
func (q *DropTableQuery) String() string {
169171
buf, err := q.AppendQuery(q.db.Formatter(), nil)
170172
if err != nil {
171173
panic(err)
172174
}
173-
174175
return string(buf)
175176
}

query_update.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,12 +635,13 @@ func (q *UpdateQuery) hasTableAlias(fmter schema.Formatter) bool {
635635
return fmter.HasFeature(feature.UpdateMultiTable | feature.UpdateTableAlias)
636636
}
637637

638+
// String returns the generated SQL query string. The UpdateQuery instance must not be
639+
// modified during query generation to ensure multiple calls to String() return identical results.
638640
func (q *UpdateQuery) String() string {
639641
buf, err := q.AppendQuery(q.db.Formatter(), nil)
640642
if err != nil {
641643
panic(err)
642644
}
643-
644645
return string(buf)
645646
}
646647

0 commit comments

Comments
 (0)