[Groonga-commit] groonga/gcs [master] BqTranslator: use script syntax

Back to archive index

null+****@clear***** null+****@clear*****
2012年 7月 27日 (金) 15:56:04 JST


Kouhei Sutou	2012-07-27 15:56:04 +0900 (Fri, 27 Jul 2012)

  New Revision: 5a0c747b4b8eb718f6e51075225276529ab7191f
  https://github.com/groonga/gcs/commit/5a0c747b4b8eb718f6e51075225276529ab7191f

  Log:
    BqTranslator: use script syntax

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

  Modified: lib/bq-translator.js (+17 -8)
===================================================================
--- lib/bq-translator.js    2012-07-27 15:15:22 +0900 (851146b)
+++ lib/bq-translator.js    2012-07-27 15:56:04 +0900 (3a6cfcd)
@@ -19,20 +19,24 @@ BooleanQueryTranslator.prototype = {
   },
   translateExpression: function(query, context) {
     if (query[context.offset] == "'") {
-      return this.translateExpressionValueString(query, context);
+      return this.translateExpressionValueString(query,
+                                                 context.defaultField,
+                                                 context);
     }
     if (/^[0-9]/.test(query[context.offset])) {
-      return this.translateExpressionValueUnsignedInteger(query, context);
+      return this.translateExpressionValueUnsignedInteger(query,
+                                                          context.defaultField,
+                                                          context);
     }
     return "";
   },
-  translateExpressionValueString: function(query, context) {
+  translateExpressionValueString: function(query, field, context) {
     if (query[context.offset] != "'") {
       // TODO: report error
       return "";
     }
 
-    var value = "";
+    var value = "\"";
     context.offset++;
     for (; context.offset < query.length; context.offset++) {
       var character = query[context.offset];
@@ -44,12 +48,17 @@ BooleanQueryTranslator.prototype = {
       if (character == "\\") {
         context.offset++;
         character = query[context.offset];
+        value += character;
+      } else if (character == "\"") {
+        value += "\\\"";
+      } else {
+        value += character;
       }
-      value += character;
     }
-    return value;
+    value += "\"";
+    return field + " @ " + value;
   },
-  translateExpressionValueUnsignedInteger: function(query, context) {
+  translateExpressionValueUnsignedInteger: function(query, field, context) {
     var value = "";
     for (; context.offset < query.length; context.offset++) {
       var character = query[context.offset];
@@ -58,7 +67,7 @@ BooleanQueryTranslator.prototype = {
       }
       value += character;
     }
-    return value;
+    return field + " == " + value;
   },
   translateTinia: function(query) {
     var matched = null;

  Modified: test/bq-translator.test.js (+9 -7)
===================================================================
--- test/bq-translator.test.js    2012-07-27 15:15:22 +0900 (9df5032)
+++ test/bq-translator.test.js    2012-07-27 15:56:04 +0900 (61034f6)
@@ -12,21 +12,23 @@ function testQuery(label, expected, query) {
   });
 }
 
-function testExpression(label, expectedQueryGrnExpr, expectedOffset,
+function testExpression(label, expectedScriptGrnExpr, expectedOffset,
                         expression) {
   test('expression: ' + label + ': ' +
-       '<' + expression + '> -> <' + expectedQueryGrnExpr + '>', function() {
+       '<' + expression + '> -> <' + expectedScriptGrnExpr + '>', function() {
     var translator = new BooleanQueryTranslator();
     var context = {
+      defaultField: "field",
       offset: 0
     };
-    var actualQueryGrnExpr = translator.translateExpression(expression, context);
+    var actualScriptGrnExpr =
+          translator.translateExpression(expression, context);
     assert.deepEqual({
-                       queryGrnExpr: expectedQueryGrnExpr,
+                       scriptGrnExpr: expectedScriptGrnExpr,
                        offset: expectedOffset
                      },
                      {
-                       queryGrnExpr: actualQueryGrnExpr,
+                       scriptGrnExpr: actualScriptGrnExpr,
                        offset: context.offset
                      });
   });
@@ -44,11 +46,11 @@ suite('BoolanQueryTranslator', function() {
             "(and 'query query' type:'ModelName')");
 
   testExpression("value only: stirng",
-                 "keyword1 keyword2",
+                 "field @ \"keyword1 keyword2\"",
                  "'keyword1 keyword2'".length,
                  "'keyword1 keyword2' 'other keyword'");
   testExpression("value only: unsigned integer",
-                 "29",
+                 "field == 29",
                  "29".length,
                  "29 29");
 })
-------------- next part --------------
HTML$B$NE:IU%U%!%$%k$rJ]4I$7$^$7$?(B...
Download 



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