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