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