Skip to content

QueryBuilder

QueryBuilder

QueryBuilder()

A class for constructing database queries in a structured way.

This class provides a fluent interface for building queriess. It encapsulates the query building logic, making it easier to construct complex queries in a readable and maintainable way.

Classes:

Name Description
Mode

Enum representing the different query modes.

Methods:

Name Description
set_count_mode

Sets the count mode flag.

set_delete_mode

Sets the delete mode flag.

set_equal

Sets the equality filters for the query.

set_greater_than

Sets the greater than filters for the query.

set_greater_than_or_equal

Sets the greater than or equal filters for the query.

set_included

Sets the included filter values for the query.

set_insert_data

Sets the data to be inserted in an insert query.

set_less_than

Sets the less than filters for the query.

set_less_than_or_equal

Sets the less than or equal filters for the query.

set_not_equal

Sets the non-equality filters for the query.

set_order_by_field

Sets the field and direction for ordering query results.

set_select_fields

Sets the selected fields for the query.

set_update_data

Sets the data to be updated in an update query.

Attributes:

Name Type Description
count_mode bool

Gets the count mode flag.

delete_mode bool

Gets the delete mode flag.

equal tuple[tuple[str, Any], ...]

Gets the equality filters for the query.

greater_than tuple[tuple[str, Any], ...]

Gets the greater than filters for the query.

greater_than_or_equal tuple[tuple[str, Any], ...]

Gets the greater than or equal filters for the query.

included tuple[tuple[str, Any], ...]

Gets the included filter values for the query.

insert_data dict[str, Any] | None

Gets the data to be inserted in an insert query.

less_than tuple[tuple[str, Any], ...]

Gets the less than filters for the query.

less_than_or_equal tuple[tuple[str, Any], ...]

Gets the less than or equal filters for the query.

mode Mode

Determines the query mode based on the current state of the QueryBuilder.

not_equal tuple[tuple[str, Any], ...]

Gets the non-equality filters for the query.

order_by_field tuple[str, bool] | None

Gets the field and direction for ordering query results.

select_fields tuple[str, ...] | Literal['*']

Gets the selected fields for the query.

update_data dict[str, Any] | None

Gets the data to be updated in an update query.

Source code in supadantic/query_builder.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
def __init__(self) -> None:
    self._select_fields: tuple[str, ...] | None = None
    self._equal: tuple[tuple[str, Any], ...] = ()
    self._not_equal: tuple[tuple[str, Any], ...] = ()
    self._less_than_or_equal: tuple[tuple[str, Any], ...] = ()
    self._greater_than: tuple[tuple[str, Any], ...] = ()
    self._less_than: tuple[tuple[str, Any], ...] = ()
    self._included: tuple[tuple[str, Any], ...] = ()
    self._greater_than_or_equal: tuple[tuple[str, Any], ...] = ()
    self._insert_data: dict[str, Any] | None = None
    self._update_data: dict[str, Any] | None = None
    self._delete_mode: bool = False
    self._count_mode: bool = False
    self._order_by_field: tuple[str, bool] | None = None

count_mode property

count_mode: bool

Gets the count mode flag.

Returns:

Type Description
bool

True if the query is in count mode, False otherwise.

delete_mode property

delete_mode: bool

Gets the delete mode flag.

Returns:

Type Description
bool

True if the query is in delete mode, False otherwise.

equal property

equal: tuple[tuple[str, Any], ...]

Gets the equality filters for the query.

Returns:

Type Description
tuple[tuple[str, Any], ...]

A tuple of tuples, where each inner tuple contains a field name and its desired value for equality filtering.

greater_than property

greater_than: tuple[tuple[str, Any], ...]

Gets the greater than filters for the query.

Returns:

Type Description
tuple[tuple[str, Any], ...]

A tuple of tuples, where each inner tuple contains a field name and its desired value for greater than filtering.

greater_than_or_equal property

greater_than_or_equal: tuple[tuple[str, Any], ...]

Gets the greater than or equal filters for the query.

