genia.likes.science@gmail.com | b91b577a | 2013-06-13 14:36:06 -0700 | [diff] [blame] | 1 | function AsterixDBConnection(configuration) { |
| 2 | this._properties = {}; |
| 3 | this._properties["dataverse"] = ""; |
| 4 | this._properties["mode"] = "synchronous"; |
| 5 | |
| 6 | var configuration = arguments || {}; |
| 7 | |
| 8 | for (var key in configuration) { |
| 9 | this._properties[key] = configuration[key]; |
| 10 | } |
| 11 | |
| 12 | return this; |
| 13 | } |
| 14 | |
| 15 | |
| 16 | AsterixDBConnection.prototype.dataverse = function(dataverseName) { |
| 17 | this._properties["dataverse"] = dataverseName; |
| 18 | |
| 19 | return this; |
| 20 | }; |
| 21 | |
| 22 | |
| 23 | AsterixDBConnection.prototype.run = function(statements, successFn) { |
| 24 | |
| 25 | var success_fn = successFn; |
| 26 | |
| 27 | if ( typeof statements === 'string') { |
| 28 | statements = [ statements ]; |
| 29 | } |
| 30 | |
genia.likes.science@gmail.com | 78593d7 | 2013-07-02 06:33:39 -0700 | [diff] [blame^] | 31 | var query = "use dataverse " + this._properties["dataverse"] + ";\n" + statements.join("\n"); |
genia.likes.science@gmail.com | b91b577a | 2013-06-13 14:36:06 -0700 | [diff] [blame] | 32 | var mode = this._properties["mode"]; |
| 33 | |
| 34 | $.ajax({ |
| 35 | type : 'GET', |
| 36 | url : "http://localhost:19002/query", |
| 37 | data : { |
| 38 | "query" : query, |
| 39 | "mode" : mode |
| 40 | }, |
| 41 | dataType : "json", |
| 42 | success : function(data) { |
| 43 | success_fn(data); |
| 44 | }, |
| 45 | error: function(r) { |
genia.likes.science@gmail.com | f8cca19 | 2013-06-13 15:45:53 -0700 | [diff] [blame] | 46 | //alert("AsterixSDK ERROR\n" + JSON.stringify(r)); |
genia.likes.science@gmail.com | b91b577a | 2013-06-13 14:36:06 -0700 | [diff] [blame] | 47 | } |
| 48 | }); |
| 49 | |
| 50 | return this; |
| 51 | }; |
| 52 | |
| 53 | |
| 54 | // Asterix Expressions |
genia.likes.science@gmail.com | 90d0872 | 2013-05-28 04:40:12 -0700 | [diff] [blame] | 55 | function AExpression () { |
genia.likes.science@gmail.com | 512454d | 2013-05-28 03:32:20 -0700 | [diff] [blame] | 56 | this._properties = {}; |
| 57 | this._success = function() {}; |
| 58 | |
genia.likes.science@gmail.com | 512454d | 2013-05-28 03:32:20 -0700 | [diff] [blame] | 59 | return this; |
| 60 | } |
| 61 | |
genia.likes.science@gmail.com | 90d0872 | 2013-05-28 04:40:12 -0700 | [diff] [blame] | 62 | |
| 63 | AExpression.prototype.bind = function(options) { |
genia.likes.science@gmail.com | 512454d | 2013-05-28 03:32:20 -0700 | [diff] [blame] | 64 | var options = options || {}; |
| 65 | |
genia.likes.science@gmail.com | 512454d | 2013-05-28 03:32:20 -0700 | [diff] [blame] | 66 | if (options.hasOwnProperty("success")) { |
| 67 | this._success = options["success"]; |
| 68 | } |
genia.likes.science@gmail.com | 3861263 | 2013-05-28 13:11:52 -0700 | [diff] [blame] | 69 | |
| 70 | if (options.hasOwnProperty("return")) { |
| 71 | this._properties["return"] = " return " + options["return"].val(); |
| 72 | } |
genia.likes.science@gmail.com | 512454d | 2013-05-28 03:32:20 -0700 | [diff] [blame] | 73 | }; |
| 74 | |
genia.likes.science@gmail.com | 512454d | 2013-05-28 03:32:20 -0700 | [diff] [blame] | 75 | |
genia.likes.science@gmail.com | 18f3bf2 | 2013-06-12 07:45:02 -0700 | [diff] [blame] | 76 | AExpression.prototype.run = function(successFn) { |
| 77 | var success_fn = successFn; |
genia.likes.science@gmail.com | 5a7c2d7 | 2013-06-06 00:54:28 -0700 | [diff] [blame] | 78 | |
genia.likes.science@gmail.com | 512454d | 2013-05-28 03:32:20 -0700 | [diff] [blame] | 79 | $.ajax({ |
| 80 | type : 'GET', |
genia.likes.science@gmail.com | fc5f509 | 2013-06-12 00:49:46 -0700 | [diff] [blame] | 81 | url : "http://localhost:19002/query", |
genia.likes.science@gmail.com | 18f3bf2 | 2013-06-12 07:45:02 -0700 | [diff] [blame] | 82 | data : {"query" : "use dataverse TinySocial;\n" + this.val()}, |
genia.likes.science@gmail.com | 512454d | 2013-05-28 03:32:20 -0700 | [diff] [blame] | 83 | dataType : "json", |
| 84 | success : function(data) { |
| 85 | success_fn(data); |
genia.likes.science@gmail.com | 5f42534 | 2013-06-13 13:51:26 -0700 | [diff] [blame] | 86 | }, |
| 87 | error: function(r) { |
| 88 | //alert(JSON.stringify(r)); |
genia.likes.science@gmail.com | 1d82514 | 2013-06-06 08:01:31 -0700 | [diff] [blame] | 89 | } |
genia.likes.science@gmail.com | 1d82514 | 2013-06-06 08:01:31 -0700 | [diff] [blame] | 90 | }); |
| 91 | |
| 92 | /*$.ajax({ |
| 93 | "type" : 'GET', |
| 94 | "url" : endpoint, |
| 95 | "data" : payload, |
| 96 | "dataType" : "json", |
| 97 | "success" : function(response) { |
| 98 | |
| 99 | alert("DEBUG: Run Response: " + JSON.stringify(response)); |
| 100 | |
genia.likes.science@gmail.com | 5a7c2d7 | 2013-06-06 00:54:28 -0700 | [diff] [blame] | 101 | if (response && response["error-code"]) { |
| 102 | |
| 103 | alert("Error [Code" + response["error-code"][0] + "]: " + response["error-code"][1]); |
| 104 | |
| 105 | } else if (response && response["results"]) { |
| 106 | |
| 107 | var fn_callback = myThis._callbacks["sync"]; |
| 108 | fn_callback(response, myThis._extras); |
| 109 | |
| 110 | } else if (response["handle"]) { |
| 111 | |
| 112 | var fn_callback = myThis._callbacks["async"]; |
| 113 | fn_callback(response, myThis._extras); |
| 114 | |
| 115 | } else if (response["status"]) { |
| 116 | |
| 117 | var fn_callback = myThis._callbacks["sync"]; |
| 118 | fn_callback(response, myThis._extras); |
| 119 | } |
genia.likes.science@gmail.com | 1d82514 | 2013-06-06 08:01:31 -0700 | [diff] [blame] | 120 | }, |
| 121 | "error": function (xhr, ajaxOptions, thrownError) { |
| 122 | alert("AJAX ERROR " + thrownError); |
genia.likes.science@gmail.com | 512454d | 2013-05-28 03:32:20 -0700 | [diff] [blame] | 123 | } |
genia.likes.science@gmail.com | 1d82514 | 2013-06-06 08:01:31 -0700 | [diff] [blame] | 124 | });*/ |
genia.likes.science@gmail.com | 512454d | 2013-05-28 03:32:20 -0700 | [diff] [blame] | 125 | |
| 126 | return this; |
genia.likes.science@gmail.com | 90d0872 | 2013-05-28 04:40:12 -0700 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | |
| 130 | AExpression.prototype.val = function() { |
| 131 | |
genia.likes.science@gmail.com | 2ba3224 | 2013-05-31 03:10:48 -0700 | [diff] [blame] | 132 | var value = ""; |
| 133 | |
genia.likes.science@gmail.com | 90d0872 | 2013-05-28 04:40:12 -0700 | [diff] [blame] | 134 | // If there is a dataverse defined, provide it. |
| 135 | if (this._properties.hasOwnProperty("dataverse")) { |
genia.likes.science@gmail.com | 2ba3224 | 2013-05-31 03:10:48 -0700 | [diff] [blame] | 136 | value += "use dataverse " + this._properties["dataverse"] + ";\n"; |
| 137 | }; |
| 138 | |
| 139 | if (this._properties.hasOwnProperty("value")) { |
| 140 | value += this._properties["value"]; |
genia.likes.science@gmail.com | 90d0872 | 2013-05-28 04:40:12 -0700 | [diff] [blame] | 141 | } |
genia.likes.science@gmail.com | 2ba3224 | 2013-05-31 03:10:48 -0700 | [diff] [blame] | 142 | |
| 143 | return value; |
genia.likes.science@gmail.com | 3861263 | 2013-05-28 13:11:52 -0700 | [diff] [blame] | 144 | }; |
| 145 | |
genia.likes.science@gmail.com | 2ba3224 | 2013-05-31 03:10:48 -0700 | [diff] [blame] | 146 | // @param expressionValue [String] |
| 147 | AExpression.prototype.set = function(expressionValue) { |
| 148 | this._properties["value"] = expressionValue; |
| 149 | return this; |
genia.likes.science@gmail.com | 3861263 | 2013-05-28 13:11:52 -0700 | [diff] [blame] | 150 | }; |
| 151 | |
genia.likes.science@gmail.com | 73dcc70 | 2013-05-28 04:21:28 -0700 | [diff] [blame] | 152 | |
genia.likes.science@gmail.com | 90d0872 | 2013-05-28 04:40:12 -0700 | [diff] [blame] | 153 | // FunctionExpression |
| 154 | // Parent: AsterixExpression |
| 155 | // |
| 156 | // @param options [Various], |
| 157 | // @key function [String], a function to be applid to the expression |
genia.likes.science@gmail.com | 18f3bf2 | 2013-06-12 07:45:02 -0700 | [diff] [blame] | 158 | // @key expression [AsterixExpression or AQLClause] an AsterixExpression/Clause to which the fn will be applied |
| 159 | function FunctionExpression() { |
genia.likes.science@gmail.com | 90d0872 | 2013-05-28 04:40:12 -0700 | [diff] [blame] | 160 | |
| 161 | // Initialize superclass |
| 162 | AExpression.call(this); |
genia.likes.science@gmail.com | 18f3bf2 | 2013-06-12 07:45:02 -0700 | [diff] [blame] | 163 | |
| 164 | this._properties["function"] = ""; |
| 165 | this._properties["expression"] = new AExpression().set(""); |
genia.likes.science@gmail.com | 90d0872 | 2013-05-28 04:40:12 -0700 | [diff] [blame] | 166 | |
genia.likes.science@gmail.com | 18f3bf2 | 2013-06-12 07:45:02 -0700 | [diff] [blame] | 167 | // Check for fn/expression input |
| 168 | if (arguments.length == 2 && typeof arguments[0] == "string" && |
| 169 | (arguments[1] instanceof AExpression || arguments[1] instanceof AQLClause)) { |
| 170 | |
| 171 | this._properties["function"] = arguments[0]; |
| 172 | this._properties["expression"] = arguments[1]; |
| 173 | |
| 174 | } |
genia.likes.science@gmail.com | 90d0872 | 2013-05-28 04:40:12 -0700 | [diff] [blame] | 175 | |
| 176 | // Return object |
| 177 | return this; |
| 178 | } |
| 179 | |
| 180 | |
| 181 | FunctionExpression.prototype = Object.create(AExpression.prototype); |
| 182 | FunctionExpression.prototype.constructor = FunctionExpression; |
| 183 | |
| 184 | |
genia.likes.science@gmail.com | 18f3bf2 | 2013-06-12 07:45:02 -0700 | [diff] [blame] | 185 | FunctionExpression.prototype.fn = function(fnName) { |
genia.likes.science@gmail.com | 90d0872 | 2013-05-28 04:40:12 -0700 | [diff] [blame] | 186 | |
genia.likes.science@gmail.com | 18f3bf2 | 2013-06-12 07:45:02 -0700 | [diff] [blame] | 187 | if (typeof fnName == "string") { |
| 188 | this._properties["function"] = fnName; |
| 189 | } |
genia.likes.science@gmail.com | 90d0872 | 2013-05-28 04:40:12 -0700 | [diff] [blame] | 190 | |
genia.likes.science@gmail.com | 90d0872 | 2013-05-28 04:40:12 -0700 | [diff] [blame] | 191 | return this; |
| 192 | }; |
| 193 | |
genia.likes.science@gmail.com | 18f3bf2 | 2013-06-12 07:45:02 -0700 | [diff] [blame] | 194 | |
| 195 | FunctionExpression.prototype.expression = function(expression) { |
| 196 | if (expression instanceof AExpression || expression instanceof AQLClause) { |
| 197 | this._properties["expression"] = expression; |
| 198 | } |
| 199 | |
| 200 | return this; |
| 201 | }; |
| 202 | |
| 203 | |
genia.likes.science@gmail.com | 90d0872 | 2013-05-28 04:40:12 -0700 | [diff] [blame] | 204 | FunctionExpression.prototype.val = function () { |
genia.likes.science@gmail.com | 2ba3224 | 2013-05-31 03:10:48 -0700 | [diff] [blame] | 205 | return this._properties["function"] + "(" + this._properties["expression"].val() + ")"; |
genia.likes.science@gmail.com | 90d0872 | 2013-05-28 04:40:12 -0700 | [diff] [blame] | 206 | }; |
| 207 | |
genia.likes.science@gmail.com | 73dcc70 | 2013-05-28 04:21:28 -0700 | [diff] [blame] | 208 | |
genia.likes.science@gmail.com | 3861263 | 2013-05-28 13:11:52 -0700 | [diff] [blame] | 209 | // FLWOGRExpression |
genia.likes.science@gmail.com | 90d0872 | 2013-05-28 04:40:12 -0700 | [diff] [blame] | 210 | // |
| 211 | // FLWOGRExpression ::= ( ForClause | LetClause ) ( Clause )* "return" Expression |
genia.likes.science@gmail.com | 3861263 | 2013-05-28 13:11:52 -0700 | [diff] [blame] | 212 | function FLWOGRExpression (options) { |
| 213 | // Initialize superclass |
| 214 | AExpression.call(this); |
genia.likes.science@gmail.com | 90d0872 | 2013-05-28 04:40:12 -0700 | [diff] [blame] | 215 | |
genia.likes.science@gmail.com | 3861263 | 2013-05-28 13:11:52 -0700 | [diff] [blame] | 216 | this._properties["clauses"] = []; |
genia.likes.science@gmail.com | 79e603a | 2013-05-31 10:03:23 -0700 | [diff] [blame] | 217 | this._properties["minSize"] = 0; |
genia.likes.science@gmail.com | 3861263 | 2013-05-28 13:11:52 -0700 | [diff] [blame] | 218 | |
| 219 | // Bind options and return |
| 220 | this.bind(options); |
| 221 | return this; |
genia.likes.science@gmail.com | 90d0872 | 2013-05-28 04:40:12 -0700 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | |
genia.likes.science@gmail.com | 3861263 | 2013-05-28 13:11:52 -0700 | [diff] [blame] | 225 | FLWOGRExpression.prototype = Object.create(AExpression.prototype); |
| 226 | FLWOGRExpression.prototype.constructor = FLWOGRExpression; |
| 227 | |
| 228 | |
| 229 | FLWOGRExpression.prototype.bind = function(options) { |
| 230 | AExpression.prototype.bind.call(this, options); |
| 231 | |
| 232 | var options = options || {}; |
| 233 | |
genia.likes.science@gmail.com | 79e603a | 2013-05-31 10:03:23 -0700 | [diff] [blame] | 234 | if (options instanceof SetStatement) { |
| 235 | this._properties["clauses"].push(options); |
| 236 | this._properties["minSize"] += 1; |
| 237 | } |
| 238 | |
| 239 | if (this._properties["clauses"].length <= this._properties["minSize"]) { |
genia.likes.science@gmail.com | 3861263 | 2013-05-28 13:11:52 -0700 | [diff] [blame] | 240 | // Needs to start with for or let clause |
| 241 | if (options instanceof ForClause || options instanceof LetClause) { |
| 242 | this._properties["clauses"].push(options); |
| 243 | } |
| 244 | } else { |
| 245 | if (options instanceof AQLClause) { |
| 246 | this._properties["clauses"].push(options); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | return this; |
| 251 | }; |
| 252 | |
| 253 | |
| 254 | FLWOGRExpression.prototype.val = function() { |
| 255 | var value = AExpression.prototype.val.call(this); |
| 256 | |
genia.likes.science@gmail.com | 2ba3224 | 2013-05-31 03:10:48 -0700 | [diff] [blame] | 257 | var clauseValues = []; |
genia.likes.science@gmail.com | 3861263 | 2013-05-28 13:11:52 -0700 | [diff] [blame] | 258 | for (var c in this._properties["clauses"]) { |
genia.likes.science@gmail.com | 2ba3224 | 2013-05-31 03:10:48 -0700 | [diff] [blame] | 259 | clauseValues.push(this._properties["clauses"][c].val()); |
genia.likes.science@gmail.com | 3861263 | 2013-05-28 13:11:52 -0700 | [diff] [blame] | 260 | } |
| 261 | |
genia.likes.science@gmail.com | 79e603a | 2013-05-31 10:03:23 -0700 | [diff] [blame] | 262 | return value + clauseValues.join("\n");// + ";"; |
genia.likes.science@gmail.com | 3861263 | 2013-05-28 13:11:52 -0700 | [diff] [blame] | 263 | }; |
| 264 | |
genia.likes.science@gmail.com | 2ba3224 | 2013-05-31 03:10:48 -0700 | [diff] [blame] | 265 | |
genia.likes.science@gmail.com | 5f42534 | 2013-06-13 13:51:26 -0700 | [diff] [blame] | 266 | FLWOGRExpression.prototype.ReturnClause = function(expression) { |
| 267 | return this.bind(new ReturnClause(expression)); |
| 268 | }; |
| 269 | |
genia.likes.science@gmail.com | 73dcc70 | 2013-05-28 04:21:28 -0700 | [diff] [blame] | 270 | // AQLClause |
| 271 | // |
| 272 | // Base Clause ::= ForClause | LetClause | WhereClause | OrderbyClause | GroupClause | LimitClause | DistinctClause |
| 273 | function AQLClause() { |
| 274 | this._properties = {}; |
| 275 | this._properties["clause"] = ""; |
| 276 | } |
| 277 | |
| 278 | AQLClause.prototype.val = function() { |
| 279 | var value = this._properties["clause"]; |
genia.likes.science@gmail.com | 73dcc70 | 2013-05-28 04:21:28 -0700 | [diff] [blame] | 280 | |
| 281 | return value; |
| 282 | }; |
| 283 | |
| 284 | AQLClause.prototype.bind = function(options) { |
genia.likes.science@gmail.com | 73dcc70 | 2013-05-28 04:21:28 -0700 | [diff] [blame] | 285 | |
genia.likes.science@gmail.com | 18f3bf2 | 2013-06-12 07:45:02 -0700 | [diff] [blame] | 286 | if (options instanceof AQLClause) { |
| 287 | this._properties["clause"] += " " + options.val(); |
genia.likes.science@gmail.com | 73dcc70 | 2013-05-28 04:21:28 -0700 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | return this; |
| 291 | }; |
| 292 | |
genia.likes.science@gmail.com | 3957830 | 2013-05-31 04:42:26 -0700 | [diff] [blame] | 293 | AQLClause.prototype.set = function(value) { |
| 294 | this._properties["clause"] = value; |
| 295 | return this; |
| 296 | }; |
| 297 | |
genia.likes.science@gmail.com | 73dcc70 | 2013-05-28 04:21:28 -0700 | [diff] [blame] | 298 | |
| 299 | // ForClause |
| 300 | // |
| 301 | // Grammar: |
| 302 | // "for" Variable ( "at" Variable )? "in" ( Expression ) |
| 303 | // |
| 304 | // @param for_variable [String], REQUIRED, first variable in clause |
| 305 | // @param at_variable [String], NOT REQUIRED, first variable in clause |
| 306 | // @param expression [AsterixExpression], REQUIRED, expression to evaluate |
genia.likes.science@gmail.com | 73dcc70 | 2013-05-28 04:21:28 -0700 | [diff] [blame] | 307 | function ForClause(for_variable, at_variable, expression) { |
| 308 | AQLClause.call(this); |
| 309 | |
genia.likes.science@gmail.com | 78593d7 | 2013-07-02 06:33:39 -0700 | [diff] [blame^] | 310 | this._properties["clause"] = "for " + arguments[0]; |
genia.likes.science@gmail.com | f8cca19 | 2013-06-13 15:45:53 -0700 | [diff] [blame] | 311 | |
| 312 | if (arguments.length == 3) { |
genia.likes.science@gmail.com | 78593d7 | 2013-07-02 06:33:39 -0700 | [diff] [blame^] | 313 | this._properties["clause"] += " at " + arguments[1]; |
genia.likes.science@gmail.com | f8cca19 | 2013-06-13 15:45:53 -0700 | [diff] [blame] | 314 | this._properties["clause"] += " in " + arguments[2].val(); |
| 315 | } else if (arguments.length == 2) { |
| 316 | this._properties["clause"] += " in " + arguments[1].val(); |
genia.likes.science@gmail.com | 73dcc70 | 2013-05-28 04:21:28 -0700 | [diff] [blame] | 317 | } |
genia.likes.science@gmail.com | f8cca19 | 2013-06-13 15:45:53 -0700 | [diff] [blame] | 318 | |
genia.likes.science@gmail.com | 73dcc70 | 2013-05-28 04:21:28 -0700 | [diff] [blame] | 319 | return this; |
| 320 | } |
| 321 | |
| 322 | ForClause.prototype = Object.create(AQLClause.prototype); |
| 323 | ForClause.prototype.constructor = ForClause; |
genia.likes.science@gmail.com | 3861263 | 2013-05-28 13:11:52 -0700 | [diff] [blame] | 324 | |
| 325 | |
| 326 | // LetClause |
| 327 | // |
| 328 | // Grammar: |
| 329 | // LetClause ::= "let" Variable ":=" Expression |
| 330 | // |
| 331 | // @param let_variable [String] |
| 332 | // @param expression [AExpression] |
| 333 | // |
| 334 | // TODO Vigorous error checking |
| 335 | function LetClause(let_variable, expression) { |
| 336 | AQLClause.call(this); |
| 337 | |
genia.likes.science@gmail.com | 78593d7 | 2013-07-02 06:33:39 -0700 | [diff] [blame^] | 338 | this._properties["clause"] = "let " + let_variable + " := "; |
genia.likes.science@gmail.com | 3861263 | 2013-05-28 13:11:52 -0700 | [diff] [blame] | 339 | this._properties["clause"] += expression.val(); |
| 340 | |
| 341 | return this; |
| 342 | } |
| 343 | |
| 344 | LetClause.prototype = Object.create(AQLClause.prototype); |
| 345 | LetClause.prototype.constructor = LetClause; |
| 346 | |
| 347 | |
genia.likes.science@gmail.com | 5ca104c | 2013-05-29 05:39:26 -0700 | [diff] [blame] | 348 | // ReturnClause |
| 349 | // |
| 350 | // Grammar: |
| 351 | // return [AQLExpression] |
| 352 | function ReturnClause(expression) { |
| 353 | AQLClause.call(this); |
| 354 | |
| 355 | this._properties["clause"] = "return "; |
genia.likes.science@gmail.com | 5749ef9 | 2013-06-12 07:59:25 -0700 | [diff] [blame] | 356 | |
genia.likes.science@gmail.com | 79e603a | 2013-05-31 10:03:23 -0700 | [diff] [blame] | 357 | if (expression instanceof AExpression || expression instanceof AQLClause) { |
genia.likes.science@gmail.com | 5ca104c | 2013-05-29 05:39:26 -0700 | [diff] [blame] | 358 | this._properties["clause"] += expression.val(); |
genia.likes.science@gmail.com | 5749ef9 | 2013-06-12 07:59:25 -0700 | [diff] [blame] | 359 | |
| 360 | } else if ( typeof expression == "object" && Object.getPrototypeOf( expression ) === Object.prototype ) { |
| 361 | |
| 362 | // TODO Null object check |
genia.likes.science@gmail.com | 2ba3224 | 2013-05-31 03:10:48 -0700 | [diff] [blame] | 363 | |
| 364 | this._properties["clause"] += "{"; |
| 365 | var returnStatements = []; |
| 366 | for (returnValue in expression) { |
| 367 | |
| 368 | if (expression[returnValue] instanceof AExpression) { |
genia.likes.science@gmail.com | fba7cc8 | 2013-05-31 04:04:08 -0700 | [diff] [blame] | 369 | returnStatements.push('"' + returnValue + '" ' + " : " + expression[returnValue].val()); |
genia.likes.science@gmail.com | 2ba3224 | 2013-05-31 03:10:48 -0700 | [diff] [blame] | 370 | } else if (typeof expression[returnValue] == "string") { |
genia.likes.science@gmail.com | fba7cc8 | 2013-05-31 04:04:08 -0700 | [diff] [blame] | 371 | returnStatements.push('"' + returnValue + '" ' + " : " + expression[returnValue]); |
genia.likes.science@gmail.com | 2ba3224 | 2013-05-31 03:10:48 -0700 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | this._properties["clause"] += returnStatements.join(",\n"); |
genia.likes.science@gmail.com | f7929d8 | 2013-06-12 11:30:01 -0700 | [diff] [blame] | 375 | this._properties["clause"] += "\n}"; |
genia.likes.science@gmail.com | 2ba3224 | 2013-05-31 03:10:48 -0700 | [diff] [blame] | 376 | |
genia.likes.science@gmail.com | 5ca104c | 2013-05-29 05:39:26 -0700 | [diff] [blame] | 377 | } else { |
genia.likes.science@gmail.com | 5749ef9 | 2013-06-12 07:59:25 -0700 | [diff] [blame] | 378 | this._properties["clause"] += new AQLClause().set(expression).val(); |
genia.likes.science@gmail.com | 5ca104c | 2013-05-29 05:39:26 -0700 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | return this; |
| 382 | } |
| 383 | |
genia.likes.science@gmail.com | 5749ef9 | 2013-06-12 07:59:25 -0700 | [diff] [blame] | 384 | |
genia.likes.science@gmail.com | 5ca104c | 2013-05-29 05:39:26 -0700 | [diff] [blame] | 385 | ReturnClause.prototype = Object.create(AQLClause.prototype); |
| 386 | ReturnClause.prototype.constructor = ReturnClause; |
| 387 | |
genia.likes.science@gmail.com | 2ba3224 | 2013-05-31 03:10:48 -0700 | [diff] [blame] | 388 | |
genia.likes.science@gmail.com | 3861263 | 2013-05-28 13:11:52 -0700 | [diff] [blame] | 389 | // WhereClause |
| 390 | // |
| 391 | // Grammar: |
| 392 | // ::= "where" Expression |
| 393 | // |
| 394 | // @param expression [BooleanExpression], pushes this expression onto the stack |
genia.likes.science@gmail.com | 3861263 | 2013-05-28 13:11:52 -0700 | [diff] [blame] | 395 | function WhereClause(expression) { |
| 396 | AQLClause.call(this); |
| 397 | |
| 398 | this._properties["stack"] = []; |
| 399 | |
| 400 | this.bind(expression); |
| 401 | |
| 402 | return this; |
| 403 | } |
| 404 | |
| 405 | |
| 406 | WhereClause.prototype = Object.create(AQLClause.prototype); |
| 407 | WhereClause.prototype.constructor = WhereClause; |
| 408 | |
| 409 | |
| 410 | WhereClause.prototype.bind = function(expression) { |
genia.likes.science@gmail.com | 64ca88e | 2013-05-31 10:40:39 -0700 | [diff] [blame] | 411 | if (expression instanceof AExpression) { |
genia.likes.science@gmail.com | 3861263 | 2013-05-28 13:11:52 -0700 | [diff] [blame] | 412 | this._properties["stack"].push(expression); |
| 413 | } |
| 414 | }; |
| 415 | |
| 416 | |
| 417 | WhereClause.prototype.val = function() { |
| 418 | var value = "where "; |
| 419 | |
| 420 | var count = this._properties["stack"].length - 1; |
| 421 | while (count >= 0) { |
| 422 | value += this._properties["stack"][count].val() + " "; |
| 423 | count -= 1; |
| 424 | } |
| 425 | |
| 426 | return value; |
genia.likes.science@gmail.com | 64ca88e | 2013-05-31 10:40:39 -0700 | [diff] [blame] | 427 | }; |
genia.likes.science@gmail.com | 3861263 | 2013-05-28 13:11:52 -0700 | [diff] [blame] | 428 | |
| 429 | |
genia.likes.science@gmail.com | d2f753e | 2013-06-12 13:51:03 -0700 | [diff] [blame] | 430 | WhereClause.prototype.and = function() { |
| 431 | |
| 432 | var andClauses = []; |
| 433 | for (var expression in arguments) { |
| 434 | |
| 435 | if (arguments[expression] instanceof AExpression) { |
| 436 | andClauses.push(arguments[expression].val()); |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | if (andClauses.length > 0) { |
| 441 | this._properties["stack"].push(new AExpression().set(andClauses.join(" and "))); |
| 442 | } |
| 443 | |
| 444 | return this; |
| 445 | }; |
| 446 | |
| 447 | |
| 448 | WhereClause.prototype.or = function() { |
| 449 | var orClauses = []; |
| 450 | for (var expression in arguments) { |
| 451 | |
| 452 | if (arguments[expression] instanceof AExpression) { |
| 453 | orClauses.push(arguments[expression].val()); |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | if (andClauses.length > 0) { |
| 458 | this._properties["stack"].push(new AExpression().set(orClauses.join(" and "))); |
| 459 | } |
| 460 | |
| 461 | return this; |
| 462 | }; |
| 463 | |
genia.likes.science@gmail.com | 2ba3224 | 2013-05-31 03:10:48 -0700 | [diff] [blame] | 464 | // LimitClause |
| 465 | // Grammar: |
| 466 | // LimitClause ::= "limit" Expression ( "offset" Expression )? |
| 467 | // |
| 468 | // @param limitExpression [REQUIRED, AQLExpression] |
| 469 | // @param offsetExpression [OPTIONAL, AQLExpression] |
| 470 | function LimitClause(limitExpression, offsetExpression) { |
| 471 | |
| 472 | AQLClause.call(this); |
| 473 | |
| 474 | // limitExpression required |
| 475 | this._properties["clause"] = "limit " + limitExpression.val(); |
| 476 | |
| 477 | // Optional: Offset |
| 478 | var offset = typeof offsetExpression ? offsetExpression : null; |
| 479 | if (offset != null) { |
| 480 | this._properties["clause"] += " offset " + offsetExpression.val(); |
| 481 | } |
| 482 | |
| 483 | return this; |
| 484 | } |
| 485 | |
| 486 | LimitClause.prototype = Object.create(AQLClause.prototype); |
| 487 | LimitClause.prototype.constructor = LimitClause; |
| 488 | |
| 489 | |
| 490 | // OrderbyClause |
| 491 | // |
| 492 | // Grammar: |
| 493 | // OrderbyClause ::= "order" "by" Expression ( ( "asc" ) | ( "desc" ) )? ( "," Expression ( ( "asc" ) | ( "desc" ) )? )* |
| 494 | // |
| 495 | // @params AQLExpressions and asc/desc strings, in any quantity. At least one required. |
| 496 | function OrderbyClause() { |
| 497 | |
| 498 | AQLClause.call(this); |
| 499 | |
| 500 | // At least one argument expression is required, and first should be expression |
| 501 | if (arguments.length == 0 || !(arguments[0] instanceof AExpression)) { |
genia.likes.science@gmail.com | 18f3bf2 | 2013-06-12 07:45:02 -0700 | [diff] [blame] | 502 | |
genia.likes.science@gmail.com | 2ba3224 | 2013-05-31 03:10:48 -0700 | [diff] [blame] | 503 | // TODO Not sure which error to throw for an empty OrderBy but this should fail. |
| 504 | alert("Order By Error"); |
| 505 | this._properties["clause"] = null; |
| 506 | return this; |
| 507 | } |
| 508 | |
| 509 | var expc = 0; |
| 510 | var expressions = []; |
| 511 | |
| 512 | while (expc < arguments.length) { |
| 513 | |
| 514 | var expression = ""; |
| 515 | |
| 516 | if (arguments[expc] instanceof AExpression) { |
| 517 | expression += arguments[expc].val(); |
| 518 | } |
| 519 | |
| 520 | var next = expc + 1; |
| 521 | if (next < arguments.length && (arguments[next] == "asc" || arguments[next] == "desc")) { |
| 522 | expc++; |
| 523 | expression += " " + arguments[expc]; |
| 524 | } |
| 525 | |
| 526 | expressions.push(expression); |
| 527 | |
| 528 | expc++; |
| 529 | } |
| 530 | |
| 531 | this._properties["clause"] = "order by " + expressions.join(", "); |
| 532 | return this; |
| 533 | } |
| 534 | |
| 535 | OrderbyClause.prototype = Object.create(AQLClause.prototype); |
| 536 | OrderbyClause.prototype.constructor = OrderbyClause; |
| 537 | |
| 538 | |
genia.likes.science@gmail.com | 6e39291 | 2013-05-31 03:46:59 -0700 | [diff] [blame] | 539 | // GroupClause |
| 540 | // |
| 541 | // Grammar: |
| 542 | // GroupClause ::= "group" "by" ( Variable ":=" )? Expression ( "," ( Variable ":=" )? Expression )* ( "decor" Variable ":=" Expression ( "," "decor" Variable ":=" Expression )* )? "with" VariableRef ( "," VariableRef )* |
| 543 | function GroupClause() { |
| 544 | AQLClause.call(this); |
| 545 | |
| 546 | if (arguments.length == 0) { |
| 547 | // TODO Not sure which error to throw for an empty GroupBy but this should fail. |
| 548 | alert("Group Error"); |
| 549 | this._properties["clause"] = null; |
| 550 | return this; |
| 551 | } |
| 552 | |
| 553 | var expc = 0; |
| 554 | var expressions = []; |
| 555 | var variableRefs = []; |
| 556 | var isDecor = false; |
genia.likes.science@gmail.com | 5f42534 | 2013-06-13 13:51:26 -0700 | [diff] [blame] | 557 | |
genia.likes.science@gmail.com | 6e39291 | 2013-05-31 03:46:59 -0700 | [diff] [blame] | 558 | while (expc < arguments.length) { |
| 559 | |
| 560 | if (arguments[expc] instanceof AExpression) { |
| 561 | |
| 562 | isDecor = false; |
| 563 | expressions.push(arguments[expc].val()); |
| 564 | |
| 565 | } else if (typeof arguments[expc] == "string") { |
| 566 | |
| 567 | // Special keywords, decor & with |
| 568 | if (arguments[expc] == "decor") { |
| 569 | isDecor = true; |
| 570 | } else if (arguments[expc] == "with") { |
| 571 | isDecor = false; |
| 572 | expc++; |
| 573 | while (expc < arguments.length) { |
| 574 | variableRefs.push("$" + arguments[expc]); |
| 575 | expc++; |
| 576 | } |
| 577 | |
| 578 | // Variables and variable refs |
| 579 | } else { |
| 580 | |
| 581 | var nextc = expc + 1; |
| 582 | var expression = ""; |
| 583 | |
| 584 | if (isDecor) { |
| 585 | expression += "decor "; |
| 586 | isDecor = false; |
| 587 | } |
| 588 | |
| 589 | expression += "$" + arguments[expc] + " := " + arguments[nextc].val(); |
| 590 | expressions.push(expression); |
| 591 | expc++; |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | expc++; |
| 596 | } |
| 597 | |
| 598 | this._properties["clause"] = "group by " + expressions.join(", ") + " with " + variableRefs.join(", "); |
| 599 | return this; |
| 600 | } |
| 601 | |
| 602 | GroupClause.prototype = Object.create(AQLClause.prototype); |
| 603 | GroupClause.prototype.constructor = GroupClause; |
| 604 | |
genia.likes.science@gmail.com | 79e603a | 2013-05-31 10:03:23 -0700 | [diff] [blame] | 605 | |
| 606 | // SetStatement |
| 607 | // |
| 608 | // Grammar |
| 609 | // "set" Identifier StringLiteral |
| 610 | function SetStatement (identifier, stringLiteral) { |
| 611 | AExpression.call(this); |
| 612 | |
| 613 | var statement = "set " + identifier + ' "' + stringLiteral + '";'; |
| 614 | |
| 615 | AExpression.prototype.set.call(this, statement); |
| 616 | |
| 617 | return this; |
| 618 | } |
| 619 | |
| 620 | SetStatement.prototype = Object.create(AExpression.prototype); |
| 621 | SetStatement.prototype.constructor = SetStatement; |
genia.likes.science@gmail.com | e34c57b | 2013-05-31 10:15:49 -0700 | [diff] [blame] | 622 | |
| 623 | |
| 624 | // Quantified Expression |
| 625 | // |
| 626 | // Grammar |
| 627 | // QuantifiedExpression ::= ( ( "some" ) | ( "every" ) ) Variable "in" Expression ( "," Variable "in" Expression )* "satisfies" Expression |
| 628 | // |
| 629 | // @param String some/every |
| 630 | // @param [AExpression] |
| 631 | // @param [Aexpression] satisfiesExpression |
| 632 | function QuantifiedExpression (keyword, expressions, satisfiesExpression) { |
| 633 | AExpression.call(this); |
| 634 | |
| 635 | var expression = keyword + " "; |
| 636 | var varsInExpressions = []; |
| 637 | |
| 638 | for (var varInExpression in expressions) { |
| 639 | varsInExpressions.push(varInExpression + " in " + expressions[varInExpression].val()); |
| 640 | } |
| 641 | expression += varsInExpressions.join(", ") + " satisfies " + satisfiesExpression.val(); |
| 642 | |
| 643 | AExpression.prototype.set.call(this, expression); |
| 644 | |
| 645 | return this; |
| 646 | } |
| 647 | |
| 648 | QuantifiedExpression.prototype = Object.create(AExpression.prototype); |
| 649 | QuantifiedExpression.prototype.constructor = QuantifiedExpression; |
genia.likes.science@gmail.com | 64ca88e | 2013-05-31 10:40:39 -0700 | [diff] [blame] | 650 | |
| 651 | QuantifiedExpression.prototype.val = function() { |
| 652 | var value = AExpression.prototype.val.call(this); |
| 653 | return "(" + value + ")"; |
| 654 | }; |