Ian Maxon | 032a178 | 2015-06-30 17:10:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2009-2013 by The Regents of the University of California |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * you may obtain a copy of the License from |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | |
genia.likes.science@gmail.com | b91b577a | 2013-06-13 14:36:06 -0700 | [diff] [blame] | 16 | function AsterixDBConnection(configuration) { |
| 17 | this._properties = {}; |
| 18 | this._properties["dataverse"] = ""; |
| 19 | this._properties["mode"] = "synchronous"; |
| 20 | |
| 21 | var configuration = arguments || {}; |
| 22 | |
| 23 | for (var key in configuration) { |
| 24 | this._properties[key] = configuration[key]; |
| 25 | } |
| 26 | |
| 27 | return this; |
| 28 | } |
| 29 | |
| 30 | |
| 31 | AsterixDBConnection.prototype.dataverse = function(dataverseName) { |
| 32 | this._properties["dataverse"] = dataverseName; |
| 33 | |
| 34 | return this; |
| 35 | }; |
| 36 | |
| 37 | |
genia.likes.science@gmail.com | 4a5dfe1 | 2013-07-05 07:10:00 -0700 | [diff] [blame] | 38 | AsterixDBConnection.prototype.query = function(statements, successFn, mode) { |
genia.likes.science@gmail.com | 3395d5b | 2013-07-05 00:47:11 -0700 | [diff] [blame] | 39 | |
genia.likes.science@gmail.com | b91b577a | 2013-06-13 14:36:06 -0700 | [diff] [blame] | 40 | if ( typeof statements === 'string') { |
| 41 | statements = [ statements ]; |
| 42 | } |
| 43 | |
genia.likes.science@gmail.com | 212a644 | 2013-07-13 20:40:11 -0700 | [diff] [blame] | 44 | var m = typeof mode ? mode : "synchronous"; |
| 45 | |
genia.likes.science@gmail.com | 78593d7 | 2013-07-02 06:33:39 -0700 | [diff] [blame] | 46 | var query = "use dataverse " + this._properties["dataverse"] + ";\n" + statements.join("\n"); |
genia.likes.science@gmail.com | d2795fb | 2013-07-14 15:48:04 -0700 | [diff] [blame] | 47 | |
genia.likes.science@gmail.com | 3395d5b | 2013-07-05 00:47:11 -0700 | [diff] [blame] | 48 | this._api( |
| 49 | { |
genia.likes.science@gmail.com | b91b577a | 2013-06-13 14:36:06 -0700 | [diff] [blame] | 50 | "query" : query, |
genia.likes.science@gmail.com | 4a5dfe1 | 2013-07-05 07:10:00 -0700 | [diff] [blame] | 51 | "mode" : m |
genia.likes.science@gmail.com | b91b577a | 2013-06-13 14:36:06 -0700 | [diff] [blame] | 52 | }, |
genia.likes.science@gmail.com | 3395d5b | 2013-07-05 00:47:11 -0700 | [diff] [blame] | 53 | successFn, |
| 54 | "http://localhost:19002/query" |
| 55 | ); |
genia.likes.science@gmail.com | b91b577a | 2013-06-13 14:36:06 -0700 | [diff] [blame] | 56 | |
| 57 | return this; |
| 58 | }; |
| 59 | |
| 60 | |
genia.likes.science@gmail.com | 4a5dfe1 | 2013-07-05 07:10:00 -0700 | [diff] [blame] | 61 | AsterixDBConnection.prototype.query_status = function(data, successFn) { |
| 62 | |
| 63 | this._api( |
| 64 | data, |
| 65 | successFn, |
| 66 | "http://localhost:19002/query/status" |
| 67 | ); |
| 68 | |
| 69 | return this; |
| 70 | }; |
| 71 | |
| 72 | |
| 73 | AsterixDBConnection.prototype.query_result = function(data, successFn) { |
| 74 | this._api( |
| 75 | data, |
| 76 | successFn, |
| 77 | "http://localhost:19002/query/result" |
| 78 | ); |
| 79 | |
| 80 | return this; |
| 81 | }; |
| 82 | |
genia.likes.science@gmail.com | 6686c45 | 2013-07-05 08:39:35 -0700 | [diff] [blame] | 83 | |
| 84 | AsterixDBConnection.prototype.ddl = function(statements, successFn) { |
| 85 | if ( typeof statements === 'string') { |
| 86 | statements = [ statements ]; |
| 87 | } |
| 88 | |
| 89 | this._api( |
| 90 | { |
| 91 | "ddl" : "use dataverse " + this._properties["dataverse"] + ";\n" + statements.join("\n") |
| 92 | }, |
| 93 | successFn, |
| 94 | "http://localhost:19002/ddl" |
| 95 | ); |
| 96 | } |
| 97 | |
| 98 | |
| 99 | AsterixDBConnection.prototype.update = function(statements, successFn) { |
| 100 | if ( typeof statements === 'string') { |
| 101 | statements = [ statements ]; |
| 102 | } |
| 103 | |
| 104 | this._api( |
| 105 | { |
| 106 | "statements" : "use dataverse " + this._properties["dataverse"] + ";\n" + statements.join("\n") |
| 107 | }, |
| 108 | successFn, |
| 109 | "http://localhost:19002/update" |
| 110 | ); |
| 111 | } |
| 112 | |
| 113 | |
genia.likes.science@gmail.com | 3395d5b | 2013-07-05 00:47:11 -0700 | [diff] [blame] | 114 | AsterixDBConnection.prototype._api = function(json, onSuccess, endpoint) { |
| 115 | var success_fn = onSuccess; |
| 116 | |
| 117 | $.ajax({ |
| 118 | type: 'GET', |
| 119 | url: endpoint, |
| 120 | data : json, |
| 121 | dataType: "json", |
| 122 | success: function(data) { |
| 123 | success_fn(data); |
| 124 | } |
| 125 | // TODO error: |
| 126 | }); |
| 127 | |
| 128 | return this; |
| 129 | }; |
| 130 | |
| 131 | |
genia.likes.science@gmail.com | 90d0872 | 2013-05-28 04:40:12 -0700 | [diff] [blame] | 132 | function AExpression () { |
genia.likes.science@gmail.com | d2795fb | 2013-07-14 15:48:04 -0700 | [diff] [blame] | 133 | |
genia.likes.science@gmail.com | 512454d | 2013-05-28 03:32:20 -0700 | [diff] [blame] | 134 | this._properties = {}; |
| 135 | this._success = function() {}; |
| 136 | |
genia.likes.science@gmail.com | d2795fb | 2013-07-14 15:48:04 -0700 | [diff] [blame] | 137 | if (typeof arguments[0] == 'string') { |
| 138 | this._properties["value"] = arguments[0]; |
| 139 | } |
| 140 | |
genia.likes.science@gmail.com | 512454d | 2013-05-28 03:32:20 -0700 | [diff] [blame] | 141 | return this; |
| 142 | } |
| 143 | |
genia.likes.science@gmail.com | 90d0872 | 2013-05-28 04:40:12 -0700 | [diff] [blame] | 144 | |
| 145 | AExpression.prototype.bind = function(options) { |
genia.likes.science@gmail.com | 512454d | 2013-05-28 03:32:20 -0700 | [diff] [blame] | 146 | var options = options || {}; |
| 147 | |
genia.likes.science@gmail.com | 512454d | 2013-05-28 03:32:20 -0700 | [diff] [blame] | 148 | if (options.hasOwnProperty("success")) { |
| 149 | this._success = options["success"]; |
| 150 | } |
genia.likes.science@gmail.com | 3861263 | 2013-05-28 13:11:52 -0700 | [diff] [blame] | 151 | |
| 152 | if (options.hasOwnProperty("return")) { |
| 153 | this._properties["return"] = " return " + options["return"].val(); |
| 154 | } |
genia.likes.science@gmail.com | 512454d | 2013-05-28 03:32:20 -0700 | [diff] [blame] | 155 | }; |
| 156 | |
genia.likes.science@gmail.com | 512454d | 2013-05-28 03:32:20 -0700 | [diff] [blame] | 157 | |
genia.likes.science@gmail.com | 18f3bf2 | 2013-06-12 07:45:02 -0700 | [diff] [blame] | 158 | AExpression.prototype.run = function(successFn) { |
genia.likes.science@gmail.com | 512454d | 2013-05-28 03:32:20 -0700 | [diff] [blame] | 159 | return this; |
genia.likes.science@gmail.com | 90d0872 | 2013-05-28 04:40:12 -0700 | [diff] [blame] | 160 | }; |
| 161 | |
| 162 | |
| 163 | AExpression.prototype.val = function() { |
| 164 | |
genia.likes.science@gmail.com | 2ba3224 | 2013-05-31 03:10:48 -0700 | [diff] [blame] | 165 | var value = ""; |
| 166 | |
genia.likes.science@gmail.com | 90d0872 | 2013-05-28 04:40:12 -0700 | [diff] [blame] | 167 | // If there is a dataverse defined, provide it. |
| 168 | if (this._properties.hasOwnProperty("dataverse")) { |
genia.likes.science@gmail.com | 2ba3224 | 2013-05-31 03:10:48 -0700 | [diff] [blame] | 169 | value += "use dataverse " + this._properties["dataverse"] + ";\n"; |
| 170 | }; |
| 171 | |
| 172 | if (this._properties.hasOwnProperty("value")) { |
| 173 | value += this._properties["value"]; |
genia.likes.science@gmail.com | 90d0872 | 2013-05-28 04:40:12 -0700 | [diff] [blame] | 174 | } |
genia.likes.science@gmail.com | 2ba3224 | 2013-05-31 03:10:48 -0700 | [diff] [blame] | 175 | |
| 176 | return value; |
genia.likes.science@gmail.com | 3861263 | 2013-05-28 13:11:52 -0700 | [diff] [blame] | 177 | }; |
| 178 | |
genia.likes.science@gmail.com | 2ba3224 | 2013-05-31 03:10:48 -0700 | [diff] [blame] | 179 | // @param expressionValue [String] |
| 180 | AExpression.prototype.set = function(expressionValue) { |
| 181 | this._properties["value"] = expressionValue; |
| 182 | return this; |
genia.likes.science@gmail.com | 3861263 | 2013-05-28 13:11:52 -0700 | [diff] [blame] | 183 | }; |
| 184 | |
genia.likes.science@gmail.com | 73dcc70 | 2013-05-28 04:21:28 -0700 | [diff] [blame] | 185 | |
genia.likes.science@gmail.com | 90d0872 | 2013-05-28 04:40:12 -0700 | [diff] [blame] | 186 | // FunctionExpression |
| 187 | // Parent: AsterixExpression |
| 188 | // |
| 189 | // @param options [Various], |
| 190 | // @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] | 191 | // @key expression [AsterixExpression or AQLClause] an AsterixExpression/Clause to which the fn will be applied |
| 192 | function FunctionExpression() { |
genia.likes.science@gmail.com | 90d0872 | 2013-05-28 04:40:12 -0700 | [diff] [blame] | 193 | |
| 194 | // Initialize superclass |
| 195 | AExpression.call(this); |
genia.likes.science@gmail.com | 18f3bf2 | 2013-06-12 07:45:02 -0700 | [diff] [blame] | 196 | |
| 197 | this._properties["function"] = ""; |
| 198 | this._properties["expression"] = new AExpression().set(""); |
genia.likes.science@gmail.com | 90d0872 | 2013-05-28 04:40:12 -0700 | [diff] [blame] | 199 | |
genia.likes.science@gmail.com | 18f3bf2 | 2013-06-12 07:45:02 -0700 | [diff] [blame] | 200 | // Check for fn/expression input |
| 201 | if (arguments.length == 2 && typeof arguments[0] == "string" && |
| 202 | (arguments[1] instanceof AExpression || arguments[1] instanceof AQLClause)) { |
| 203 | |
| 204 | this._properties["function"] = arguments[0]; |
| 205 | this._properties["expression"] = arguments[1]; |
| 206 | |
| 207 | } |
genia.likes.science@gmail.com | 90d0872 | 2013-05-28 04:40:12 -0700 | [diff] [blame] | 208 | |
| 209 | // Return object |
| 210 | return this; |
| 211 | } |
| 212 | |
| 213 | |
| 214 | FunctionExpression.prototype = Object.create(AExpression.prototype); |
| 215 | FunctionExpression.prototype.constructor = FunctionExpression; |
| 216 | |
| 217 | |
genia.likes.science@gmail.com | 18f3bf2 | 2013-06-12 07:45:02 -0700 | [diff] [blame] | 218 | FunctionExpression.prototype.fn = function(fnName) { |
genia.likes.science@gmail.com | 90d0872 | 2013-05-28 04:40:12 -0700 | [diff] [blame] | 219 | |
genia.likes.science@gmail.com | 18f3bf2 | 2013-06-12 07:45:02 -0700 | [diff] [blame] | 220 | if (typeof fnName == "string") { |
| 221 | this._properties["function"] = fnName; |
| 222 | } |
genia.likes.science@gmail.com | 90d0872 | 2013-05-28 04:40:12 -0700 | [diff] [blame] | 223 | |
genia.likes.science@gmail.com | 90d0872 | 2013-05-28 04:40:12 -0700 | [diff] [blame] | 224 | return this; |
| 225 | }; |
| 226 | |
genia.likes.science@gmail.com | 18f3bf2 | 2013-06-12 07:45:02 -0700 | [diff] [blame] | 227 | |
| 228 | FunctionExpression.prototype.expression = function(expression) { |
| 229 | if (expression instanceof AExpression || expression instanceof AQLClause) { |
| 230 | this._properties["expression"] = expression; |
| 231 | } |
| 232 | |
| 233 | return this; |
| 234 | }; |
| 235 | |
| 236 | |
genia.likes.science@gmail.com | 90d0872 | 2013-05-28 04:40:12 -0700 | [diff] [blame] | 237 | FunctionExpression.prototype.val = function () { |
genia.likes.science@gmail.com | 2ba3224 | 2013-05-31 03:10:48 -0700 | [diff] [blame] | 238 | return this._properties["function"] + "(" + this._properties["expression"].val() + ")"; |
genia.likes.science@gmail.com | 90d0872 | 2013-05-28 04:40:12 -0700 | [diff] [blame] | 239 | }; |
| 240 | |
genia.likes.science@gmail.com | 73dcc70 | 2013-05-28 04:21:28 -0700 | [diff] [blame] | 241 | |
genia.likes.science@gmail.com | 3861263 | 2013-05-28 13:11:52 -0700 | [diff] [blame] | 242 | // FLWOGRExpression |
genia.likes.science@gmail.com | 90d0872 | 2013-05-28 04:40:12 -0700 | [diff] [blame] | 243 | // |
| 244 | // FLWOGRExpression ::= ( ForClause | LetClause ) ( Clause )* "return" Expression |
genia.likes.science@gmail.com | 3861263 | 2013-05-28 13:11:52 -0700 | [diff] [blame] | 245 | function FLWOGRExpression (options) { |
| 246 | // Initialize superclass |
| 247 | AExpression.call(this); |
genia.likes.science@gmail.com | 90d0872 | 2013-05-28 04:40:12 -0700 | [diff] [blame] | 248 | |
genia.likes.science@gmail.com | 3861263 | 2013-05-28 13:11:52 -0700 | [diff] [blame] | 249 | this._properties["clauses"] = []; |
genia.likes.science@gmail.com | 79e603a | 2013-05-31 10:03:23 -0700 | [diff] [blame] | 250 | this._properties["minSize"] = 0; |
genia.likes.science@gmail.com | 3861263 | 2013-05-28 13:11:52 -0700 | [diff] [blame] | 251 | |
| 252 | // Bind options and return |
| 253 | this.bind(options); |
| 254 | return this; |
genia.likes.science@gmail.com | 90d0872 | 2013-05-28 04:40:12 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | |
genia.likes.science@gmail.com | 3861263 | 2013-05-28 13:11:52 -0700 | [diff] [blame] | 258 | FLWOGRExpression.prototype = Object.create(AExpression.prototype); |
| 259 | FLWOGRExpression.prototype.constructor = FLWOGRExpression; |
| 260 | |
| 261 | |
| 262 | FLWOGRExpression.prototype.bind = function(options) { |
| 263 | AExpression.prototype.bind.call(this, options); |
| 264 | |
| 265 | var options = options || {}; |
| 266 | |
genia.likes.science@gmail.com | 79e603a | 2013-05-31 10:03:23 -0700 | [diff] [blame] | 267 | if (options instanceof SetStatement) { |
| 268 | this._properties["clauses"].push(options); |
| 269 | this._properties["minSize"] += 1; |
| 270 | } |
| 271 | |
| 272 | if (this._properties["clauses"].length <= this._properties["minSize"]) { |
genia.likes.science@gmail.com | 3861263 | 2013-05-28 13:11:52 -0700 | [diff] [blame] | 273 | // Needs to start with for or let clause |
| 274 | if (options instanceof ForClause || options instanceof LetClause) { |
| 275 | this._properties["clauses"].push(options); |
| 276 | } |
| 277 | } else { |
| 278 | if (options instanceof AQLClause) { |
| 279 | this._properties["clauses"].push(options); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | return this; |
| 284 | }; |
| 285 | |
| 286 | |
| 287 | FLWOGRExpression.prototype.val = function() { |
| 288 | var value = AExpression.prototype.val.call(this); |
| 289 | |
genia.likes.science@gmail.com | 2ba3224 | 2013-05-31 03:10:48 -0700 | [diff] [blame] | 290 | var clauseValues = []; |
genia.likes.science@gmail.com | 3861263 | 2013-05-28 13:11:52 -0700 | [diff] [blame] | 291 | for (var c in this._properties["clauses"]) { |
genia.likes.science@gmail.com | 2ba3224 | 2013-05-31 03:10:48 -0700 | [diff] [blame] | 292 | clauseValues.push(this._properties["clauses"][c].val()); |
genia.likes.science@gmail.com | 3861263 | 2013-05-28 13:11:52 -0700 | [diff] [blame] | 293 | } |
| 294 | |
genia.likes.science@gmail.com | 79e603a | 2013-05-31 10:03:23 -0700 | [diff] [blame] | 295 | return value + clauseValues.join("\n");// + ";"; |
genia.likes.science@gmail.com | 3861263 | 2013-05-28 13:11:52 -0700 | [diff] [blame] | 296 | }; |
| 297 | |
genia.likes.science@gmail.com | d2795fb | 2013-07-14 15:48:04 -0700 | [diff] [blame] | 298 | // Pretty Expression Shorthand |
genia.likes.science@gmail.com | 2ba3224 | 2013-05-31 03:10:48 -0700 | [diff] [blame] | 299 | |
genia.likes.science@gmail.com | 5f42534 | 2013-06-13 13:51:26 -0700 | [diff] [blame] | 300 | FLWOGRExpression.prototype.ReturnClause = function(expression) { |
| 301 | return this.bind(new ReturnClause(expression)); |
| 302 | }; |
| 303 | |
genia.likes.science@gmail.com | d2795fb | 2013-07-14 15:48:04 -0700 | [diff] [blame] | 304 | FLWOGRExpression.prototype.ForClause = function() { |
| 305 | return this.bind(new ForClause(Array.prototype.slice.call(arguments))); |
| 306 | }; |
| 307 | |
| 308 | FLWOGRExpression.prototype.LetClause = function() { |
| 309 | return this.bind(new LetClause(Array.prototype.slice.call(arguments))); |
| 310 | }; |
| 311 | |
| 312 | FLWOGRExpression.prototype.WhereClause = function() { |
| 313 | return this.bind(new WhereClause(Array.prototype.slice.call(arguments))); |
| 314 | }; |
| 315 | |
| 316 | FLWOGRExpression.prototype.and = function() { |
| 317 | var args = Array.prototype.slice.call(arguments); |
| 318 | args.push(true); |
| 319 | return this.bind(new WhereClause().and(args)); |
| 320 | }; |
| 321 | |
| 322 | FLWOGRExpression.prototype.or = function() { |
| 323 | var args = Array.prototype.slice.call(arguments); |
| 324 | args.push(true); |
| 325 | return this.bind(new WhereClause().or(args)); |
| 326 | }; |
| 327 | |
| 328 | FLWOGRExpression.prototype.OrderbyClause = function() { |
| 329 | return this.bind(new OrderbyClause(Array.prototype.slice.call(arguments))); |
| 330 | }; |
| 331 | |
| 332 | |
| 333 | FLWOGRExpression.prototype.GroupClause = function() { |
| 334 | return this.bind(new GroupClause(Array.prototype.slice.call(arguments))); |
| 335 | }; |
| 336 | |
| 337 | FLWOGRExpression.prototype.LimitClause = function() { |
| 338 | return this.bind(new LimitClause(Array.prototype.slice.call(arguments))); |
| 339 | }; |
| 340 | |
| 341 | FLWOGRExpression.prototype.DistinctClause = function() { |
| 342 | return this.bind(new DistinctClause(Array.prototype.slice.call(arguments))); |
| 343 | }; |
| 344 | |
| 345 | FLWOGRExpression.prototype.AQLClause = function() { |
| 346 | return this.bind(new AQLClause(Array.prototype.slice.call(arguments))); |
| 347 | }; |
| 348 | |
| 349 | |
genia.likes.science@gmail.com | 73dcc70 | 2013-05-28 04:21:28 -0700 | [diff] [blame] | 350 | // AQLClause |
| 351 | // |
| 352 | // Base Clause ::= ForClause | LetClause | WhereClause | OrderbyClause | GroupClause | LimitClause | DistinctClause |
| 353 | function AQLClause() { |
| 354 | this._properties = {}; |
| 355 | this._properties["clause"] = ""; |
genia.likes.science@gmail.com | d2795fb | 2013-07-14 15:48:04 -0700 | [diff] [blame] | 356 | this._properties["stack"] = []; |
| 357 | if (typeof arguments[0] == 'string') { |
| 358 | this._properties["clause"] = arguments[0]; |
| 359 | } |
| 360 | return this; |
genia.likes.science@gmail.com | 73dcc70 | 2013-05-28 04:21:28 -0700 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | AQLClause.prototype.val = function() { |
| 364 | var value = this._properties["clause"]; |
genia.likes.science@gmail.com | 73dcc70 | 2013-05-28 04:21:28 -0700 | [diff] [blame] | 365 | |
| 366 | return value; |
| 367 | }; |
| 368 | |
| 369 | AQLClause.prototype.bind = function(options) { |
genia.likes.science@gmail.com | 73dcc70 | 2013-05-28 04:21:28 -0700 | [diff] [blame] | 370 | |
genia.likes.science@gmail.com | 18f3bf2 | 2013-06-12 07:45:02 -0700 | [diff] [blame] | 371 | if (options instanceof AQLClause) { |
| 372 | this._properties["clause"] += " " + options.val(); |
genia.likes.science@gmail.com | 73dcc70 | 2013-05-28 04:21:28 -0700 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | return this; |
| 376 | }; |
| 377 | |
genia.likes.science@gmail.com | 3957830 | 2013-05-31 04:42:26 -0700 | [diff] [blame] | 378 | AQLClause.prototype.set = function(value) { |
| 379 | this._properties["clause"] = value; |
| 380 | return this; |
| 381 | }; |
| 382 | |
genia.likes.science@gmail.com | 73dcc70 | 2013-05-28 04:21:28 -0700 | [diff] [blame] | 383 | |
| 384 | // ForClause |
| 385 | // |
| 386 | // Grammar: |
| 387 | // "for" Variable ( "at" Variable )? "in" ( Expression ) |
| 388 | // |
| 389 | // @param for_variable [String], REQUIRED, first variable in clause |
| 390 | // @param at_variable [String], NOT REQUIRED, first variable in clause |
| 391 | // @param expression [AsterixExpression], REQUIRED, expression to evaluate |
genia.likes.science@gmail.com | 73dcc70 | 2013-05-28 04:21:28 -0700 | [diff] [blame] | 392 | function ForClause(for_variable, at_variable, expression) { |
| 393 | AQLClause.call(this); |
| 394 | |
genia.likes.science@gmail.com | d2795fb | 2013-07-14 15:48:04 -0700 | [diff] [blame] | 395 | var parameters = []; |
| 396 | if (arguments[0] instanceof Array) { |
| 397 | parameters = arguments[0]; |
| 398 | } else { |
| 399 | parameters = arguments; |
| 400 | } |
| 401 | |
| 402 | this._properties["clause"] = "for " + parameters[0]; |
genia.likes.science@gmail.com | f8cca19 | 2013-06-13 15:45:53 -0700 | [diff] [blame] | 403 | |
genia.likes.science@gmail.com | d2795fb | 2013-07-14 15:48:04 -0700 | [diff] [blame] | 404 | if (parameters.length == 3) { |
| 405 | this._properties["clause"] += " at " + parameters[1]; |
| 406 | this._properties["clause"] += " in " + parameters[2].val(); |
| 407 | } else if (parameters.length == 2) { |
| 408 | this._properties["clause"] += " in " + parameters[1].val(); |
genia.likes.science@gmail.com | 73dcc70 | 2013-05-28 04:21:28 -0700 | [diff] [blame] | 409 | } |
genia.likes.science@gmail.com | f8cca19 | 2013-06-13 15:45:53 -0700 | [diff] [blame] | 410 | |
genia.likes.science@gmail.com | 73dcc70 | 2013-05-28 04:21:28 -0700 | [diff] [blame] | 411 | return this; |
| 412 | } |
| 413 | |
| 414 | ForClause.prototype = Object.create(AQLClause.prototype); |
| 415 | ForClause.prototype.constructor = ForClause; |
genia.likes.science@gmail.com | 3861263 | 2013-05-28 13:11:52 -0700 | [diff] [blame] | 416 | |
| 417 | |
| 418 | // LetClause |
| 419 | // |
| 420 | // Grammar: |
| 421 | // LetClause ::= "let" Variable ":=" Expression |
| 422 | // |
| 423 | // @param let_variable [String] |
| 424 | // @param expression [AExpression] |
| 425 | // |
| 426 | // TODO Vigorous error checking |
| 427 | function LetClause(let_variable, expression) { |
| 428 | AQLClause.call(this); |
| 429 | |
genia.likes.science@gmail.com | d2795fb | 2013-07-14 15:48:04 -0700 | [diff] [blame] | 430 | var parameters = []; |
| 431 | if (arguments[0] instanceof Array) { |
| 432 | parameters = arguments[0]; |
| 433 | } else { |
| 434 | parameters = arguments; |
| 435 | } |
| 436 | |
| 437 | this._properties["clause"] = "let " + parameters[0] + " := "; |
| 438 | this._properties["clause"] += parameters[1].val(); |
genia.likes.science@gmail.com | 3861263 | 2013-05-28 13:11:52 -0700 | [diff] [blame] | 439 | |
| 440 | return this; |
| 441 | } |
| 442 | |
| 443 | LetClause.prototype = Object.create(AQLClause.prototype); |
| 444 | LetClause.prototype.constructor = LetClause; |
| 445 | |
| 446 | |
genia.likes.science@gmail.com | 5ca104c | 2013-05-29 05:39:26 -0700 | [diff] [blame] | 447 | // ReturnClause |
| 448 | // |
| 449 | // Grammar: |
| 450 | // return [AQLExpression] |
| 451 | function ReturnClause(expression) { |
| 452 | AQLClause.call(this); |
| 453 | |
| 454 | this._properties["clause"] = "return "; |
genia.likes.science@gmail.com | 5749ef9 | 2013-06-12 07:59:25 -0700 | [diff] [blame] | 455 | |
genia.likes.science@gmail.com | 79e603a | 2013-05-31 10:03:23 -0700 | [diff] [blame] | 456 | if (expression instanceof AExpression || expression instanceof AQLClause) { |
genia.likes.science@gmail.com | 5ca104c | 2013-05-29 05:39:26 -0700 | [diff] [blame] | 457 | this._properties["clause"] += expression.val(); |
genia.likes.science@gmail.com | 5749ef9 | 2013-06-12 07:59:25 -0700 | [diff] [blame] | 458 | |
| 459 | } else if ( typeof expression == "object" && Object.getPrototypeOf( expression ) === Object.prototype ) { |
| 460 | |
| 461 | // TODO Null object check |
genia.likes.science@gmail.com | 2ba3224 | 2013-05-31 03:10:48 -0700 | [diff] [blame] | 462 | |
| 463 | this._properties["clause"] += "{"; |
| 464 | var returnStatements = []; |
| 465 | for (returnValue in expression) { |
| 466 | |
| 467 | if (expression[returnValue] instanceof AExpression) { |
genia.likes.science@gmail.com | fba7cc8 | 2013-05-31 04:04:08 -0700 | [diff] [blame] | 468 | returnStatements.push('"' + returnValue + '" ' + " : " + expression[returnValue].val()); |
genia.likes.science@gmail.com | 2ba3224 | 2013-05-31 03:10:48 -0700 | [diff] [blame] | 469 | } else if (typeof expression[returnValue] == "string") { |
genia.likes.science@gmail.com | fba7cc8 | 2013-05-31 04:04:08 -0700 | [diff] [blame] | 470 | returnStatements.push('"' + returnValue + '" ' + " : " + expression[returnValue]); |
genia.likes.science@gmail.com | 2ba3224 | 2013-05-31 03:10:48 -0700 | [diff] [blame] | 471 | } |
| 472 | } |
| 473 | this._properties["clause"] += returnStatements.join(",\n"); |
genia.likes.science@gmail.com | f7929d8 | 2013-06-12 11:30:01 -0700 | [diff] [blame] | 474 | this._properties["clause"] += "\n}"; |
genia.likes.science@gmail.com | 2ba3224 | 2013-05-31 03:10:48 -0700 | [diff] [blame] | 475 | |
genia.likes.science@gmail.com | 5ca104c | 2013-05-29 05:39:26 -0700 | [diff] [blame] | 476 | } else { |
genia.likes.science@gmail.com | 5749ef9 | 2013-06-12 07:59:25 -0700 | [diff] [blame] | 477 | this._properties["clause"] += new AQLClause().set(expression).val(); |
genia.likes.science@gmail.com | 5ca104c | 2013-05-29 05:39:26 -0700 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | return this; |
| 481 | } |
| 482 | |
genia.likes.science@gmail.com | 5749ef9 | 2013-06-12 07:59:25 -0700 | [diff] [blame] | 483 | |
genia.likes.science@gmail.com | 5ca104c | 2013-05-29 05:39:26 -0700 | [diff] [blame] | 484 | ReturnClause.prototype = Object.create(AQLClause.prototype); |
| 485 | ReturnClause.prototype.constructor = ReturnClause; |
| 486 | |
genia.likes.science@gmail.com | 2ba3224 | 2013-05-31 03:10:48 -0700 | [diff] [blame] | 487 | |
genia.likes.science@gmail.com | 3861263 | 2013-05-28 13:11:52 -0700 | [diff] [blame] | 488 | // WhereClause |
| 489 | // |
| 490 | // Grammar: |
| 491 | // ::= "where" Expression |
| 492 | // |
| 493 | // @param expression [BooleanExpression], pushes this expression onto the stack |
genia.likes.science@gmail.com | 3861263 | 2013-05-28 13:11:52 -0700 | [diff] [blame] | 494 | function WhereClause(expression) { |
| 495 | AQLClause.call(this); |
genia.likes.science@gmail.com | d2795fb | 2013-07-14 15:48:04 -0700 | [diff] [blame] | 496 | |
genia.likes.science@gmail.com | 3861263 | 2013-05-28 13:11:52 -0700 | [diff] [blame] | 497 | this._properties["stack"] = []; |
| 498 | |
genia.likes.science@gmail.com | d2795fb | 2013-07-14 15:48:04 -0700 | [diff] [blame] | 499 | if (expression instanceof Array) { |
| 500 | this.bind(expression[0]); |
| 501 | } else { |
| 502 | this.bind(expression); |
| 503 | } |
| 504 | |
genia.likes.science@gmail.com | 3861263 | 2013-05-28 13:11:52 -0700 | [diff] [blame] | 505 | return this; |
| 506 | } |
| 507 | |
| 508 | |
| 509 | WhereClause.prototype = Object.create(AQLClause.prototype); |
| 510 | WhereClause.prototype.constructor = WhereClause; |
| 511 | |
| 512 | |
| 513 | WhereClause.prototype.bind = function(expression) { |
genia.likes.science@gmail.com | 64ca88e | 2013-05-31 10:40:39 -0700 | [diff] [blame] | 514 | if (expression instanceof AExpression) { |
genia.likes.science@gmail.com | 3861263 | 2013-05-28 13:11:52 -0700 | [diff] [blame] | 515 | this._properties["stack"].push(expression); |
| 516 | } |
genia.likes.science@gmail.com | d2795fb | 2013-07-14 15:48:04 -0700 | [diff] [blame] | 517 | return this; |
genia.likes.science@gmail.com | 3861263 | 2013-05-28 13:11:52 -0700 | [diff] [blame] | 518 | }; |
| 519 | |
| 520 | |
| 521 | WhereClause.prototype.val = function() { |
genia.likes.science@gmail.com | d2795fb | 2013-07-14 15:48:04 -0700 | [diff] [blame] | 522 | var value = ""; |
| 523 | |
| 524 | if (this._properties["stack"].length == 0) { |
| 525 | return value; |
| 526 | } |
genia.likes.science@gmail.com | 3861263 | 2013-05-28 13:11:52 -0700 | [diff] [blame] | 527 | |
| 528 | var count = this._properties["stack"].length - 1; |
| 529 | while (count >= 0) { |
| 530 | value += this._properties["stack"][count].val() + " "; |
| 531 | count -= 1; |
| 532 | } |
| 533 | |
genia.likes.science@gmail.com | d2795fb | 2013-07-14 15:48:04 -0700 | [diff] [blame] | 534 | return "where " + value; |
genia.likes.science@gmail.com | 64ca88e | 2013-05-31 10:40:39 -0700 | [diff] [blame] | 535 | }; |
genia.likes.science@gmail.com | 3861263 | 2013-05-28 13:11:52 -0700 | [diff] [blame] | 536 | |
| 537 | |
genia.likes.science@gmail.com | d2f753e | 2013-06-12 13:51:03 -0700 | [diff] [blame] | 538 | WhereClause.prototype.and = function() { |
| 539 | |
genia.likes.science@gmail.com | d2795fb | 2013-07-14 15:48:04 -0700 | [diff] [blame] | 540 | var parameters = []; |
| 541 | if (arguments[0] instanceof Array) { |
| 542 | parameters = arguments[0]; |
| 543 | } else { |
| 544 | parameters = arguments; |
| 545 | } |
| 546 | |
genia.likes.science@gmail.com | d2f753e | 2013-06-12 13:51:03 -0700 | [diff] [blame] | 547 | var andClauses = []; |
genia.likes.science@gmail.com | d2795fb | 2013-07-14 15:48:04 -0700 | [diff] [blame] | 548 | for (var expression in parameters) { |
genia.likes.science@gmail.com | d2f753e | 2013-06-12 13:51:03 -0700 | [diff] [blame] | 549 | |
genia.likes.science@gmail.com | d2795fb | 2013-07-14 15:48:04 -0700 | [diff] [blame] | 550 | if (parameters[expression] instanceof AExpression) { |
| 551 | andClauses.push(parameters[expression].val()); |
genia.likes.science@gmail.com | d2f753e | 2013-06-12 13:51:03 -0700 | [diff] [blame] | 552 | } |
| 553 | } |
| 554 | |
| 555 | if (andClauses.length > 0) { |
| 556 | this._properties["stack"].push(new AExpression().set(andClauses.join(" and "))); |
| 557 | } |
| 558 | |
| 559 | return this; |
| 560 | }; |
| 561 | |
| 562 | |
| 563 | WhereClause.prototype.or = function() { |
genia.likes.science@gmail.com | d2795fb | 2013-07-14 15:48:04 -0700 | [diff] [blame] | 564 | |
| 565 | var parameters = []; |
| 566 | if (arguments[0] instanceof Array) { |
| 567 | parameters = arguments[0]; |
| 568 | } else { |
| 569 | parameters = arguments; |
| 570 | } |
| 571 | |
genia.likes.science@gmail.com | d2f753e | 2013-06-12 13:51:03 -0700 | [diff] [blame] | 572 | var orClauses = []; |
genia.likes.science@gmail.com | d2795fb | 2013-07-14 15:48:04 -0700 | [diff] [blame] | 573 | for (var expression in parameters) { |
genia.likes.science@gmail.com | d2f753e | 2013-06-12 13:51:03 -0700 | [diff] [blame] | 574 | |
genia.likes.science@gmail.com | d2795fb | 2013-07-14 15:48:04 -0700 | [diff] [blame] | 575 | if (parameters[expression] instanceof AExpression) { |
| 576 | orClauses.push(parameters[expression].val()); |
genia.likes.science@gmail.com | d2f753e | 2013-06-12 13:51:03 -0700 | [diff] [blame] | 577 | } |
| 578 | } |
| 579 | |
| 580 | if (andClauses.length > 0) { |
| 581 | this._properties["stack"].push(new AExpression().set(orClauses.join(" and "))); |
| 582 | } |
| 583 | |
| 584 | return this; |
| 585 | }; |
| 586 | |
genia.likes.science@gmail.com | 2ba3224 | 2013-05-31 03:10:48 -0700 | [diff] [blame] | 587 | // LimitClause |
| 588 | // Grammar: |
| 589 | // LimitClause ::= "limit" Expression ( "offset" Expression )? |
| 590 | // |
| 591 | // @param limitExpression [REQUIRED, AQLExpression] |
| 592 | // @param offsetExpression [OPTIONAL, AQLExpression] |
| 593 | function LimitClause(limitExpression, offsetExpression) { |
| 594 | |
| 595 | AQLClause.call(this); |
genia.likes.science@gmail.com | d2795fb | 2013-07-14 15:48:04 -0700 | [diff] [blame] | 596 | |
| 597 | var parameters = []; |
| 598 | if (arguments[0] instanceof Array) { |
| 599 | parameters = arguments[0]; |
| 600 | } else { |
| 601 | parameters = arguments; |
| 602 | } |
genia.likes.science@gmail.com | 2ba3224 | 2013-05-31 03:10:48 -0700 | [diff] [blame] | 603 | |
| 604 | // limitExpression required |
genia.likes.science@gmail.com | d2795fb | 2013-07-14 15:48:04 -0700 | [diff] [blame] | 605 | this._properties["clause"] = "limit " + parameters[0].val(); |
genia.likes.science@gmail.com | 2ba3224 | 2013-05-31 03:10:48 -0700 | [diff] [blame] | 606 | |
| 607 | // Optional: Offset |
genia.likes.science@gmail.com | d2795fb | 2013-07-14 15:48:04 -0700 | [diff] [blame] | 608 | if (parameters.length == 2) { |
| 609 | this._properties["clause"] += " offset " + parameters[1].val(); |
genia.likes.science@gmail.com | 2ba3224 | 2013-05-31 03:10:48 -0700 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | return this; |
| 613 | } |
| 614 | |
| 615 | LimitClause.prototype = Object.create(AQLClause.prototype); |
| 616 | LimitClause.prototype.constructor = LimitClause; |
| 617 | |
| 618 | |
| 619 | // OrderbyClause |
| 620 | // |
| 621 | // Grammar: |
| 622 | // OrderbyClause ::= "order" "by" Expression ( ( "asc" ) | ( "desc" ) )? ( "," Expression ( ( "asc" ) | ( "desc" ) )? )* |
| 623 | // |
| 624 | // @params AQLExpressions and asc/desc strings, in any quantity. At least one required. |
| 625 | function OrderbyClause() { |
| 626 | |
| 627 | AQLClause.call(this); |
| 628 | |
| 629 | // At least one argument expression is required, and first should be expression |
genia.likes.science@gmail.com | d2795fb | 2013-07-14 15:48:04 -0700 | [diff] [blame] | 630 | if (arguments.length == 0) { |
genia.likes.science@gmail.com | 18f3bf2 | 2013-06-12 07:45:02 -0700 | [diff] [blame] | 631 | |
genia.likes.science@gmail.com | 2ba3224 | 2013-05-31 03:10:48 -0700 | [diff] [blame] | 632 | alert("Order By Error"); |
| 633 | this._properties["clause"] = null; |
| 634 | return this; |
genia.likes.science@gmail.com | d2795fb | 2013-07-14 15:48:04 -0700 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | var parameters = []; |
| 638 | if (arguments[0] instanceof Array) { |
| 639 | parameters = arguments[0]; |
| 640 | } else { |
| 641 | parameters = arguments; |
| 642 | } |
genia.likes.science@gmail.com | 2ba3224 | 2013-05-31 03:10:48 -0700 | [diff] [blame] | 643 | |
| 644 | var expc = 0; |
| 645 | var expressions = []; |
| 646 | |
genia.likes.science@gmail.com | d2795fb | 2013-07-14 15:48:04 -0700 | [diff] [blame] | 647 | while (expc < parameters.length) { |
genia.likes.science@gmail.com | 2ba3224 | 2013-05-31 03:10:48 -0700 | [diff] [blame] | 648 | |
| 649 | var expression = ""; |
| 650 | |
genia.likes.science@gmail.com | d2795fb | 2013-07-14 15:48:04 -0700 | [diff] [blame] | 651 | if (parameters[expc] instanceof AExpression) { |
| 652 | expression += parameters[expc].val(); |
genia.likes.science@gmail.com | 2ba3224 | 2013-05-31 03:10:48 -0700 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | var next = expc + 1; |
genia.likes.science@gmail.com | d2795fb | 2013-07-14 15:48:04 -0700 | [diff] [blame] | 656 | if (next < parameters.length && (parameters[next] == "asc" || parameters[next] == "desc")) { |
genia.likes.science@gmail.com | 2ba3224 | 2013-05-31 03:10:48 -0700 | [diff] [blame] | 657 | expc++; |
genia.likes.science@gmail.com | d2795fb | 2013-07-14 15:48:04 -0700 | [diff] [blame] | 658 | expression += " " + parameters[expc]; |
genia.likes.science@gmail.com | 2ba3224 | 2013-05-31 03:10:48 -0700 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | expressions.push(expression); |
| 662 | |
| 663 | expc++; |
| 664 | } |
| 665 | |
| 666 | this._properties["clause"] = "order by " + expressions.join(", "); |
| 667 | return this; |
| 668 | } |
| 669 | |
| 670 | OrderbyClause.prototype = Object.create(AQLClause.prototype); |
| 671 | OrderbyClause.prototype.constructor = OrderbyClause; |
| 672 | |
| 673 | |
genia.likes.science@gmail.com | 6e39291 | 2013-05-31 03:46:59 -0700 | [diff] [blame] | 674 | // GroupClause |
| 675 | // |
| 676 | // Grammar: |
| 677 | // GroupClause ::= "group" "by" ( Variable ":=" )? Expression ( "," ( Variable ":=" )? Expression )* ( "decor" Variable ":=" Expression ( "," "decor" Variable ":=" Expression )* )? "with" VariableRef ( "," VariableRef )* |
| 678 | function GroupClause() { |
| 679 | AQLClause.call(this); |
| 680 | |
| 681 | if (arguments.length == 0) { |
| 682 | // TODO Not sure which error to throw for an empty GroupBy but this should fail. |
| 683 | alert("Group Error"); |
| 684 | this._properties["clause"] = null; |
| 685 | return this; |
| 686 | } |
genia.likes.science@gmail.com | d2795fb | 2013-07-14 15:48:04 -0700 | [diff] [blame] | 687 | |
| 688 | var parameters = []; |
| 689 | if (arguments[0] instanceof Array) { |
| 690 | parameters = arguments[0]; |
| 691 | } else { |
| 692 | parameters = arguments; |
| 693 | } |
genia.likes.science@gmail.com | 6e39291 | 2013-05-31 03:46:59 -0700 | [diff] [blame] | 694 | |
| 695 | var expc = 0; |
| 696 | var expressions = []; |
| 697 | var variableRefs = []; |
| 698 | var isDecor = false; |
genia.likes.science@gmail.com | 5f42534 | 2013-06-13 13:51:26 -0700 | [diff] [blame] | 699 | |
genia.likes.science@gmail.com | d2795fb | 2013-07-14 15:48:04 -0700 | [diff] [blame] | 700 | while (expc < parameters.length) { |
genia.likes.science@gmail.com | 6e39291 | 2013-05-31 03:46:59 -0700 | [diff] [blame] | 701 | |
genia.likes.science@gmail.com | d2795fb | 2013-07-14 15:48:04 -0700 | [diff] [blame] | 702 | if (parameters[expc] instanceof AExpression) { |
genia.likes.science@gmail.com | 6e39291 | 2013-05-31 03:46:59 -0700 | [diff] [blame] | 703 | |
| 704 | isDecor = false; |
genia.likes.science@gmail.com | d2795fb | 2013-07-14 15:48:04 -0700 | [diff] [blame] | 705 | expressions.push(parameters[expc].val()); |
genia.likes.science@gmail.com | 6e39291 | 2013-05-31 03:46:59 -0700 | [diff] [blame] | 706 | |
genia.likes.science@gmail.com | d2795fb | 2013-07-14 15:48:04 -0700 | [diff] [blame] | 707 | } else if (typeof parameters[expc] == "string") { |
genia.likes.science@gmail.com | 6e39291 | 2013-05-31 03:46:59 -0700 | [diff] [blame] | 708 | |
| 709 | // Special keywords, decor & with |
genia.likes.science@gmail.com | d2795fb | 2013-07-14 15:48:04 -0700 | [diff] [blame] | 710 | if (parameters[expc] == "decor") { |
genia.likes.science@gmail.com | 6e39291 | 2013-05-31 03:46:59 -0700 | [diff] [blame] | 711 | isDecor = true; |
genia.likes.science@gmail.com | d2795fb | 2013-07-14 15:48:04 -0700 | [diff] [blame] | 712 | } else if (parameters[expc] == "with") { |
genia.likes.science@gmail.com | 6e39291 | 2013-05-31 03:46:59 -0700 | [diff] [blame] | 713 | isDecor = false; |
| 714 | expc++; |
genia.likes.science@gmail.com | d2795fb | 2013-07-14 15:48:04 -0700 | [diff] [blame] | 715 | while (expc < parameters.length) { |
| 716 | variableRefs.push(parameters[expc]); |
genia.likes.science@gmail.com | 6e39291 | 2013-05-31 03:46:59 -0700 | [diff] [blame] | 717 | expc++; |
| 718 | } |
| 719 | |
| 720 | // Variables and variable refs |
| 721 | } else { |
| 722 | |
| 723 | var nextc = expc + 1; |
| 724 | var expression = ""; |
| 725 | |
| 726 | if (isDecor) { |
| 727 | expression += "decor "; |
| 728 | isDecor = false; |
| 729 | } |
| 730 | |
genia.likes.science@gmail.com | d2795fb | 2013-07-14 15:48:04 -0700 | [diff] [blame] | 731 | expression += parameters[expc] + " := " + parameters[nextc].val(); |
genia.likes.science@gmail.com | 6e39291 | 2013-05-31 03:46:59 -0700 | [diff] [blame] | 732 | expressions.push(expression); |
| 733 | expc++; |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | expc++; |
| 738 | } |
| 739 | |
| 740 | this._properties["clause"] = "group by " + expressions.join(", ") + " with " + variableRefs.join(", "); |
| 741 | return this; |
| 742 | } |
| 743 | |
| 744 | GroupClause.prototype = Object.create(AQLClause.prototype); |
| 745 | GroupClause.prototype.constructor = GroupClause; |
| 746 | |
genia.likes.science@gmail.com | 79e603a | 2013-05-31 10:03:23 -0700 | [diff] [blame] | 747 | |
| 748 | // SetStatement |
| 749 | // |
| 750 | // Grammar |
| 751 | // "set" Identifier StringLiteral |
| 752 | function SetStatement (identifier, stringLiteral) { |
| 753 | AExpression.call(this); |
| 754 | |
| 755 | var statement = "set " + identifier + ' "' + stringLiteral + '";'; |
| 756 | |
| 757 | AExpression.prototype.set.call(this, statement); |
| 758 | |
| 759 | return this; |
| 760 | } |
| 761 | |
| 762 | SetStatement.prototype = Object.create(AExpression.prototype); |
| 763 | SetStatement.prototype.constructor = SetStatement; |
genia.likes.science@gmail.com | e34c57b | 2013-05-31 10:15:49 -0700 | [diff] [blame] | 764 | |
| 765 | |
| 766 | // Quantified Expression |
| 767 | // |
| 768 | // Grammar |
| 769 | // QuantifiedExpression ::= ( ( "some" ) | ( "every" ) ) Variable "in" Expression ( "," Variable "in" Expression )* "satisfies" Expression |
| 770 | // |
| 771 | // @param String some/every |
| 772 | // @param [AExpression] |
| 773 | // @param [Aexpression] satisfiesExpression |
| 774 | function QuantifiedExpression (keyword, expressions, satisfiesExpression) { |
| 775 | AExpression.call(this); |
| 776 | |
| 777 | var expression = keyword + " "; |
| 778 | var varsInExpressions = []; |
| 779 | |
| 780 | for (var varInExpression in expressions) { |
| 781 | varsInExpressions.push(varInExpression + " in " + expressions[varInExpression].val()); |
| 782 | } |
| 783 | expression += varsInExpressions.join(", ") + " satisfies " + satisfiesExpression.val(); |
| 784 | |
| 785 | AExpression.prototype.set.call(this, expression); |
| 786 | |
| 787 | return this; |
| 788 | } |
| 789 | |
| 790 | QuantifiedExpression.prototype = Object.create(AExpression.prototype); |
| 791 | QuantifiedExpression.prototype.constructor = QuantifiedExpression; |
genia.likes.science@gmail.com | 64ca88e | 2013-05-31 10:40:39 -0700 | [diff] [blame] | 792 | |
| 793 | QuantifiedExpression.prototype.val = function() { |
| 794 | var value = AExpression.prototype.val.call(this); |
| 795 | return "(" + value + ")"; |
| 796 | }; |