MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / visit_override_binds

Method visit_override_binds

lib/sqlalchemy/sql/compiler.py:2452–2514  ·  view source on GitHub ↗

SQL compile the nested element of an _OverrideBinds with bindparams swapped out. The _OverrideBinds is not normally expected to be compiled; it is meant to be used when an already cached statement is to be used, the compilation was already performed, and only the bou

(self, override_binds, **kw)

Source from the content-addressed store, hash-verified

2450 return ""
2451
2452 def visit_override_binds(self, override_binds, **kw):
2453 """SQL compile the nested element of an _OverrideBinds with
2454 bindparams swapped out.
2455
2456 The _OverrideBinds is not normally expected to be compiled; it
2457 is meant to be used when an already cached statement is to be used,
2458 the compilation was already performed, and only the bound params should
2459 be swapped in at execution time.
2460
2461 However, there are test cases that exericise this object, and
2462 additionally the ORM subquery loader is known to feed in expressions
2463 which include this construct into new queries (discovered in #11173),
2464 so it has to do the right thing at compile time as well.
2465
2466 """
2467
2468 # get SQL text first
2469 sqltext = override_binds.element._compiler_dispatch(self, **kw)
2470
2471 # for a test compile that is not for caching, change binds after the
2472 # fact. note that we don't try to
2473 # swap the bindparam as we compile, because our element may be
2474 # elsewhere in the statement already (e.g. a subquery or perhaps a
2475 # CTE) and was already visited / compiled. See
2476 # test_relationship_criteria.py ->
2477 # test_selectinload_local_criteria_subquery
2478 for k in override_binds.translate:
2479 if k not in self.binds:
2480 continue
2481 bp = self.binds[k]
2482
2483 # so this would work, just change the value of bp in place.
2484 # but we dont want to mutate things outside.
2485 # bp.value = override_binds.translate[bp.key]
2486 # continue
2487
2488 # instead, need to replace bp with new_bp or otherwise accommodate
2489 # in all internal collections
2490 new_bp = bp._with_value(
2491 override_binds.translate[bp.key],
2492 maintain_key=True,
2493 required=False,
2494 )
2495
2496 name = self.bind_names[bp]
2497 self.binds[k] = self.binds[name] = new_bp
2498 self.bind_names[new_bp] = name
2499 self.bind_names.pop(bp, None)
2500
2501 if bp in self.post_compile_params:
2502 self.post_compile_params |= {new_bp}
2503 if bp in self.literal_execute_params:
2504 self.literal_execute_params |= {new_bp}
2505
2506 ckbm_tuple = self._cache_key_bind_match
2507 if ckbm_tuple:
2508 ckbm, cksm = ckbm_tuple
2509 for bp in bp._cloned_set:

Callers

nothing calls this directly

Calls 4

_with_valueMethod · 0.80
_compiler_dispatchMethod · 0.45
popMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected