blob: 272cb05b9528ae56979dde122cc7ede384e3c72d [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.com4a5dfe12013-07-05 07:10:00 -070023AsterixDBConnection.prototype.query = function(statements, successFn, mode) {
genia.likes.science@gmail.com3395d5b2013-07-05 00:47:11 -070024
genia.likes.science@gmail.comb91b577a2013-06-13 14:36:06 -070025 if ( typeof statements === 'string') {
26 statements = [ statements ];
27 }
28
genia.likes.science@gmail.com212a6442013-07-13 20:40:11 -070029 var m = typeof mode ? mode : "synchronous";
30
genia.likes.science@gmail.com78593d72013-07-02 06:33:39 -070031 var query = "use dataverse " + this._properties["dataverse"] + ";\n" + statements.join("\n");
genia.likes.science@gmail.comb91b577a2013-06-13 14:36:06 -070032
genia.likes.science@gmail.com212a6442013-07-13 20:40:11 -070033 alert(query);
34
genia.likes.science@gmail.com3395d5b2013-07-05 00:47:11 -070035 this._api(
36 {
genia.likes.science@gmail.comb91b577a2013-06-13 14:36:06 -070037 "query" : query,
genia.likes.science@gmail.com4a5dfe12013-07-05 07:10:00 -070038 "mode" : m
genia.likes.science@gmail.comb91b577a2013-06-13 14:36:06 -070039 },
genia.likes.science@gmail.com3395d5b2013-07-05 00:47:11 -070040 successFn,
41 "http://localhost:19002/query"
42 );
genia.likes.science@gmail.comb91b577a2013-06-13 14:36:06 -070043
44 return this;
45};
46
47
genia.likes.science@gmail.com4a5dfe12013-07-05 07:10:00 -070048AsterixDBConnection.prototype.query_status = function(data, successFn) {
49
50 this._api(
51 data,
52 successFn,
53 "http://localhost:19002/query/status"
54 );
55
56 return this;
57};
58
59
60AsterixDBConnection.prototype.query_result = function(data, successFn) {
61 this._api(
62 data,
63 successFn,
64 "http://localhost:19002/query/result"
65 );
66
67 return this;
68};
69
genia.likes.science@gmail.com6686c452013-07-05 08:39:35 -070070
71AsterixDBConnection.prototype.ddl = function(statements, successFn) {
72 if ( typeof statements === 'string') {
73 statements = [ statements ];
74 }
75
76 this._api(
77 {
78 "ddl" : "use dataverse " + this._properties["dataverse"] + ";\n" + statements.join("\n")
79 },
80 successFn,
81 "http://localhost:19002/ddl"
82 );
83}
84
85
86AsterixDBConnection.prototype.update = function(statements, successFn) {
87 if ( typeof statements === 'string') {
88 statements = [ statements ];
89 }
90
91 this._api(
92 {
93 "statements" : "use dataverse " + this._properties["dataverse"] + ";\n" + statements.join("\n")
94 },
95 successFn,
96 "http://localhost:19002/update"
97 );
98}
99
100
genia.likes.science@gmail.com3395d5b2013-07-05 00:47:11 -0700101AsterixDBConnection.prototype._api = function(json, onSuccess, endpoint) {
102 var success_fn = onSuccess;
103
104 $.ajax({
105 type: 'GET',
106 url: endpoint,
107 data : json,
108 dataType: "json",
109 success: function(data) {
110 success_fn(data);
111 }
112 // TODO error:
113 });
114
115 return this;
116};
117
118
genia.likes.science@gmail.comb91b577a2013-06-13 14:36:06 -0700119// Asterix Expressions
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700120function AExpression () {
genia.likes.science@gmail.com512454d2013-05-28 03:32:20 -0700121 this._properties = {};
122 this._success = function() {};
123
genia.likes.science@gmail.com512454d2013-05-28 03:32:20 -0700124 return this;
125}
126
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700127
128AExpression.prototype.bind = function(options) {
genia.likes.science@gmail.com512454d2013-05-28 03:32:20 -0700129 var options = options || {};
130
genia.likes.science@gmail.com512454d2013-05-28 03:32:20 -0700131 if (options.hasOwnProperty("success")) {
132 this._success = options["success"];
133 }
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700134
135 if (options.hasOwnProperty("return")) {
136 this._properties["return"] = " return " + options["return"].val();
137 }
genia.likes.science@gmail.com512454d2013-05-28 03:32:20 -0700138};
139
genia.likes.science@gmail.com512454d2013-05-28 03:32:20 -0700140
genia.likes.science@gmail.com18f3bf22013-06-12 07:45:02 -0700141AExpression.prototype.run = function(successFn) {
genia.likes.science@gmail.com512454d2013-05-28 03:32:20 -0700142 return this;
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700143};
144
145
146AExpression.prototype.val = function() {
147
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700148 var value = "";
149
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700150 // If there is a dataverse defined, provide it.
151 if (this._properties.hasOwnProperty("dataverse")) {
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700152 value += "use dataverse " + this._properties["dataverse"] + ";\n";
153 };
154
155 if (this._properties.hasOwnProperty("value")) {
156 value += this._properties["value"];
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700157 }
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700158
159 return value;
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700160};
161
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700162// @param expressionValue [String]
163AExpression.prototype.set = function(expressionValue) {
164 this._properties["value"] = expressionValue;
165 return this;
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700166};
167
genia.likes.science@gmail.com73dcc702013-05-28 04:21:28 -0700168
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700169// FunctionExpression
170// Parent: AsterixExpression
171//
172// @param options [Various],
173// @key function [String], a function to be applid to the expression
genia.likes.science@gmail.com18f3bf22013-06-12 07:45:02 -0700174// @key expression [AsterixExpression or AQLClause] an AsterixExpression/Clause to which the fn will be applied
175function FunctionExpression() {
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700176
177 // Initialize superclass
178 AExpression.call(this);
genia.likes.science@gmail.com18f3bf22013-06-12 07:45:02 -0700179
180 this._properties["function"] = "";
181 this._properties["expression"] = new AExpression().set("");
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700182
genia.likes.science@gmail.com18f3bf22013-06-12 07:45:02 -0700183 // Check for fn/expression input
184 if (arguments.length == 2 && typeof arguments[0] == "string" &&
185 (arguments[1] instanceof AExpression || arguments[1] instanceof AQLClause)) {
186
187 this._properties["function"] = arguments[0];
188 this._properties["expression"] = arguments[1];
189
190 }
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700191
192 // Return object
193 return this;
194}
195
196
197FunctionExpression.prototype = Object.create(AExpression.prototype);
198FunctionExpression.prototype.constructor = FunctionExpression;
199
200
genia.likes.science@gmail.com18f3bf22013-06-12 07:45:02 -0700201FunctionExpression.prototype.fn = function(fnName) {
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700202
genia.likes.science@gmail.com18f3bf22013-06-12 07:45:02 -0700203 if (typeof fnName == "string") {
204 this._properties["function"] = fnName;
205 }
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700206
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700207 return this;
208};
209
genia.likes.science@gmail.com18f3bf22013-06-12 07:45:02 -0700210
211FunctionExpression.prototype.expression = function(expression) {
212 if (expression instanceof AExpression || expression instanceof AQLClause) {
213 this._properties["expression"] = expression;
214 }
215
216 return this;
217};
218
219
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700220FunctionExpression.prototype.val = function () {
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700221 return this._properties["function"] + "(" + this._properties["expression"].val() + ")";
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700222};
223
genia.likes.science@gmail.com73dcc702013-05-28 04:21:28 -0700224
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700225// FLWOGRExpression
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700226//
227// FLWOGRExpression ::= ( ForClause | LetClause ) ( Clause )* "return" Expression
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700228function FLWOGRExpression (options) {
229 // Initialize superclass
230 AExpression.call(this);
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700231
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700232 this._properties["clauses"] = [];
genia.likes.science@gmail.com79e603a2013-05-31 10:03:23 -0700233 this._properties["minSize"] = 0;
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700234
235 // Bind options and return
236 this.bind(options);
237 return this;
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700238}
239
240
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700241FLWOGRExpression.prototype = Object.create(AExpression.prototype);
242FLWOGRExpression.prototype.constructor = FLWOGRExpression;
243
244
245FLWOGRExpression.prototype.bind = function(options) {
246 AExpression.prototype.bind.call(this, options);
247
248 var options = options || {};
249
genia.likes.science@gmail.com79e603a2013-05-31 10:03:23 -0700250 if (options instanceof SetStatement) {
251 this._properties["clauses"].push(options);
252 this._properties["minSize"] += 1;
253 }
254
255 if (this._properties["clauses"].length <= this._properties["minSize"]) {
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700256 // Needs to start with for or let clause
257 if (options instanceof ForClause || options instanceof LetClause) {
258 this._properties["clauses"].push(options);
259 }
260 } else {
261 if (options instanceof AQLClause) {
262 this._properties["clauses"].push(options);
263 }
264 }
265
266 return this;
267};
268
269
270FLWOGRExpression.prototype.val = function() {
271 var value = AExpression.prototype.val.call(this);
272
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700273 var clauseValues = [];
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700274 for (var c in this._properties["clauses"]) {
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700275 clauseValues.push(this._properties["clauses"][c].val());
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700276 }
277
genia.likes.science@gmail.com79e603a2013-05-31 10:03:23 -0700278 return value + clauseValues.join("\n");// + ";";
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700279};
280
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700281
genia.likes.science@gmail.com5f425342013-06-13 13:51:26 -0700282FLWOGRExpression.prototype.ReturnClause = function(expression) {
283 return this.bind(new ReturnClause(expression));
284};
285
genia.likes.science@gmail.com73dcc702013-05-28 04:21:28 -0700286// AQLClause
287//
288// Base Clause ::= ForClause | LetClause | WhereClause | OrderbyClause | GroupClause | LimitClause | DistinctClause
289function AQLClause() {
290 this._properties = {};
291 this._properties["clause"] = "";
292}
293
294AQLClause.prototype.val = function() {
295 var value = this._properties["clause"];
genia.likes.science@gmail.com73dcc702013-05-28 04:21:28 -0700296
297 return value;
298};
299
300AQLClause.prototype.bind = function(options) {
genia.likes.science@gmail.com73dcc702013-05-28 04:21:28 -0700301
genia.likes.science@gmail.com18f3bf22013-06-12 07:45:02 -0700302 if (options instanceof AQLClause) {
303 this._properties["clause"] += " " + options.val();
genia.likes.science@gmail.com73dcc702013-05-28 04:21:28 -0700304 }
305
306 return this;
307};
308
genia.likes.science@gmail.com39578302013-05-31 04:42:26 -0700309AQLClause.prototype.set = function(value) {
310 this._properties["clause"] = value;
311 return this;
312};
313
genia.likes.science@gmail.com73dcc702013-05-28 04:21:28 -0700314
315// ForClause
316//
317// Grammar:
318// "for" Variable ( "at" Variable )? "in" ( Expression )
319//
320// @param for_variable [String], REQUIRED, first variable in clause
321// @param at_variable [String], NOT REQUIRED, first variable in clause
322// @param expression [AsterixExpression], REQUIRED, expression to evaluate
genia.likes.science@gmail.com73dcc702013-05-28 04:21:28 -0700323function ForClause(for_variable, at_variable, expression) {
324 AQLClause.call(this);
325
genia.likes.science@gmail.com78593d72013-07-02 06:33:39 -0700326 this._properties["clause"] = "for " + arguments[0];
genia.likes.science@gmail.comf8cca192013-06-13 15:45:53 -0700327
328 if (arguments.length == 3) {
genia.likes.science@gmail.com78593d72013-07-02 06:33:39 -0700329 this._properties["clause"] += " at " + arguments[1];
genia.likes.science@gmail.comf8cca192013-06-13 15:45:53 -0700330 this._properties["clause"] += " in " + arguments[2].val();
331 } else if (arguments.length == 2) {
332 this._properties["clause"] += " in " + arguments[1].val();
genia.likes.science@gmail.com73dcc702013-05-28 04:21:28 -0700333 }
genia.likes.science@gmail.comf8cca192013-06-13 15:45:53 -0700334
genia.likes.science@gmail.com73dcc702013-05-28 04:21:28 -0700335 return this;
336}
337
338ForClause.prototype = Object.create(AQLClause.prototype);
339ForClause.prototype.constructor = ForClause;
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700340
341
342// LetClause
343//
344// Grammar:
345// LetClause ::= "let" Variable ":=" Expression
346//
347// @param let_variable [String]
348// @param expression [AExpression]
349//
350// TODO Vigorous error checking
351function LetClause(let_variable, expression) {
352 AQLClause.call(this);
353
genia.likes.science@gmail.com78593d72013-07-02 06:33:39 -0700354 this._properties["clause"] = "let " + let_variable + " := ";
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700355 this._properties["clause"] += expression.val();
356
357 return this;
358}
359
360LetClause.prototype = Object.create(AQLClause.prototype);
361LetClause.prototype.constructor = LetClause;
362
363
genia.likes.science@gmail.com5ca104c2013-05-29 05:39:26 -0700364// ReturnClause
365//
366// Grammar:
367// return [AQLExpression]
368function ReturnClause(expression) {
369 AQLClause.call(this);
370
371 this._properties["clause"] = "return ";
genia.likes.science@gmail.com5749ef92013-06-12 07:59:25 -0700372
genia.likes.science@gmail.com79e603a2013-05-31 10:03:23 -0700373 if (expression instanceof AExpression || expression instanceof AQLClause) {
genia.likes.science@gmail.com5ca104c2013-05-29 05:39:26 -0700374 this._properties["clause"] += expression.val();
genia.likes.science@gmail.com5749ef92013-06-12 07:59:25 -0700375
376 } else if ( typeof expression == "object" && Object.getPrototypeOf( expression ) === Object.prototype ) {
377
378 // TODO Null object check
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700379
380 this._properties["clause"] += "{";
381 var returnStatements = [];
382 for (returnValue in expression) {
383
384 if (expression[returnValue] instanceof AExpression) {
genia.likes.science@gmail.comfba7cc82013-05-31 04:04:08 -0700385 returnStatements.push('"' + returnValue + '" ' + " : " + expression[returnValue].val());
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700386 } else if (typeof expression[returnValue] == "string") {
genia.likes.science@gmail.comfba7cc82013-05-31 04:04:08 -0700387 returnStatements.push('"' + returnValue + '" ' + " : " + expression[returnValue]);
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700388 }
389 }
390 this._properties["clause"] += returnStatements.join(",\n");
genia.likes.science@gmail.comf7929d82013-06-12 11:30:01 -0700391 this._properties["clause"] += "\n}";
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700392
genia.likes.science@gmail.com5ca104c2013-05-29 05:39:26 -0700393 } else {
genia.likes.science@gmail.com5749ef92013-06-12 07:59:25 -0700394 this._properties["clause"] += new AQLClause().set(expression).val();
genia.likes.science@gmail.com5ca104c2013-05-29 05:39:26 -0700395 }
396
397 return this;
398}
399
genia.likes.science@gmail.com5749ef92013-06-12 07:59:25 -0700400
genia.likes.science@gmail.com5ca104c2013-05-29 05:39:26 -0700401ReturnClause.prototype = Object.create(AQLClause.prototype);
402ReturnClause.prototype.constructor = ReturnClause;
403
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700404
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700405// WhereClause
406//
407// Grammar:
408// ::= "where" Expression
409//
410// @param expression [BooleanExpression], pushes this expression onto the stack
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700411function WhereClause(expression) {
412 AQLClause.call(this);
413
414 this._properties["stack"] = [];
415
416 this.bind(expression);
417
418 return this;
419}
420
421
422WhereClause.prototype = Object.create(AQLClause.prototype);
423WhereClause.prototype.constructor = WhereClause;
424
425
426WhereClause.prototype.bind = function(expression) {
genia.likes.science@gmail.com64ca88e2013-05-31 10:40:39 -0700427 if (expression instanceof AExpression) {
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700428 this._properties["stack"].push(expression);
429 }
430};
431
432
433WhereClause.prototype.val = function() {
434 var value = "where ";
435
436 var count = this._properties["stack"].length - 1;
437 while (count >= 0) {
438 value += this._properties["stack"][count].val() + " ";
439 count -= 1;
440 }
441
442 return value;
genia.likes.science@gmail.com64ca88e2013-05-31 10:40:39 -0700443};
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700444
445
genia.likes.science@gmail.comd2f753e2013-06-12 13:51:03 -0700446WhereClause.prototype.and = function() {
447
448 var andClauses = [];
449 for (var expression in arguments) {
450
451 if (arguments[expression] instanceof AExpression) {
452 andClauses.push(arguments[expression].val());
453 }
454 }
455
456 if (andClauses.length > 0) {
457 this._properties["stack"].push(new AExpression().set(andClauses.join(" and ")));
458 }
459
460 return this;
461};
462
463
464WhereClause.prototype.or = function() {
465 var orClauses = [];
466 for (var expression in arguments) {
467
468 if (arguments[expression] instanceof AExpression) {
469 orClauses.push(arguments[expression].val());
470 }
471 }
472
473 if (andClauses.length > 0) {
474 this._properties["stack"].push(new AExpression().set(orClauses.join(" and ")));
475 }
476
477 return this;
478};
479
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700480// LimitClause
481// Grammar:
482// LimitClause ::= "limit" Expression ( "offset" Expression )?
483//
484// @param limitExpression [REQUIRED, AQLExpression]
485// @param offsetExpression [OPTIONAL, AQLExpression]
486function LimitClause(limitExpression, offsetExpression) {
487
488 AQLClause.call(this);
489
490 // limitExpression required
491 this._properties["clause"] = "limit " + limitExpression.val();
492
493 // Optional: Offset
494 var offset = typeof offsetExpression ? offsetExpression : null;
495 if (offset != null) {
496 this._properties["clause"] += " offset " + offsetExpression.val();
497 }
498
499 return this;
500}
501
502LimitClause.prototype = Object.create(AQLClause.prototype);
503LimitClause.prototype.constructor = LimitClause;
504
505
506// OrderbyClause
507//
508// Grammar:
509// OrderbyClause ::= "order" "by" Expression ( ( "asc" ) | ( "desc" ) )? ( "," Expression ( ( "asc" ) | ( "desc" ) )? )*
510//
511// @params AQLExpressions and asc/desc strings, in any quantity. At least one required.
512function OrderbyClause() {
513
514 AQLClause.call(this);
515
516 // At least one argument expression is required, and first should be expression
517 if (arguments.length == 0 || !(arguments[0] instanceof AExpression)) {
genia.likes.science@gmail.com18f3bf22013-06-12 07:45:02 -0700518
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700519 // TODO Not sure which error to throw for an empty OrderBy but this should fail.
520 alert("Order By Error");
521 this._properties["clause"] = null;
522 return this;
523 }
524
525 var expc = 0;
526 var expressions = [];
527
528 while (expc < arguments.length) {
529
530 var expression = "";
531
532 if (arguments[expc] instanceof AExpression) {
533 expression += arguments[expc].val();
534 }
535
536 var next = expc + 1;
537 if (next < arguments.length && (arguments[next] == "asc" || arguments[next] == "desc")) {
538 expc++;
539 expression += " " + arguments[expc];
540 }
541
542 expressions.push(expression);
543
544 expc++;
545 }
546
547 this._properties["clause"] = "order by " + expressions.join(", ");
548 return this;
549}
550
551OrderbyClause.prototype = Object.create(AQLClause.prototype);
552OrderbyClause.prototype.constructor = OrderbyClause;
553
554
genia.likes.science@gmail.com6e392912013-05-31 03:46:59 -0700555// GroupClause
556//
557// Grammar:
558// GroupClause ::= "group" "by" ( Variable ":=" )? Expression ( "," ( Variable ":=" )? Expression )* ( "decor" Variable ":=" Expression ( "," "decor" Variable ":=" Expression )* )? "with" VariableRef ( "," VariableRef )*
559function GroupClause() {
560 AQLClause.call(this);
561
562 if (arguments.length == 0) {
563 // TODO Not sure which error to throw for an empty GroupBy but this should fail.
564 alert("Group Error");
565 this._properties["clause"] = null;
566 return this;
567 }
568
569 var expc = 0;
570 var expressions = [];
571 var variableRefs = [];
572 var isDecor = false;
genia.likes.science@gmail.com5f425342013-06-13 13:51:26 -0700573
genia.likes.science@gmail.com6e392912013-05-31 03:46:59 -0700574 while (expc < arguments.length) {
575
576 if (arguments[expc] instanceof AExpression) {
577
578 isDecor = false;
579 expressions.push(arguments[expc].val());
580
581 } else if (typeof arguments[expc] == "string") {
582
583 // Special keywords, decor & with
584 if (arguments[expc] == "decor") {
585 isDecor = true;
586 } else if (arguments[expc] == "with") {
587 isDecor = false;
588 expc++;
589 while (expc < arguments.length) {
genia.likes.science@gmail.com212a6442013-07-13 20:40:11 -0700590 variableRefs.push(arguments[expc]);
genia.likes.science@gmail.com6e392912013-05-31 03:46:59 -0700591 expc++;
592 }
593
594 // Variables and variable refs
595 } else {
596
597 var nextc = expc + 1;
598 var expression = "";
599
600 if (isDecor) {
601 expression += "decor ";
602 isDecor = false;
603 }
604
genia.likes.science@gmail.com212a6442013-07-13 20:40:11 -0700605 expression += arguments[expc] + " := " + arguments[nextc].val();
genia.likes.science@gmail.com6e392912013-05-31 03:46:59 -0700606 expressions.push(expression);
607 expc++;
608 }
609 }
610
611 expc++;
612 }
613
614 this._properties["clause"] = "group by " + expressions.join(", ") + " with " + variableRefs.join(", ");
615 return this;
616}
617
618GroupClause.prototype = Object.create(AQLClause.prototype);
619GroupClause.prototype.constructor = GroupClause;
620
genia.likes.science@gmail.com79e603a2013-05-31 10:03:23 -0700621
622// SetStatement
623//
624// Grammar
625// "set" Identifier StringLiteral
626function SetStatement (identifier, stringLiteral) {
627 AExpression.call(this);
628
629 var statement = "set " + identifier + ' "' + stringLiteral + '";';
630
631 AExpression.prototype.set.call(this, statement);
632
633 return this;
634}
635
636SetStatement.prototype = Object.create(AExpression.prototype);
637SetStatement.prototype.constructor = SetStatement;
genia.likes.science@gmail.come34c57b2013-05-31 10:15:49 -0700638
639
640// Quantified Expression
641//
642// Grammar
643// QuantifiedExpression ::= ( ( "some" ) | ( "every" ) ) Variable "in" Expression ( "," Variable "in" Expression )* "satisfies" Expression
644//
645// @param String some/every
646// @param [AExpression]
647// @param [Aexpression] satisfiesExpression
648function QuantifiedExpression (keyword, expressions, satisfiesExpression) {
649 AExpression.call(this);
650
651 var expression = keyword + " ";
652 var varsInExpressions = [];
653
654 for (var varInExpression in expressions) {
655 varsInExpressions.push(varInExpression + " in " + expressions[varInExpression].val());
656 }
657 expression += varsInExpressions.join(", ") + " satisfies " + satisfiesExpression.val();
658
659 AExpression.prototype.set.call(this, expression);
660
661 return this;
662}
663
664QuantifiedExpression.prototype = Object.create(AExpression.prototype);
665QuantifiedExpression.prototype.constructor = QuantifiedExpression;
genia.likes.science@gmail.com64ca88e2013-05-31 10:40:39 -0700666
667QuantifiedExpression.prototype.val = function() {
668 var value = AExpression.prototype.val.call(this);
669 return "(" + value + ")";
670};