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