def test_aliased_column_adapt(self): clause = t1.select() aliased = t1.select().alias() aliased2 = t1.alias() adapter = sql_util.ColumnAdapter(aliased) f = select([adapter.columns[c] for c in aliased2.c]).select_from(aliased) s = select([aliased2]).select_from(aliased) eq_(str(s), str(f)) f = select([adapter.columns[func.count(aliased2.c.col1)] ]).select_from(aliased) eq_(str(select([func.count(aliased2.c.col1)]).select_from(aliased)), str(f))
def process_query_property(self, query, paths, mappers): if self.alias is not None: if isinstance(self.alias, basestring): mapper = mappers[-1] (root_mapper, propname) = paths[-1][-2:] prop = mapper.get_property(propname, resolve_synonyms=True) self.alias = prop.target.alias(self.alias) query._attributes[("user_defined_eager_row_processor", interfaces._reduce_path( paths[-1]))] = sql_util.ColumnAdapter( self.alias) else: (root_mapper, propname) = paths[-1][-2:] mapper = mappers[-1] prop = mapper.get_property(propname, resolve_synonyms=True) adapter = query._polymorphic_adapters.get(prop.mapper, None) query._attributes[("user_defined_eager_row_processor", interfaces._reduce_path(paths[-1]))] = adapter
def test_columnadapter_anonymized(self): """test issue #3148 Testing the anonymization applied from the ColumnAdapter.columns collection, typically as used in eager loading. """ exprs = [ table1.c.myid, table1.c.name.label("t1name"), func.foo("hoho").label("x"), ] ta = table1.alias() adapter = sql_util.ColumnAdapter(ta, anonymize_labels=True) s1 = (select([adapter.columns[expr] for expr in exprs ]).apply_labels().order_by("myid", "t1name", "x")) def go(): # the labels here are anonymized, so label naming # can't catch these. self.assert_compile( s1, "SELECT mytable_1.myid AS mytable_1_myid, " "mytable_1.name AS name_1, foo(:foo_2) AS foo_1 " "FROM mytable AS mytable_1 ORDER BY mytable_1.myid, t1name, x", ) assert_warnings( go, [ "Can't resolve label reference 't1name'", "Can't resolve label reference 'x'", ], regex=True, )
def test_columnadapter_non_anonymized(self): """test issue #3148 Testing the anonymization applied from the ColumnAdapter.columns collection, typically as used in eager loading. """ exprs = [ table1.c.myid, table1.c.name.label('t1name'), func.foo("hoho").label('x') ] ta = table1.alias() adapter = sql_util.ColumnAdapter(ta) s1 = select([adapter.columns[expr] for expr in exprs]).\ apply_labels().order_by("myid", "t1name", "x") # labels are maintained self.assert_compile( s1, "SELECT mytable_1.myid AS mytable_1_myid, " "mytable_1.name AS t1name, foo(:foo_1) AS x " "FROM mytable AS mytable_1 ORDER BY mytable_1.myid, t1name, x")
def go(): adapter = sql_util.ColumnAdapter(inspect(u1).selectable) adapter.columns[User.id]