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