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