def transform(original): query = original.filter() query.__dict__.update({ '_criterion': replacement_traverse(query.__dict__['_criterion'], {}, replacer), '_from_obj': tuple(replacement_traverse(fo, {}, replacer) for fo in query.__dict__['_from_obj']), '_join_entities': tuple(new_mapper if ent is old_mapper else ent for ent in query.__dict__['_join_entities']), '_joinpoint': {k: new if v is old else v for k,v in query.__dict__['_joinpoint'].items()}, }) return query
def test_annotate_unique_traversal(self): """test that items are copied only once during annotate, deannotate traversal #2453 """ table1 = table('table1', column('x')) table2 = table('table2', column('y')) a1 = table1.alias() s = select([a1.c.x]).select_from( a1.join(table2, a1.c.x==table2.c.y) ) for sel in ( sql_util._deep_deannotate(s), sql_util._deep_annotate(s, {'foo':'bar'}), visitors.cloned_traverse(s, {}, {}), visitors.replacement_traverse(s, {}, lambda x:None) ): # the columns clause isn't changed at all assert sel._raw_columns[0].table is a1 # the from objects are internally consistent, # i.e. the Alias at position 0 is the same # Alias in the Join object in position 1 assert sel._froms[0] is sel._froms[1].left eq_(str(s), str(sel))
def test_annotate_fromlist_preservation(self): """test the FROM list in select still works even when multiple annotate runs have created copies of the same selectable #2453, continued """ table1 = table('table1', column('x')) table2 = table('table2', column('y')) a1 = table1.alias() s = select([a1.c.x]).select_from( a1.join(table2, a1.c.x==table2.c.y) ) assert_s = select([select([s])]) for fn in ( sql_util._deep_deannotate, lambda s: sql_util._deep_annotate(s, {'foo':'bar'}), lambda s:visitors.cloned_traverse(s, {}, {}), lambda s:visitors.replacement_traverse(s, {}, lambda x:None) ): sel = fn(select([fn(select([fn(s)]))])) eq_(str(assert_s), str(sel))
def transform(original): query = original.filter() query.__dict__.update({ '_criterion': replacement_traverse(query.__dict__['_criterion'], {}, replacer), '_from_obj': tuple( replacement_traverse(fo, {}, replacer) for fo in query.__dict__['_from_obj']), '_join_entities': tuple(new_mapper if ent is old_mapper else ent for ent in query.__dict__['_join_entities']), '_joinpoint': { k: new if v is old else v for k, v in list(query.__dict__['_joinpoint'].items()) }, }) return query
def _create_lazy_clause(cls, prop, reverse_direction=False): binds = util.column_dict() lookup = util.column_dict() equated_columns = util.column_dict() if reverse_direction and not prop.secondaryjoin: for l, r in prop.local_remote_pairs: _list = lookup.setdefault(r, []) _list.append((r, l)) equated_columns[l] = r else: for l, r in prop.local_remote_pairs: _list = lookup.setdefault(l, []) _list.append((l, r)) equated_columns[r] = l def col_to_bind(col): if col in lookup: for tobind, equated in lookup[col]: if equated in binds: return None if col not in binds: binds[col] = sql.bindparam(None, None, type_=col.type) return binds[col] return None lazywhere = prop.primaryjoin if not prop.secondaryjoin or not reverse_direction: lazywhere = visitors.replacement_traverse(lazywhere, {}, col_to_bind) if prop.secondaryjoin is not None: secondaryjoin = prop.secondaryjoin if reverse_direction: secondaryjoin = visitors.replacement_traverse( secondaryjoin, {}, col_to_bind) lazywhere = sql.and_(lazywhere, secondaryjoin) bind_to_col = dict((binds[col].key, col) for col in binds) return (lazywhere, bind_to_col, equated_columns)
def _create_lazy_clause(cls, prop, reverse_direction=False): binds = util.column_dict() lookup = util.column_dict() equated_columns = util.column_dict() if reverse_direction and prop.secondaryjoin is None: for l, r in prop.local_remote_pairs: _list = lookup.setdefault(r, []) _list.append((r, l)) equated_columns[l] = r else: for l, r in prop.local_remote_pairs: _list = lookup.setdefault(l, []) _list.append((l, r)) equated_columns[r] = l def col_to_bind(col): if col in lookup: for tobind, equated in lookup[col]: if equated in binds: return None if col not in binds: binds[col] = sql.bindparam(None, None, type_=col.type) return binds[col] return None lazywhere = prop.primaryjoin if prop.secondaryjoin is None or not reverse_direction: lazywhere = visitors.replacement_traverse( lazywhere, {}, col_to_bind) if prop.secondaryjoin is not None: secondaryjoin = prop.secondaryjoin if reverse_direction: secondaryjoin = visitors.replacement_traverse( secondaryjoin, {}, col_to_bind) lazywhere = sql.and_(lazywhere, secondaryjoin) bind_to_col = dict((binds[col].key, col) for col in binds) return lazywhere, bind_to_col, equated_columns
def add_match(self, value): def replace(node): if isinstance(node, MatchClause): return MatchClause(' '.join([node.value, value])) return node if self._criterion is not None: self._criterion = replacement_traverse(self._criterion, {}, replace) else: criterion = _literal_as_text(MatchClause(value)) self._criterion = self._adapt_clause(criterion, True, True)
def match(self, *args, **kwargs): clause_already_exists = [False] def replace(node): if isinstance(node, MatchClause): clause_already_exists[0] = True node.extend(args, kwargs) return node self._whereclause = replacement_traverse(self._whereclause, {}, replace) if not clause_already_exists[0]: self.append_whereclause(MatchClause(args, kwargs))
def test_copy_internals(self): for fixtures_, compare_values in [ (self.fixtures, True), (self.dont_compare_values_fixtures, False), ]: for fixture in fixtures_: case_a = fixture() case_b = fixture() assert case_a[0].compare( case_b[0], compare_values=compare_values ) clone = visitors.replacement_traverse( case_a[0], {}, lambda elem: None ) assert clone.compare(case_b[0], compare_values=compare_values) stack = [clone] seen = {clone} found_elements = False while stack: obj = stack.pop(0) items = [ subelem for key, elem in clone.__dict__.items() if key != "_is_clone_of" and elem is not None for subelem in util.to_list(elem) if ( isinstance(subelem, (ColumnElement, ClauseList)) and subelem not in seen and not isinstance(subelem, Immutable) and subelem is not case_a[0] ) ] stack.extend(items) seen.update(items) if obj is not clone: found_elements = True # ensure the element will not compare as true obj.compare = lambda other, **kw: False obj.__visit_name__ = "dont_match" if found_elements: assert not clone.compare( case_b[0], compare_values=compare_values ) assert case_a[0].compare( case_b[0], compare_values=compare_values )
def match(self, *args, **kwargs): clause_already_exists = [False] def replace(node): if isinstance(node, MatchClause): clause_already_exists[0] = True node.extend(args, kwargs) return node self._criterion = replacement_traverse(self._criterion, {}, replace) if not clause_already_exists[0]: criterion = _literal_as_text(MatchClause(args, kwargs)) self._criterion = self._adapt_clause(criterion, True, True)
def _hash(self, arg): if isinstance(arg, ClauseElement): def _replace(arg): if isinstance(arg, BindParameter): return literal_column( self._literal_sql_parameter(arg.effective_value) ) convert_binds = visitors.replacement_traverse(arg, {}, _replace) expr = str(convert_binds) return expr else: assert hash(arg) return arg
def _copy_expression(expression: _CE, target_table: "Table") -> _CE: def replace(col): if (isinstance(col, Column) and col.table is not None and col.table is not target_table): if col.name in target_table.c: return target_table.c[col.name] else: c = _copy(col) target_table.append_column(c) return c else: return None return visitors.replacement_traverse(expression, {}, replace)
def _add_alias(join_clause, relationship, alias): right_mapper = relationship.prop.mapper adapter = ORMAdapter( alias, equivalents=right_mapper and right_mapper._equivalent_columns or {}, ).replace def replace(elem): e = adapter(elem) if e is not None: return e join_clause = visitors.replacement_traverse( join_clause, {}, replace) return join_clause
def expression_as_ddl(clause): """Given a SQL expression, convert for usage in DDL, such as CREATE INDEX and CHECK CONSTRAINT. Converts bind params into quoted literals, column identifiers into detached column constructs so that the parent table identifier is not included. """ def repl(element): if isinstance(element, expression._BindParamClause): return expression.literal_column(_quote_ddl_expr(element.value)) elif isinstance(element, expression.ColumnClause) and \ element.table is not None: return expression.column(element.name) else: return None return visitors.replacement_traverse(clause, {}, repl)
def test_copy_internals(self): for fixtures_, compare_values in [ (self.fixtures, True), (self.dont_compare_values_fixtures, False), ]: for fixture in fixtures_: case_a = fixture() case_b = fixture() for idx in range(len(case_a)): assert case_a[idx].compare( case_b[idx], compare_values=compare_values ) clone = visitors.replacement_traverse( case_a[idx], {}, lambda elem: None ) assert clone.compare( case_b[idx], compare_values=compare_values ) assert case_a[idx].compare( case_b[idx], compare_values=compare_values ) # copy internals of Select is very different than other # elements and additionally this is extremely well tested # in test_selectable and test_external_traversal, so # skip these if isinstance(case_a[idx], Select): continue for elema, elemb in zip( visitors.iterate(case_a[idx], {}), visitors.iterate(clone, {}), ): if isinstance(elema, ClauseElement) and not isinstance( elema, Immutable ): assert elema is not elemb
def go(): replacement_traverse(statement, {}, lambda x: None)
def apply(self, target, options={}): return replacement_traverse(target, options, self)