Tables API documentation

TableFactory

class querybuilder.tables.TableFactory

Creates the correct table class based on the type of the passed table

static __new__(cls, table, *args, **kwargs)

Determines which type of table class to instantiate based on the table argument

Parameters:table (str or dict or Table or ModelBase or Query) – The table used in determining which type of Table object to return. This can be a string of the table name, a dict of {‘alias’: table}, a Table instance, a django model class, or a Query instance
Returns:The Table instance if a valid type was determined, otherwise None
Return type:Table or None

Table

class querybuilder.tables.Table(table=None, fields=None, schema=None, extract_fields=False, prefix_fields=False, field_prefix=None, owner=None, alias=None)

Abstract table class that all table types extend.

Properties:

name: str
The name that identifies this table if there is no alias
alias: str
The optional alias used to identify this table
auto_alias: str
An alias that is set automatically by the Query if needed for inner query namespacing
__init__(table=None, fields=None, schema=None, extract_fields=False, prefix_fields=False, field_prefix=None, owner=None, alias=None)

Initializes the table and sets default values

Parameters:
  • table (str or dict or Query or ModelBase) – The table name or model. This can be a string of the table name, a dict of {‘alias’: table}, a Query instance, or a django Model instance
  • fields (str or tuple or list or Field) – The fields to select from table. Defaults to ‘*’. This can be a single field, a tuple of fields, or a list of fields. Each field can be a string or Field instance
  • schema (str) – This is not implemented, but it will be a string of the db schema name
  • extract_fields (bool) – If True, then ‘*’ fields will be converted to individual fields for each column in the table. Defaults to False.
  • prefix_fields (bool) – If True, then the table will have each of its field names prefixed with the field_prefix. Defaults to False.
  • field_prefix (str) – The field prefix to be used in front of each field name if prefix_fields is set to True.
  • owner (Query) – A reference to the query managing this Table object
  • alias (str) – An alias to be used for this table
add_field(field)

Adds a field to this table

Parameters:field (str or dict or Field) – This can be a string of a field name, a dict of {‘alias’: field}, or a Field instance
add_fields(fields)

Adds all of the passed fields to the table’s current field list

Parameters:fields (str or tuple or list of str or list of Field or Field) – The fields to select from table. This can be a single field, a tuple of fields, or a list of fields. Each field can be a string or Field instance
before_add_field(field)

This is a template method meant to be extended by subclasses. It is called during the add_field method after the field is returned from the factory, and before calling the field’s before_add method, which is before actually appending the field to the list of fields.

find_field(field=None, alias=None)

Finds a field by name or alias.

Parameters:field (str or dict or Field) – string of the field name or alias, dict of {‘alias’: field}, or a Field instance
Returns:The field if it is found, otherwise None
Return type:Field or None
get_alias()

Gets the alias for the table or the auto_alias if one is set. If there isn’t any kind of alias, None is returned.

Returns:The table alias, auto_alias, or None
Return type:str or None
get_field_identifiers()

Loop through this tables fields and calls the get_identifier method on each of them to build a list of field identifiers

Returns:A list of field identifiers found in this table
Return type:list of str
get_field_names()

Loop through this tables fields and calls the get_name method on each of them to build a list of field names

Returns:A list of field names found in this table
Return type:list of str
get_field_prefix()

Gets the prefix to be used in front of each field. If no prefix is set, then the identifier for this table is returned

Returns:The field prefix for this table
Return type:str
get_field_sql()

Loop through this tables fields and calls the get_sql method on each of them to build the field list for the FROM clause

Returns:A list of sql for each field in this table
Return type:list of str
get_from_name()

Gets the name to be used in the FROM clause for the table. This is separate from the get_name() method so subclasses don’t always have to reference the table name by the FROM name. Otherwise Table subclasses like a QueryTable would be using the full Query sql for the get_name when getting the identifier in other parts of the query.

get_identifier()

Gets the name to reference the table within a query. If a table is aliased, it will return the alias, otherwise it returns the table name

Returns:the name to reference the table within a query
Return type:str
get_name()

Gets the name for the table and returns it. This identifies the table if there is not an alias set.

Returns:The name for the table
Return type:str
get_sql()

Gets the FROM sql portion for this table Ex: table_name AS alias

Returns:Returns the table identifier to be used in the FROM sql portion of the query
Return type:str
init_defaults()

Template method meant to be overridden by subclasses. This is called in the __init__ before calling set_fields

remove_field(field)

Removes a field from this table

Parameters:field (str or dict or Field) – This can be a string of a field name, a dict of {‘alias’: field}, or a Field instance
set_fields(fields)

