post_tags_match: replace joins with subqueries.
Refactor various post_tag_match methods to use subqueries instead of joins. This simplifies things inside PostQueryBuilder, since now we can assume we're always dealing with a Post relation, rather than some other table joined with the posts table.
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
class PostQueryBuilder
|
||||
attr_accessor :query_string
|
||||
attr_accessor :query_string, :read_only
|
||||
|
||||
def initialize(query_string)
|
||||
def initialize(query_string, read_only: false)
|
||||
@query_string = query_string
|
||||
@read_only = read_only
|
||||
end
|
||||
|
||||
def add_range_relation(arr, field, relation)
|
||||
@@ -91,14 +92,12 @@ class PostQueryBuilder
|
||||
return CurrentUser.user.hide_deleted_posts?
|
||||
end
|
||||
|
||||
def build(relation = nil)
|
||||
def build
|
||||
unless query_string.is_a?(Hash)
|
||||
q = Tag.parse_query(query_string)
|
||||
end
|
||||
|
||||
if relation.nil?
|
||||
relation = Post.where("true")
|
||||
end
|
||||
relation = read_only ? PostReadOnly.all : Post.all
|
||||
|
||||
if q[:tag_count].to_i > Danbooru.config.tag_query_limit
|
||||
raise ::Post::SearchError.new("You cannot search for more than #{Danbooru.config.tag_query_limit} tags at a time")
|
||||
|
||||
@@ -19,7 +19,7 @@ class ArtistCommentary < ApplicationRecord
|
||||
end
|
||||
|
||||
def post_tags_match(query)
|
||||
PostQueryBuilder.new(query).build(self.joins(:post)).reorder("")
|
||||
where(post_id: PostQueryBuilder.new(query).build.reorder(""))
|
||||
end
|
||||
|
||||
def deleted
|
||||
|
||||
@@ -52,7 +52,7 @@ class Comment < ApplicationRecord
|
||||
end
|
||||
|
||||
def post_tags_match(query)
|
||||
PostQueryBuilder.new(query).build(self.joins(:post)).reorder("")
|
||||
where(post_id: PostQueryBuilder.new(query).build.reorder(""))
|
||||
end
|
||||
|
||||
def for_creator(user_id)
|
||||
|
||||
@@ -18,7 +18,7 @@ class Note < ApplicationRecord
|
||||
end
|
||||
|
||||
def post_tags_match(query)
|
||||
PostQueryBuilder.new(query).build(self.joins(:post)).reorder("")
|
||||
where(post_id: PostQueryBuilder.new(query).build.reorder(""))
|
||||
end
|
||||
|
||||
def for_creator(user_id)
|
||||
|
||||
@@ -1662,7 +1662,7 @@ class Post < ApplicationRecord
|
||||
|
||||
if read_only
|
||||
begin
|
||||
PostQueryBuilder.new(query).build(PostReadOnly.where("true"))
|
||||
PostQueryBuilder.new(query, read_only: true).build
|
||||
rescue PG::ConnectionBad
|
||||
PostQueryBuilder.new(query).build
|
||||
end
|
||||
|
||||
@@ -11,7 +11,7 @@ class PostAppeal < ApplicationRecord
|
||||
|
||||
module SearchMethods
|
||||
def post_tags_match(query)
|
||||
PostQueryBuilder.new(query).build(self.joins(:post))
|
||||
where(post_id: PostQueryBuilder.new(query).build.reorder(""))
|
||||
end
|
||||
|
||||
def resolved
|
||||
|
||||
@@ -35,7 +35,7 @@ class PostApproval < ApplicationRecord
|
||||
concerning :SearchMethods do
|
||||
class_methods do
|
||||
def post_tags_match(query)
|
||||
PostQueryBuilder.new(query).build(self.joins(:post))
|
||||
where(post_id: PostQueryBuilder.new(query).build.reorder(""))
|
||||
end
|
||||
|
||||
def search(params)
|
||||
|
||||
@@ -33,7 +33,7 @@ class PostFlag < ApplicationRecord
|
||||
end
|
||||
|
||||
def post_tags_match(query)
|
||||
PostQueryBuilder.new(query).build(self.joins(:post))
|
||||
where(post_id: PostQueryBuilder.new(query).build.reorder(""))
|
||||
end
|
||||
|
||||
def resolved
|
||||
|
||||
@@ -21,7 +21,7 @@ class PostReplacement < ApplicationRecord
|
||||
concerning :Search do
|
||||
class_methods do
|
||||
def post_tags_match(query)
|
||||
PostQueryBuilder.new(query).build(self.joins(:post))
|
||||
where(post_id: PostQueryBuilder.new(query).build.reorder(""))
|
||||
end
|
||||
|
||||
def search(params = {})
|
||||
|
||||
@@ -175,7 +175,7 @@ class Upload < ApplicationRecord
|
||||
end
|
||||
|
||||
def post_tags_match(query)
|
||||
PostQueryBuilder.new(query).build(self.joins(:post)).reorder("")
|
||||
where(post_id: PostQueryBuilder.new(query).build.reorder(""))
|
||||
end
|
||||
|
||||
def search(params)
|
||||
|
||||
Reference in New Issue
Block a user