From 9a22804e62df8ec6e83a3f8e9ad5b2c90001fbb7 Mon Sep 17 00:00:00 2001 From: Bilal Catic Date: Fri, 31 Jan 2020 11:45:59 +0100 Subject: [PATCH 1/5] add query generator and tests for query generator --- spec/query_generator_spec.rb | 77 +++++++++++++++++++++++++++++++++++ text_to_sql_query.rb | 79 ++++++++++++++++++++++++++++++++++++ 2 files changed, 156 insertions(+) create mode 100644 spec/query_generator_spec.rb create mode 100644 text_to_sql_query.rb diff --git a/spec/query_generator_spec.rb b/spec/query_generator_spec.rb new file mode 100644 index 0000000..df1fb1c --- /dev/null +++ b/spec/query_generator_spec.rb @@ -0,0 +1,77 @@ +require './text_to_sql_query' + +class SqlGeneratorTester + describe 'Testing query generator' do + it 'tests simple search term without column name and without quotes' do + @query = TextToSqlQuery.new('-123', [:'players.title', :'players.tag', :'players.device_id'], :'players.device_id') + + expect(@query.where_clause).to eq ['players.device_id LIKE ?', '%-123%'] + end + + it 'tests simple search term with column name and without quotes' do + @query = TextToSqlQuery.new('title:ab', [:'players.title', :'players.tag', :'players.device_id'], :'players.device_id') + + expect(@query.where_clause).to eq ['players.title LIKE ?', '%ab%'] + end + + it 'tests simple search term with unknown column name and without quotes' do + @query = TextToSqlQuery.new('unknown:ab', [:'players.title', :'players.tag', :'players.device_id'], :'players.device_id') + + expect(@query.where_clause).to eq ['players.device_id LIKE ?', '%ab%'] + end + + it 'tests simple search term without column name and with quotes' do + @query = TextToSqlQuery.new('"ab"', [:'players.title', :'players.tag', :'players.device_id'], :'players.device_id') + + expect(@query.where_clause).to eq ['players.device_id LIKE ?', '%"ab"%'] + end + + it 'tests simple search term with column name and with quotes' do + @query = TextToSqlQuery.new('tag:"ab"', [:'players.title', :'players.tag', :'players.device_id'], :'players.device_id') + + expect(@query.where_clause).to eq ['players.tag LIKE ?', '%"ab"%'] + end + + it 'tests search without operators' do + @query = TextToSqlQuery.new('123 456', [:'players.title', :'players.tag', :'players.device_id'], :'players.device_id') + + expect(@query.where_clause).to eq ['players.device_id LIKE ? OR players.device_id LIKE ?', '%123%', '%456%'] + end + + it 'tests search with OR operator' do + @query = TextToSqlQuery.new('123 or 456', [:'players.title', :'players.tag', :'players.device_id'], :'players.device_id') + + expect(@query.where_clause).to eq ['players.device_id LIKE ? OR players.device_id LIKE ?', '%123%', '%456%'] + end + + it 'tests search with AND operator' do + @query = TextToSqlQuery.new('123 and 456', [:'players.title', :'players.tag', :'players.device_id'], :'players.device_id') + + expect(@query.where_clause).to eq ['players.device_id LIKE ? AND players.device_id LIKE ?', '%123%', '%456%'] + end + + it 'tests search with NOT operator on default column' do + @query = TextToSqlQuery.new('not 23', [:'players.title', :'players.tag', :'players.device_id'], :'players.device_id') + + expect(@query.where_clause).to eq ['NOT players.device_id LIKE ?', '%23%'] + end + + it 'tests search with NOT operator on non-default column' do + @query = TextToSqlQuery.new('not tag:23', [:'players.title', :'players.tag', :'players.device_id'], :'players.device_id') + + expect(@query.where_clause).to eq ['NOT players.tag LIKE ?', '%23%'] + end + + it 'tests search with mixed logical operators' do + @query = TextToSqlQuery.new('title:ab and not tag:hf-1', [:'players.title', :'players.tag', :'players.device_id'], :'players.device_id') + + expect(@query.where_clause).to eq ['players.title LIKE ? AND NOT players.tag LIKE ?', '%ab%', '%hf-1%'] + end + + it 'tests search with mixed logical operators without NOT' do + @query = TextToSqlQuery.new('title:a and title:b or title:c', [:'players.title', :'players.tag', :'players.device_id'], :'players.device_id') + + expect(@query.where_clause).to eq ['players.title LIKE ? AND players.title LIKE ? OR players.title LIKE ?', '%a%', '%b%', '%c%'] + end + end +end \ No newline at end of file diff --git a/text_to_sql_query.rb b/text_to_sql_query.rb new file mode 100644 index 0000000..8c96f39 --- /dev/null +++ b/text_to_sql_query.rb @@ -0,0 +1,79 @@ +require './parser' + +class TextToSqlQuery + def initialize(text, fields, default_field, fields_mappings = {}) + @text = text.to_s.strip + @fields = fields.map(&:to_sym) + @default_field = default_field.to_sym + @fields_mappings = fields_mappings.merge(@fields.reduce({}) do |mappings, field| + _table_name, field_name = field.to_s.split('.') + mappings[field_name.to_sym] = field + mappings + end) + end + + def where_clause + @parser = Query.new + @parsed_tree = @parser.parse(@text) + generate_sql @parsed_tree + end + + private + + def generate_sql(tree) + @first_key = tree.keys.first + case @first_key + when :DEFAULT_COLUMN + ["#{@default_field.to_s} LIKE ?", "%#{tree[@first_key]}%"] + when :OPERATOR_OR + generate_expression_for_logical_operator(:OR, tree[@first_key]) + when :OPERATOR_AND + generate_expression_for_logical_operator(:AND, tree[@first_key]) + when :OPERATOR_NOT + @not_array = generate_sql tree[@first_key] + + if @not_array.length < 2 + raise "There should be more than 1 element for expression following NOT operator" + end + + @not_expression = @not_array.first + @not_params = @not_array[1..] + + ["NOT #{@not_expression}"] + @not_params + + else + # key is column name + @mapping = @fields_mappings[@first_key.to_sym] + if @mapping.nil? + ["#{@default_field.to_s} LIKE ?", "%#{tree[@first_key]}%"] + else + ["#{@mapping.to_s} LIKE ?", "%#{tree[@first_key]}%"] + end + end + end + + def generate_expression_for_logical_operator(operator, operator_array) + if operator_array.length != 2 + raise "There should be two array elements for #{operator.to_s} operator" + end + + @first_operand = generate_sql operator_array.first + @second_operand = generate_sql operator_array.last + + if @first_operand.length < 2 + raise 'There should be more than 1 element in first operand array' + end + + if @second_operand.length < 2 + raise 'There should be more than 1 element in second operand array' + end + + @first_operand_expression = @first_operand.first + @first_operand_params = @first_operand[1..] + + @second_operand_expression = @second_operand.first + @second_operand_params = @second_operand[1..] + + ["#{@first_operand_expression} #{operator.to_s} #{@second_operand_expression}"] + @first_operand_params + @second_operand_params + end +end \ No newline at end of file From 2dc2453039eda90a810a55ffcb0947a94684341d Mon Sep 17 00:00:00 2001 From: Bilal Catic Date: Fri, 31 Jan 2020 13:15:27 +0100 Subject: [PATCH 2/5] change instance variables to local variables --- text_to_sql_query.rb | 46 ++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/text_to_sql_query.rb b/text_to_sql_query.rb index 8c96f39..bd40ba5 100644 --- a/text_to_sql_query.rb +++ b/text_to_sql_query.rb @@ -21,33 +21,33 @@ class TextToSqlQuery private def generate_sql(tree) - @first_key = tree.keys.first - case @first_key + first_key = tree.keys.first + case first_key when :DEFAULT_COLUMN - ["#{@default_field.to_s} LIKE ?", "%#{tree[@first_key]}%"] + ["#{@default_field.to_s} LIKE ?", "%#{tree[first_key]}%"] when :OPERATOR_OR - generate_expression_for_logical_operator(:OR, tree[@first_key]) + generate_expression_for_logical_operator(:OR, tree[first_key]) when :OPERATOR_AND - generate_expression_for_logical_operator(:AND, tree[@first_key]) + generate_expression_for_logical_operator(:AND, tree[first_key]) when :OPERATOR_NOT - @not_array = generate_sql tree[@first_key] + not_array = generate_sql tree[first_key] - if @not_array.length < 2 + if not_array.length < 2 raise "There should be more than 1 element for expression following NOT operator" end - @not_expression = @not_array.first - @not_params = @not_array[1..] + not_expression = not_array.first + not_params = not_array[1..] - ["NOT #{@not_expression}"] + @not_params + ["NOT #{not_expression}"] + not_params else # key is column name - @mapping = @fields_mappings[@first_key.to_sym] - if @mapping.nil? - ["#{@default_field.to_s} LIKE ?", "%#{tree[@first_key]}%"] + mapping = @fields_mappings[first_key.to_sym] + if mapping.nil? + ["#{@default_field.to_s} LIKE ?", "%#{tree[first_key]}%"] else - ["#{@mapping.to_s} LIKE ?", "%#{tree[@first_key]}%"] + ["#{mapping.to_s} LIKE ?", "%#{tree[first_key]}%"] end end end @@ -57,23 +57,23 @@ class TextToSqlQuery raise "There should be two array elements for #{operator.to_s} operator" end - @first_operand = generate_sql operator_array.first - @second_operand = generate_sql operator_array.last + first_operand = generate_sql operator_array.first + second_operand = generate_sql operator_array.last - if @first_operand.length < 2 + if first_operand.length < 2 raise 'There should be more than 1 element in first operand array' end - if @second_operand.length < 2 + if second_operand.length < 2 raise 'There should be more than 1 element in second operand array' end - @first_operand_expression = @first_operand.first - @first_operand_params = @first_operand[1..] + first_operand_expression = first_operand.first + first_operand_params = first_operand[1..] - @second_operand_expression = @second_operand.first - @second_operand_params = @second_operand[1..] + second_operand_expression = second_operand.first + second_operand_params = second_operand[1..] - ["#{@first_operand_expression} #{operator.to_s} #{@second_operand_expression}"] + @first_operand_params + @second_operand_params + ["#{first_operand_expression} #{operator.to_s} #{second_operand_expression}"] + first_operand_params + second_operand_params end end \ No newline at end of file From 0286f3ed816ca85748c21adde534cb4581bd5e1b Mon Sep 17 00:00:00 2001 From: Bilal Catic Date: Fri, 31 Jan 2020 13:27:11 +0100 Subject: [PATCH 3/5] add brackets to expression for precedence handling --- text_to_sql_query.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/text_to_sql_query.rb b/text_to_sql_query.rb index bd40ba5..ac1e085 100644 --- a/text_to_sql_query.rb +++ b/text_to_sql_query.rb @@ -74,6 +74,6 @@ class TextToSqlQuery second_operand_expression = second_operand.first second_operand_params = second_operand[1..] - ["#{first_operand_expression} #{operator.to_s} #{second_operand_expression}"] + first_operand_params + second_operand_params + ["(#{first_operand_expression} #{operator.to_s} #{second_operand_expression})"] + first_operand_params + second_operand_params end end \ No newline at end of file From 9c2aff53d75813701ce30706cc6852113ad4d19b Mon Sep 17 00:00:00 2001 From: Bilal Catic Date: Fri, 31 Jan 2020 13:27:31 +0100 Subject: [PATCH 4/5] add tests for mixed operators expression; test precedence --- spec/query_generator_spec.rb | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/spec/query_generator_spec.rb b/spec/query_generator_spec.rb index df1fb1c..05fb720 100644 --- a/spec/query_generator_spec.rb +++ b/spec/query_generator_spec.rb @@ -35,19 +35,19 @@ class SqlGeneratorTester it 'tests search without operators' do @query = TextToSqlQuery.new('123 456', [:'players.title', :'players.tag', :'players.device_id'], :'players.device_id') - expect(@query.where_clause).to eq ['players.device_id LIKE ? OR players.device_id LIKE ?', '%123%', '%456%'] + expect(@query.where_clause).to eq ['(players.device_id LIKE ? OR players.device_id LIKE ?)', '%123%', '%456%'] end it 'tests search with OR operator' do @query = TextToSqlQuery.new('123 or 456', [:'players.title', :'players.tag', :'players.device_id'], :'players.device_id') - expect(@query.where_clause).to eq ['players.device_id LIKE ? OR players.device_id LIKE ?', '%123%', '%456%'] + expect(@query.where_clause).to eq ['(players.device_id LIKE ? OR players.device_id LIKE ?)', '%123%', '%456%'] end it 'tests search with AND operator' do @query = TextToSqlQuery.new('123 and 456', [:'players.title', :'players.tag', :'players.device_id'], :'players.device_id') - expect(@query.where_clause).to eq ['players.device_id LIKE ? AND players.device_id LIKE ?', '%123%', '%456%'] + expect(@query.where_clause).to eq ['(players.device_id LIKE ? AND players.device_id LIKE ?)', '%123%', '%456%'] end it 'tests search with NOT operator on default column' do @@ -65,13 +65,25 @@ class SqlGeneratorTester it 'tests search with mixed logical operators' do @query = TextToSqlQuery.new('title:ab and not tag:hf-1', [:'players.title', :'players.tag', :'players.device_id'], :'players.device_id') - expect(@query.where_clause).to eq ['players.title LIKE ? AND NOT players.tag LIKE ?', '%ab%', '%hf-1%'] + expect(@query.where_clause).to eq ['(players.title LIKE ? AND NOT players.tag LIKE ?)', '%ab%', '%hf-1%'] end it 'tests search with mixed logical operators without NOT' do @query = TextToSqlQuery.new('title:a and title:b or title:c', [:'players.title', :'players.tag', :'players.device_id'], :'players.device_id') - expect(@query.where_clause).to eq ['players.title LIKE ? AND players.title LIKE ? OR players.title LIKE ?', '%a%', '%b%', '%c%'] + expect(@query.where_clause).to eq ['((players.title LIKE ? AND players.title LIKE ?) OR players.title LIKE ?)', '%a%', '%b%', '%c%'] + end + + it 'tests search with brackets in expression' do + @query = TextToSqlQuery.new('title:a and (title:b or title:c)', [:'players.title', :'players.tag', :'players.device_id'], :'players.device_id') + + expect(@query.where_clause).to eq ['(players.title LIKE ? AND (players.title LIKE ? OR players.title LIKE ?))', '%a%', '%b%', '%c%'] + end + + it 'tests search with brackets in expression and with NOT operator' do + @query = TextToSqlQuery.new('title:a and not (title:b or title:c)', [:'players.title', :'players.tag', :'players.device_id'], :'players.device_id') + + expect(@query.where_clause).to eq ['(players.title LIKE ? AND NOT (players.title LIKE ? OR players.title LIKE ?))', '%a%', '%b%', '%c%'] end end end \ No newline at end of file From 793e26e01f1b619a2bb257b2a74f7636a200e635 Mon Sep 17 00:00:00 2001 From: Bilal Catic Date: Fri, 31 Jan 2020 13:58:09 +0100 Subject: [PATCH 5/5] handle special chars; write tests for special chars --- spec/query_generator_spec.rb | 6 ++++++ text_to_sql_query.rb | 23 +++++++++++++++++------ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/spec/query_generator_spec.rb b/spec/query_generator_spec.rb index 05fb720..4226f20 100644 --- a/spec/query_generator_spec.rb +++ b/spec/query_generator_spec.rb @@ -85,5 +85,11 @@ class SqlGeneratorTester expect(@query.where_clause).to eq ['(players.title LIKE ? AND NOT (players.title LIKE ? OR players.title LIKE ?))', '%a%', '%b%', '%c%'] end + + it 'tests search with special characters in search term' do + @query = TextToSqlQuery.new('title:"%a_\"', [:'players.title', :'players.tag', :'players.device_id'], :'players.device_id') + + expect(@query.where_clause).to eq ['players.title LIKE ?', '%"\%a\_\\"%'] + end end end \ No newline at end of file diff --git a/text_to_sql_query.rb b/text_to_sql_query.rb index ac1e085..241149a 100644 --- a/text_to_sql_query.rb +++ b/text_to_sql_query.rb @@ -22,15 +22,17 @@ class TextToSqlQuery def generate_sql(tree) first_key = tree.keys.first + node_value = tree[first_key] case first_key when :DEFAULT_COLUMN - ["#{@default_field.to_s} LIKE ?", "%#{tree[first_key]}%"] + escaped_node_value = escape_special_chars node_value + ["#{@default_field.to_s} LIKE ?", "%#{escaped_node_value}%"] when :OPERATOR_OR - generate_expression_for_logical_operator(:OR, tree[first_key]) + generate_expression_for_logical_operator(:OR, node_value) when :OPERATOR_AND - generate_expression_for_logical_operator(:AND, tree[first_key]) + generate_expression_for_logical_operator(:AND, node_value) when :OPERATOR_NOT - not_array = generate_sql tree[first_key] + not_array = generate_sql node_value if not_array.length < 2 raise "There should be more than 1 element for expression following NOT operator" @@ -43,11 +45,12 @@ class TextToSqlQuery else # key is column name + escaped_node_value = escape_special_chars node_value mapping = @fields_mappings[first_key.to_sym] if mapping.nil? - ["#{@default_field.to_s} LIKE ?", "%#{tree[first_key]}%"] + ["#{@default_field.to_s} LIKE ?", "%#{escaped_node_value}%"] else - ["#{mapping.to_s} LIKE ?", "%#{tree[first_key]}%"] + ["#{mapping.to_s} LIKE ?", "%#{escaped_node_value}%"] end end end @@ -76,4 +79,12 @@ class TextToSqlQuery ["(#{first_operand_expression} #{operator.to_s} #{second_operand_expression})"] + first_operand_params + second_operand_params end + + def escape_special_chars(text) + result = text + result.gsub!(/\_/, '\_') + result.tr!('\\', '\\') + result.gsub!(/%/, '\%') + result + end end \ No newline at end of file