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


Python sqlalchemy.alias方法代碼示例

本文整理匯總了Python中sqlalchemy.alias方法的典型用法代碼示例。如果您正苦於以下問題:Python sqlalchemy.alias方法的具體用法?Python sqlalchemy.alias怎麽用?Python sqlalchemy.alias使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在sqlalchemy的用法示例。


在下文中一共展示了sqlalchemy.alias方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_join_against_self_implicit_subquery

# 需要導入模塊: import sqlalchemy [as 別名]
# 或者: from sqlalchemy import alias [as 別名]
def test_join_against_self_implicit_subquery(self):
        jj = select([self.table1.c.col1.label("bar_col1")])
        with testing.expect_deprecated(
            "The SelectBase.c and SelectBase.columns attributes are "
            "deprecated and will be removed",
            "Implicit coercion of SELECT",
        ):
            jjj = join(self.table1, jj, self.table1.c.col1 == jj.c.bar_col1)

        jjj_bar_col1 = jjj.c["%s_bar_col1" % jj._implicit_subquery.name]
        assert jjj_bar_col1 is not None

        # test column directly against itself

        assert jjj.corresponding_column(jjj.c.table1_col1) is jjj.c.table1_col1
        with testing.expect_deprecated(
            "The SelectBase.c and SelectBase.columns attributes are "
            "deprecated and will be removed"
        ):
            assert jjj.corresponding_column(jj.c.bar_col1) is jjj_bar_col1

        # test alias of the join

        j2 = jjj.alias("foo")
        assert j2.corresponding_column(self.table1.c.col1) is j2.c.table1_col1 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:27,代碼來源:test_deprecations.py

示例2: test_anon_aliased_overlapping

# 需要導入模塊: import sqlalchemy [as 別名]
# 或者: from sqlalchemy import alias [as 別名]
def test_anon_aliased_overlapping(self, connection):
        text1 = self.tables.text1

        c1 = text1.c.a.label(None)
        c2 = text1.alias().c.a
        c3 = text1.alias().c.a.label(None)
        c4 = text1.c.a.label(None)

        stmt = text("select a, b, c, d from text1").columns(c1, c2, c3, c4)
        result = connection.execute(stmt)
        row = result.first()

        with testing.expect_deprecated(
            "Retrieving row values using Column objects "
            "with only matching names"
        ):
            eq_(row._mapping[text1.c.a], "a1") 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:19,代碼來源:test_deprecations.py

示例3: test_direct_correspondence_on_labels

# 需要導入模塊: import sqlalchemy [as 別名]
# 或者: from sqlalchemy import alias [as 別名]
def test_direct_correspondence_on_labels(self):
        # this test depends on labels being part
        # of the proxy set to get the right result

        l1, l2 = table1.c.col1.label("foo"), table1.c.col1.label("bar")
        sel = select([l1, l2])

        sel2 = sel.alias()
        assert sel2.corresponding_column(l1) is sel2.c.foo
        assert sel2.corresponding_column(l2) is sel2.c.bar

        sel2 = select([table1.c.col1.label("foo"), table1.c.col2.label("bar")])

        sel3 = sel.union(sel2).alias()
        assert sel3.corresponding_column(l1) is sel3.c.foo
        assert sel3.corresponding_column(l2) is sel3.c.bar 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:18,代碼來源:test_selectable.py

示例4: test_keyed_gen

# 需要導入模塊: import sqlalchemy [as 別名]
# 或者: from sqlalchemy import alias [as 別名]
def test_keyed_gen(self):
        s = select([keyed])
        eq_(s.selected_columns.colx.key, "colx")

        eq_(s.selected_columns.colx.name, "x")

        assert (
            s.selected_columns.corresponding_column(keyed.c.colx)
            is s.selected_columns.colx
        )
        assert (
            s.selected_columns.corresponding_column(keyed.c.coly)
            is s.selected_columns.coly
        )
        assert (
            s.selected_columns.corresponding_column(keyed.c.z)
            is s.selected_columns.z
        )

        sel2 = s.alias()
        assert sel2.corresponding_column(keyed.c.colx) is sel2.c.colx
        assert sel2.corresponding_column(keyed.c.coly) is sel2.c.coly
        assert sel2.corresponding_column(keyed.c.z) is sel2.c.z 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:25,代碼來源:test_selectable.py

示例5: test_keyed_label_gen

