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