mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-09-10 10:34:10 +02:00
Emit LIMIT -1 when offset is used without limit, closes #816
SQLite requires a LIMIT clause to appear before OFFSET, so passing offset
without limit generated invalid SQL such as:
select * from "t" offset 2
which raised OperationalError: near "2": syntax error.
A negative limit means "no upper bound" in SQLite, so "limit -1 offset N"
returns all rows from position N onwards.
Fixed in three places that build LIMIT/OFFSET SQL:
- Queryable.rows_where() - also covers pks_and_rows_where()
- Table.search_sql() - also covers search()
- the "sqlite-utils rows" CLI command
This commit is contained in:
parent
6a456830ca
commit
a8b3a80aec
5 changed files with 37 additions and 0 deletions
|
|
@ -59,6 +59,9 @@ def test_rows_where_order_by(where, order_by, expected_ids, fresh_db):
|
|||
(None, 3, [1, 2, 3]),
|
||||
(0, 3, [1, 2, 3]),
|
||||
(3, 3, [4, 5, 6]),
|
||||
# offset without limit should return every remaining row
|
||||
(97, None, [98, 99, 100]),
|
||||
(0, None, list(range(1, 101))),
|
||||
],
|
||||
)
|
||||
def test_rows_where_offset_limit(fresh_db, offset, limit, expected):
|
||||
|
|
@ -70,6 +73,12 @@ def test_rows_where_offset_limit(fresh_db, offset, limit, expected):
|
|||
]
|
||||
|
||||
|
||||
def test_pks_and_rows_where_offset_without_limit(fresh_db):
|
||||
table = fresh_db["rows"]
|
||||
table.insert_all([{"id": id} for id in range(1, 6)], pk="id")
|
||||
assert [pk for pk, _ in table.pks_and_rows_where(offset=3, order_by="id")] == [4, 5]
|
||||
|
||||
|
||||
def test_pks_and_rows_where_rowid(fresh_db):
|
||||
table = fresh_db["rowid_table"]
|
||||
table.insert_all({"number": i + 10} for i in range(3))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue