| 18 | |
| 19 | |
| 20 | class Query: |
| 21 | def __init__( # noqa CFQ002 |
| 22 | self, |
| 23 | model_cls: type["Model"], |
| 24 | filter_clauses: list[FilterAction], |
| 25 | exclude_clauses: list[FilterAction], |
| 26 | select_related: list, |
| 27 | limit_count: Optional[int], |
| 28 | offset: Optional[int], |
| 29 | excludable: "ExcludableItems", |
| 30 | order_bys: Optional[list["OrderAction"]], |
| 31 | limit_raw_sql: bool, |
| 32 | ) -> None: |
| 33 | self.query_offset = offset |
| 34 | self.limit_count = limit_count |
| 35 | self._select_related = select_related[:] |
| 36 | self.filter_clauses = filter_clauses[:] |
| 37 | self.exclude_clauses = exclude_clauses[:] |
| 38 | self.excludable = excludable |
| 39 | |
| 40 | self.model_cls = model_cls |
| 41 | self.table = self.model_cls.ormar_config.table |
| 42 | |
| 43 | self.used_aliases: list[str] = [] |
| 44 | |
| 45 | self.select_from: Union[Join, Table, list[str]] = [] |
| 46 | self.columns: list[Column] = [] |
| 47 | self.order_columns = order_bys |
| 48 | self.sorted_orders: dict[OrderAction, TextClause] = {} |
| 49 | self._init_sorted_orders() |
| 50 | |
| 51 | self.limit_raw_sql = limit_raw_sql |
| 52 | |
| 53 | def _init_sorted_orders(self) -> None: |
| 54 | """ |
| 55 | Initialize empty order_by dict to be populated later during the query call |
| 56 | """ |
| 57 | if self.order_columns: |
| 58 | for clause in self.order_columns: |
| 59 | self.sorted_orders[clause] = None # type: ignore |
| 60 | |
| 61 | def apply_order_bys_for_primary_model(self) -> None: # noqa: CCR001 |
| 62 | """ |
| 63 | Applies order_by queries on main model when it's used as a subquery. |
| 64 | That way the subquery with limit and offset only on main model has proper |
| 65 | sorting applied and correct models are fetched. |
| 66 | """ |
| 67 | current_table_sorted = False |
| 68 | if self.order_columns: |
| 69 | for clause in self.order_columns: |
| 70 | if clause.is_source_model_order: |
| 71 | current_table_sorted = True |
| 72 | self.sorted_orders[clause] = clause.get_text_clause() |
| 73 | |
| 74 | if not current_table_sorted: |
| 75 | self._apply_default_model_sorting() |
| 76 | |
| 77 | def _apply_default_model_sorting(self) -> None: |
no outgoing calls
no test coverage detected