MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / _init_metadata

Method _init_metadata

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

Source from the content-addressed store, hash-verified

1679 self._metadata = self._no_result_metadata
1680
1681 def _init_metadata(
1682 self,
1683 context: DefaultExecutionContext,
1684 cursor_description: _DBAPICursorDescription,
1685 ) -> CursorResultMetaData:
1686 driver_column_names = context.execution_options.get(
1687 "driver_column_names", False
1688 )
1689 if context.compiled:
1690 compiled = context.compiled
1691
1692 metadata: CursorResultMetaData
1693
1694 if driver_column_names:
1695 # TODO: test this case
1696 metadata = CursorResultMetaData(
1697 self,
1698 cursor_description,
1699 driver_column_names=True,
1700 num_sentinel_cols=context._num_sentinel_cols,
1701 )
1702 assert not metadata._safe_for_cache
1703 elif compiled._cached_metadata:
1704 metadata = compiled._cached_metadata
1705 else:
1706 metadata = CursorResultMetaData(
1707 self,
1708 cursor_description,
1709 # the number of sentinel columns is stored on the context
1710 # but it's a characteristic of the compiled object
1711 # so it's ok to apply it to a cacheable metadata.
1712 num_sentinel_cols=context._num_sentinel_cols,
1713 )
1714 if metadata._safe_for_cache:
1715 compiled._cached_metadata = metadata
1716
1717 # result rewrite/ adapt step. this is to suit the case
1718 # when we are invoked against a cached Compiled object, we want
1719 # to rewrite the ResultMetaData to reflect the Column objects
1720 # that are in our current SQL statement object, not the one
1721 # that is associated with the cached Compiled object.
1722 # the Compiled object may also tell us to not
1723 # actually do this step; this is to support the ORM where
1724 # it is to produce a new Result object in any case, and will
1725 # be using the cached Column objects against this database result
1726 # so we don't want to rewrite them.
1727 #
1728 # Basically this step suits the use case where the end user
1729 # is using Core SQL expressions and is accessing columns in the
1730 # result row using row._mapping[table.c.column].
1731 if (
1732 not context.execution_options.get(
1733 "_result_disable_adapt_to_context", False
1734 )
1735 and compiled._result_columns
1736 and context.cache_hit is context.dialect.CACHE_HIT
1737 and compiled.statement is not context.invoked_statement # type: ignore[comparison-overlap] # noqa: E501
1738 ):

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