本文整理汇总了Python中sqlalchemy.union_all方法的典型用法代码示例。如果您正苦于以下问题:Python sqlalchemy.union_all方法的具体用法?Python sqlalchemy.union_all怎么用?Python sqlalchemy.union_all使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy
的用法示例。
在下文中一共展示了sqlalchemy.union_all方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_union_all_lightweight
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import union_all [as 别名]
def test_union_all_lightweight(self, connection):
"""like test_union_all, but breaks the sub-union into
a subquery with an explicit column reference on the outside,
more palatable to a wider variety of engines.
"""
u = union(select([t1.c.col3]), select([t1.c.col3])).alias()
e = union_all(select([t1.c.col3]), select([u.c.col3]))
wanted = [("aaa",), ("aaa",), ("bbb",), ("bbb",), ("ccc",), ("ccc",)]
found1 = self._fetchall_sorted(connection.execute(e))
eq_(found1, wanted)
found2 = self._fetchall_sorted(
connection.execute(e.alias("foo").select())
)
eq_(found2, wanted)
示例2: compile
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import union_all [as 别名]
def compile(self):
def reduce_union(left, right, distincts=iter(self.distincts)):
distinct = next(distincts)
sa_func = sa.union if distinct else sa.union_all
return sa_func(left, right)
context = self.context
selects = []
for table in self.tables:
table_set = context.get_compiled_expr(table)
selects.append(table_set.cte().select())
return functools.reduce(reduce_union, selects)
示例3: test_union_all
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import union_all [as 别名]
def test_union_all(self, connection):
e = union_all(
select([t1.c.col3]),
union(select([t1.c.col3]), select([t1.c.col3])),
)
wanted = [("aaa",), ("aaa",), ("bbb",), ("bbb",), ("ccc",), ("ccc",)]
found1 = self._fetchall_sorted(connection.execute(e))
eq_(found1, wanted)
found2 = self._fetchall_sorted(
connection.execute(e.alias("foo").select())
)
eq_(found2, wanted)
示例4: test_distinct
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import union_all [as 别名]
def test_distinct(self):
Address, addresses, users, User = (
self.classes.Address,
self.tables.addresses,
self.tables.users,
self.classes.User,
)
# this is an involved 3x union of the users table to get a lot of rows.
# then see if the "distinct" works its way out. you actually get
# the same result with or without the distinct, just via less or
# more rows.
u2 = users.alias("u2")
s = sa.union_all(
u2.select(use_labels=True),
u2.select(use_labels=True),
u2.select(use_labels=True),
).alias("u")
mapper(
User,
users,
properties={
"addresses": relationship(
mapper(Address, addresses),
lazy="joined",
order_by=addresses.c.id,
)
},
)
sess = create_session()
q = sess.query(User)
def go():
result = (
q.filter(s.c.u2_id == User.id)
.distinct()
.order_by(User.id)
.all()
)
eq_(self.static.user_address_result, result)
self.assert_sql_count(testing.db, go, 1)
示例5: test_distinct
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import union_all [as 别名]
def test_distinct(self):
(
users,
items,
order_items,
orders,
Item,
User,
Address,
Order,
addresses,
) = (
self.tables.users,
self.tables.items,
self.tables.order_items,
self.tables.orders,
self.classes.Item,
self.classes.User,
self.classes.Address,
self.classes.Order,
self.tables.addresses,
)
mapper(Item, items)
mapper(
Order,
orders,
properties={
"items": relationship(
Item, secondary=order_items, lazy="select"
)
},
)
mapper(
User,
users,
properties={
"addresses": relationship(
mapper(Address, addresses), lazy="select"
),
"orders": relationship(Order, lazy="select"),
},
)
sess = create_session()
q = sess.query(User)
# use a union all to get a lot of rows to join against
u2 = users.alias("u2")
s = sa.union_all(
u2.select(use_labels=True),
u2.select(use_labels=True),
u2.select(use_labels=True),
).alias("u")
result = (
q.filter(s.c.u2_id == User.id).order_by(User.id).distinct().all()
)
eq_(self.static.user_all_result, result)