Advertisement
Asmor

TaffyDB Inner Join Extension

Jul 7th, 2012
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <html>
  2. <head>
  3. <script src="https://github.com/typicaljoe/taffydb/raw/master/taffy.js"></script>
  4. <script>
  5. (function () {
  6.     innerJoinFunction = (function () {
  7.         function _mode(rightTable, arg1, arg2) {
  8.             if (typeof rightTable.filter !== "function") {
  9.                 if (rightTable.TAFFY) {
  10.                     rightTable = rightTable(); //"Normalize" rightTable so we know we can call filter on it
  11.                 } else {
  12.                     console.log(rightTable);
  13.                     throw "TAFFY DB or result not supplied";
  14.                 }
  15.             }
  16.             if (typeof arg1 === "function") {
  17.                 return _functionMode.call(this, rightTable, arg1);
  18.             }
  19.             if (typeof arg1 === "object" && arg1.length) {
  20.                 return _arrayMode.apply(this, arguments);
  21.             }
  22.             if (typeof arg1 === "string" && typeof arg2 === "string") {
  23.                 return _equiJoinMode.call(this, rightTable, arg1, arg2);
  24.             }
  25.             throw "Not sure how to join.";
  26.         }
  27.  
  28.         function _combine(l, r) {
  29.             var out = {},
  30.                 i,
  31.                 prefix;
  32.             for (i in l) {
  33.                 if (l.hasOwnProperty(i)) {
  34.                     out[i] = l[i];
  35.                 }
  36.             }
  37.             for (i in r) {
  38.                 if (r.hasOwnProperty(i)) {
  39.                     prefix = "";
  40.                     if (out[i]) {
  41.                         prefix = "right_";
  42.                     }
  43.                     out[prefix + i] = r[i];
  44.                 }
  45.             }
  46.             return out;
  47.         }
  48.  
  49.         function _functionMode(rightTable, comparison) {
  50.             this.context({
  51.                 results: this.getDBI().query(this.context())
  52.             });
  53.             var toReturn = [];
  54.             TAFFY.each(this.context().results, function (l) {
  55.                 rightTable.each(function (r) {
  56.                     if (comparison(l, r)) {
  57.                         toReturn.push(_combine(l, r));
  58.                     }
  59.                 });
  60.             });
  61.             return TAFFY(toReturn)();
  62.         }
  63.  
  64.         function _arrayMode(rightTable) {
  65.             var args = arguments;
  66.             function _compareFunction(l, r) {
  67.                 var i,
  68.                     passed = true,
  69.                     leftCol,
  70.                     rightCol,
  71.                     operator;
  72.                 //Note: Start i at 1 since the first argument is rightTable!
  73.                 for (i = 1; i < args.length; i += 1) {
  74.                     leftCol = l[args[i][0]];
  75.                     operator = args[i][1];
  76.                     rightCol = r[args[i][2]];
  77.                     switch (operator) {
  78.                     case "==":
  79.                         passed = passed && (leftCol == rightCol);
  80.                         break;
  81.                     case "===":
  82.                         passed = passed && (leftCol === rightCol);
  83.                         break;
  84.                     case "!=":
  85.                         passed = passed && (leftCol != rightCol);
  86.                         break;
  87.                     case "!==":
  88.                         passed = passed && (leftCol !== rightCol);
  89.                         break;
  90.                     case "<":
  91.                         passed = passed && (leftCol < rightCol);
  92.                         break;
  93.                     case ">":
  94.                         passed = passed && (leftCol > rightCol);
  95.                         break;
  96.                     case "<=":
  97.                         passed = passed && (leftCol <= rightCol);
  98.                         break;
  99.                     case ">=":
  100.                         passed = passed && (leftCol >= rightCol);
  101.                         break;
  102.                     default:
  103.                         throw "Unrecognized operator: " + operator;
  104.                     }
  105.                 }
  106.                 return passed;
  107.             }
  108.             return _functionMode.call(this, rightTable, _compareFunction);
  109.         }
  110.  
  111.         function _equiJoinMode(rightTable, leftCol, rightCol) {
  112.             return _functionMode.call(this, rightTable, function (l, r) {
  113.                 return l[leftCol] === r[rightCol];
  114.             });
  115.         }
  116.  
  117.         return _mode;
  118.     }());
  119.  
  120.     TAFFY.extend("innerJoin", innerJoinFunction);
  121. }());
  122.  
  123. var cities = TAFFY([
  124.     {name:"New York",state:"NY"},
  125.     {name:"Las Vegas",state:"NV"},
  126.     {name:"Boston",state:"MA"}
  127. ]);
  128.  
  129. var states = TAFFY([
  130.     {name: "New York", abbreviation: "NY"},
  131.     {name: "Nevada", abbreviation: "NV"},
  132.     {name: "Massachusetts", abbreviation: "MA"}
  133. ]);
  134.  
  135.  
  136. console.log("Equijoin:");
  137. cities().innerJoin(states, "state", "abbreviation").each(function (r) { console.log(r)});
  138. console.log("Equijoin w/pre-filtered DB:");
  139. cities().innerJoin(states({abbreviation: "NV"}), "state", "abbreviation").each(function (r) { console.log(r)});
  140. console.log("Array Join, 'Not Equal':");
  141. cities().innerJoin(states, ["state", "!==", "abbreviation"]).each(function (r) { console.log(r)});
  142. console.log("Function join, state name contains abbreviation:");
  143. cities().innerJoin(states, function (l, r) {
  144.     return (r.name.toLowerCase().indexOf(l.state.toLowerCase()) >= 0);
  145. }).each(function (r) { console.log(r)});
  146.  
  147. </script>
  148. </head>
  149. <body>
  150. </body>
  151.    
  152. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement