Module: CommandSearch::Mysql

Defined in:
lib/command_search/backends/mysql.rb

Class Method Summary collapse

Class Method Details

.build_query(ast) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/command_search/backends/mysql.rb', line 87

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) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/command_search/backends/mysql.rb', line 13

def build_quoted_regex(str)
  str = Regexp.escape(str)
  str = quote_string(str)
  if str[/(^\W)|(\W$)/]
    head_border = '(^|[^:+[[:alnum:]]])'
    tail_border = '($|[^:+[[:alnum:]]])'
    return head_border + str + tail_border
  end
  '\\\\b' + str + '\\\\b'
end

.command_search(node) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/command_search/backends/mysql.rb', line 24

def command_search(node)
  field_node = node[:value].first
  field = field_node[:value]
  search_node = node[:value].last
  val = search_node[:value]
  type = search_node[:type]
  return '0 = 1' if field == '__CommandSearch_dummy_key__'
  if type == Boolean || type == :existence
    if val
      return "(NOT ((#{field} IS NULL) OR (#{field} LIKE '0')))"
    end
    return "((#{field} IS NULL) OR (#{field} LIKE '0'))"
  end
  if type == Time
    return '0 = 1' unless val
    return "
      (
        (#{field} > '#{val[0] - 1}') AND
        (#{field} <= '#{val[1] - 1}') AND
        (#{field} IS NOT NULL)
      )
    "
  end
  if type == :quote
    val = build_quoted_regex(val)
    return "(REGEXP_LIKE(#{field}, '#{val}', 'c') AND (#{field} IS NOT NULL))"
  end
  if type == :number
    op = '='
  else # type == :str
    op = 'LIKE'
    val = quote_string(val)
    val.gsub!('%', '\%')
    val.gsub!('_', '\_')
    val = "'%#{val}%'"
  end
  "((#{field} #{op} #{val}) AND (#{field} IS NOT NULL))"
end

.compare_search(node) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/command_search/backends/mysql.rb', line 63

def compare_search(node)
  field_node = node[:value].first
  field = field_node[:value]
  search_node = node[:value].last
  val = search_node[:value]
  type = search_node[:type]
  op = node[:nest_op]
  if node[:compare_across_fields]
    "
      (
        (#{field} #{op} #{val}) AND
        (#{field} IS NOT NULL) AND
        (#{val} IS NOT NULL)
      )
    "
  elsif type == Time && val
    "(#{field} #{op} '#{val}') AND (#{field} IS NOT NULL)"
  elsif val.is_a?(Numeric) || val == val.to_i.to_s || val == val.to_f.to_s
    "(#{field} #{op} #{val}) AND (#{field} IS NOT NULL)"
  else
    '0 = 1'
  end
end

.quote_string(str) ⇒ Object



8
9
10
11
# File 'lib/command_search/backends/mysql.rb', line 8

def quote_string(str)
  # activerecord/lib/active_record/connection_adapters/abstract/quoting.rb:62
  str.gsub('\\', '\&\&').gsub("'", "''")
end