Returns:

Type Description
tuple[tuple[str, Any], ...]

A tuple of tuples, where each inner tuple contains a field name and its desired value for greater than or equal filtering.

included property

included: tuple[tuple[str, Any], ...]

Gets the included filter values for the query.

Returns:

Type Description
tuple[tuple[str, Any], ...]

A tuple of tuples, where each inner tuple contains a field name and its desired value for included filtering.

insert_data property

insert_data: dict[str, Any] | None

Gets the data to be inserted in an insert query.

Returns:

Type Description
dict[str, Any] | None

A dictionary representing the data to be inserted, or None if no data has been set for insertion.

less_than property

less_than: tuple[tuple[str, Any], ...]

Gets the less than filters for the query.

Returns:

Type Description
tuple[tuple[str, Any], ...]

A tuple of tuples, where each inner tuple contains a field name and its desired value for less than filtering.

less_than_or_equal property

less_than_or_equal: tuple[tuple[str, Any], ...]

Gets the less than or equal filters for the query.

Returns:

Type Description
tuple[tuple[str, Any], ...]

A tuple of tuples, where each inner tuple contains a field name and its desired value for less than or equal filtering.

mode property

mode: Mode

Determines the query mode based on the current state of the QueryBuilder.

Returns:

Type Description
Mode

The QueryBuilder.Mode enum representing the determined mode.

not_equal property

not_equal: tuple[tuple[str, Any], ...]

Gets the non-equality filters for the query.

Returns:

Type Description
tuple[tuple[str, Any], ...]

A tuple of tuples, where each inner tuple contains a field name and its desired value for non-equality filtering.

order_by_field property

order_by_field: tuple[str, bool] | None

Gets the field and direction for ordering query results.

Returns:

Type Description
tuple[str, bool] | None

A tuple containing the field name and a boolean indicating descending order (True) or ascending order (False), or None if no ordering has been set.

select_fields property

select_fields: tuple[str, ...] | Literal['*']

Gets the selected fields for the query.

If no specific fields have been selected, this property returns "*", which indicates that all fields should be selected.

Returns:

Type Description
tuple[str, ...] | Literal[*]

A tuple of strings representing the selected fields, or "*" if no fields have been explicitly selected.

update_data property

update_data: dict[str, Any] | None

Gets the data to be updated in an update query.

Returns:

Type Description
dict[str, Any] | None

A dictionary representing the data to be updated, or None if no data has been set for updating.

Mode

Bases: Enum

Enum representing the different query modes.

The Mode enum defines the different types of queries that can be built using the QueryBuilder class.

set_count_mode

set_count_mode(value: bool) -> None

Sets the count mode flag.

Parameters:

Name Type Description Default
value bool

True to set the query to count mode, False otherwise.

required
Source code in supadantic/query_builder.py
367
368
369
370
371
372
373
374
375
def set_count_mode(self, value: bool) -> None:
    """
    Sets the count mode flag.

    Args:
        value (bool): True to set the query to count mode, False otherwise.
    """

    self._count_mode = value

set_delete_mode

set_delete_mode(value: bool) -> None

Sets the delete mode flag.

Parameters:

Name Type Description Default
value bool

True to set the query to delete mode, False otherwise.

required
Source code in supadantic/query_builder.py
357
358
359
360
361
362
363
364
365
def set_delete_mode(self, value: bool) -> None:
    """
    Sets the delete mode flag.

    Args:
        value: True to set the query to delete mode, False otherwise.
    """

    self._delete_mode = value

set_equal

set_equal(**kwargs) -> None

Sets the equality filters for the query.

This method accepts keyword arguments representing the equality filters and appends them to the existing set of equality filters.

Parameters:

Name Type Description Default
**kwargs

Key-value pairs where keys are field names and values are the desired values for equality.

