本文整理汇总了Python中sqlalchemy.func.count方法的典型用法代码示例。如果您正苦于以下问题:Python func.count方法的具体用法?Python func.count怎么用?Python func.count使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy.func
的用法示例。
在下文中一共展示了func.count方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_top_logins
# 需要导入模块: from sqlalchemy import func [as 别名]
# 或者: from sqlalchemy.func import count [as 别名]
def get_top_logins(limit=10):
""" Return most popular logins for auto-completion """
count_ = func.count('*')
results = get_session().query(SecretModel.login, count_).\
filter(SecretModel.login != '').\
group_by(SecretModel.login).\
order_by(count_.desc()).\
limit(limit).\
all()
if results:
return [result.login for result in results]
return []
示例2: get_count_of_expired_temporary_dids
# 需要导入模块: from sqlalchemy import func [as 别名]
# 或者: from sqlalchemy.func import count [as 别名]
def get_count_of_expired_temporary_dids(rse_id, session=None):
"""
List expired temporary DIDs.
:param rse_id: the rse id.
:param session: The database session in use.
:returns: a count number.
"""
is_none = None
count = session.query(func.count(models.TemporaryDataIdentifier.scope)).\
with_hint(models.TemporaryDataIdentifier, "INDEX(tmp_dids TMP_DIDS_EXPIRED_AT_IDX)", 'oracle').\
filter(case([(models.TemporaryDataIdentifier.expired_at != is_none, models.TemporaryDataIdentifier.rse_id), ]) == rse_id).\
one()
return count[0] or 0
示例3: list_payload_counts
# 需要导入模块: from sqlalchemy import func [as 别名]
# 或者: from sqlalchemy.func import count [as 别名]
def list_payload_counts(executable, older_than=600, hash_executable=None, session=None):
"""
Give the counts of number of threads per payload for a certain executable.
:param executable: Executable name as a string, e.g., conveyor-submitter
:param older_than: Removes specified heartbeats older than specified nr of seconds
:param hash_executable: Hash of the executable.
:param session: The database session in use.
:returns: List of tuples
"""
if not hash_executable:
hash_executable = calc_hash(executable)
query = session.query(Heartbeats.payload,
func.count(Heartbeats.payload))\
.filter(Heartbeats.executable == hash_executable)\
.filter(Heartbeats.updated_at >= datetime.datetime.utcnow() - datetime.timedelta(seconds=older_than))\
.group_by(Heartbeats.payload)\
.order_by(Heartbeats.payload)
return dict(query.all())
示例4: count
# 需要导入模块: from sqlalchemy import func [as 别名]
# 或者: from sqlalchemy.func import count [as 别名]
def count(column, value, glob=False):
"""Counts number of rows with value in a column. This function is case-insensitive.
Positional arguments:
column -- the SQLAlchemy column object to search in (e.g. Table.a_column).
value -- the value to search for, any string.
Keyword arguments:
glob -- enable %globbing% search (default False).
Returns:
Number of rows that match. Equivalent of SELECT count(*) FROM.
"""
query = db.session.query(func.count('*'))
if glob:
query = query.filter(column.ilike(value))
else:
query = query.filter(func.lower(column) == value.lower())
return query.one()[0]
示例5: review_probable_validated
# 需要导入模块: from sqlalchemy import func [as 别名]
# 或者: from sqlalchemy.func import count [as 别名]
def review_probable_validated(self):
self.log.info("Doing optional validation")
with db.session_context() as db_sess:
new_items = db_sess.query(db.NuReleaseItem) \
.filter(db.NuReleaseItem.validated == True) \
.filter(db.NuReleaseItem.reviewed == 'unverified') \
.filter(db.NuReleaseItem.actual_target != None) \
.order_by(desc(db.NuReleaseItem.first_seen)) \
.all()
unverified = db_sess.query(db.NuReleaseItem) \
.filter(db.NuReleaseItem.validated == False) \
.filter(db.NuReleaseItem.actual_target != None) \
.count()
self.log.info("Have %s items to do validity checks on", len(new_items))
self.log.info("%s items needing checking", unverified)
for row in new_items:
self.review_probable_validated_row(row)
db_sess.commit()
示例6: get_samples
# 需要导入模块: from sqlalchemy import func [as 别名]
# 或者: from sqlalchemy.func import count [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
示例7: input_prepare_query_object
# 需要导入模块: from sqlalchemy import func [as 别名]
# 或者: from sqlalchemy.func import count [as 别名]
def input_prepare_query_object(self, query_object):
# When we count, we don't care about certain things
if query_object.get('count', False):
# Performance: do not sort when counting
query_object.pop('sort', None)
# We don't care about projections either
query_object.pop('project', None)
# Also, remove all skips & limits
query_object.pop('skip', None)
query_object.pop('limit', None)
# Remove all join, but not joinf (as it may filter)
query_object.pop('join', None)
# Finally, when we count, we have to remove `max_items` setting from MongoLimit.
# Only MongoLimit can do it, and it will do it for us.
# See: MongoLimit.input_prepare_query_object
return query_object
示例8: __init__
# 需要导入模块: from sqlalchemy import func [as 别名]
# 或者: from sqlalchemy.func import count [as 别名]
def __init__(self, query: Query):
# The original query. We store it just in case.
self._original_query = query
# The current query
# It differs from the originla query in that it is modified with a window function counting the rows
self._query = query
# The iterator for query results ; `None` if the query has not yet been executed
# If the query has been executed, there is always an iterator available, even if there were no results
self._query_iterator = None
# The total count ; `None` if the query has not yet been executed
self._count = None
# Whether the query is going to return single entities
self._single_entity = ( # copied from sqlalchemy.orm.loading.instances
not getattr(query, '_only_return_tuples', False) # accessing protected properties
and len(query._entities) == 1
and query._entities[0].supports_single_entity
)
# The method that will fix result rows
self._row_fixer = self._fix_result_tuple__single_entity if self._single_entity else self._fix_result_tuple__tuple
示例9: find_tenants
# 需要导入模块: from sqlalchemy import func [as 别名]
# 或者: from sqlalchemy.func import count [as 别名]
def find_tenants(self, context):
# returns an array of tenant_id & count of their zones
query = select([tables.zones.c.tenant_id,
func.count(tables.zones.c.id)])
query = self._apply_tenant_criteria(context, tables.zones, query)
query = self._apply_deleted_criteria(context, tables.zones, query)
query = query.group_by(tables.zones.c.tenant_id)
resultproxy = self.session.execute(query)
results = resultproxy.fetchall()
tenant_list = objects.TenantList(
objects=[objects.Tenant(id=t[0], zone_count=t[1]) for t in
results])
tenant_list.obj_reset_changes()
return tenant_list
示例10: count_tenants
# 需要导入模块: from sqlalchemy import func [as 别名]
# 或者: from sqlalchemy.func import count [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
##
示例11: count_recordsets
# 需要导入模块: from sqlalchemy import func [as 别名]
# 或者: from sqlalchemy.func import count [as 别名]
def count_recordsets(self, context, criterion=None):
# Ensure that we return only active recordsets
rjoin = tables.recordsets.join(
tables.zones,
tables.recordsets.c.zone_id == tables.zones.c.id)
query = select([func.count(tables.recordsets.c.id)]).\
select_from(rjoin).\
where(tables.zones.c.deleted == '0')
query = self._apply_criterion(tables.recordsets, query, criterion)
query = self._apply_tenant_criteria(context, tables.recordsets, query)
query = self._apply_deleted_criteria(context, tables.recordsets, query)
resultproxy = self.session.execute(query)
result = resultproxy.fetchone()
if result is None:
return 0
return result[0]
# Record Methods
示例12: has_session
# 需要导入模块: from sqlalchemy import func [as 别名]
# 或者: from sqlalchemy.func import count [as 别名]
def has_session(self, session_id: str) -> bool:
if self.core_mode:
t = self.Session.__table__
rows = self.db_engine.execute(select([func.count(t.c.auth_key)])
.where(and_(t.c.session_id == session_id,
t.c.auth_key != b'')))
try:
count, = next(rows)
return count > 0
except StopIteration:
return False
else:
return self.Session.query.filter(self.Session.session_id == session_id).count() > 0
示例13: count
# 需要导入模块: from sqlalchemy import func [as 别名]
# 或者: from sqlalchemy.func import count [as 别名]
def count():
"""
Return a count of all secrets
"""
return get_session().query(SecretModel).count()
示例14: get_count_query
# 需要导入模块: from sqlalchemy import func [as 别名]
# 或者: from sqlalchemy.func import count [as 别名]
def get_count_query(self):
"""
Return a the count query for the model type
A ``query(self.model).count()`` approach produces an excessive
subquery, so ``query(func.count('*'))`` should be used instead.
See commit ``#45a2723`` for details.
"""
if 'polymorphic_identity' in getattr(self.model, "__mapper_args__",
{}):
return self.session.query(func.count('*')).select_from(
self.model.query.selectable)
return self.session.query(func.count('*')).select_from(self.model)
示例15: fast_count
# 需要导入模块: from sqlalchemy import func [as 别名]
# 或者: from sqlalchemy.func import count [as 别名]
def fast_count(query):
"""
It turns out the built-in SQLAlchemy function for query.count() is pretty
slow. Alternatively, use this optimized function instead.
"""
count_query = (query
.statement.with_only_columns([func.count()]).order_by(None))
count = query.session.execute(count_query).scalar()
return count