| 555 | ).statement # type: ignore |
| 556 | |
| 557 | def _statement_20( |
| 558 | self, for_statement: bool = False, use_legacy_query_style: bool = True |
| 559 | ) -> Union[Select[_T], FromStatement[_T]]: |
| 560 | # TODO: this event needs to be deprecated, as it currently applies |
| 561 | # only to ORM query and occurs at this spot that is now more |
| 562 | # or less an artificial spot |
| 563 | if self.dispatch.before_compile: |
| 564 | for fn in self.dispatch.before_compile: |
| 565 | new_query = fn(self) |
| 566 | if new_query is not None and new_query is not self: |
| 567 | self = new_query |
| 568 | if not fn._bake_ok: # type: ignore |
| 569 | self._compile_options += {"_bake_ok": False} |
| 570 | |
| 571 | compile_options = self._compile_options |
| 572 | compile_options += { |
| 573 | "_for_statement": for_statement, |
| 574 | "_use_legacy_query_style": use_legacy_query_style, |
| 575 | } |
| 576 | |
| 577 | stmt: Union[Select[_T], FromStatement[_T]] |
| 578 | |
| 579 | if self._statement is not None: |
| 580 | stmt = FromStatement(self._raw_columns, self._statement) |
| 581 | stmt.__dict__.update( |
| 582 | _with_options=self._with_options, |
| 583 | _with_context_options=self._with_context_options, |
| 584 | _compile_options=compile_options, |
| 585 | _execution_options=self._execution_options, |
| 586 | _propagate_attrs=self._propagate_attrs, |
| 587 | ) |
| 588 | else: |
| 589 | # Query / select() internal attributes are 99% cross-compatible |
| 590 | stmt = Select._create_raw_select(**self.__dict__) |
| 591 | stmt.__dict__.update( |
| 592 | _label_style=self._label_style, |
| 593 | _compile_options=compile_options, |
| 594 | _propagate_attrs=self._propagate_attrs, |
| 595 | ) |
| 596 | stmt.__dict__.pop("session", None) |
| 597 | |
| 598 | # ensure the ORM context is used to compile the statement, even |
| 599 | # if it has no ORM entities. This is so ORM-only things like |
| 600 | # _legacy_joins are picked up that wouldn't be picked up by the |
| 601 | # Core statement context |
| 602 | if "compile_state_plugin" not in stmt._propagate_attrs: |
| 603 | stmt._propagate_attrs = stmt._propagate_attrs.union( |
| 604 | {"compile_state_plugin": "orm", "plugin_subject": None} |
| 605 | ) |
| 606 | |
| 607 | return stmt |
| 608 | |
| 609 | def subquery( |
| 610 | self, |