{}
Source code in supadantic/query_builder.py
244
245
246
247
248
249
250
251
252
253
254
255
def set_equal(self, **kwargs) -> None:
    """
    Sets the equality filters for the query.

    This method accepts keyword arguments representing the equality filters
    and appends them to the existing set of equality filters.

    Args:
        **kwargs: Key-value pairs where keys are field names and values are the desired values for equality.
    """

    self._equal += self._dict_to_tuple(data=kwargs)

set_greater_than

set_greater_than(**kwargs) -> None

Sets the greater than filters for the query.

This method accepts keyword arguments representing the greater than filters and appends them to the existing set of greater than filters.

Parameters:

Name Type Description Default
**kwargs

Key-value pairs where keys are field names and values are the values to exclude.

{}
Source code in supadantic/query_builder.py
283
284
285
286
287
288
289
290
291
292
293
294
def set_greater_than(self, **kwargs) -> None:
    """
    Sets the greater than filters for the query.

    This method accepts keyword arguments representing the greater than filters
    and appends them to the existing set of greater than filters.

    Args:
        **kwargs: Key-value pairs where keys are field names and values are the values to exclude.
    """

    self._greater_than += self._dict_to_tuple(data=kwargs)

set_greater_than_or_equal

set_greater_than_or_equal(**kwargs) -> None

Sets the greater than or equal filters for the query.

This method accepts keyword arguments representing the greater than or equal filters and appends them to the existing set of greater than or equal filters.

Parameters:

Name Type Description Default
**kwargs

Key-value pairs where keys are field names and values are the values to exclude.

{}
Source code in supadantic/query_builder.py
309
310
311
312
313
314
315
316
317
318
319
320
def set_greater_than_or_equal(self, **kwargs) -> None:
    """
    Sets the greater than or equal filters for the query.

    This method accepts keyword arguments representing the greater than or equal filters
    and appends them to the existing set of greater than or equal filters.

    Args:
        **kwargs: Key-value pairs where keys are field names and values are the values to exclude.
    """

    self._greater_than_or_equal += self._dict_to_tuple(data=kwargs)

set_included

set_included(**kwargs) -> None

Sets the included filter values for the query.

This method accepts keyword arguments representing the include filters and appends them to the existing set of included filter values.

Parameters:

Name Type Description Default
**kwargs

Key-value pairs where keys are field names and values are the values to exclude.

{}
Source code in supadantic/query_builder.py
322
323
324
325
326
327
328
329
330
331
332
333
def set_included(self, **kwargs) -> None:
    """
    Sets the included filter values for the query.

    This method accepts keyword arguments representing the include filters
    and appends them to the existing set of included filter values.

    Args:
        **kwargs: Key-value pairs where keys are field names and values are the values to exclude.
    """

    self._included += self._dict_to_tuple(data=kwargs)

set_insert_data

set_insert_data(data: dict[str, Any]) -> None

Sets the data to be inserted in an insert query.

Parameters:

Name Type Description Default
data dict[str, Any]

A dictionary representing the data to be inserted, where keys are field names and values are the corresponding values.

required
Source code in supadantic/query_builder.py
335
336
337
338
339
340
341
342
343
344
def set_insert_data(self, data: dict[str, Any]) -> None:
    """
    Sets the data to be inserted in an insert query.

    Args:
        data (dict[str, Any]): A dictionary representing the data to be inserted,
                               where keys are field names and values are the corresponding values.
    """

    self._insert_data = data

set_less_than

set_less_than(**kwargs) -> None

Sets the less than filters for the query.

This method accepts keyword arguments representing the less than filters and appends them to the existing set of less than filters.

Parameters:

Name Type Description Default
**kwargs

Key-value pairs where keys are field names and values are the values to exclude.

{}
Source code in supadantic/query_builder.py
296
297
298
299
300
301
302
303
304
305
306
307
def set_less_than(self, **kwargs) -> None:
    """
    Sets the less than filters for the query.

    This method accepts keyword arguments representing the less than filters
    and appends them to the existing set of less than filters.

    Args:
        **kwargs: Key-value pairs where keys are field names and values are the values to exclude.
    """

    self._less_than += self._dict_to_tuple(data=kwargs)

set_less_than_or_equal

