Skip to content

Commit dc7318a

Browse files
authored
Don't remove current offset when chaining limits (#48)
* Don't remove current offset when chaining limits * wip
1 parent 6229fb7 commit dc7318a

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

Sources/StructuredQueriesCore/Documentation.docc/Articles/SelectStatements.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,8 @@ The `limit(_:offset:)` function is used to change a query's `LIMIT` and `OFFSET`
721721
}
722722
}
723723

724-
Multiple chained calls to `limit` will override the limit and offset to the last call:
724+
Multiple chained calls to `limit` will override the limit and offset to the last call, using the
725+
existing offset if none is provided:
725726

726727
@Row {
727728
@Column {
@@ -755,6 +756,22 @@ Multiple chained calls to `limit` will override the limit and offset to the last
755756
}
756757
}
757758

759+
@Row {
760+
@Column {
761+
```swift
762+
Reminder
763+
.limit(10, offset: 10)
764+
.limit(20, offset: 20)
765+
```
766+
}
767+
@Column {
768+
```sql
769+
SELECT … FROM "reminders"
770+
LIMIT 20 OFFSET 20
771+
```
772+
}
773+
}
774+
758775
### Compound selects
759776

760777
It is possible to combine multiple selects together using the `union`, `intersect`, and `except`

Sources/StructuredQueriesCore/Statements/Select.swift

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,8 +1282,8 @@ extension Select {
12821282
where Joins == (repeat each J) {
12831283
var select = self
12841284
select.limit = _LimitClause(
1285-
maxLength: maxLength(From.columns, repeat (each J).columns),
1286-
offset: offset?(From.columns, repeat (each J).columns)
1285+
maxLength: maxLength(From.columns, repeat (each J).columns).queryFragment,
1286+
offset: offset?(From.columns, repeat (each J).columns).queryFragment ?? select.limit?.offset
12871287
)
12881288
return select
12891289
}
@@ -1297,7 +1297,10 @@ extension Select {
12971297
public func limit<each J: Table>(_ maxLength: Int, offset: Int? = nil) -> Self
12981298
where Joins == (repeat each J) {
12991299
var select = self
1300-
select.limit = _LimitClause(maxLength: maxLength, offset: offset)
1300+
select.limit = _LimitClause(
1301+
maxLength: maxLength.queryFragment,
1302+
offset: offset?.queryFragment ?? select.limit?.offset
1303+
)
13011304
return select
13021305
}
13031306

@@ -1488,14 +1491,6 @@ public struct _LimitClause: QueryExpression {
14881491
let maxLength: QueryFragment
14891492
let offset: QueryFragment?
14901493

1491-
init(
1492-
maxLength: some QueryExpression,
1493-
offset: (some QueryExpression)? = Int?.none
1494-
) {
1495-
self.maxLength = maxLength.queryFragment
1496-
self.offset = offset?.queryFragment
1497-
}
1498-
14991494
public var queryFragment: QueryFragment {
15001495
var query: QueryFragment = "LIMIT \(maxLength)"
15011496
if let offset {

0 commit comments

Comments
 (0)