Skip to content

OrmQuery

ormdantic.generator._query.OrmQuery

OrmQuery(model, table_map, processed_models=None, query=None)

Build SQL queries for model CRUD operations.

PARAMETER DESCRIPTION
model

TYPE: ModelType

table_map

TYPE: Map

processed_models

TYPE: list[ModelType] | None DEFAULT: None

query

TYPE: Query | PostgreSQLQuery | None DEFAULT: None

Source code in ormdantic/generator/_query.py
def __init__(
    self,
    model: ModelType,
    table_map: Map,
    processed_models: list[ModelType] | None = None,
    query: Query | PostgreSQLQuery | None = None,
) -> None:
    self._model = model
    # PostgreSQLQuery works for SQLite and PostgreSQL.
    self._query: QueryBuilder | PostgreSQLQueryBuilder | Query | PostgreSQLQuery = (
        query or PostgreSQLQuery
    )
    self._table_map = table_map
    self._processed_models = processed_models or []
    self._table_data = self._table_map.model_to_data[type(self._model)]
    self._table = Table(self._table_data.tablename)

get_insert_query

get_insert_query()

Get queries to insert model tree.

Source code in ormdantic/generator/_query.py
def get_insert_query(self) -> QueryBuilder | PostgreSQLQueryBuilder:
    """Get queries to insert model tree."""
    return self._get_inserts_or_upserts(is_upsert=False)

get_upsert_query

get_upsert_query()

Get queries to upsert model tree.

Source code in ormdantic/generator/_query.py
def get_upsert_query(self) -> QueryBuilder | PostgreSQLQueryBuilder:
    """Get queries to upsert model tree."""
    return self._get_inserts_or_upserts(is_upsert=True)

get_update_queries

get_update_queries()

Get queries to update model tree.

Source code in ormdantic/generator/_query.py
def get_update_queries(self) -> QueryBuilder | PostgreSQLQueryBuilder:
    """Get queries to update model tree."""
    self._query = self._query.update(self._table)
    for column, value in self._get_columns_and_values().items():
        self._query = self._query.set(column, value)
    self._query = self._query.where(
        self._table.field(self._table_data.pk)
        == self._model.__dict__[self._table_data.pk]
    )
    return self._query

get_patch_queries

get_patch_queries()

Get queries to patch model tree.

Source code in ormdantic/generator/_query.py
def get_patch_queries(  # type: ignore
    self,
) -> list[QueryBuilder | PostgreSQLQueryBuilder]:
    """Get queries to patch model tree."""