# 需要導入模塊: import sqlalchemy [as 別名]
# 或者: from sqlalchemy import alias [as 別名]
def test_keyed_label_gen(self):
        s = select([keyed]).apply_labels()

        assert (
            s.selected_columns.corresponding_column(keyed.c.colx)
            is s.selected_columns.keyed_colx
        )
        assert (
            s.selected_columns.corresponding_column(keyed.c.coly)
            is s.selected_columns.keyed_coly
        )
        assert (
            s.selected_columns.corresponding_column(keyed.c.z)
            is s.selected_columns.keyed_z
        )

        sel2 = s.alias()
        assert sel2.corresponding_column(keyed.c.colx) is sel2.c.keyed_colx
        assert sel2.corresponding_column(keyed.c.coly) is sel2.c.keyed_coly
        assert sel2.corresponding_column(keyed.c.z) is sel2.c.keyed_z 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:22,代碼來源:test_selectable.py

示例6: test_join_against_self

# 需要導入模塊: import sqlalchemy [as 別名]
# 或者: from sqlalchemy import alias [as 別名]
def test_join_against_self(self):
        jj = select([table1.c.col1.label("bar_col1")]).subquery()
        jjj = join(table1, jj, table1.c.col1 == jj.c.bar_col1)

        # test column directly against itself

        # joins necessarily have to prefix column names with the name
        # of the selectable, else the same-named columns will overwrite
        # one another.  In this case, we unfortunately have this unfriendly
        # "anonymous" name, whereas before when select() could be a FROM
        # the "bar_col1" label would be directly in the join() object.  However
        # this was a useless join() object because PG and MySQL don't accept
        # unnamed subqueries in joins in any case.
        name = "%s_bar_col1" % (jj.name,)

        assert jjj.corresponding_column(jjj.c.table1_col1) is jjj.c.table1_col1
        assert jjj.corresponding_column(jj.c.bar_col1) is jjj.c[name]

        # test alias of the join

        j2 = jjj.alias("foo")
        assert j2.corresponding_column(table1.c.col1) is j2.c.table1_col1 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:24,代碼來源:test_selectable.py

示例7: test_alias_handles_column_context

# 需要導入模塊: import sqlalchemy [as 別名]
# 或者: from sqlalchemy import alias [as 別名]
def test_alias_handles_column_context(self):
        # not quite a use case yet but this is expected to become
        # prominent w/ PostgreSQL's tuple functions

        stmt = select([table1.c.col1, table1.c.col2])
        a = stmt.alias("a")

        # TODO: this case is crazy, sending SELECT or FROMCLAUSE has to
        # be figured out - is it a scalar row query?  what kinds of
        # statements go into functions in PG. seems likely select statement,
        # but not alias, subquery or other FROM object
        self.assert_compile(
            select([func.foo(a)]),
            "SELECT foo(SELECT table1.col1, table1.col2 FROM table1) "
            "AS foo_1 FROM "
            "(SELECT table1.col1 AS col1, table1.col2 AS col2 FROM table1) "
            "AS a",
        ) 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:20,代碼來源:test_selectable.py

示例8: test_alias_alias_samename_init

# 需要導入模塊: import sqlalchemy [as 別名]
# 或者: from sqlalchemy import alias [as 別名]
def test_alias_alias_samename_init(self):
        a = table("a", column("x"))
        b = table("b", column("y"))
        s1 = select([a, b]).apply_labels().alias()
        s2 = s1.alias()

        s1.c
        s2.c

        q = column("x")
        b.append_column(q)

        assert "_columns" in s2.__dict__

        s2._refresh_for_new_column(q)

        assert "_columns" not in s2.__dict__
        is_(s1.corresponding_column(s2.c.b_x), s1.c.b_x) 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:20,代碼來源:test_selectable.py

示例9: test_alias

# 需要導入模塊: import sqlalchemy [as 別名]
# 或者: from sqlalchemy import alias [as 別名]
def test_alias(self):
        meta = MetaData()
        t1 = Table(
            "t1",
            meta,
            Column("c1", Integer, primary_key=True),
            Column("c2", String(30)),
        )
        t2 = Table(
            "t2",
            meta,
            Column("c1", Integer, primary_key=True),
            Column("c2", String(30)),
        )

        assert t1.alias().is_derived_from(t1)
        assert not t2.alias().is_derived_from(t1)
        assert not t1.is_derived_from(t1.alias())
        assert not t1.is_derived_from(t2.alias()) 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:21,代碼來源:test_selectable.py

示例10: test_select

# 需要導入模塊: import sqlalchemy [as 別名]
# 或者: from sqlalchemy import alias [as 別名]
def test_select(self):
        meta = MetaData()

        t1 = Table(
            "t1",
            meta,
            Column("c1", Integer, primary_key=True),
            Column("c2", String(30)),
        )
        t2 = Table(
            "t2",
            meta,
            Column("c1", Integer, primary_key=True),
            Column("c2", String(30)),
        )

        assert t1.select().is_derived_from(t1)
        assert not t2.select().is_derived_from(t1)

        assert select([t1, t2]).is_derived_from(t1)

        assert t1.select().alias("foo").is_derived_from(t1)
        assert select([t1, t2]).alias("foo").is_derived_from(t1)
        assert not t2.select().alias("foo").is_derived_from(t1) 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:26,代碼來源:test_selectable.py

