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


Python sqlalchemy.distinct方法代碼示例

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

示例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 
開發者ID:ewels,項目名稱:MegaQC,代碼行數:19,代碼來源:utils.py

示例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
    ## 
開發者ID:openstack,項目名稱:designate,代碼行數:20,代碼來源:__init__.py

示例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 
開發者ID:AyraHikari,項目名稱:EmiliaHikari,代碼行數:20,代碼來源:cust_filters_sql.py

示例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) 
開發者ID:Code4SA,項目名稱:mma-dexter,代碼行數:26,代碼來源:entities.py

示例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()]) 
開發者ID:Pagure,項目名稱:pagure,代碼行數:14,代碼來源:query.py

示例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() 
開發者ID:Pagure,項目名稱:pagure,代碼行數:35,代碼來源:query.py

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

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

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

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

示例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 
開發者ID:jpush,項目名稱:jbox,代碼行數:32,代碼來源:query.py

示例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
        } 
開發者ID:jpush,項目名稱:jbox,代碼行數:12,代碼來源:query.py

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

示例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() 
開發者ID:jpush,項目名稱:jbox,代碼行數:41,代碼來源:query.py


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