This will clear the table’s current fields and add all new fields

Parameters:fields (str or tuple or list of str or list of Field or Field) – The fields to select from table. This can be a single field, a tuple of fields, or a list of fields. Each field can be a string or Field instance

SimpleTable

class querybuilder.tables.SimpleTable(table=None, fields=None, schema=None, extract_fields=False, prefix_fields=False, field_prefix=None, owner=None, alias=None)

A table that is created with just the string name of the table

__init__(table=None, fields=None, schema=None, extract_fields=False, prefix_fields=False, field_prefix=None, owner=None, alias=None)

Initializes the table and sets default values

Parameters:
  • table (str or dict or Query or ModelBase) – The table name or model. This can be a string of the table name, a dict of {‘alias’: table}, a Query instance, or a django Model instance
  • fields (str or tuple or list or Field) – The fields to select from table. Defaults to ‘*’. This can be a single field, a tuple of fields, or a list of fields. Each field can be a string or Field instance
  • schema (str) – This is not implemented, but it will be a string of the db schema name
  • extract_fields (bool) – If True, then ‘*’ fields will be converted to individual fields for each column in the table. Defaults to False.
  • prefix_fields (bool) – If True, then the table will have each of its field names prefixed with the field_prefix. Defaults to False.
  • field_prefix (str) – The field prefix to be used in front of each field name if prefix_fields is set to True.
  • owner (Query) – A reference to the query managing this Table object
  • alias (str) – An alias to be used for this table
init_defaults()

Sets the name of the table to the passed in table value

ModelTable

class querybuilder.tables.ModelTable(table=None, fields=None, schema=None, extract_fields=False, prefix_fields=False, field_prefix=None, owner=None, alias=None)

A table that is created by passing a django model for the table field. This allows fields to be extract and for joins to be made without specifying a condition.

__init__(table=None, fields=None, schema=None, extract_fields=False, prefix_fields=False, field_prefix=None, owner=None, alias=None)

Initializes the table and sets default values

Parameters:
  • table (str or dict or Query or ModelBase) – The table name or model. This can be a string of the table name, a dict of {‘alias’: table}, a Query instance, or a django Model instance
  • fields (str or tuple or list or Field) – The fields to select from table. Defaults to ‘*’. This can be a single field, a tuple of fields, or a list of fields. Each field can be a string or Field instance
  • schema (str) – This is not implemented, but it will be a string of the db schema name
  • extract_fields (bool) – If True, then ‘*’ fields will be converted to individual fields for each column in the table. Defaults to False.
  • prefix_fields (bool) – If True, then the table will have each of its field names prefixed with the field_prefix. Defaults to False.
  • field_prefix (str) – The field prefix to be used in front of each field name if prefix_fields is set to True.
  • owner (Query) – A reference to the query managing this Table object
  • alias (str) – An alias to be used for this table
before_add_field(field)

If extract_fields is set to True, then ‘*’ fields will be removed and each individual field will read from the model meta data and added.

init_defaults()

Sets a model instance variable to the table value and sets the name to the table name as determined from the model class

QueryTable

class querybuilder.tables.QueryTable(table=None, fields=None, schema=None, extract_fields=False, prefix_fields=False, field_prefix=None, owner=None, alias=None)

A table that contains a Query object. This is used for inner queries in more complex queries, usually involving window functions or some sort of aggregation.

__init__(table=None, fields=None, schema=None, extract_fields=False, prefix_fields=False, field_prefix=None, owner=None, alias=None)

Initializes the table and sets default values

Parameters:
  • table (str or dict or Query or ModelBase) – The table name or model. This can be a string of the table name, a dict of {‘alias’: table}, a Query instance, or a django Model instance
  • fields (str or tuple or list or Field) – The fields to select from table. Defaults to ‘*’. This can be a single field, a tuple of fields, or a list of fields. Each field can be a string or Field instance
  • schema (str) – This is not implemented, but it will be a string of the db schema name
  • extract_fields (bool) – If True, then ‘*’ fields will be converted to individual fields for each column in the table. Defaults to False.
  • prefix_fields (bool) – If True, then the table will have each of its field names prefixed with the field_prefix. Defaults to False.
  • field_prefix (str) – The field prefix to be used in front of each field name if prefix_fields is set to True.
  • owner (Query) – A reference to the query managing this Table object
  • alias (str) – An alias to be used for this table
get_from_name()

Return the query sql in the FROM clause of the query when building the table sql

get_sql()

Gets the FROM sql portion for this table Ex: table_name AS alias

Returns:Returns the table identifier to be used in the FROM sql portion of the query
Return type:str
init_defaults()

Sets a query instance variable to the table value