[Groonga-commit] groonga/gcs [master] BqTranslator: support (and ...)

Back to archive index

null+****@clear***** null+****@clear*****
2012年 7月 27日 (金) 17:05:10 JST


Kouhei Sutou	2012-07-27 17:05:10 +0900 (Fri, 27 Jul 2012)

  New Revision: d1339461a666a176490493cd93ac7fac0a334bed
  https://github.com/groonga/gcs/commit/d1339461a666a176490493cd93ac7fac0a334bed

  Log:
    BqTranslator: support (and ...)

  Modified files:
    lib/bq-translator.js
    test/bq-translator.test.js

  Modified: lib/bq-translator.js (+55 -0)
===================================================================
--- lib/bq-translator.js    2012-07-27 16:27:36 +0900 (b9a7ab5)
+++ lib/bq-translator.js    2012-07-27 17:05:10 +0900 (ce81495)
@@ -17,6 +17,61 @@ BooleanQueryTranslator.prototype = {
   translate: function(query) {
     return this.translateTinia(query);
   },
+  skipSpaces: function(query, context) {
+    for (; context.offset < query.length; context.offset++) {
+      if (query[context.offset] != " ") {
+        return;
+      }
+    }
+  },
+  translateGroup: function(query, context) {
+    if (query[context.offset] != "(") {
+      // TODO: report error
+      return "";
+    }
+
+    context.offset++;
+    var operator = "";
+    for (; context.offset < query.length; context.offset++) {
+      var character = query[context.offset];
+      if (/^[a-z]$/.test(character)) {
+        operator += character;
+      } else if (character == " ") {
+        this.skipSpaces(query, context);
+        var expression = this.translateGroupAnd(query, context);
+        this.skipSpaces(query, context);
+        if (query[context.offset] != ")") {
+          // TODO: report error: have garbage
+          return "";
+        }
+        context.offset++;
+        return expression;
+      } else if (character == ")") {
+        // TODO: report error: no arguments for operator
+        return "";
+      } else {
+        // TODO: invalid operator
+        return "";
+      }
+    }
+
+    // TODO: report error: missing close paren <)>
+    return "";
+  },
+  translateGroupAnd: function(query, context) {
+    var expressions = [];
+    while (context.offset < query.length) {
+      this.skipSpaces(query, context);
+      if (query[context.offset] == ")") {
+        return expressions.join(" && ");
+      } else {
+        expressions.push(this.translateExpression(query, context));
+      }
+    }
+
+    // TODO: report error: missing close paren <)>
+    return "";
+  },
   translateExpression: function(query, context) {
     var startOffset = context.offset;
     var field;

  Modified: test/bq-translator.test.js (+25 -0)
===================================================================
--- test/bq-translator.test.js    2012-07-27 16:27:36 +0900 (e8037d7)
+++ test/bq-translator.test.js    2012-07-27 17:05:10 +0900 (f333a63)
@@ -12,6 +12,26 @@ function testQuery(label, expected, query) {
   });
 }
 
+function testGroup(label, expectedScriptGrnExpr, expectedOffset, group) {
+  test('gorup: ' + label + ': ' +
+       '<' + group + '> -> <' + expectedScriptGrnExpr + '>', function() {
+    var translator = new BooleanQueryTranslator();
+    var context = {
+      defaultField: "field",
+      offset: 0
+    };
+    var actualScriptGrnExpr = translator.translateGroup(group, context);
+    assert.deepEqual({
+                       scriptGrnExpr: expectedScriptGrnExpr,
+                       offset: expectedOffset
+                     },
+                     {
+                       scriptGrnExpr: actualScriptGrnExpr,
+                       offset: context.offset
+                     });
+  });
+}
+
 function testExpression(label, expectedScriptGrnExpr, expectedOffset,
                         expression) {
   test('expression: ' + label + ': ' +
@@ -45,6 +65,11 @@ suite('BoolanQueryTranslator', function() {
             '"query query" type:"ModelName"',
             "(and 'query query' type:'ModelName')");
 
+  testGroup("and",
+            "field1 @ \"keyword1\" && field2 @ \"keyword2\"",
+            "(and field1:'keyword1' field2:'keyword2')".length,
+            "(and field1:'keyword1' field2:'keyword2') (other group)");
+
   testExpression("value only: stirng: keywords",
                  "field @ \"keyword1\" && field @ \"keyword2\"",
                  "'keyword1 keyword2'".length,
-------------- next part --------------
HTML$B$NE:IU%U%!%$%k$rJ]4I$7$^$7$?(B...
Download 



Groonga-commit メーリングリストの案内
Back to archive index