[Groonga-commit] groonga/gcs [master] Format results to a hash if "_key" column is used

Back to archive index

null+****@clear***** null+****@clear*****
2012年 8月 9日 (木) 13:52:23 JST


SHIMODA Hiroshi	2012-08-09 13:52:23 +0900 (Thu, 09 Aug 2012)

  New Revision: 335e0468c638c137eaedc8b653ce3f9e97b8dbea
  https://github.com/groonga/gcs/commit/335e0468c638c137eaedc8b653ce3f9e97b8dbea

  Log:
    Format results to a hash if "_key" column is used

  Modified files:
    lib/wrapped-nroonga.js

  Modified: lib/wrapped-nroonga.js (+27 -10)
===================================================================
--- lib/wrapped-nroonga.js    2012-08-09 13:44:03 +0900 (ef29e50)
+++ lib/wrapped-nroonga.js    2012-08-09 13:52:23 +0900 (cabc83c)
@@ -118,26 +118,43 @@ function formatResults(results) {
     return [];
 
   var columns = {};
+  var keyColumnIndex = -1;
   results[0].forEach(function(column, index) {
     columns[index] = column[0];
+    if (column[0] == '_key')
+      keyColumnIndex = index;
   });
 
-  var hashResults = [];
-  results.slice(1).forEach(function(result) {
-    var hashResult = {};
-    result.forEach(function(column, index) {
-      hashResult[columns[index]] = column;
+  var hashResults;
+  if (keyColumnIndex > -1) {
+    hashResults = {};
+    results.slice(1).forEach(function(result) {
+      var hashResult = {};
+      result.forEach(function(column, index) {
+        if (index == keyColumnIndex) return;
+        hashResult[columns[index]] = column;
+      });
+      hashResults[result[keyColumnIndex]] = hashResult;
     });
-    hashResults.push(hashResult);
-  });
-  return hashResults;
+    return hashResults;
+  } else {
+    hashResults = [];
+    results.slice(1).forEach(function(result) {
+      var hashResult = {};
+      result.forEach(function(column, index) {
+        hashResult[columns[index]] = column;
+      });
+      hashResults.push(hashResult);
+    });
+    return hashResults;
+  }
 }
 exports.formatResults = formatResults;
 
 function formatSelectResult(result) {
   return {
-    count: result[0][0],
-    results: formatResults(result.slice(1))
+    count: result[0][0][0],
+    results: formatResults(result[0].slice(1))
   };
 }
 exports.formatSelectResult = formatSelectResult;
-------------- next part --------------
HTML$B$NE:IU%U%!%$%k$rJ]4I$7$^$7$?(B...
Download 



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