MCPcopy Create free account
hub / github.com/sqlalchemy/sqlalchemy / _init_metadata

Method _init_metadata

lib/sqlalchemy/engine/cursor.py:1596–1646  ·  view source on GitHub ↗
(
        self,
        context: DefaultExecutionContext,
        cursor_description: _DBAPICursorDescription,
    )

Source from the content-addressed store, hash-verified

1594 self._metadata = self._no_result_metadata
1595
1596 def _init_metadata(
1597 self,
1598 context: DefaultExecutionContext,
1599 cursor_description: _DBAPICursorDescription,
1600 ) -> CursorResultMetaData:
1601
1602 if context.compiled:
1603 compiled = context.compiled
1604
1605 if compiled._cached_metadata:
1606 metadata = compiled._cached_metadata
1607 else:
1608 metadata = CursorResultMetaData(self, cursor_description)
1609 if metadata._safe_for_cache:
1610 compiled._cached_metadata = metadata
1611
1612 # result rewrite/ adapt step. this is to suit the case
1613 # when we are invoked against a cached Compiled object, we want
1614 # to rewrite the ResultMetaData to reflect the Column objects
1615 # that are in our current SQL statement object, not the one
1616 # that is associated with the cached Compiled object.
1617 # the Compiled object may also tell us to not
1618 # actually do this step; this is to support the ORM where
1619 # it is to produce a new Result object in any case, and will
1620 # be using the cached Column objects against this database result
1621 # so we don't want to rewrite them.
1622 #
1623 # Basically this step suits the use case where the end user
1624 # is using Core SQL expressions and is accessing columns in the
1625 # result row using row._mapping[table.c.column].
1626 if (
1627 not context.execution_options.get(
1628 "_result_disable_adapt_to_context", False
1629 )
1630 and compiled._result_columns
1631 and context.cache_hit is context.dialect.CACHE_HIT
1632 and compiled.statement is not context.invoked_statement # type: ignore[comparison-overlap] # noqa: E501
1633 ):
1634 metadata = metadata._adapt_to_context(context) # type: ignore[assignment] # noqa: E501
1635
1636 self._metadata = metadata
1637
1638 else:
1639 self._metadata = metadata = CursorResultMetaData(
1640 self, cursor_description
1641 )
1642 if self._echo:
1643 context.connection._log_debug(
1644 "Col %r", tuple(x[0] for x in cursor_description)
1645 )
1646 return metadata
1647
1648 def _soft_close(self, hard: bool = False) -> None:
1649 """Soft close this :class:`_engine.CursorResult`.

Callers 1

__init__Method · 0.95

Calls 4

_adapt_to_contextMethod · 0.95
getMethod · 0.45
_log_debugMethod · 0.45

Tested by

no test coverage detected