mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-09-07 17:14:09 +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
|
|
@ -2003,6 +2003,10 @@ class Queryable:
|
|||
if limit is not None:
|
||||
sql += f" limit {limit}"
|
||||
if offset is not None:
|
||||
# SQLite requires a limit clause before offset - a negative limit
|
||||
# means "no upper bound", so offset works without an explicit limit
|
||||
if limit is None:
|
||||
sql += " limit -1"
|
||||
sql += f" offset {offset}"
|
||||
cursor = self.db.execute(sql, where_args or [])
|
||||
columns = dedupe_keys(c[0] for c in cursor.description)
|
||||
|
|
@ -3594,6 +3598,10 @@ class Table(Queryable):
|
|||
if limit is not None:
|
||||
limit_offset += f" limit {limit}"
|
||||
if offset is not None:
|
||||
# SQLite requires a limit clause before offset - a negative limit
|
||||
# means "no upper bound", so offset works without an explicit limit
|
||||
if limit is None:
|
||||
limit_offset += " limit -1"
|
||||
limit_offset += f" offset {offset}"
|
||||
return sql.format(
|
||||
dbtable=quote_identifier(self.name),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue