diff --git a/server/szurubooru/db/post.py b/server/szurubooru/db/post.py index da443381..2d9c50d3 100644 --- a/server/szurubooru/db/post.py +++ b/server/szurubooru/db/post.py @@ -58,13 +58,9 @@ class Post(Base): secondaryjoin=post_id == PostRelation.child_id) tag_count = column_property( - select( - [func.count('1')], - PostTag.post_id == post_id - ) \ - .correlate('Post') \ - .label('tag_count') - ) + select([func.count('1')]) + .where(PostTag.post_id == post_id) \ + .correlate('Post')) # TODO: wire these fav_count = Column('auto_fav_count', Integer, nullable=False, default=0) diff --git a/server/szurubooru/db/tag.py b/server/szurubooru/db/tag.py index 3254ba41..21553ebb 100644 --- a/server/szurubooru/db/tag.py +++ b/server/szurubooru/db/tag.py @@ -55,10 +55,25 @@ class Tag(Base): secondaryjoin=tag_id == TagImplication.child_id) post_count = column_property( - select( - [func.count('Post.post_id')], - PostTag.tag_id == tag_id - ) \ - .correlate('Tag') \ - .label('post_count') - ) + select([func.count('Post.post_id')]) \ + .where(PostTag.tag_id == tag_id) \ + .correlate('Tag')) + + first_name = column_property( + select([TagName.name]) \ + .where(TagName.tag_id == tag_id) \ + .limit(1) \ + .as_scalar(), + deferred=True) + + suggestion_count = column_property( + select([func.count(TagSuggestion.child_id)]) \ + .where(TagSuggestion.parent_id == tag_id) \ + .as_scalar(), + deferred=True) + + implication_count = column_property( + select([func.count(TagImplication.child_id)]) \ + .where(TagImplication.parent_id == tag_id) \ + .as_scalar(), + deferred=True) diff --git a/server/szurubooru/search/tag_search_config.py b/server/szurubooru/search/tag_search_config.py index 434bf146..984eaaf2 100644 --- a/server/szurubooru/search/tag_search_config.py +++ b/server/szurubooru/search/tag_search_config.py @@ -1,4 +1,3 @@ -import sqlalchemy from sqlalchemy.sql.expression import func from szurubooru import db from szurubooru.search.base_search_config import BaseSearchConfig @@ -8,11 +7,11 @@ class TagSearchConfig(BaseSearchConfig): return session.query(db.Tag) def finalize_query(self, query): - return query.order_by(self._first_name_subquery.asc()) + return query.order_by(db.Tag.first_name.asc()) @property def anonymous_filter(self): - return self._name_filter + return self._create_str_filter(db.Tag.first_name) @property def special_filters(self): @@ -21,7 +20,7 @@ class TagSearchConfig(BaseSearchConfig): @property def named_filters(self): return { - 'name': self._name_filter, + 'name': self._create_str_filter(db.Tag.first_name), 'category': self._create_str_filter(db.Tag.category), 'creation-date': self._create_date_filter(db.Tag.creation_time), 'creation-time': self._create_date_filter(db.Tag.creation_time), @@ -32,15 +31,15 @@ class TagSearchConfig(BaseSearchConfig): 'usages': self._create_num_filter(db.Tag.post_count), 'usage-count': self._create_num_filter(db.Tag.post_count), 'post-count': self._create_num_filter(db.Tag.post_count), - 'suggestion-count': self._suggestion_count_filter, - 'implication-count': self._implication_count_filter, + 'suggestion-count': self._create_num_filter(db.Tag.suggestion_count), + 'implication-count': self._create_num_filter(db.Tag.implication_count), } @property def order_columns(self): return { 'random': (func.random(), None), - 'name': (self._first_name_subquery, self.ORDER_ASC), + 'name': (db.Tag.first_name, self.ORDER_ASC), 'category': (db.Tag.category, self.ORDER_ASC), 'creation-date': (db.Tag.creation_time, self.ORDER_DESC), 'creation-time': (db.Tag.creation_time, self.ORDER_DESC), @@ -51,43 +50,6 @@ class TagSearchConfig(BaseSearchConfig): 'usages': (db.Tag.post_count, self.ORDER_DESC), 'usage-count': (db.Tag.post_count, self.ORDER_DESC), 'post-count': (db.Tag.post_count, self.ORDER_DESC), - 'suggestion-count': - (self._suggestion_count_subquery, self.ORDER_DESC), - 'implication-count': - (self._implication_count_subquery, self.ORDER_DESC), + 'suggestion-count': (db.Tag.suggestion_count, self.ORDER_DESC), + 'implication-count': (db.Tag.implication_count, self.ORDER_DESC), } - - def _name_filter(self, query, criterion): - str_filter = self._create_str_filter(db.TagName.name) - return query.filter( - db.Tag.tag_id.in_( - str_filter(query.session.query(db.TagName.tag_id), criterion))) - - def _suggestion_count_filter(self, query, criterion): - return query.filter( - self._apply_num_criterion_to_column( - self._suggestion_count_subquery, criterion)) - - def _implication_count_filter(self, query, criterion): - return query.filter( - self._apply_num_criterion_to_column( - self._implication_count_subquery, criterion)) - - @property - def _first_name_subquery(self): - return sqlalchemy.select([db.TagName.name]) \ - .limit(1) \ - .where(db.TagName.tag_id == db.Tag.tag_id) \ - .as_scalar() - - @property - def _suggestion_count_subquery(self): - return sqlalchemy.select([func.count(db.TagSuggestion.child_id)]) \ - .where(db.TagSuggestion.parent_id == db.Tag.tag_id) \ - .as_scalar() - - @property - def _implication_count_subquery(self): - return sqlalchemy.select([func.count(1)]) \ - .where(db.TagImplication.parent_id == db.Tag.tag_id) \ - .as_scalar()