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
|
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
|
@query_string = query_string
|
||||||
|
@read_only = read_only
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_range_relation(arr, field, relation)
|
def add_range_relation(arr, field, relation)
|
||||||
@@ -91,14 +92,12 @@ class PostQueryBuilder
|
|||||||
return CurrentUser.user.hide_deleted_posts?
|
return CurrentUser.user.hide_deleted_posts?
|
||||||
end
|
end
|
||||||
|
|
||||||
def build(relation = nil)
|
def build
|
||||||
unless query_string.is_a?(Hash)
|
unless query_string.is_a?(Hash)
|
||||||
q = Tag.parse_query(query_string)
|
q = Tag.parse_query(query_string)
|
||||||
end
|
end
|
||||||
|
|
||||||
if relation.nil?
|
relation = read_only ? PostReadOnly.all : Post.all
|
||||||
relation = Post.where("true")
|
|
||||||
end
|
|
||||||
|
|
||||||
if q[:tag_count].to_i > Danbooru.config.tag_query_limit
|
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")
|
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
|
end
|
||||||
|
|
||||||
def post_tags_match(query)
|
def post_tags_match(query)
|
||||||
PostQueryBuilder.new(query).build(self.joins(:post)).reorder("")
|
where(post_id: PostQueryBuilder.new(query).build.reorder(""))
|
||||||
end
|
end
|
||||||
|
|
||||||
def deleted
|
def deleted
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ class Comment < ApplicationRecord
|
|||||||
end
|
end
|
||||||
|
|
||||||
def post_tags_match(query)
|
def post_tags_match(query)
|
||||||
PostQueryBuilder.new(query).build(self.joins(:post)).reorder("")
|
where(post_id: PostQueryBuilder.new(query).build.reorder(""))
|
||||||
end
|
end
|
||||||
|
|
||||||
def for_creator(user_id)
|
def for_creator(user_id)
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ class Note < ApplicationRecord
|
|||||||
end
|
end
|
||||||
|
|
||||||
def post_tags_match(query)
|
def post_tags_match(query)
|
||||||
PostQueryBuilder.new(query).build(self.joins(:post)).reorder("")
|
where(post_id: PostQueryBuilder.new(query).build.reorder(""))
|
||||||
end
|
end
|
||||||
|
|
||||||
def for_creator(user_id)
|
def for_creator(user_id)
|
||||||
|
|||||||
@@ -1662,7 +1662,7 @@ class Post < ApplicationRecord
|
|||||||
|
|
||||||
if read_only
|
if read_only
|
||||||
begin
|
begin
|
||||||
PostQueryBuilder.new(query).build(PostReadOnly.where("true"))
|
PostQueryBuilder.new(query, read_only: true).build
|
||||||
rescue PG::ConnectionBad
|
rescue PG::ConnectionBad
|
||||||
PostQueryBuilder.new(query).build
|
PostQueryBuilder.new(query).build
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ class PostAppeal < ApplicationRecord
|
|||||||
|
|
||||||
module SearchMethods
|
module SearchMethods
|
||||||
def post_tags_match(query)
|
def post_tags_match(query)
|
||||||
PostQueryBuilder.new(query).build(self.joins(:post))
|
where(post_id: PostQueryBuilder.new(query).build.reorder(""))
|
||||||
end
|
end
|
||||||
|
|
||||||
def resolved
|
def resolved
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ class PostApproval < ApplicationRecord
|
|||||||
concerning :SearchMethods do
|
concerning :SearchMethods do
|
||||||
class_methods do
|
class_methods do
|
||||||
def post_tags_match(query)
|
def post_tags_match(query)
|
||||||
PostQueryBuilder.new(query).build(self.joins(:post))
|
where(post_id: PostQueryBuilder.new(query).build.reorder(""))
|
||||||
end
|
end
|
||||||
|
|
||||||
def search(params)
|
def search(params)
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ class PostFlag < ApplicationRecord
|
|||||||
end
|
end
|
||||||
|
|
||||||
def post_tags_match(query)
|
def post_tags_match(query)
|
||||||
PostQueryBuilder.new(query).build(self.joins(:post))
|
where(post_id: PostQueryBuilder.new(query).build.reorder(""))
|
||||||
end
|
end
|
||||||
|
|
||||||
def resolved
|
def resolved
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ class PostReplacement < ApplicationRecord
|
|||||||
concerning :Search do
|
concerning :Search do
|
||||||
class_methods do
|
class_methods do
|
||||||
def post_tags_match(query)
|
def post_tags_match(query)
|
||||||
PostQueryBuilder.new(query).build(self.joins(:post))
|
where(post_id: PostQueryBuilder.new(query).build.reorder(""))
|
||||||
end
|
end
|
||||||
|
|
||||||
def search(params = {})
|
def search(params = {})
|
||||||
|
|||||||
@@ -175,7 +175,7 @@ class Upload < ApplicationRecord
|
|||||||
end
|
end
|
||||||
|
|
||||||
def post_tags_match(query)
|
def post_tags_match(query)
|
||||||
PostQueryBuilder.new(query).build(self.joins(:post)).reorder("")
|
where(post_id: PostQueryBuilder.new(query).build.reorder(""))
|
||||||
end
|
end
|
||||||
|
|
||||||
def search(params)
|
def search(params)
|
||||||
|
|||||||
Reference in New Issue
Block a user