当前位置: 首页>>代码示例>>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;未经允许,请勿转载。