Kouhei Sutou
null+****@clear*****
Thu Apr 27 09:24:00 JST 2017
Kouhei Sutou 2017-04-27 09:24:00 +0900 (Thu, 27 Apr 2017) New Revision: 8ff9649b1491dbf2533602536948a743dbd9fe69 https://github.com/ranguba/groonga-client/commit/8ff9649b1491dbf2533602536948a743dbd9fe69 Message: select: change Symbol handling in script syntax This is a backward incompatible change. But the use case affected by this change will make Ruby scripts difficult to maintain. I hope that existing users didn't match the use case. The use case is using Symbol as string value in script syntax like the following: filter("title", :value) # => --filter "title == \"value\"" Before this change, all Symbol are treated as string value in script syntax. We can't specify column (object in general) in script syntax with the specification. With this change, Symbol is treated as identifier. It means that Symbol is treated as column (object in general) in script syntax like the following: filter("title", :normalized_title) # => --filter "title == normalized_title" For backward compatibility, invalid identifier in script syntax is fallbacked to string value in script syntax like the following: filter("title", :"Hello World") # => --filter "title == \"Hello World\"" I think that an exception should be raised for the case but I choose the above specification... Modified files: lib/groonga/client/request/select.rb test/request/select/test-filter-equal-parameter.rb test/request/select/test-filter-expression-parameter.rb test/request/test-select.rb Modified: lib/groonga/client/request/select.rb (+12 -1) =================================================================== --- lib/groonga/client/request/select.rb 2017-04-26 15:19:43 +0900 (ed46d84) +++ lib/groonga/client/request/select.rb 2017-04-27 09:24:00 +0900 (923fae5) @@ -433,7 +433,11 @@ module Groonga when String ScriptSyntax.format_string(value) when Symbol - ScriptSyntax.format_string(value.to_s) + if valid_script_syntax_identifier?(value) + value.to_s + else + ScriptSyntax.format_string(value.to_s) + end when ::Array escaped_value = "[" value.each_with_index do |element, i| @@ -456,6 +460,13 @@ module Groonga value end end + + identifier_part = "[a-zA-Z_][a-zA-Z0-9_]*" + VALID_SCRIPT_SYNTAX_IDENTIFIER_PATTERN = + /\A#{identifier_part}(?:\.#{identifier_part})*\z/ + def valid_script_syntax_identifier?(value) + VALID_SCRIPT_SYNTAX_IDENTIFIER_PATTERN === value.to_s + end end # @private Modified: test/request/select/test-filter-equal-parameter.rb (+14 -5) =================================================================== --- test/request/select/test-filter-equal-parameter.rb 2017-04-26 15:19:43 +0900 (0594e87) +++ test/request/select/test-filter-equal-parameter.rb 2017-04-27 09:24:00 +0900 (ca397df) @@ -51,11 +51,20 @@ title == "[\"He\\ llo\"]" to_parameters("title", "[\"He\\ llo\"]")) end - def test_symbol - assert_equal({ - :filter => "title == \"Hello\"", - }, - to_parameters("title", :Hello)) + sub_test_case("Symbol") do + def test_id + assert_equal({ + :filter => "title == normalized_title", + }, + to_parameters("title", :normalized_title)) + end + + def test_not_id + assert_equal({ + :filter => "title == \"Hello World\"", + }, + to_parameters("title", :"Hello World")) + end end def test_number Modified: test/request/select/test-filter-expression-parameter.rb (+17 -6) =================================================================== --- test/request/select/test-filter-expression-parameter.rb 2017-04-26 15:19:43 +0900 (dc9f3a6) +++ test/request/select/test-filter-expression-parameter.rb 2017-04-27 09:24:00 +0900 (4bf146e) @@ -55,12 +55,23 @@ title == "[\"He\\ llo\"]" :value => "[\"He\\ llo\"]")) end - def test_symbol - assert_equal({ - :filter => "title == \"Hello\"", - }, - to_parameters("title == %{value}", - :value => :Hello)) + sub_test_case("Symbol") do + def test_valid_id + assert_equal({ + :filter => "title == \"Hello\"", + }, + to_parameters("%{column} == %{value}", + :column => :title, + :value => "Hello")) + end + + def test_invalid_id + assert_equal({ + :filter => "title == \"Hello World\"", + }, + to_parameters("title == %{value}", + :value => :"Hello World")) + end end def test_number Modified: test/request/test-select.rb (+3 -1) =================================================================== --- test/request/test-select.rb 2017-04-26 15:19:43 +0900 (c6b9f40) +++ test/request/test-select.rb 2017-04-27 09:24:00 +0900 (b9bf744) @@ -69,7 +69,9 @@ class TestRequestSelect < Test::Unit::TestCase :table => "posts", :filter => "title == \"Hello\"", }, - filter("title == %{title}", :title => :Hello)) + filter("%{column} == %{value}", + :column => :title, + :value => "Hello")) end test("Array") do -------------- next part -------------- HTML����������������������������...Download