| 572 | ).statement # type: ignore |
| 573 | |
| 574 | def _statement_20( |
| 575 | self, for_statement: bool = False, use_legacy_query_style: bool = True |
| 576 | ) -> Union[Select[_T], FromStatement[_T]]: |
| 577 | # TODO: this event needs to be deprecated, as it currently applies |
| 578 | # only to ORM query and occurs at this spot that is now more |
| 579 | # or less an artificial spot |
| 580 | if self.dispatch.before_compile: |
| 581 | for fn in self.dispatch.before_compile: |
| 582 | new_query = fn(self) |
| 583 | if new_query is not None and new_query is not self: |
| 584 | self = new_query |
| 585 | if not fn._bake_ok: # type: ignore |
| 586 | self._compile_options += {"_bake_ok": False} |
| 587 | |
| 588 | compile_options = self._compile_options |
| 589 | compile_options += { |
| 590 | "_for_statement": for_statement, |
| 591 | "_use_legacy_query_style": use_legacy_query_style, |
| 592 | } |
| 593 | |
| 594 | stmt: Union[Select[_T], FromStatement[_T]] |
| 595 | |
| 596 | if self._statement is not None: |
| 597 | stmt = FromStatement(self._raw_columns, self._statement) |
| 598 | stmt.__dict__.update( |
| 599 | _with_options=self._with_options, |
| 600 | _with_context_options=self._compile_state_funcs, |
| 601 | _compile_options=compile_options, |
| 602 | _execution_options=self._execution_options, |
| 603 | _propagate_attrs=self._propagate_attrs, |
| 604 | ) |
| 605 | else: |
| 606 | # Query / select() internal attributes are 99% cross-compatible |
| 607 | stmt = Select._create_raw_select(**self.__dict__) |
| 608 | |
| 609 | stmt.__dict__.update( |
| 610 | _label_style=self._label_style, |
| 611 | _compile_options=compile_options, |
| 612 | _propagate_attrs=self._propagate_attrs, |
| 613 | ) |
| 614 | for ext in self._syntax_extensions: |
| 615 | stmt._apply_syntax_extension_to_self(ext) |
| 616 | stmt.__dict__.pop("session", None) |
| 617 | |
| 618 | # ensure the ORM context is used to compile the statement, even |
| 619 | # if it has no ORM entities. This is so ORM-only things like |
| 620 | # _legacy_joins are picked up that wouldn't be picked up by the |
| 621 | # Core statement context |
| 622 | if "compile_state_plugin" not in stmt._propagate_attrs: |
| 623 | stmt._propagate_attrs = stmt._propagate_attrs.union( |
| 624 | {"compile_state_plugin": "orm", "plugin_subject": None} |
| 625 | ) |
| 626 | |
| 627 | return stmt |
| 628 | |
| 629 | def subquery( |
| 630 | self, |