當前位置: 首頁>>代碼示例>>Python>>正文


Python sqlalchemy.union_all方法代碼示例

本文整理匯總了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) 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:21,代碼來源:test_query.py

示例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) 
開發者ID:ibis-project,項目名稱:ibis,代碼行數:16,代碼來源:alchemy.py

示例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) 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:16,代碼來源:test_query.py

示例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) 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:46,代碼來源:test_eager_relations.py

示例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) 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:60,代碼來源:test_lazy_relations.py


注:本文中的sqlalchemy.union_all方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。