command_search 0.11.1 → 0.11.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 647f1b1fa534c1187bd63333ae986e357fb122a777e9f56d9af2d7d6f34ee6c8
4
- data.tar.gz: d070864c2e1bba273a8fa837b20c7732efcbb5768149046cf2e68ee666e6f0b3
3
+ metadata.gz: c9a7f59d4e3aefbd46963b94d52d18d49d63263ab51ceb31a207ee17ca23bf1b
4
+ data.tar.gz: 43fe311f65ec44dd89dcb997f0b61daa1347466f63fae1721c0168eb62bfa377
5
5
  SHA512:
6
- metadata.gz: 3b37775511b9cb93c556c9ce766bc76b3df6f15fb8a2671c814fa0809d32c96a5ffff0371cce4f9fb53dd9de0123f477c233611be04280883c811635a0775208
7
- data.tar.gz: e9a636dd33c9e53211e381a00e6af3cf2aa0901fdd5e6a12079053285465eb8b4775206f48b295146b2a2e08e4e440b3888e8bff0c1bd8270edff520ad00e0b5
6
+ metadata.gz: f9751ac591a874cf99009d28fc691b6543b5fc038dbbb6d7a7b063cf47f134da0e8ee500300877565e5c2b4164f82a446d983c9a6730c2eab35a86a6ae6a92ed
7
+ data.tar.gz: 47173f4d0904edcb9ddcbd1f2a24e00f9cd70d528c9234cd1840c861cb4cb7e85681bc6b3362fb73fefff095d16cb6aa277f83027185bc5068ed43c3b5b9b757
@@ -8,6 +8,7 @@ load(__dir__ + '/command_search/backends/memory.rb')
8
8
  load(__dir__ + '/command_search/backends/mongoer.rb')
9
9
  load(__dir__ + '/command_search/backends/postgres.rb')
10
10
  load(__dir__ + '/command_search/backends/sqlite.rb')
11
+ load(__dir__ + '/command_search/backends/mysql.rb')
11
12
 
12
13
  class Boolean; end
13
14
 
@@ -29,6 +30,10 @@ module CommandSearch
29
30
  Normalizer.normalize!(ast, fields, false)
30
31
  return Sqlite.build_query(ast)
31
32
  end
33
+ if type == :mysql
34
+ Normalizer.normalize!(ast, fields, false)
35
+ return Mysql.build_query(ast)
36
+ end
32
37
  Normalizer.normalize!(ast, fields)
33
38
  return Mongoer.build_query(ast) if type == :mongo
34
39
  ast
@@ -47,6 +52,10 @@ module CommandSearch
47
52
  ast = CommandSearch.build(:sqlite, query, options)
48
53
  return source.where(ast)
49
54
  end
55
+ if source.respond_to?(:mysql2_connection)
56
+ ast = CommandSearch.build(:mysql, query, options)
57
+ return source.where(ast)
58
+ end
50
59
  ast = CommandSearch.build(:other, query, options)
51
60
  source.select { |x| CommandSearch::Memory.check(x, ast) }
52
61
  end
