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