Query¶
Query builders for sqbooster database backends.
Provides a chainable, database-agnostic query interface for both SQL and in-memory (non-SQL) backends.
- Supported filter operators:
field=value WHERE field = value field__ne=value WHERE field != value field__gt=value WHERE field > value field__gte=value WHERE field >= value field__lt=value WHERE field < value field__lte=value WHERE field <= value field__like=value WHERE field LIKE value field__contains=v WHERE field LIKE ‘%value%’ field__startswith=v WHERE field LIKE ‘value%’ field__endswith=v WHERE field LIKE ‘%value’ field__in=value WHERE field IN (value) (value must be list/tuple) field__notin=value WHERE field NOT IN (value) field__isnull=True WHERE field IS NULL field__isnull=False WHERE field IS NOT NULL
- class sqbooster.query.Query(backend, table)[source]¶
Bases:
objectChainable query builder for database operations.
- Parameters:
backend – A DatabaseBackend instance.
table – Table name to query.
Example
- results = (db.query(“users”)
.filter(age__gte=25) .filter(name__like=”%Ali%”) .order_by(“-age”) .limit(10) .all())
- property placeholder¶
Return the placeholder character for this backend.
- filter(**kwargs)[source]¶
Add filter conditions to the query.
- Uses double-underscore notation for operators:
filter(age__gt=25, name__like=”%Ali%”)
- order_by(*columns)[source]¶
Set ordering. Prefix with ‘-’ for descending.
Example
order_by(“name”) -> ORDER BY name ASC order_by(“-age”, “name”) -> ORDER BY age DESC, name ASC
- all()[source]¶
Execute the query and return all results as list of dicts.
Results are type-converted using the table schema when available.
- Returns:
List of dictionaries, one per row.
- first()[source]¶
Execute the query and return only the first result.
- Returns:
Dictionary for the first row, or None if no results.
- one()[source]¶
Execute the query and return exactly one result.
- Returns:
Dictionary for the single row.
- Raises:
ValueError – If zero or more than one row is returned.
- class sqbooster.query.InMemoryQuery(rows, schema=None)[source]¶
Bases:
objectChainable query builder that operates on in-memory Python data.
Used by non-SQL backends (JSON, Pickle, Redis, Mongo) to provide the same query API as the SQL Query class without generating SQL.
- Parameters:
rows – List of dicts representing table rows.
schema – Optional TableSchema for type-aware operations.
Example
query = InMemoryQuery(rows, schema) results = query.filter(age__gte=25).order_by(“-name”).all()