blob: e4bd564e9cc11c44676ff5e838773fad00a0c877 [file] [log] [blame]
genia.likes.science@gmail.comb91b577a2013-06-13 14:36:06 -07001function 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
16AsterixDBConnection.prototype.dataverse = function(dataverseName) {
17 this._properties["dataverse"] = dataverseName;
18
19 return this;
20};
21
22
genia.likes.science@gmail.com3395d5b2013-07-05 00:47:11 -070023AsterixDBConnection.prototype.query = function(statements, successFn) {
24
genia.likes.science@gmail.comb91b577a2013-06-13 14:36:06 -070025 if ( typeof statements === 'string') {
26 statements = [ statements ];
27 }
28
genia.likes.science@gmail.com78593d72013-07-02 06:33:39 -070029 var query = "use dataverse " + this._properties["dataverse"] + ";\n" + statements.join("\n");
genia.likes.science@gmail.comb91b577a2013-06-13 14:36:06 -070030 var mode = this._properties["mode"];
31
genia.likes.science@gmail.com3395d5b2013-07-05 00:47:11 -070032 this._api(
33 {
genia.likes.science@gmail.comb91b577a2013-06-13 14:36:06 -070034 "query" : query,
genia.likes.science@gmail.com3395d5b2013-07-05 00:47:11 -070035 "mode" : mode
genia.likes.science@gmail.comb91b577a2013-06-13 14:36:06 -070036 },
genia.likes.science@gmail.com3395d5b2013-07-05 00:47:11 -070037 successFn,
38 "http://localhost:19002/query"
39 );
genia.likes.science@gmail.comb91b577a2013-06-13 14:36:06 -070040
41 return this;
42};
43
44
genia.likes.science@gmail.com3395d5b2013-07-05 00:47:11 -070045AsterixDBConnection.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.comb91b577a2013-06-13 14:36:06 -070063// Asterix Expressions
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -070064function AExpression () {
genia.likes.science@gmail.com512454d2013-05-28 03:32:20 -070065 this._properties = {};
66 this._success = function() {};
67
genia.likes.science@gmail.com512454d2013-05-28 03:32:20 -070068 return this;
69}
70
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -070071
72AExpression.prototype.bind = function(options) {
genia.likes.science@gmail.com512454d2013-05-28 03:32:20 -070073 var options = options || {};
74
genia.likes.science@gmail.com512454d2013-05-28 03:32:20 -070075 if (options.hasOwnProperty("success")) {
76 this._success = options["success"];
77 }
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -070078
79 if (options.hasOwnProperty("return")) {
80 this._properties["return"] = " return " + options["return"].val();
81 }
genia.likes.science@gmail.com512454d2013-05-28 03:32:20 -070082};
83
genia.likes.science@gmail.com512454d2013-05-28 03:32:20 -070084
genia.likes.science@gmail.com18f3bf22013-06-12 07:45:02 -070085AExpression.prototype.run = function(successFn) {
86 var success_fn = successFn;
genia.likes.science@gmail.com5a7c2d72013-06-06 00:54:28 -070087
genia.likes.science@gmail.com512454d2013-05-28 03:32:20 -070088 $.ajax({
89 type : 'GET',
genia.likes.science@gmail.comfc5f5092013-06-12 00:49:46 -070090 url : "http://localhost:19002/query",
genia.likes.science@gmail.com18f3bf22013-06-12 07:45:02 -070091 data : {"query" : "use dataverse TinySocial;\n" + this.val()},
genia.likes.science@gmail.com512454d2013-05-28 03:32:20 -070092 dataType : "json",
93 success : function(data) {
94 success_fn(data);
genia.likes.science@gmail.com5f425342013-06-13 13:51:26 -070095 },
96 error: function(r) {
97 //alert(JSON.stringify(r));
genia.likes.science@gmail.com1d825142013-06-06 08:01:31 -070098 }
genia.likes.science@gmail.com1d825142013-06-06 08:01:31 -070099 });
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.com5a7c2d72013-06-06 00:54:28 -0700110 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.com1d825142013-06-06 08:01:31 -0700129 },
130 "error": function (xhr, ajaxOptions, thrownError) {
131 alert("AJAX ERROR " + thrownError);
genia.likes.science@gmail.com512454d2013-05-28 03:32:20 -0700132 }
genia.likes.science@gmail.com1d825142013-06-06 08:01:31 -0700133 });*/
genia.likes.science@gmail.com512454d2013-05-28 03:32:20 -0700134
135 return this;
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700136};
137
138
139AExpression.prototype.val = function() {
140
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700141 var value = "";
142
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700143 // If there is a dataverse defined, provide it.
144 if (this._properties.hasOwnProperty("dataverse")) {
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700145 value += "use dataverse " + this._properties["dataverse"] + ";\n";
146 };
147
148 if (this._properties.hasOwnProperty("value")) {
149 value += this._properties["value"];
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700150 }
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700151
152 return value;
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700153};
154
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700155// @param expressionValue [String]
156AExpression.prototype.set = function(expressionValue) {
157 this._properties["value"] = expressionValue;
158 return this;
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700159};
160
genia.likes.science@gmail.com73dcc702013-05-28 04:21:28 -0700161
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700162// 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.com18f3bf22013-06-12 07:45:02 -0700167// @key expression [AsterixExpression or AQLClause] an AsterixExpression/Clause to which the fn will be applied
168function FunctionExpression() {
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700169
170 // Initialize superclass
171 AExpression.call(this);
genia.likes.science@gmail.com18f3bf22013-06-12 07:45:02 -0700172
173 this._properties["function"] = "";
174 this._properties["expression"] = new AExpression().set("");
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700175
genia.likes.science@gmail.com18f3bf22013-06-12 07:45:02 -0700176 // 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.com90d08722013-05-28 04:40:12 -0700184
185 // Return object
186 return this;
187}
188
189
190FunctionExpression.prototype = Object.create(AExpression.prototype);
191FunctionExpression.prototype.constructor = FunctionExpression;
192
193
genia.likes.science@gmail.com18f3bf22013-06-12 07:45:02 -0700194FunctionExpression.prototype.fn = function(fnName) {
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700195
genia.likes.science@gmail.com18f3bf22013-06-12 07:45:02 -0700196 if (typeof fnName == "string") {
197 this._properties["function"] = fnName;
198 }
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700199
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700200 return this;
201};
202
genia.likes.science@gmail.com18f3bf22013-06-12 07:45:02 -0700203
204FunctionExpression.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.com90d08722013-05-28 04:40:12 -0700213FunctionExpression.prototype.val = function () {
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700214 return this._properties["function"] + "(" + this._properties["expression"].val() + ")";
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700215};
216
genia.likes.science@gmail.com73dcc702013-05-28 04:21:28 -0700217
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700218// FLWOGRExpression
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700219//
220// FLWOGRExpression ::= ( ForClause | LetClause ) ( Clause )* "return" Expression
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700221function FLWOGRExpression (options) {
222 // Initialize superclass
223 AExpression.call(this);
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700224
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700225 this._properties["clauses"] = [];
genia.likes.science@gmail.com79e603a2013-05-31 10:03:23 -0700226 this._properties["minSize"] = 0;
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700227
228 // Bind options and return
229 this.bind(options);
230 return this;
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700231}
232
233
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700234FLWOGRExpression.prototype = Object.create(AExpression.prototype);
235FLWOGRExpression.prototype.constructor = FLWOGRExpression;
236
237
238FLWOGRExpression.prototype.bind = function(options) {
239 AExpression.prototype.bind.call(this, options);
240
241 var options = options || {};
242
genia.likes.science@gmail.com79e603a2013-05-31 10:03:23 -0700243 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.com38612632013-05-28 13:11:52 -0700249 // 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
263FLWOGRExpression.prototype.val = function() {
264 var value = AExpression.prototype.val.call(this);
265
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700266 var clauseValues = [];
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700267 for (var c in this._properties["clauses"]) {
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700268 clauseValues.push(this._properties["clauses"][c].val());
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700269 }
270
genia.likes.science@gmail.com79e603a2013-05-31 10:03:23 -0700271 return value + clauseValues.join("\n");// + ";";
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700272};
273
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700274
genia.likes.science@gmail.com5f425342013-06-13 13:51:26 -0700275FLWOGRExpression.prototype.ReturnClause = function(expression) {
276 return this.bind(new ReturnClause(expression));
277};
278
genia.likes.science@gmail.com73dcc702013-05-28 04:21:28 -0700279// AQLClause
280//
281// Base Clause ::= ForClause | LetClause | WhereClause | OrderbyClause | GroupClause | LimitClause | DistinctClause
282function AQLClause() {
283 this._properties = {};
284 this._properties["clause"] = "";
285}
286
287AQLClause.prototype.val = function() {
288 var value = this._properties["clause"];
genia.likes.science@gmail.com73dcc702013-05-28 04:21:28 -0700289
290 return value;
291};
292
293AQLClause.prototype.bind = function(options) {
genia.likes.science@gmail.com73dcc702013-05-28 04:21:28 -0700294
genia.likes.science@gmail.com18f3bf22013-06-12 07:45:02 -0700295 if (options instanceof AQLClause) {
296 this._properties["clause"] += " " + options.val();
genia.likes.science@gmail.com73dcc702013-05-28 04:21:28 -0700297 }
298
299 return this;
300};
301
genia.likes.science@gmail.com39578302013-05-31 04:42:26 -0700302AQLClause.prototype.set = function(value) {
303 this._properties["clause"] = value;
304 return this;
305};
306
genia.likes.science@gmail.com73dcc702013-05-28 04:21:28 -0700307
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.com73dcc702013-05-28 04:21:28 -0700316function ForClause(for_variable, at_variable, expression) {
317 AQLClause.call(this);
318
genia.likes.science@gmail.com78593d72013-07-02 06:33:39 -0700319 this._properties["clause"] = "for " + arguments[0];
genia.likes.science@gmail.comf8cca192013-06-13 15:45:53 -0700320
321 if (arguments.length == 3) {
genia.likes.science@gmail.com78593d72013-07-02 06:33:39 -0700322 this._properties["clause"] += " at " + arguments[1];
genia.likes.science@gmail.comf8cca192013-06-13 15:45:53 -0700323 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.com73dcc702013-05-28 04:21:28 -0700326 }
genia.likes.science@gmail.comf8cca192013-06-13 15:45:53 -0700327
genia.likes.science@gmail.com73dcc702013-05-28 04:21:28 -0700328 return this;
329}
330
331ForClause.prototype = Object.create(AQLClause.prototype);
332ForClause.prototype.constructor = ForClause;
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700333
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
344function LetClause(let_variable, expression) {
345 AQLClause.call(this);
346
genia.likes.science@gmail.com78593d72013-07-02 06:33:39 -0700347 this._properties["clause"] = "let " + let_variable + " := ";
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700348 this._properties["clause"] += expression.val();
349
350 return this;
351}
352
353LetClause.prototype = Object.create(AQLClause.prototype);
354LetClause.prototype.constructor = LetClause;
355
356
genia.likes.science@gmail.com5ca104c2013-05-29 05:39:26 -0700357// ReturnClause
358//
359// Grammar:
360// return [AQLExpression]
361function ReturnClause(expression) {
362 AQLClause.call(this);
363
364 this._properties["clause"] = "return ";
genia.likes.science@gmail.com5749ef92013-06-12 07:59:25 -0700365
genia.likes.science@gmail.com79e603a2013-05-31 10:03:23 -0700366 if (expression instanceof AExpression || expression instanceof AQLClause) {
genia.likes.science@gmail.com5ca104c2013-05-29 05:39:26 -0700367 this._properties["clause"] += expression.val();
genia.likes.science@gmail.com5749ef92013-06-12 07:59:25 -0700368
369 } else if ( typeof expression == "object" && Object.getPrototypeOf( expression ) === Object.prototype ) {
370
371 // TODO Null object check
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700372
373 this._properties["clause"] += "{";
374 var returnStatements = [];
375 for (returnValue in expression) {
376
377 if (expression[returnValue] instanceof AExpression) {
genia.likes.science@gmail.comfba7cc82013-05-31 04:04:08 -0700378 returnStatements.push('"' + returnValue + '" ' + " : " + expression[returnValue].val());
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700379 } else if (typeof expression[returnValue] == "string") {
genia.likes.science@gmail.comfba7cc82013-05-31 04:04:08 -0700380 returnStatements.push('"' + returnValue + '" ' + " : " + expression[returnValue]);
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700381 }
382 }
383 this._properties["clause"] += returnStatements.join(",\n");
genia.likes.science@gmail.comf7929d82013-06-12 11:30:01 -0700384 this._properties["clause"] += "\n}";
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700385
genia.likes.science@gmail.com5ca104c2013-05-29 05:39:26 -0700386 } else {
genia.likes.science@gmail.com5749ef92013-06-12 07:59:25 -0700387 this._properties["clause"] += new AQLClause().set(expression).val();
genia.likes.science@gmail.com5ca104c2013-05-29 05:39:26 -0700388 }
389
390 return this;
391}
392
genia.likes.science@gmail.com5749ef92013-06-12 07:59:25 -0700393
genia.likes.science@gmail.com5ca104c2013-05-29 05:39:26 -0700394ReturnClause.prototype = Object.create(AQLClause.prototype);
395ReturnClause.prototype.constructor = ReturnClause;
396
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700397
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700398// WhereClause
399//
400// Grammar:
401// ::= "where" Expression
402//
403// @param expression [BooleanExpression], pushes this expression onto the stack
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700404function WhereClause(expression) {
405 AQLClause.call(this);
406
407 this._properties["stack"] = [];
408
409 this.bind(expression);
410
411 return this;
412}
413
414
415WhereClause.prototype = Object.create(AQLClause.prototype);
416WhereClause.prototype.constructor = WhereClause;
417
418
419WhereClause.prototype.bind = function(expression) {
genia.likes.science@gmail.com64ca88e2013-05-31 10:40:39 -0700420 if (expression instanceof AExpression) {
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700421 this._properties["stack"].push(expression);
422 }
423};
424
425
426WhereClause.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.com64ca88e2013-05-31 10:40:39 -0700436};
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700437
438
genia.likes.science@gmail.comd2f753e2013-06-12 13:51:03 -0700439WhereClause.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
457WhereClause.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.com2ba32242013-05-31 03:10:48 -0700473// LimitClause
474// Grammar:
475// LimitClause ::= "limit" Expression ( "offset" Expression )?
476//
477// @param limitExpression [REQUIRED, AQLExpression]
478// @param offsetExpression [OPTIONAL, AQLExpression]
479function 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
495LimitClause.prototype = Object.create(AQLClause.prototype);
496LimitClause.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.
505function 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.com18f3bf22013-06-12 07:45:02 -0700511
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700512 // 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
544OrderbyClause.prototype = Object.create(AQLClause.prototype);
545OrderbyClause.prototype.constructor = OrderbyClause;
546
547
genia.likes.science@gmail.com6e392912013-05-31 03:46:59 -0700548// GroupClause
549//
550// Grammar:
551// GroupClause ::= "group" "by" ( Variable ":=" )? Expression ( "," ( Variable ":=" )? Expression )* ( "decor" Variable ":=" Expression ( "," "decor" Variable ":=" Expression )* )? "with" VariableRef ( "," VariableRef )*
552function 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.com5f425342013-06-13 13:51:26 -0700566
genia.likes.science@gmail.com6e392912013-05-31 03:46:59 -0700567 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
611GroupClause.prototype = Object.create(AQLClause.prototype);
612GroupClause.prototype.constructor = GroupClause;
613
genia.likes.science@gmail.com79e603a2013-05-31 10:03:23 -0700614
615// SetStatement
616//
617// Grammar
618// "set" Identifier StringLiteral
619function 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
629SetStatement.prototype = Object.create(AExpression.prototype);
630SetStatement.prototype.constructor = SetStatement;
genia.likes.science@gmail.come34c57b2013-05-31 10:15:49 -0700631
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
641function 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
657QuantifiedExpression.prototype = Object.create(AExpression.prototype);
658QuantifiedExpression.prototype.constructor = QuantifiedExpression;
genia.likes.science@gmail.com64ca88e2013-05-31 10:40:39 -0700659
660QuantifiedExpression.prototype.val = function() {
661 var value = AExpression.prototype.val.call(this);
662 return "(" + value + ")";
663};