@classmethod
def create_for_statement(
cls,
- statement: Union[Select, FromStatement],
- compiler: Optional[SQLCompiler],
+ statement: Executable,
+ compiler: SQLCompiler,
**kw: Any,
- ) -> _AbstractORMCompileState:
+ ) -> CompileState:
"""Create a context for a statement given a :class:`.Compiler`.
This method is always invoked in the context of SQLCompiler.process().
def __init__(self, *arg, **kw):
raise NotImplementedError()
- if TYPE_CHECKING:
+ @classmethod
+ def create_for_statement(
+ cls,
+ statement: Executable,
+ compiler: SQLCompiler,
+ **kw: Any,
+ ) -> _ORMCompileState:
+ return cls._create_orm_context(
+ cast("Union[Select, FromStatement]", statement),
+ toplevel=not compiler.stack,
+ compiler=compiler,
+ **kw,
+ )
- @classmethod
- def create_for_statement(
- cls,
- statement: Union[Select, FromStatement],
- compiler: Optional[SQLCompiler],
- **kw: Any,
- ) -> _ORMCompileState: ...
+ @classmethod
+ def _create_orm_context(
+ cls,
+ statement: Union[Select, FromStatement],
+ *,
+ toplevel: bool,
+ compiler: Optional[SQLCompiler],
+ **kw: Any,
+ ) -> _ORMCompileState:
+ raise NotImplementedError()
def _append_dedupe_col_collection(self, obj, col_collection):
dedupe = self.dedupe_columns
eager_joins = _EMPTY_DICT
@classmethod
- def create_for_statement(
+ def _create_orm_context(
cls,
- statement_container: Union[Select, FromStatement],
+ statement: Union[Select, FromStatement],
+ *,
+ toplevel: bool,
compiler: Optional[SQLCompiler],
**kw: Any,
) -> _ORMFromStatementCompileState:
+ statement_container = statement
+
assert isinstance(statement_container, FromStatement)
if compiler is not None and compiler.stack:
_having_criteria = ()
@classmethod
- def create_for_statement(
+ def _create_orm_context(
cls,
statement: Union[Select, FromStatement],
+ *,
+ toplevel: bool,
compiler: Optional[SQLCompiler],
**kw: Any,
) -> _ORMSelectCompileState:
- """compiler hook, we arrive here from compiler.visit_select() only."""
self = cls.__new__(cls)
- if compiler is not None:
- toplevel = not compiler.stack
- else:
- toplevel = True
-
select_statement = statement
# if we are a select() that was never a legacy Query, we won't
_ORMCompileState._get_plugin_class_for_plugin(stmt, "orm"),
)
- return compile_state_cls.create_for_statement(stmt, None)
+ return compile_state_cls._create_orm_context(
+ stmt, toplevel=True, compiler=None
+ )
def _compile_context(self, for_statement: bool = False) -> QueryContext:
compile_state = self._compile_state(for_statement=for_statement)
from ._orm_types import DMLStrategyArgument
from ._orm_types import SynchronizeSessionArgument
from ._typing import _CLE
+ from .compiler import SQLCompiler
from .elements import BindParameter
from .elements import ClauseList
from .elements import ColumnClause # noqa
_ambiguous_table_name_map: Optional[_AmbiguousTableNameMap]
@classmethod
- def create_for_statement(cls, statement, compiler, **kw):
+ def create_for_statement(
+ cls, statement: Executable, compiler: SQLCompiler, **kw: Any
+ ) -> CompileState:
# factory construction.
if statement._propagate_attrs:
if bind:
dialect = bind.dialect
elif self.stringify_dialect == "default":
- default = util.preloaded.engine_default
- dialect = default.StrCompileDialect()
+ dialect = self._default_dialect()
else:
url = util.preloaded.engine_url
dialect = url.URL.create(
return self._compiler(dialect, **kw)
+ def _default_dialect(self):
+ default = util.preloaded.engine_default
+ return default.StrCompileDialect()
+
def _compiler(self, dialect: Dialect, **kw: Any) -> Compiled:
"""Return a compiler appropriate for this ClauseElement, given a
Dialect."""
self._propagate_attrs = util.immutabledict(values)
return self
+ def _default_compiler(self) -> SQLCompiler:
+ dialect = self._default_dialect()
+ return dialect.statement_compiler(dialect, self) # type: ignore
+
def _clone(self, **kw: Any) -> Self:
"""Create a shallow copy of this ClauseElement.
def __init__(
self,
statement: Select[Unpack[TupleAny]],
- compiler: Optional[SQLCompiler],
+ compiler: SQLCompiler,
**kw: Any,
):
self.statement = statement
:attr:`_sql.Select.columns_clause_froms`
"""
+ compiler = self._default_compiler()
- return self._compile_state_factory(self, None)._get_display_froms()
+ return self._compile_state_factory(self, compiler)._get_display_froms()
@property
@util.deprecated(
from sqlalchemy.orm import relationship
from sqlalchemy.orm import Session
from sqlalchemy.orm import synonym
+from sqlalchemy.orm.context import _ORMSelectCompileState
from sqlalchemy.sql import coercions
from sqlalchemy.sql import operators
from sqlalchemy.sql import roles
"SELECT a.id, a.foo FROM a",
)
- compile_state = stmt._compile_state_factory(stmt, None)
+ compile_state = _ORMSelectCompileState._create_orm_context(
+ stmt, toplevel=True, compiler=None
+ )
eq_(
compile_state._column_naming_convention(
LABEL_STYLE_DISAMBIGUATE_ONLY, legacy=False
.order_by(User.id)
)
- compile_state = _ORMSelectCompileState.create_for_statement(stmt, None)
+ compile_state = _ORMSelectCompileState._create_orm_context(
+ stmt, toplevel=True, compiler=None
+ )
is_(compile_state._primary_entity, None)
def test_column_queries_one(self):