本文整理汇总了Python中sqlalchemy.distinct方法的典型用法代码示例。如果您正苦于以下问题:Python sqlalchemy.distinct方法的具体用法?Python sqlalchemy.distinct怎么用?Python sqlalchemy.distinct使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy
的用法示例。
在下文中一共展示了sqlalchemy.distinct方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _adjust_for_single_inheritance
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import distinct [as 别名]
def _adjust_for_single_inheritance(self, context):
"""Apply single-table-inheritance filtering.
For all distinct single-table-inheritance mappers represented in
the columns clause of this query, add criterion to the WHERE
clause of the given QueryContext such that only the appropriate
subtypes are selected from the total results.
"""
for (ext_info, adapter) in set(self._mapper_adapter_map.values()):
if ext_info in self._join_entities:
continue
single_crit = ext_info.mapper._single_table_criterion
if single_crit is not None:
if adapter:
single_crit = adapter.traverse(single_crit)
single_crit = self._adapt_clause(single_crit, False, False)
context.whereclause = sql.and_(
sql.True_._ifnone(context.whereclause),
single_crit)
示例2: get_samples
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import distinct [as 别名]
def get_samples(filters=None, count=False, ids=False):
if not filters:
filters = []
if count:
sample_query = db.session.query(func.count(distinct(Sample.sample_name)))
sample_query = build_filter(sample_query, filters, Sample)
return sample_query.one()[0]
elif ids:
sample_query = db.session.query(distinct(Sample.sample_id))
sample_query = build_filter(sample_query, filters, Sample)
samples = [x[0] for x in sample_query.all()]
return samples
else:
sample_query = db.session.query(distinct(Sample.sample_name))
sample_query = build_filter(sample_query, filters, Sample)
samples = [x[0] for x in sample_query.all()]
return samples
示例3: count_tenants
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import distinct [as 别名]
def count_tenants(self, context):
# tenants are the owner of zones, count the number of unique tenants
# select count(distinct tenant_id) from zones
query = select([func.count(distinct(tables.zones.c.tenant_id))])
query = self._apply_tenant_criteria(context, tables.zones, query)
query = self._apply_deleted_criteria(context, tables.zones, query)
resultproxy = self.session.execute(query)
result = resultproxy.fetchone()
if result is None:
return 0
return result[0]
##
# Zone Methods
##
示例4: __load_chat_filters
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import distinct [as 别名]
def __load_chat_filters():
global CHAT_FILTERS
try:
chats = SESSION.query(CustomFilters.chat_id).distinct().all()
for (chat_id,) in chats: # remove tuple by ( ,)
CHAT_FILTERS[chat_id] = []
all_filters = SESSION.query(CustomFilters).all()
for x in all_filters:
CHAT_FILTERS[x.chat_id] += [x.keyword]
CHAT_FILTERS = {x: sorted(set(y), key=lambda i: (-len(i), i)) for x, y in CHAT_FILTERS.items()}
finally:
SESSION.close()
# ONLY USE FOR MIGRATE OLD FILTERS TO NEW FILTERS
示例5: show_entity
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import distinct [as 别名]
def show_entity(group, name):
entity = Entity.query.filter(Entity.group==group, Entity.name==name).first()
if not entity:
return make_response("The specified entity could not be found.", 404)
if entity.person:
return redirect(url_for('show_person', id=entity.person.id))
query = db.session.query(distinct(Document.id))\
.join(DocumentEntity)\
.filter(DocumentEntity.entity_id == entity.id)\
.order_by(Document.published_at.desc())
pagination = paginate(query, int(request.args.get('page', 1)), 50)
doc_ids = [r[0] for r in pagination.items]
docs = Document.query\
.options(subqueryload(Document.utterances))\
.filter(Document.id.in_(doc_ids))\
.order_by(Document.published_at.desc())\
.all()
return render_template('entities/show.haml', entity=entity, docs=docs, pagination=pagination)
示例6: get_active_milestones
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import distinct [as 别名]
def get_active_milestones(session, project):
""" Returns the list of all the active milestones for a given project.
"""
query = (
session.query(model.Issue.milestone)
.filter(model.Issue.project_id == project.id)
.filter(model.Issue.status == "Open")
.filter(model.Issue.milestone.isnot(None))
)
return sorted([item[0] for item in query.distinct()])
示例7: get_project_family
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import distinct [as 别名]
def get_project_family(session, project):
""" Retrieve the family of the specified project, ie: all the forks
of the main project.
If the specified project is a fork, let's work our way up the chain
until we find the main project so we can go down and get all the forks
and the forks of the forks (but not one level more).
:arg session: The SQLAlchemy session to use
:type session: sqlalchemy.orm.session.Session
:arg project: The project whose family is searched
:type project: pagure.lib.model.Project
"""
parent = project
while parent.is_fork:
parent = parent.parent
sub = session.query(sqlalchemy.distinct(model.Project.id)).filter(
model.Project.parent_id == parent.id
)
query = (
session.query(model.Project)
.filter(
sqlalchemy.or_(
model.Project.parent_id.in_(sub.subquery()),
model.Project.parent_id == parent.id,
)
)
.filter(model.Project.user_id == model.User.id)
.order_by(model.User.user)
)
return [parent] + query.all()
示例8: _create_distinct
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import distinct [as 别名]
def _create_distinct(cls, expr):
"""Produce an column-expression-level unary ``DISTINCT`` clause.
This applies the ``DISTINCT`` keyword to an individual column
expression, and is typically contained within an aggregate function,
as in::
from sqlalchemy import distinct, func
stmt = select([func.count(distinct(users_table.c.name))])
The above would produce an expression resembling::
SELECT COUNT(DISTINCT name) FROM user
The :func:`.distinct` function is also available as a column-level
method, e.g. :meth:`.ColumnElement.distinct`, as in::
stmt = select([func.count(users_table.c.name.distinct())])
The :func:`.distinct` operator is different from the
:meth:`.Select.distinct` method of :class:`.Select`,
which produces a ``SELECT`` statement
with ``DISTINCT`` applied to the result set as a whole,
e.g. a ``SELECT DISTINCT`` expression. See that method for further
information.
.. seealso::
:meth:`.ColumnElement.distinct`
:meth:`.Select.distinct`
:data:`.func`
"""
expr = _literal_as_binds(expr)
return UnaryExpression(
expr, operator=operators.distinct_op,
type_=expr.type, wraps_column_expression=False)
示例9: _get_condition
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import distinct [as 别名]
def _get_condition(self):
return self._no_criterion_condition(
"get", order_by=False, distinct=False)
示例10: _get_existing_condition
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import distinct [as 别名]
def _get_existing_condition(self):
self._no_criterion_assertion("get", order_by=False, distinct=False)
示例11: _no_criterion_assertion
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import distinct [as 别名]
def _no_criterion_assertion(self, meth, order_by=True, distinct=True):
if not self._enable_assertions:
return
if self._criterion is not None or \
self._statement is not None or self._from_obj or \
self._limit is not None or self._offset is not None or \
self._group_by or (order_by and self._order_by) or \
(distinct and self._distinct):
raise sa_exc.InvalidRequestError(
"Query.%s() being called on a "
"Query with existing criterion. " % meth)
示例12: distinct
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import distinct [as 别名]
def distinct(self, *criterion):
"""Apply a ``DISTINCT`` to the query and return the newly resulting
``Query``.
.. note::
The :meth:`.distinct` call includes logic that will automatically
add columns from the ORDER BY of the query to the columns
clause of the SELECT statement, to satisfy the common need
of the database backend that ORDER BY columns be part of the
SELECT list when DISTINCT is used. These columns *are not*
added to the list of columns actually fetched by the
:class:`.Query`, however, so would not affect results.
The columns are passed through when using the
:attr:`.Query.statement` accessor, however.
:param \*expr: optional column expressions. When present,
the Postgresql dialect will render a ``DISTINCT ON (<expressions>>)``
construct.
"""
if not criterion:
self._distinct = True
else:
criterion = self._adapt_col_list(criterion)
if isinstance(self._distinct, list):
self._distinct += criterion
else:
self._distinct = criterion
示例13: _select_args
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import distinct [as 别名]
def _select_args(self):
return {
'limit': self._limit,
'offset': self._offset,
'distinct': self._distinct,
'prefixes': self._prefixes,
'suffixes': self._suffixes,
'group_by': self._group_by or None,
'having': self._having
}
示例14: _should_nest_selectable
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import distinct [as 别名]
def _should_nest_selectable(self):
kwargs = self._select_args
return (kwargs.get('limit') is not None or
kwargs.get('offset') is not None or
kwargs.get('distinct', False))
示例15: count
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import distinct [as 别名]
def count(self):
"""Return a count of rows this Query would return.
This generates the SQL for this Query as follows::
SELECT count(1) AS count_1 FROM (
SELECT <rest of query follows...>
) AS anon_1
.. versionchanged:: 0.7
The above scheme is newly refined as of 0.7b3.
For fine grained control over specific columns
to count, to skip the usage of a subquery or
otherwise control of the FROM clause,
or to use other aggregate functions,
use :attr:`~sqlalchemy.sql.expression.func`
expressions in conjunction
with :meth:`~.Session.query`, i.e.::
from sqlalchemy import func
# count User records, without
# using a subquery.
session.query(func.count(User.id))
# return count of user "id" grouped
# by "name"
session.query(func.count(User.id)).\\
group_by(User.name)
from sqlalchemy import distinct
# count distinct "name" values
session.query(func.count(distinct(User.name)))
"""
col = sql.func.count(sql.literal_column('*'))
return self.from_self(col).scalar()