The key used to identify a SQL statement construct in the SQL compilation cache. .. seealso:: :ref:`sql_caching`
| 421 | |
| 422 | |
| 423 | class CacheKey(NamedTuple): |
| 424 | """The key used to identify a SQL statement construct in the |
| 425 | SQL compilation cache. |
| 426 | |
| 427 | .. seealso:: |
| 428 | |
| 429 | :ref:`sql_caching` |
| 430 | |
| 431 | """ |
| 432 | |
| 433 | key: Tuple[Any, ...] |
| 434 | bindparams: Sequence[BindParameter[Any]] |
| 435 | |
| 436 | # can't set __hash__ attribute because it interferes |
| 437 | # with namedtuple |
| 438 | # can't use "if not TYPE_CHECKING" because mypy rejects it |
| 439 | # inside of a NamedTuple |
| 440 | def __hash__(self) -> Optional[int]: # type: ignore |
| 441 | """CacheKey itself is not hashable - hash the .key portion""" |
| 442 | return None |
| 443 | |
| 444 | def to_offline_string( |
| 445 | self, |
| 446 | statement_cache: MutableMapping[Any, str], |
| 447 | statement: ClauseElement, |
| 448 | parameters: _CoreSingleExecuteParams, |
| 449 | ) -> str: |
| 450 | """Generate an "offline string" form of this :class:`.CacheKey` |
| 451 | |
| 452 | The "offline string" is basically the string SQL for the |
| 453 | statement plus a repr of the bound parameter values in series. |
| 454 | Whereas the :class:`.CacheKey` object is dependent on in-memory |
| 455 | identities in order to work as a cache key, the "offline" version |
| 456 | is suitable for a cache that will work for other processes as well. |
| 457 | |
| 458 | The given ``statement_cache`` is a dictionary-like object where the |
| 459 | string form of the statement itself will be cached. This dictionary |
| 460 | should be in a longer lived scope in order to reduce the time spent |
| 461 | stringifying statements. |
| 462 | |
| 463 | |
| 464 | """ |
| 465 | if self.key not in statement_cache: |
| 466 | statement_cache[self.key] = sql_str = str(statement) |
| 467 | else: |
| 468 | sql_str = statement_cache[self.key] |
| 469 | |
| 470 | if not self.bindparams: |
| 471 | param_tuple = tuple(parameters[key] for key in sorted(parameters)) |
| 472 | else: |
| 473 | param_tuple = tuple( |
| 474 | parameters.get(bindparam.key, bindparam.value) |
| 475 | for bindparam in self.bindparams |
| 476 | ) |
| 477 | |
| 478 | return repr((sql_str, param_tuple)) |
| 479 | |
| 480 | def __eq__(self, other: Any) -> bool: |
no outgoing calls
no test coverage detected