module CommandSearch::MysqlV5

Public Instance Methods

build_query(ast) click to toggle source
# File lib/command_search/backends/mysql_v5.rb, line 85
def build_query(ast)
  out = []
  ast = [ast] unless ast.is_a?(Array)
  ast.each do |node|
    type = node[:type]
    if type == :colon
      out.push(command_search(node))
    elsif type == :compare
      out.push(compare_search(node))
    elsif type == :and
      out.push(build_query(node[:value]))
    elsif type == :or
      clauses = node[:value].map { |x| build_query(x) }
      clause = clauses.join(' OR ')
      out.push("(#{clause})")
    elsif type == :not
      clause = build_query(node[:value])
      out.push("NOT (#{clause})")
    end
  end
  out.join(' AND ')
end
build_quoted_regex(str) click to toggle source
# File lib/command_search/backends/mysql_v5.rb, line 12
def build_quoted_regex(str)
  str = Regexp.escape(str)
  str = quote_string(str)
  if str[/(^\W)|(\W$)/]
    head_border = '(^|[[:<:]]|\\\\()'
    tail_border = '($|[[:>:]]|\\\\))'
    return head_border + str + tail_border
  end
  '[[:<:]]' + str + '[[:>:]]'
end
quote_string(str) click to toggle source
# File lib/command_search/backends/mysql_v5.rb, line 7
def quote_string(str)
  # activerecord/lib/active_record/connection_adapters/abstract/quoting.rb:62
  str.gsub('\\', '\&\&').gsub("'", "''")
end