示例11: test_proxy_set_iteration_includes_annotated

# 需要導入模塊: import sqlalchemy [as 別名]
# 或者: from sqlalchemy import alias [as 別名]
def test_proxy_set_iteration_includes_annotated(self):
        from sqlalchemy.schema import Column

        c1 = Column("foo", Integer)

        stmt = select([c1]).alias()
        proxy = stmt.c.foo

        proxy.proxy_set

        # create an annotated of the column
        p2 = proxy._annotate({"weight": 10})

        # now see if our annotated version is in that column's
        # proxy_set, as corresponding_column iterates through proxy_set
        # in this way
        d = {}
        for col in p2._uncached_proxy_set():
            d.update(col._annotations)
        eq_(d, {"weight": 10}) 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:22,代碼來源:test_selectable.py

示例12: test_nested_label_targeting

# 需要導入模塊: import sqlalchemy [as 別名]
# 或者: from sqlalchemy import alias [as 別名]
def test_nested_label_targeting(self):
        """test nested anonymous label generation.

        """
        s1 = table1.select()
        s2 = s1.alias()
        s3 = select([s2], use_labels=True)
        s4 = s3.alias()
        s5 = select([s4], use_labels=True)
        self.assert_compile(
            s5,
            "SELECT anon_1.anon_2_myid AS "
            "anon_1_anon_2_myid, anon_1.anon_2_name AS "
            "anon_1_anon_2_name, anon_1.anon_2_descript"
            "ion AS anon_1_anon_2_description FROM "
            "(SELECT anon_2.myid AS anon_2_myid, "
            "anon_2.name AS anon_2_name, "
            "anon_2.description AS anon_2_description "
            "FROM (SELECT mytable.myid AS myid, "
            "mytable.name AS name, mytable.description "
            "AS description FROM mytable) AS anon_2) "
            "AS anon_1",
        ) 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:25,代碼來源:test_compiler.py

示例13: subquery

# 需要導入模塊: import sqlalchemy [as 別名]
# 或者: from sqlalchemy import alias [as 別名]
def subquery(alias, *args, **kwargs):
    """Return an :class:`.Alias` object derived
    from a :class:`.Select`.

    name
      alias name

    \*args, \**kwargs

      all other arguments are delivered to the
      :func:`select` function.

    """
    return Select(*args, **kwargs).alias(alias) 
開發者ID:jpush,項目名稱:jbox,代碼行數:16,代碼來源:selectable.py

示例14: alias

# 需要導入模塊: import sqlalchemy [as 別名]
# 或者: from sqlalchemy import alias [as 別名]
def alias(selectable, name=None, flat=False):
    """Return an :class:`.Alias` object.

    An :class:`.Alias` represents any :class:`.FromClause`
    with an alternate name assigned within SQL, typically using the ``AS``
    clause when generated, e.g. ``SELECT * FROM table AS aliasname``.

    Similar functionality is available via the
    :meth:`~.FromClause.alias` method
    available on all :class:`.FromClause` subclasses.

    When an :class:`.Alias` is created from a :class:`.Table` object,
    this has the effect of the table being rendered
    as ``tablename AS aliasname`` in a SELECT statement.

    For :func:`.select` objects, the effect is that of creating a named
    subquery, i.e. ``(select ...) AS aliasname``.

    The ``name`` parameter is optional, and provides the name
    to use in the rendered SQL.  If blank, an "anonymous" name
    will be deterministically generated at compile time.
    Deterministic means the name is guaranteed to be unique against
    other constructs used in the same statement, and will also be the
    same name for each successive compilation of the same statement
    object.

    :param selectable: any :class:`.FromClause` subclass,
        such as a table, select statement, etc.

    :param name: string name to be assigned as the alias.
        If ``None``, a name will be deterministically generated
        at compile time.

    :param flat: Will be passed through to if the given selectable
     is an instance of :class:`.Join` - see :meth:`.Join.alias`
     for details.

     .. versionadded:: 0.9.0

    """
    return selectable.alias(name=name, flat=flat) 
開發者ID:jpush,項目名稱:jbox,代碼行數:43,代碼來源:selectable.py

示例15: replace_selectable

# 需要導入模塊: import sqlalchemy [as 別名]
# 或者: from sqlalchemy import alias [as 別名]
def replace_selectable(self, sqlutil, old, alias):
        """replace all occurrences of FromClause 'old' with the given Alias
        object, returning a copy of this :class:`.FromClause`.

        """

        return sqlutil.ClauseAdapter(alias).traverse(self) 
開發者ID:jpush,項目名稱:jbox,代碼行數:9,代碼來源:selectable.py


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