@@ -0,0 +1,106 @@
1
+ module CommandSearch
2
+ module Mysql
3
+ module_function
4
+
5
+ def quote_string(str)
6
+ # activerecord/lib/active_record/connection_adapters/abstract/quoting.rb:62
7
+ str.gsub('\\', '\&\&').gsub("'", "''")
8
+ end
9
+
10
+ def build_quoted_regex(str)
11
+ str = Regexp.escape(str)
12
+ str = quote_string(str)
13
+ if str[/(^\W)|(\W$)/]
14
+ head_border = '(^|[^:+[[:alnum:]]])'
15
+ tail_border = '($|[^:+[[:alnum:]]])'
16
+ return head_border + str + tail_border
17
+ end
18
+ '\\\\b' + str + '\\\\b'
19
+ end
20
+
21
+ def command_search(node)
22
+ field_node = node[:value].first
23
+ field = field_node[:value]
24
+ search_node = node[:value].last
25
+ val = search_node[:value]
26
+ type = search_node[:type]
27
+ return '0 = 1' if field == '__CommandSearch_dummy_key__'
28
+ if type == Boolean || type == :existence
29
+ if val
30
+ return "(NOT ((#{field} IS NULL) OR (#{field} LIKE '0')))"
31
+ end
32
+ return "((#{field} IS NULL) OR (#{field} LIKE '0'))"
33
+ end
34
+ if type == Time
35
+ return '0 = 1' unless val
36
+ return "
37
+ (
38
+ (#{field} > '#{val[0] - 1}') AND
39
+ (#{field} <= '#{val[1] - 1}') AND
40
+ (#{field} IS NOT NULL)
41
+ )
42
+ "
43
+ end
44
+ if type == :quote
45
+ op = 'RLIKE BINARY'
46
+ val = "'#{build_quoted_regex(val)}'"
47
+ elsif type == :str
48
+ op = 'LIKE'
49
+ val = quote_string(val)
50
+ val.gsub!('%', '\%')
51
+ val.gsub!('_', '\_')
52
+ val = "'%#{val}%'"
53
+ elsif type == :number
54
+ op = '='
55
+ end
56
+ "((#{field} #{op} #{val}) AND (#{field} IS NOT NULL))"
57
+ end
58
+
59
+ def compare_search(node)
60
+ field_node = node[:value].first
61
+ field = field_node[:value]
62
+ search_node = node[:value].last
63
+ val = search_node[:value]
64
+ type = search_node[:type]
65
+ op = node[:nest_op]
66
+ if node[:compare_across_fields]
67
+ "
68
+ (
69
+ (#{field} #{op} #{val}) AND
70
+ (#{field} IS NOT NULL) AND
71
+ (#{val} IS NOT NULL)
72
+ )
73
+ "
74
+ elsif type == Time && val
75
+ "(#{field} #{op} '#{val}') AND (#{field} IS NOT NULL)"
76
+ elsif val.is_a?(Numeric) || val == val.to_i.to_s || val == val.to_f.to_s
77
+ "(#{field} #{op} #{val}) AND (#{field} IS NOT NULL)"
78
+ else
79
+ '0 = 1'
80
+ end
81
+ end
82
+
83
+ def build_query(ast)
84
+ out = []
85
+ ast = [ast] unless ast.is_a?(Array)
86
+ ast.each do |node|
87
+ type = node[:type]
88
+ if type == :colon
89
+ out.push(command_search(node))
90
+ elsif type == :compare
91
+ out.push(compare_search(node))
92
+ elsif type == :and
93
+ out.push(build_query(node[:value]))
94
+ elsif type == :or
95
+ clauses = node[:value].map { |x| build_query(x) }
96
+ clause = clauses.join(' OR ')
97
+ out.push("(#{clause})")
98
+ elsif type == :not
99
+ clause = build_query(node[:value])
100
+ out.push("NOT (#{clause})")
101
+ end
102
+ end
103
+ out.join(' AND ')
104
+ end
105
+ end
106
+ end
@@ -45,7 +45,7 @@ module CommandSearch
45
45
  end
46
46
  if type == :quote
47
47
  op = '~'
48
- val = "'#{build_quoted_regex(val)}'"
48
+ val = "'#{build_quoted_regex(val)}'"
49
49
  elsif type == :str
50
50
  op = '~~*'
51
51
  val = quote_string(val)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: command_search
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.1
4
+ version: 0.11.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - zumbalogy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-12-18 00:00:00.000000000 Z
11
+ date: 2019-12-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: chronic
@@ -34,6 +34,7 @@ files:
34
34
  - lib/command_search/aliaser.rb
35
35
  - lib/command_search/backends/memory.rb
36
36
  - lib/command_search/backends/mongoer.rb
37
+ - lib/command_search/backends/mysql.rb
37
38
  - lib/command_search/backends/postgres.rb
38
39
  - lib/command_search/backends/sqlite.rb
39
40
  - lib/command_search/lexer.rb