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