| 420 | } |
| 421 | |
| 422 | func (sfq *StorageFileQuery) sqlQuery(ctx context.Context) *sql.Selector { |
| 423 | builder := sql.Dialect(sfq.driver.Dialect()) |
| 424 | t1 := builder.Table(storagefile.Table) |
| 425 | columns := sfq.ctx.Fields |
| 426 | if len(columns) == 0 { |
| 427 | columns = storagefile.Columns |
| 428 | } |
| 429 | selector := builder.Select(t1.Columns(columns...)...).From(t1) |
| 430 | if sfq.sql != nil { |
| 431 | selector = sfq.sql |
| 432 | selector.Select(selector.Columns(columns...)...) |
| 433 | } |
| 434 | if sfq.ctx.Unique != nil && *sfq.ctx.Unique { |
| 435 | selector.Distinct() |
| 436 | } |
| 437 | for _, m := range sfq.modifiers { |
| 438 | m(selector) |
| 439 | } |
| 440 | for _, p := range sfq.predicates { |
| 441 | p(selector) |
| 442 | } |
| 443 | for _, p := range sfq.order { |
| 444 | p(selector) |
| 445 | } |
| 446 | if offset := sfq.ctx.Offset; offset != nil { |
| 447 | // limit is mandatory for offset clause. We start |
| 448 | // with default value, and override it below if needed. |
| 449 | selector.Offset(*offset).Limit(math.MaxInt32) |
| 450 | } |
| 451 | if limit := sfq.ctx.Limit; limit != nil { |
| 452 | selector.Limit(*limit) |
| 453 | } |
| 454 | return selector |
| 455 | } |
| 456 | |
| 457 | // ForUpdate locks the selected rows against concurrent updates, and prevent them from being |
| 458 | // updated, deleted or "selected ... for update" by other sessions, until the transaction is |