set_less_than_or_equal(**kwargs) -> None

Sets the less than or equal filters for the query.

This method accepts keyword arguments representing the less than or equal filters and appends them to the existing set of less than or equal filters.

Parameters:

Name Type Description Default
**kwargs

Key-value pairs where keys are field names and values are the values to exclude.

{}
Source code in supadantic/query_builder.py
270
271
272
273
274
275
276
277
278
279
280
281
def set_less_than_or_equal(self, **kwargs) -> None:
    """
    Sets the less than or equal filters for the query.

    This method accepts keyword arguments representing the less than or equal filters
    and appends them to the existing set of less than or equal filters.

    Args:
        **kwargs: Key-value pairs where keys are field names and values are the values to exclude.
    """

    self._less_than_or_equal += self._dict_to_tuple(data=kwargs)

set_not_equal

set_not_equal(**kwargs) -> None

Sets the non-equality filters for the query.

This method accepts keyword arguments representing the non-equality filters and appends them to the existing set of non-equality filters.

Parameters:

Name Type Description Default
**kwargs

Key-value pairs where keys are field names and values are the values to exclude.

{}
Source code in supadantic/query_builder.py
257
258
259
260
261
262
263
264
265
266
267
268
def set_not_equal(self, **kwargs) -> None:
    """
    Sets the non-equality filters for the query.

    This method accepts keyword arguments representing the non-equality filters
    and appends them to the existing set of non-equality filters.

    Args:
        **kwargs: Key-value pairs where keys are field names and values are the values to exclude.
    """

    self._not_equal += self._dict_to_tuple(data=kwargs)

set_order_by_field

set_order_by_field(value: str) -> None

Sets the field and direction for ordering query results.

This method parses the input string to determine the field name and sort direction. A field name prefixed with '-' indicates descending order.

Parameters:

Name Type Description Default
value str

The field name to order by, optionally prefixed with '-' for descending order.

required

Examples:

>>> query_builder.set_order_by_field('name')      # ascending order
>>> query_builder.set_order_by_field('-created')  # descending order
Source code in supadantic/query_builder.py
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
def set_order_by_field(self, value: str) -> None:
    """
    Sets the field and direction for ordering query results.

    This method parses the input string to determine the field name and sort direction.
    A field name prefixed with '-' indicates descending order.

    Args:
        value (str): The field name to order by, optionally prefixed with '-' for descending order.

    Examples:
        >>> query_builder.set_order_by_field('name')      # ascending order
        >>> query_builder.set_order_by_field('-created')  # descending order
    """

    column = value.split('-')[-1]
    desc = value.startswith('-')
    self._order_by_field = (column, desc)

set_select_fields

set_select_fields(fields: Iterable[str]) -> None

Sets the selected fields for the query.

This method appends the provided fields to the existing set of selected fields. It converts the input to a tuple.

Parameters:

Name Type Description Default
fields Iterable[str]

An iterable of strings representing the fields to select.

required
Source code in supadantic/query_builder.py
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
def set_select_fields(self, fields: 'Iterable[str]') -> None:
    """
    Sets the selected fields for the query.

    This method appends the provided fields to the existing set of selected
    fields. It converts the input to a tuple.

    Args:
        fields (Iterable[str]): An iterable of strings representing the fields to select.
    """

    if self._select_fields is None:
        self._select_fields = tuple(fields)
    else:
        self._select_fields += tuple(fields)

set_update_data

set_update_data(data: dict[str, Any]) -> None

Sets the data to be updated in an update query.

Parameters:

Name Type Description Default
data dict[str, Any]

A dictionary representing the data to be updated, where keys are field names and values are the new values.

required
Source code in supadantic/query_builder.py
346
347
348
349
350
351
352
353
354
355
def set_update_data(self, data: dict[str, Any]) -> None:
    """
    Sets the data to be updated in an update query.

    Args:
        data (dict[str, Any]): A dictionary representing the data to be updated,
                               where keys are field names and values are the new values.
    """

    self._update_data = data