Module: CommandSearch::Postgres
- Defined in:
- lib/command_search/backends/postgres.rb
Class Method Summary collapse
- .build_query(ast) ⇒ Object
- .build_quoted_regex(input) ⇒ Object
- .command_search(node) ⇒ Object
- .compare_search(node) ⇒ Object
- .quote_string(str) ⇒ Object
Class Method Details
.build_query(ast) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/command_search/backends/postgres.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(input) ⇒ Object
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/command_search/backends/postgres.rb', line 10 def build_quoted_regex(input) str = quote_string(input) str = Regexp.escape(str) if str[/(^\W)|(\W$)/] head_border = '(^|[^:+\w])' tail_border = '($|[^:+\w])' return head_border + str + tail_border end '\m' + str + '\y' end |
.command_search(node) ⇒ Object
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 |
# File 'lib/command_search/backends/postgres.rb', line 21 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 = "'f'" 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]}') AND (#{field} < '#{val[1]}') AND (#{field} IS NOT NULL) ) " end if type == :quote op = '~' val = "'#{build_quoted_regex(val)}'" elsif type == :str op = '~~*' val = quote_string(val) val.gsub!('%', '\%') val.gsub!('_', '\_') val = "'%#{val}%'" elsif type == :number op = '=' end "(#{field} #{op} #{val}) AND (#{field} IS NOT NULL)" end |
.compare_search(node) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/command_search/backends/postgres.rb', line 61 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/postgres.rb', line 5 def quote_string(str) # activerecord/lib/active_record/connection_adapters/abstract/quoting.rb:62 str.gsub('\\', '\&\&').gsub("'", "''") end |