Skip to content

OrmField

ormdantic.generator._field.OrmField

OrmField(table_data, table_map)

Build SQL queries from field information.

Build CRUD queries from tablename and field info.

:param table_data: Meta data of target table for SQL script. :param table_map: Map of tablenames and models.

PARAMETER DESCRIPTION
table_data

TYPE: OrmTable

table_map

TYPE: Map

Source code in ormdantic/generator/_field.py
def __init__(self, table_data: OrmTable, table_map: Map) -> None:  # type: ignore
    """Build CRUD queries from tablename and field info.

    :param table_data: Meta data of target table for SQL script.
    :param table_map: Map of tablenames and models.
    """
    self._table_data = table_data
    self._table_map = table_map
    self._table = Table(table_data.tablename)
    self._query = Query.from_(self._table)

get_find_one_query

get_find_one_query(pk, depth=1)

Get query to find one model.

PARAMETER DESCRIPTION
pk

TYPE: Any

depth

TYPE: int DEFAULT: 1

Source code in ormdantic/generator/_field.py
def get_find_one_query(self, pk: Any, depth: int = 1) -> QueryBuilder:
    """Get query to find one model."""
    query, columns = self._build_joins(
        Query.from_(self._table),
        self._table_data,
        depth,
        self._columns(depth),
    )
    query = query.where(
        self._table.field(self._table_data.pk)
        == py_type_to_sql(self._table_map, pk)
    ).select(*columns)
    return query

get_find_many_query

get_find_many_query(where, order_by, order, limit, offset, depth)

Get find query for many records.

:param where: Dictionary of column name to desired value. :param order_by: Columns to order by. :param order: Order results by ascending or descending. :param limit: Number of records to return. :param offset: Number of records to offset by. :param depth: Depth of relations to populate. :return: A list of models representing table records.

PARAMETER DESCRIPTION
where

TYPE: dict[str, Any] | None

order_by

TYPE: list[str] | None

order

TYPE: Order

limit

TYPE: int

offset

TYPE: int

depth

TYPE: int

Source code in ormdantic/generator/_field.py
def get_find_many_query(
    self,
    where: dict[str, Any] | None,
    order_by: list[str] | None,
    order: Order,
    limit: int,
    offset: int,
    depth: int,
) -> QueryBuilder:
    """Get find query for many records.

    :param where: Dictionary of column name to desired value.
    :param order_by: Columns to order by.
    :param order: Order results by ascending or descending.
    :param limit: Number of records to return.
    :param offset: Number of records to offset by.
    :param depth: Depth of relations to populate.
    :return: A list of models representing table records.
    """
    where = where or {}
    order_by = order_by or []
    query, columns = self._build_joins(
        Query.from_(self._table),
        self._table_data,
        depth,
        self._columns(depth),
    )
    for field, value in where.items():
        query = query.where(self._table.field(field) == value)
    query = query.orderby(*order_by, order=order).select(*columns)
    if limit:
        query = query.limit(limit)
    if offset:
        query = query.offset(offset)
    return query

get_delete_query

get_delete_query(pk)

Get a delete query.

:param pk: Primary key of the record to delete. :return: Query to delete a record.

PARAMETER DESCRIPTION
pk

TYPE: Any

Source code in ormdantic/generator/_field.py
def get_delete_query(self, pk: Any) -> QueryBuilder:
    """Get a `delete` query.

    :param pk: Primary key of the record to delete.
    :return: Query to delete a record.
    """
    return self._query.where(self._table.field(self._table_data.pk) == pk).delete()

get_count_query

get_count_query(where, depth)

Get a count query.

:param where: Dictionary of column name to desired value. :param depth: Depth of relations to populate. :return: Query to count records.

PARAMETER DESCRIPTION
where

TYPE: dict[str, Any] | None

depth

TYPE: int

Source code in ormdantic/generator/_field.py
def get_count_query(
    self,
    where: dict[str, Any] | None,
    depth: int,
) -> QueryBuilder:
    """Get a `count` query.

    :param where: Dictionary of column name to desired value.
    :param depth: Depth of relations to populate.
    :return: Query to count records.
    """
    where = where or {}
    query, columns = self._build_joins(
        Query.from_(self._table),
        self._table_data,
        depth,
        self._columns(depth),
    )
    for field, value in where.items():
        query = query.where(self._table.field(field) == value)
    return query.select(Count("*"))