Module: CommandSearch::Sqlite

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

Class Method Summary collapse

Class Method Details

.build_query(ast) ⇒ Object



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

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

.command_search(node) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
62
# File 'lib/command_search/backends/sqlite.rb', line 10

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
    false_val = 'false'
    false_val = 0 if field_node[:field_type] == Numeric
    if val
      return "NOT ((#{field} = #{false_val}) OR (#{field} IS NULL))"
    end
    return "((#{field} = #{false_val}) OR (#{field} IS NULL))"
  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 = quote_string(val)
    val.gsub!('[', '[[]')
    val.gsub!('*', '[*]')
    val.gsub!('?', '[?]')
    border = '[ .,()?"\'\']'
    return "
      (
        (#{field} IS NOT NULL) AND
        (
          (#{field} GLOB '#{val}') OR
          (#{field} GLOB '*#{border}#{val}#{border}*') OR
          (#{field} GLOB '*#{border}#{val}') OR
          (#{field} GLOB '#{val}#{border}*')
        )
      )
    "
  elsif type == :str
    op = 'LIKE'
    val = quote_string(val)
    val.gsub!('%', '\%')
    val.gsub!('_', '\_')
    val = "'%#{val}%' ESCAPE '\\'"
  elsif type == :number
    op = '='
  end
  "((#{field} #{op} #{val}) AND (#{field} IS NOT NULL))"
end

.compare_search(node) ⇒ Object



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

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



5
6
7
8
# File 'lib/command_search/backends/sqlite.rb', line 5

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