Python sqlalchemy 模块,alias() 实例源码
我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用sqlalchemy.alias()。
def self_group(self, against=None):
"""Apply a 'grouping' to this :class:`.ClauseElement`.
This method is overridden by subclasses to return a
"grouping" construct,i.e. parenthesis. In particular
it's used by "binary" expressions to provide a grouping
around themselves when placed into a larger expression,
as well as by :func:`.select` constructs when placed into
the FROM clause of another :func:`.select`. (Note that
subqueries should be normally created using the
:func:`.Select.alias` method,as many platforms require
nested SELECT statements to be named).
As expressions are composed together,the application of
:meth:`self_group` is automatic - end-user code should never
need to use this method directly. Note that sqlAlchemy's
clause constructs take operator precedence into account -
so parenthesis might not be needed,for example,in
an expression like ``x OR (y AND z)`` - AND takes precedence
over OR.
The base :meth:`self_group` method of :class:`.ClauseElement`
just returns self.
"""
return self
def inverse_join(selectable, left_alias, right_alias, relationship):
if relationship.property.secondary is not None:
secondary_alias = sa.alias(relationship.property.secondary)
return selectable.join(
secondary_alias,
adapt_expr(
relationship.property.secondaryjoin,
sa.inspect(left_alias).selectable,
secondary_alias
)
).join(
right_alias,
adapt_expr(
relationship.property.primaryjoin,
sa.inspect(right_alias).selectable,
secondary_alias
)
)
else:
join = sa.orm.join(right_alias, relationship)
onclause = join.onclause
return selectable.join(right_alias, onclause)
def chained_inverse_join(relationships, leaf_model):
selectable = sa.inspect(leaf_model).selectable
aliases = [leaf_model]
for index, relationship in enumerate(relationships[1:]):
aliases.append(sa.orm.aliased(relationship.mapper.class_))
selectable = inverse_join(
selectable,
aliases[index],
aliases[index + 1],
relationships[index]
)
if relationships[-1].property.secondary is not None:
secondary_alias = sa.alias(relationships[-1].property.secondary)
selectable = selectable.join(
secondary_alias,
adapt_expr(
relationships[-1].property.secondaryjoin,
secondary_alias,
sa.inspect(aliases[-1]).selectable
)
)
aliases.append(secondary_alias)
return selectable, aliases
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)
def alias(self, flat=False):
"""return an alias of this :class:`.FromClause`.
This is shorthand for calling::
from sqlalchemy import alias
a = alias(self,name=name)
See :func:`~.expression.alias` for details.
"""
return Alias(self, name)
def alias(self, **kw):
return FromGrouping(self.element.alias(**kw))
def with_hint(self, selectable, text, dialect_name='*'):
"""Add an indexing or other executional context hint for the given
selectable to this :class:`.Select`.
The text of the hint is rendered in the appropriate
location for the database backend in use,relative
to the given :class:`.Table` or :class:`.Alias` passed as the
``selectable`` argument. The dialect implementation
typically uses Python string substitution Syntax
with the token ``%(name)s`` to render the name of
the table or alias. E.g. when using Oracle,the
following::
select([mytable]).\\
with_hint(mytable,"index(%(name)s ix_mytable)")
Would render sql as::
select /*+ index(mytable ix_mytable) */ ... from mytable
The ``dialect_name`` option will limit the rendering of a particular
hint to a particular backend. Such as,to add hints for both Oracle
and Sybase simultaneously::
select([mytable]).\\
with_hint(mytable,"index(%(name)s ix_mytable)",'oracle').\\
with_hint(mytable,"WITH INDEX ix_mytable",'sybase')
.. seealso::
:meth:`.Select.with_statement_hint`
"""
if selectable is None:
self._statement_hints += ((dialect_name, text), )
else:
self._hints = self._hints.union(
{(selectable, dialect_name): text})
def alias(self, **kw):
return FromGrouping(self.element.alias(**kw))
def alias(self, flat=False):
return CTE(
self.original,
name=name,
recursive=self.recursive,
_cte_alias=self,
)
def with_hint(self, dialect_name='*'):
"""Add an indexing hint for the given selectable to this
:class:`.Select`.
The text of the hint is rendered in the appropriate
location for the database backend in use,'sybase')
"""
self._hints = self._hints.union(
{(selectable, dialect_name): text})
def alias(self, **kw):
return FromGrouping(self.element.alias(**kw))
def alias(selectable, name=None):
"""Return an :class:`.Alias` object.
An :class:`.Alias` represents any :class:`.FromClause`
with an alternate name assigned within sql,a name will be deterministically generated
at compile time.
"""
return Alias(selectable, name=name)
def alias(self, name=None):
"""return an alias of this :class:`.FromClause`.
This is shorthand for calling::
from sqlalchemy import alias
a = alias(self, name)
def with_hint(self,"+ index(%(name)s ix_mytable)")
Would render sql as::
select /*+ index(mytable ix_mytable) */ ... from mytable
The ``dialect_name`` option will limit the rendering of a particular
hint to a particular backend. Such as,"+ index(%(name)s ix_mytable)",'sybase')
"""
self._hints = self._hints.union(
{(selectable, dialect_name): text})
def alias(self, **kw):
return FromGrouping(self.element.alias(**kw))
def alias(self, **kw):
return FromGrouping(self.element.alias(**kw))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。