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: object

Chainable 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

limit(n)[source]

Limit the number of results.

offset(n)[source]

Offset the results (for pagination).

select(*columns)[source]

Select specific columns instead of all.

Example

select(“name”, “email”)

build()[source]

Build the complete SQL query.

Returns:

Tuple of (sql_string, params_list).

build_count()[source]

Build a COUNT query.

Returns:

Tuple of (sql_string, params_list).

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.

count()[source]

Execute a COUNT query and return the number of matching rows.

Returns:

Integer count.

exists()[source]

Check if any matching rows exist.

Returns:

Boolean.

class sqbooster.query.InMemoryQuery(rows, schema=None)[source]

Bases: object

Chainable 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()

filter(**kwargs)[source]

Add filter conditions (same operators as Query).

order_by(*columns)[source]

Set ordering. Prefix with ‘-’ for descending.

limit(n)[source]
offset(n)[source]
select(*columns)[source]
all()[source]

Return all matching rows.

first()[source]

Return the first matching row, or None.

one()[source]

Return exactly one matching row. Raises ValueError otherwise.

count()[source]

Return the count of matching rows.

exists()[source]

Return True if any matching rows exist.