Skip to content

Commit

Permalink
Refactor TSearch to remove sanitize_sql_array call
Browse files Browse the repository at this point in the history
Fixes #83
  • Loading branch information
nertzy committed Apr 2, 2013
1 parent b3d6b79 commit aa35cab
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
6 changes: 0 additions & 6 deletions lib/pg_search/features/feature.rb
Expand Up @@ -3,12 +3,6 @@ module Features
class Feature class Feature
delegate :connection, :quoted_table_name, :to => :'@model' delegate :connection, :quoted_table_name, :to => :'@model'


begin
include ActiveRecord::Sanitization::ClassMethods
rescue NameError
delegate :sanitize_sql_array, :to => :'@model'
end

def initialize(query, options, columns, model, normalizer) def initialize(query, options, columns, model, normalizer)
@query = query @query = query
@options = options || {} @options = options || {}
Expand Down
27 changes: 13 additions & 14 deletions lib/pg_search/features/tsearch.rb
Expand Up @@ -15,20 +15,16 @@ def initialize(*args)


def conditions def conditions
Arel::Nodes::Grouping.new( Arel::Nodes::Grouping.new(
Arel::Nodes::InfixOperation.new("@@", arel_wrap(tsdocument, interpolations), arel_wrap(tsquery, interpolations)) Arel::Nodes::InfixOperation.new("@@", arel_wrap(tsdocument), arel_wrap(tsquery))
) )
end end


def rank def rank
arel_wrap(tsearch_rank, interpolations) arel_wrap(tsearch_rank)
end end


private private


def interpolations
{:query => query.to_s, :dictionary => dictionary.to_s}
end

DISALLOWED_TSQUERY_CHARACTERS = /['?\\:]/ DISALLOWED_TSQUERY_CHARACTERS = /['?\\:]/


def tsquery_for_term(term) def tsquery_for_term(term)
Expand All @@ -45,7 +41,10 @@ def tsquery_for_term(term)
(connection.quote(':*') if options[:prefix]) (connection.quote(':*') if options[:prefix])
].compact.join(" || ") ].compact.join(" || ")


"to_tsquery(:dictionary, #{tsquery_sql})" Arel::Nodes::NamedFunction.new(
"to_tsquery",
[dictionary, Arel.sql(tsquery_sql)]
).to_sql
end end


def tsquery def tsquery
Expand All @@ -61,7 +60,11 @@ def tsdocument
"#{quoted_table_name}.#{column_name}" "#{quoted_table_name}.#{column_name}"
else else
columns.map do |search_column| columns.map do |search_column|
tsvector = "to_tsvector(:dictionary, #{normalize(search_column.to_sql)})" tsvector = Arel::Nodes::NamedFunction.new(
"to_tsvector",
[dictionary, Arel.sql(normalize(search_column.to_sql))]
).to_sql

if search_column.weight.nil? if search_column.weight.nil?
tsvector tsvector
else else
Expand Down Expand Up @@ -92,12 +95,8 @@ def dictionary
options[:dictionary] || :simple options[:dictionary] || :simple
end end


def arel_wrap(sql_string, interpolations = {}) def arel_wrap(sql_string)
Arel::Nodes::Grouping.new( Arel::Nodes::Grouping.new(Arel.sql(sql_string))
Arel.sql(
sanitize_sql_array([sql_string, interpolations])
)
)
end end
end end
end end
Expand Down

0 comments on commit aa35cab

Please sign in to comment.