[Groonga-commit] ranguba/rroonga at 7bf7527 [master] Add :no_syntax_error to Groonga::Expression#parse

Back to archive index

Kouhei Sutou null+****@clear*****
Sun Jan 28 23:47:36 JST 2018


Kouhei Sutou	2018-01-28 23:47:36 +0900 (Sun, 28 Jan 2018)

  New Revision: 7bf7527677ad2f40f78e2abcd4970ba4b2923cf1
  https://github.com/ranguba/rroonga/commit/7bf7527677ad2f40f78e2abcd4970ba4b2923cf1

  Message:
    Add :no_syntax_error to Groonga::Expression#parse

  Modified files:
    ext/groonga/rb-grn-expression.c
    test/test-command-select.rb

  Modified: ext/groonga/rb-grn-expression.c (+23 -1)
===================================================================
--- ext/groonga/rb-grn-expression.c    2018-01-28 23:44:12 +0900 (c36d4c15)
+++ ext/groonga/rb-grn-expression.c    2018-01-28 23:47:36 +0900 (83dd7cc9)
@@ -1,6 +1,6 @@
 /* -*- coding: utf-8; mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
 /*
-  Copyright (C) 2009-2015  Kouhei Sutou <kou �� clear-code.com>
+  Copyright (C) 2009-2018  Kouhei Sutou <kou �� clear-code.com>
   Copyright (C) 2014  Masafumi Yokoyama <myokoym �� gmail.com>
 
   This library is free software; you can redistribute it and/or
@@ -424,6 +424,18 @@ rb_grn_expression_append_operation (VALUE self, VALUE rb_operation,
  *     デフォルトでは更新操作を利用できる。
  *
  *     参考: "Groongaのクエリ構文のドキュメント":http://groonga.org/ja/docs/reference/grn_expr/query_syntax.html
+ *   @option options :no_syntax_error
+ *     Specifies whether preventing syntax error in query syntax.
+ *
+ *     If it's `true`, special characters are treated as normal
+ *     characters on syntax error.
+ *
+ *     If it's `false`, syntax error is reported on syntax error.
+ *     It's the default.
+ *
+ *     You can't use this option in script syntax.
+ *
+ *     @see [Query syntax document in Groonga](http://groonga.org/docs/reference/grn_expr/query_syntax.html)
  */
 static VALUE
 rb_grn_expression_parse (int argc, VALUE *argv, VALUE self)
@@ -440,6 +452,7 @@ rb_grn_expression_parse (int argc, VALUE *argv, VALUE self)
     VALUE options, rb_query, rb_default_column, rb_default_operator;
     VALUE rb_default_mode, rb_syntax;
     VALUE rb_allow_pragma, rb_allow_column, rb_allow_update, rb_allow_leading_not;
+    VALUE rb_no_syntax_error;
     VALUE exception = Qnil;
 
     rb_scan_args(argc, argv, "11", &rb_query, &options);
@@ -452,6 +465,7 @@ rb_grn_expression_parse (int argc, VALUE *argv, VALUE self)
                         "allow_column", &rb_allow_column,
                         "allow_update", &rb_allow_update,
                         "allow_leading_not", &rb_allow_leading_not,
+                        "no_syntax_error", &rb_no_syntax_error,
                         NULL);
 
     query = StringValuePtr(rb_query);
@@ -521,6 +535,14 @@ rb_grn_expression_parse (int argc, VALUE *argv, VALUE self)
             flags |= GRN_EXPR_ALLOW_LEADING_NOT;
     }
 
+    if (!NIL_P(rb_no_syntax_error)) {
+        if ((flags & GRN_EXPR_SYNTAX_SCRIPT))
+            rb_raise(rb_eArgError,
+                     ":no_syntax_error isn't allowed in script syntax");
+        if (RVAL2CBOOL(rb_no_syntax_error))
+            flags |= GRN_EXPR_QUERY_NO_SYNTAX_ERROR;
+    }
+
     rc = grn_expr_parse(context, expression, query, query_size,
                         default_column, default_mode, default_operator,
                         flags);

  Modified: test/test-command-select.rb (+36 -1)
===================================================================
--- test/test-command-select.rb    2018-01-28 23:44:12 +0900 (2b299307)
+++ test/test-command-select.rb    2018-01-28 23:47:36 +0900 (ff6dd8da)
@@ -1,4 +1,4 @@
-# Copyright (C) 2010-2012  Kouhei Sutou <kou �� clear-code.com>
+# Copyright (C) 2010-2018  Kouhei Sutou <kou �� clear-code.com>
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -190,6 +190,41 @@ class CommandSelectTest < Test::Unit::TestCase
         end
       end
     end
+
+    class TestNoSyntaxError < self
+      def setup_data
+        @paren =
+          @entries.add("Have paren",
+                       "content" => "(hello)")
+        @no_paren =
+          @entries.add("Not have paren",
+                       "content" => "hello")
+      end
+
+      def test_true
+        result =****@entri***** do |record|
+          record[:content].match("(", :no_syntax_error => true)
+        end
+        assert_equal_select_result([@paren],
+                                   result)
+      end
+
+      def test_false
+        assert_raise(Groonga::SyntaxError) do
+          @entries.select do |record|
+            record[:content].match("(", :no_syntax_error => false)
+          end
+        end
+      end
+
+      def test_default
+        assert_raise(Groonga::SyntaxError) do
+          @entries.select do |record|
+            record[:content].match("(")
+          end
+        end
+      end
+    end
   end
 
   private
-------------- next part --------------
HTML����������������������������...
URL: https://lists.osdn.me/mailman/archives/groonga-commit/attachments/20180128/b0bf7b59/attachment-0001.htm 



More information about the Groonga-commit mailing list
Back to archive index