Module: Knitsearch::Query

Extended by:
Query
Included in:
Query
Defined in:
lib/knitsearch/query.rb

Overview

FTS5 match-string builder. Escapes user input so it can’t break out of FTS5 syntax. Quotes each token, doubles internal quotes, strips control characters, and joins with the specified operator. Returns nil for empty input — the caller decides what to do (typically: return an empty relation).

Constant Summary collapse

CONTROL_CHARACTERS =
/[\x00-\x1f\x7f]/

Instance Method Summary collapse

Instance Method Details

#escape(input, operator: :and, prefix: false, match: :word) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/knitsearch/query.rb', line 13

def escape(input, operator: :and, prefix: false, match: :word)
  return nil if input.nil?

  cleaned = input.to_s.gsub(CONTROL_CHARACTERS, " ").strip
  return nil if cleaned.empty?

  tokens = cleaned.split(/\s+/).reject(&:empty?)
  return nil if tokens.empty?

  case match
  when :word
    build_word_match(tokens, operator, prefix)
  when :phrase
    build_phrase_match(tokens, operator)
  else
    raise ArgumentError, "match must be :word or :phrase, got: #{match.inspect}"
  end
end