models: replace raw LIKE queries with where_like.
This commit is contained in:
@@ -51,7 +51,7 @@ class TagBatchChangeJob < ApplicationJob
|
|||||||
query = normalized_antecedent
|
query = normalized_antecedent
|
||||||
adds = normalized_consequent
|
adds = normalized_consequent
|
||||||
arel = query.inject(User.none) do |scope, x|
|
arel = query.inject(User.none) do |scope, x|
|
||||||
scope.or(User.where("blacklisted_tags like ?", "%" + x.to_escaped_for_sql_like + "%"))
|
scope.or(User.where_like(:blacklisted_tags, "*#{x}*"))
|
||||||
end
|
end
|
||||||
|
|
||||||
arel.find_each do |user|
|
arel.find_each do |user|
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ module TagAutocomplete
|
|||||||
|
|
||||||
def search_exact(query, n = 4)
|
def search_exact(query, n = 4)
|
||||||
Tag
|
Tag
|
||||||
.where("name like ? escape e'\\\\'", query.to_escaped_for_sql_like + "%")
|
.where_like(:name, query + "*")
|
||||||
.where("post_count > 0")
|
.where("post_count > 0")
|
||||||
.order("post_count desc")
|
.order("post_count desc")
|
||||||
.limit(n)
|
.limit(n)
|
||||||
@@ -58,7 +58,7 @@ module TagAutocomplete
|
|||||||
Tag
|
Tag
|
||||||
.where("name % ?", query)
|
.where("name % ?", query)
|
||||||
.where("abs(length(name) - ?) <= 3", query.size)
|
.where("abs(length(name) - ?) <= 3", query.size)
|
||||||
.where("name like ? escape E'\\\\'", query[0].to_escaped_for_sql_like + '%')
|
.where_like(:name, query[0] + "*")
|
||||||
.where("post_count > 0")
|
.where("post_count > 0")
|
||||||
.order(Arel.sql("similarity(name, #{Tag.connection.quote(query)}) DESC"))
|
.order(Arel.sql("similarity(name, #{Tag.connection.quote(query)}) DESC"))
|
||||||
.limit(n)
|
.limit(n)
|
||||||
@@ -102,9 +102,9 @@ module TagAutocomplete
|
|||||||
TagAlias
|
TagAlias
|
||||||
.select("tags.name, tags.post_count, tags.category, tag_aliases.antecedent_name")
|
.select("tags.name, tags.post_count, tags.category, tag_aliases.antecedent_name")
|
||||||
.joins("INNER JOIN tags ON tags.name = tag_aliases.consequent_name")
|
.joins("INNER JOIN tags ON tags.name = tag_aliases.consequent_name")
|
||||||
.where("tag_aliases.antecedent_name LIKE ? ESCAPE E'\\\\'", wildcard_name.to_escaped_for_sql_like)
|
.where_like(:antecedent_name, wildcard_name)
|
||||||
.active
|
.active
|
||||||
.where("tags.name NOT LIKE ? ESCAPE E'\\\\'", wildcard_name.to_escaped_for_sql_like)
|
.where_not_like("tags.name", wildcard_name)
|
||||||
.where("tags.post_count > 0")
|
.where("tags.post_count > 0")
|
||||||
.order("tags.post_count desc")
|
.order("tags.post_count desc")
|
||||||
.limit(n)
|
.limit(n)
|
||||||
|
|||||||
@@ -17,7 +17,11 @@ class ApplicationRecord < ActiveRecord::Base
|
|||||||
concerning :SearchMethods do
|
concerning :SearchMethods do
|
||||||
class_methods do
|
class_methods do
|
||||||
def qualified_column_for(attr)
|
def qualified_column_for(attr)
|
||||||
"#{table_name}.#{column_for_attribute(attr).name}"
|
if attr.is_a?(Symbol)
|
||||||
|
"#{table_name}.#{column_for_attribute(attr).name}"
|
||||||
|
else
|
||||||
|
attr.to_s
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def where_like(attr, value)
|
def where_like(attr, value)
|
||||||
|
|||||||
@@ -438,11 +438,11 @@ class Artist < ApplicationRecord
|
|||||||
|
|
||||||
module SearchMethods
|
module SearchMethods
|
||||||
def any_other_name_matches(regex)
|
def any_other_name_matches(regex)
|
||||||
where(id: Artist.from("unnest(other_names) AS other_name").where("other_name ~ ?", regex))
|
where(id: Artist.from("unnest(other_names) AS other_name").where_regex("other_name", regex))
|
||||||
end
|
end
|
||||||
|
|
||||||
def any_other_name_like(name)
|
def any_other_name_like(name)
|
||||||
where(id: Artist.from("unnest(other_names) AS other_name").where("other_name LIKE ?", name.to_escaped_for_sql_like))
|
where(id: Artist.from("unnest(other_names) AS other_name").where_like("other_name", name))
|
||||||
end
|
end
|
||||||
|
|
||||||
def any_name_matches(query)
|
def any_name_matches(query)
|
||||||
|
|||||||
@@ -14,8 +14,11 @@ class ArtistCommentary < ApplicationRecord
|
|||||||
module SearchMethods
|
module SearchMethods
|
||||||
def text_matches(query)
|
def text_matches(query)
|
||||||
query = "*#{query}*" unless query =~ /\*/
|
query = "*#{query}*" unless query =~ /\*/
|
||||||
escaped_query = query.to_escaped_for_sql_like
|
|
||||||
where("original_title ILIKE ? ESCAPE E'\\\\' OR original_description ILIKE ? ESCAPE E'\\\\' OR translated_title ILIKE ? ESCAPE E'\\\\' OR translated_description ILIKE ? ESCAPE E'\\\\'", escaped_query, escaped_query, escaped_query, escaped_query)
|
where_ilike(:original_title, query)
|
||||||
|
.or(where_ilike(:original_description, query))
|
||||||
|
.or(where_ilike(:translated_title, query))
|
||||||
|
.or(where_ilike(:translated_description, query))
|
||||||
end
|
end
|
||||||
|
|
||||||
def deleted
|
def deleted
|
||||||
|
|||||||
@@ -17,14 +17,6 @@ class Ban < ApplicationRecord
|
|||||||
exists?(["user_id = ? AND expires_at > ?", user.id, Time.now])
|
exists?(["user_id = ? AND expires_at > ?", user.id, Time.now])
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.reason_matches(query)
|
|
||||||
if query =~ /\*/
|
|
||||||
where("lower(bans.reason) LIKE ?", query.mb_chars.downcase.to_escaped_for_sql_like)
|
|
||||||
else
|
|
||||||
where("bans.reason @@ plainto_tsquery(?)", query)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.search(params)
|
def self.search(params)
|
||||||
q = super
|
q = super
|
||||||
|
|
||||||
|
|||||||
@@ -810,7 +810,7 @@ class Tag < ApplicationRecord
|
|||||||
end
|
end
|
||||||
|
|
||||||
def name_matches(name)
|
def name_matches(name)
|
||||||
where("tags.name LIKE ? ESCAPE E'\\\\'", normalize_name(name).to_escaped_for_sql_like)
|
where_like(:name, normalize_name(name))
|
||||||
end
|
end
|
||||||
|
|
||||||
def search(params)
|
def search(params)
|
||||||
@@ -876,9 +876,9 @@ class Tag < ApplicationRecord
|
|||||||
TagAlias
|
TagAlias
|
||||||
.select("tags.name, tags.post_count, tags.category, tag_aliases.antecedent_name")
|
.select("tags.name, tags.post_count, tags.category, tag_aliases.antecedent_name")
|
||||||
.joins("INNER JOIN tags ON tags.name = tag_aliases.consequent_name")
|
.joins("INNER JOIN tags ON tags.name = tag_aliases.consequent_name")
|
||||||
.where("tag_aliases.antecedent_name LIKE ? ESCAPE E'\\\\'", wildcard_name.to_escaped_for_sql_like)
|
.where_like(:antecedent_name, wildcard_name)
|
||||||
.active
|
.active
|
||||||
.where("tags.name NOT LIKE ? ESCAPE E'\\\\'", wildcard_name.to_escaped_for_sql_like)
|
.where_not_like("tags.name", wildcard_name)
|
||||||
.where("tags.post_count > 0")
|
.where("tags.post_count > 0")
|
||||||
.order("tags.post_count desc")
|
.order("tags.post_count desc")
|
||||||
.limit(limit * 2) # Get extra records in case some duplicates get filtered out.
|
.limit(limit * 2) # Get extra records in case some duplicates get filtered out.
|
||||||
|
|||||||
@@ -85,7 +85,7 @@ class TagRelationship < ApplicationRecord
|
|||||||
|
|
||||||
module SearchMethods
|
module SearchMethods
|
||||||
def name_matches(name)
|
def name_matches(name)
|
||||||
where("(antecedent_name like ? escape E'\\\\' or consequent_name like ? escape E'\\\\')", name.mb_chars.downcase.to_escaped_for_sql_like, name.mb_chars.downcase.to_escaped_for_sql_like)
|
where_ilike(:antecedent_name, name).or(where_ilike(:consequent_name, name))
|
||||||
end
|
end
|
||||||
|
|
||||||
def status_matches(status)
|
def status_matches(status)
|
||||||
|
|||||||
@@ -203,7 +203,7 @@ class Upload < ApplicationRecord
|
|||||||
q = q.search_attributes(params, :uploader, :post, :source, :rating, :parent_id, :server, :md5, :server, :file_ext, :file_size, :image_width, :image_height, :referer_url)
|
q = q.search_attributes(params, :uploader, :post, :source, :rating, :parent_id, :server, :md5, :server, :file_ext, :file_size, :image_width, :image_height, :referer_url)
|
||||||
|
|
||||||
if params[:source_matches].present?
|
if params[:source_matches].present?
|
||||||
q = q.where("uploads.source LIKE ? ESCAPE E'\\\\'", params[:source_matches].to_escaped_for_sql_like)
|
q = q.where_like(:source, params[:source_matches])
|
||||||
end
|
end
|
||||||
|
|
||||||
if params[:has_post].to_s.truthy?
|
if params[:has_post].to_s.truthy?
|
||||||
@@ -213,15 +213,15 @@ class Upload < ApplicationRecord
|
|||||||
end
|
end
|
||||||
|
|
||||||
if params[:status].present?
|
if params[:status].present?
|
||||||
q = q.where("uploads.status LIKE ? ESCAPE E'\\\\'", params[:status].to_escaped_for_sql_like)
|
q = q.where_like(:status, params[:status])
|
||||||
end
|
end
|
||||||
|
|
||||||
if params[:backtrace].present?
|
if params[:backtrace].present?
|
||||||
q = q.where("uploads.backtrace LIKE ? ESCAPE E'\\\\'", params[:backtrace].to_escaped_for_sql_like)
|
q = q.where_like(:backtrace, params[:backtrace])
|
||||||
end
|
end
|
||||||
|
|
||||||
if params[:tag_string].present?
|
if params[:tag_string].present?
|
||||||
q = q.where("uploads.tag_string LIKE ? ESCAPE E'\\\\'", params[:tag_string].to_escaped_for_sql_like)
|
q = q.where_like(:tag_string, params[:tag_string])
|
||||||
end
|
end
|
||||||
|
|
||||||
q.apply_default_order(params)
|
q.apply_default_order(params)
|
||||||
|
|||||||
@@ -43,14 +43,14 @@ class WikiPage < ApplicationRecord
|
|||||||
end
|
end
|
||||||
|
|
||||||
def other_names_include(name)
|
def other_names_include(name)
|
||||||
name = normalize_other_name(name).downcase
|
name = normalize_other_name(name)
|
||||||
subquery = WikiPage.from("unnest(other_names) AS other_name").where("lower(other_name) = ?", name)
|
subquery = WikiPage.from("unnest(other_names) AS other_name").where_iequals("other_name", name)
|
||||||
where(id: subquery)
|
where(id: subquery)
|
||||||
end
|
end
|
||||||
|
|
||||||
def other_names_match(name)
|
def other_names_match(name)
|
||||||
if name =~ /\*/
|
if name =~ /\*/
|
||||||
subquery = WikiPage.from("unnest(other_names) AS other_name").where("other_name ILIKE ?", name.to_escaped_for_sql_like)
|
subquery = WikiPage.from("unnest(other_names) AS other_name").where_ilike("other_name", name)
|
||||||
where(id: subquery)
|
where(id: subquery)
|
||||||
else
|
else
|
||||||
other_names_include(name)
|
other_names_include(name)
|
||||||
|
|||||||
Reference in New Issue
Block a user