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