本文整理汇总了Python中sqlalchemy.sql.visitors.cloned_traverse方法的典型用法代码示例。如果您正苦于以下问题:Python visitors.cloned_traverse方法的具体用法?Python visitors.cloned_traverse怎么用?Python visitors.cloned_traverse使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy.sql.visitors
的用法示例。
在下文中一共展示了visitors.cloned_traverse方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_annotated_visit
# 需要导入模块: from sqlalchemy.sql import visitors [as 别名]
# 或者: from sqlalchemy.sql.visitors import cloned_traverse [as 别名]
def test_annotated_visit(self):
table1 = table("table1", column("col1"), column("col2"))
bin_ = table1.c.col1 == bindparam("foo", value=None)
assert str(bin_) == "table1.col1 = :foo"
def visit_binary(b):
b.right = table1.c.col2
b2 = visitors.cloned_traverse(bin_, {}, {"binary": visit_binary})
assert str(b2) == "table1.col1 = table1.col2"
b3 = visitors.cloned_traverse(
bin_._annotate({}), {}, {"binary": visit_binary}
)
assert str(b3) == "table1.col1 = table1.col2"
def visit_binary(b):
b.left = bindparam("bar")
b4 = visitors.cloned_traverse(b2, {}, {"binary": visit_binary})
assert str(b4) == ":bar = table1.col2"
b5 = visitors.cloned_traverse(b3, {}, {"binary": visit_binary})
assert str(b5) == ":bar = table1.col2"
示例2: test_annotate_fromlist_preservation
# 需要导入模块: from sqlalchemy.sql import visitors [as 别名]
# 或者: from sqlalchemy.sql.visitors import cloned_traverse [as 别名]
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.subquery()]).subquery()])
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.subquery())]).subquery())]))
eq_(str(assert_s), str(sel))
示例3: test_aliased_cloned_column_adapt_exported
# 需要导入模块: from sqlalchemy.sql import visitors [as 别名]
# 或者: from sqlalchemy.sql.visitors import cloned_traverse [as 别名]
def test_aliased_cloned_column_adapt_exported(self):
clause = select(
[t1.c.col1, func.foo(t1.c.col2).label("foo")]
).subquery()
aliased1 = select([clause.c.col1, clause.c.foo]).subquery()
aliased2 = clause
aliased2.c.col1, aliased2.c.foo
aliased3 = cloned_traverse(aliased2, {}, {})
# also fixed by [ticket:2419]. When we look at the
# *outside* columns of aliased3, they previously did not
# have an _is_clone_of pointer. But we now modified _make_proxy
# to assign this.
adapter = sql_util.ColumnAdapter(aliased1)
f1 = select([adapter.columns[c] for c in aliased2.c])
f2 = select([adapter.columns[c] for c in aliased3.c])
eq_(str(f1), str(f2))
示例4: test_aliased_cloned_schema_column_adapt_exported
# 需要导入模块: from sqlalchemy.sql import visitors [as 别名]
# 或者: from sqlalchemy.sql.visitors import cloned_traverse [as 别名]
def test_aliased_cloned_schema_column_adapt_exported(self):
clause = select(
[t3.c.col1, func.foo(t3.c.col2).label("foo")]
).subquery()
aliased1 = select([clause.c.col1, clause.c.foo]).subquery()
aliased2 = clause
aliased2.c.col1, aliased2.c.foo
aliased3 = cloned_traverse(aliased2, {}, {})
# also fixed by [ticket:2419]. When we look at the
# *outside* columns of aliased3, they previously did not
# have an _is_clone_of pointer. But we now modified _make_proxy
# to assign this.
adapter = sql_util.ColumnAdapter(aliased1)
f1 = select([adapter.columns[c] for c in aliased2.c])
f2 = select([adapter.columns[c] for c in aliased3.c])
eq_(str(f1), str(f2))
示例5: test_cte_w_union
# 需要导入模块: from sqlalchemy.sql import visitors [as 别名]
# 或者: from sqlalchemy.sql.visitors import cloned_traverse [as 别名]
def test_cte_w_union(self):
t = select([func.values(1).label("n")]).cte("t", recursive=True)
t = t.union_all(select([t.c.n + 1]).where(t.c.n < 100))
s = select([func.sum(t.c.n)])
from sqlalchemy.sql.visitors import cloned_traverse
cloned = cloned_traverse(s, {}, {})
self.assert_compile(
cloned,
"WITH RECURSIVE t(n) AS "
"(SELECT values(:values_1) AS n "
"UNION ALL SELECT t.n + :n_1 AS anon_1 "
"FROM t "
"WHERE t.n < :n_2) "
"SELECT sum(t.n) AS sum_1 FROM t",
)
示例6: test_aliased_cte_w_union
# 需要导入模块: from sqlalchemy.sql import visitors [as 别名]
# 或者: from sqlalchemy.sql.visitors import cloned_traverse [as 别名]
def test_aliased_cte_w_union(self):
t = (
select([func.values(1).label("n")])
.cte("t", recursive=True)
.alias("foo")
)
t = t.union_all(select([t.c.n + 1]).where(t.c.n < 100))
s = select([func.sum(t.c.n)])
from sqlalchemy.sql.visitors import cloned_traverse
cloned = cloned_traverse(s, {}, {})
self.assert_compile(
cloned,
"WITH RECURSIVE foo(n) AS (SELECT values(:values_1) AS n "
"UNION ALL SELECT foo.n + :n_1 AS anon_1 FROM foo "
"WHERE foo.n < :n_2) SELECT sum(foo.n) AS sum_1 FROM foo",
)
示例7: _get_nonansi_join_whereclause
# 需要导入模块: from sqlalchemy.sql import visitors [as 别名]
# 或者: from sqlalchemy.sql.visitors import cloned_traverse [as 别名]
def _get_nonansi_join_whereclause(self, froms):
clauses = []
def visit_join(join):
if join.isouter:
def visit_binary(binary):
if binary.operator == sql_operators.eq:
if join.right.is_derived_from(binary.left.table):
binary.left = _OuterJoinColumn(binary.left)
elif join.right.is_derived_from(binary.right.table):
binary.right = _OuterJoinColumn(binary.right)
clauses.append(visitors.cloned_traverse(
join.onclause, {}, {'binary': visit_binary}))
else:
clauses.append(join.onclause)
for j in join.left, join.right:
if isinstance(j, expression.Join):
visit_join(j)
elif isinstance(j, expression.FromGrouping):
visit_join(j.element)
for f in froms:
if isinstance(f, expression.Join):
visit_join(f)
if not clauses:
return None
else:
return sql.and_(*clauses)
示例8: _get_nonansi_join_whereclause
# 需要导入模块: from sqlalchemy.sql import visitors [as 别名]
# 或者: from sqlalchemy.sql.visitors import cloned_traverse [as 别名]
def _get_nonansi_join_whereclause(self, froms):
clauses = []
def visit_join(join):
if join.isouter:
# https://docs.oracle.com/database/121/SQLRF/queries006.htm#SQLRF52354
# "apply the outer join operator (+) to all columns of B in
# the join condition in the WHERE clause" - that is,
# unconditionally regardless of operator or the other side
def visit_binary(binary):
if isinstance(binary.left, expression.ColumnClause) \
and join.right.is_derived_from(binary.left.table):
binary.left = _OuterJoinColumn(binary.left)
elif isinstance(binary.right, expression.ColumnClause) \
and join.right.is_derived_from(binary.right.table):
binary.right = _OuterJoinColumn(binary.right)
clauses.append(visitors.cloned_traverse(
join.onclause, {}, {'binary': visit_binary}))
else:
clauses.append(join.onclause)
for j in join.left, join.right:
if isinstance(j, expression.Join):
visit_join(j)
elif isinstance(j, expression.FromGrouping):
visit_join(j.element)
for f in froms:
if isinstance(f, expression.Join):
visit_join(f)
if not clauses:
return None
else:
return sql.and_(*clauses)
示例9: test_clone_col_list_changes_then_proxy
# 需要导入模块: from sqlalchemy.sql import visitors [as 别名]
# 或者: from sqlalchemy.sql.visitors import cloned_traverse [as 别名]
def test_clone_col_list_changes_then_proxy(self):
t = table("t", column("q"), column("p"))
stmt = select([t.c.q]).subquery()
def add_column(stmt):
stmt.add_columns.non_generative(stmt, t.c.p)
stmt2 = visitors.cloned_traverse(stmt, {}, {"select": add_column})
eq_(list(stmt.c.keys()), ["q"])
eq_(list(stmt2.c.keys()), ["q", "p"])
示例10: test_clone_col_list_changes_then_schema_proxy
# 需要导入模块: from sqlalchemy.sql import visitors [as 别名]
# 或者: from sqlalchemy.sql.visitors import cloned_traverse [as 别名]
def test_clone_col_list_changes_then_schema_proxy(self):
t = Table("t", MetaData(), Column("q", Integer), Column("p", Integer))
stmt = select([t.c.q]).subquery()
def add_column(stmt):
stmt.add_columns.non_generative(stmt, t.c.p)
stmt2 = visitors.cloned_traverse(stmt, {}, {"select": add_column})
eq_(list(stmt.c.keys()), ["q"])
eq_(list(stmt2.c.keys()), ["q", "p"])
示例11: test_annotate_unique_traversal
# 需要导入模块: from sqlalchemy.sql import visitors [as 别名]
# 或者: from sqlalchemy.sql.visitors import cloned_traverse [as 别名]
def test_annotate_unique_traversal(self):
"""test that items are copied only once during
annotate, deannotate traversal
#2453 - however note this was modified by
#1401, and it's likely that re49563072578
is helping us with the str() comparison
case now, as deannotate is making
clones again in some cases.
"""
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),
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
froms = list(sel._iterate_from_elements())
assert froms[0].element is froms[1].left.element
eq_(str(s), str(sel))
# when we are modifying annotations sets only
# partially, elements are copied uniquely based on id().
# this is new as of 1.4, previously they'd be copied every time
for sel in (
sql_util._deep_deannotate(s, {"foo": "bar"}),
sql_util._deep_annotate(s, {"foo": "bar"}),
):
froms = list(sel._iterate_from_elements())
assert froms[0] is not froms[1].left
# but things still work out due to
# re49563072578
eq_(str(s), str(sel))
示例12: _get_nonansi_join_whereclause
# 需要导入模块: from sqlalchemy.sql import visitors [as 别名]
# 或者: from sqlalchemy.sql.visitors import cloned_traverse [as 别名]
def _get_nonansi_join_whereclause(self, froms):
clauses = []
def visit_join(join):
if join.isouter:
def visit_binary(binary):
if binary.operator == sql_operators.eq:
if join.right.is_derived_from(binary.left.table):
binary.left = _OuterJoinColumn(binary.left)
elif join.right.is_derived_from(binary.right.table):
binary.right = _OuterJoinColumn(binary.right)
clauses.append(visitors.cloned_traverse(join.onclause, {},
{'binary': visit_binary}))
else:
clauses.append(join.onclause)
for j in join.left, join.right:
if isinstance(j, expression.Join):
visit_join(j)
elif isinstance(j, expression.FromGrouping):
visit_join(j.element)
for f in froms:
if isinstance(f, expression.Join):
visit_join(f)
if not clauses:
return None
else:
return sql.and_(*clauses)
示例13: test_union_cte_aliases
# 需要导入模块: from sqlalchemy.sql import visitors [as 别名]
# 或者: from sqlalchemy.sql.visitors import cloned_traverse [as 别名]
def test_union_cte_aliases(self):
orders = table("orders", column("region"), column("amount"))
regional_sales = (
select([orders.c.region, orders.c.amount])
.cte("regional_sales")
.alias("rs")
)
s = select([regional_sales.c.region]).where(
regional_sales.c.amount > 500
)
self.assert_compile(
s,
"WITH regional_sales AS "
"(SELECT orders.region AS region, "
"orders.amount AS amount FROM orders) "
"SELECT rs.region "
"FROM regional_sales AS rs WHERE "
"rs.amount > :amount_1",
)
s = s.union_all(
select([regional_sales.c.region]).where(
regional_sales.c.amount < 300
)
)
self.assert_compile(
s,
"WITH regional_sales AS "
"(SELECT orders.region AS region, "
"orders.amount AS amount FROM orders) "
"SELECT rs.region FROM regional_sales AS rs "
"WHERE rs.amount > :amount_1 "
"UNION ALL SELECT rs.region "
"FROM regional_sales AS rs WHERE "
"rs.amount < :amount_2",
)
cloned = cloned_traverse(s, {}, {})
self.assert_compile(
cloned,
"WITH regional_sales AS "
"(SELECT orders.region AS region, "
"orders.amount AS amount FROM orders) "
"SELECT rs.region FROM regional_sales AS rs "
"WHERE rs.amount > :amount_1 "
"UNION ALL SELECT rs.region "
"FROM regional_sales AS rs WHERE "
"rs.amount < :amount_2",
)