blob: eef557b4bb27c79aa509c9a81137563dca4cd690 [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.comd2795fb2013-07-14 15:48:04 -070032
genia.likes.science@gmail.com3395d5b2013-07-05 00:47:11 -070033 this._api(
34 {
genia.likes.science@gmail.comb91b577a2013-06-13 14:36:06 -070035 "query" : query,
genia.likes.science@gmail.com4a5dfe12013-07-05 07:10:00 -070036 "mode" : m
genia.likes.science@gmail.comb91b577a2013-06-13 14:36:06 -070037 },
genia.likes.science@gmail.com3395d5b2013-07-05 00:47:11 -070038 successFn,
39 "http://localhost:19002/query"
40 );
genia.likes.science@gmail.comb91b577a2013-06-13 14:36:06 -070041
42 return this;
43};
44
45
genia.likes.science@gmail.com4a5dfe12013-07-05 07:10:00 -070046AsterixDBConnection.prototype.query_status = function(data, successFn) {
47
48 this._api(
49 data,
50 successFn,
51 "http://localhost:19002/query/status"
52 );
53
54 return this;
55};
56
57
58AsterixDBConnection.prototype.query_result = function(data, successFn) {
59 this._api(
60 data,
61 successFn,
62 "http://localhost:19002/query/result"
63 );
64
65 return this;
66};
67
genia.likes.science@gmail.com6686c452013-07-05 08:39:35 -070068
69AsterixDBConnection.prototype.ddl = function(statements, successFn) {
70 if ( typeof statements === 'string') {
71 statements = [ statements ];
72 }
73
74 this._api(
75 {
76 "ddl" : "use dataverse " + this._properties["dataverse"] + ";\n" + statements.join("\n")
77 },
78 successFn,
79 "http://localhost:19002/ddl"
80 );
81}
82
83
84AsterixDBConnection.prototype.update = function(statements, successFn) {
85 if ( typeof statements === 'string') {
86 statements = [ statements ];
87 }
88
89 this._api(
90 {
91 "statements" : "use dataverse " + this._properties["dataverse"] + ";\n" + statements.join("\n")
92 },
93 successFn,
94 "http://localhost:19002/update"
95 );
96}
97
98
genia.likes.science@gmail.com3395d5b2013-07-05 00:47:11 -070099AsterixDBConnection.prototype._api = function(json, onSuccess, endpoint) {
100 var success_fn = onSuccess;
101
102 $.ajax({
103 type: 'GET',
104 url: endpoint,
105 data : json,
106 dataType: "json",
107 success: function(data) {
108 success_fn(data);
109 }
110 // TODO error:
111 });
112
113 return this;
114};
115
116
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700117function AExpression () {
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700118
genia.likes.science@gmail.com512454d2013-05-28 03:32:20 -0700119 this._properties = {};
120 this._success = function() {};
121
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700122 if (typeof arguments[0] == 'string') {
123 this._properties["value"] = arguments[0];
124 }
125
genia.likes.science@gmail.com512454d2013-05-28 03:32:20 -0700126 return this;
127}
128
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700129
130AExpression.prototype.bind = function(options) {
genia.likes.science@gmail.com512454d2013-05-28 03:32:20 -0700131 var options = options || {};
132
genia.likes.science@gmail.com512454d2013-05-28 03:32:20 -0700133 if (options.hasOwnProperty("success")) {
134 this._success = options["success"];
135 }
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700136
137 if (options.hasOwnProperty("return")) {
138 this._properties["return"] = " return " + options["return"].val();
139 }
genia.likes.science@gmail.com512454d2013-05-28 03:32:20 -0700140};
141
genia.likes.science@gmail.com512454d2013-05-28 03:32:20 -0700142
genia.likes.science@gmail.com18f3bf22013-06-12 07:45:02 -0700143AExpression.prototype.run = function(successFn) {
genia.likes.science@gmail.com512454d2013-05-28 03:32:20 -0700144 return this;
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700145};
146
147
148AExpression.prototype.val = function() {
149
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700150 var value = "";
151
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700152 // If there is a dataverse defined, provide it.
153 if (this._properties.hasOwnProperty("dataverse")) {
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700154 value += "use dataverse " + this._properties["dataverse"] + ";\n";
155 };
156
157 if (this._properties.hasOwnProperty("value")) {
158 value += this._properties["value"];
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700159 }
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700160
161 return value;
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700162};
163
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700164// @param expressionValue [String]
165AExpression.prototype.set = function(expressionValue) {
166 this._properties["value"] = expressionValue;
167 return this;
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700168};
169
genia.likes.science@gmail.com73dcc702013-05-28 04:21:28 -0700170
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700171// FunctionExpression
172// Parent: AsterixExpression
173//
174// @param options [Various],
175// @key function [String], a function to be applid to the expression
genia.likes.science@gmail.com18f3bf22013-06-12 07:45:02 -0700176// @key expression [AsterixExpression or AQLClause] an AsterixExpression/Clause to which the fn will be applied
177function FunctionExpression() {
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700178
179 // Initialize superclass
180 AExpression.call(this);
genia.likes.science@gmail.com18f3bf22013-06-12 07:45:02 -0700181
182 this._properties["function"] = "";
183 this._properties["expression"] = new AExpression().set("");
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700184
genia.likes.science@gmail.com18f3bf22013-06-12 07:45:02 -0700185 // Check for fn/expression input
186 if (arguments.length == 2 && typeof arguments[0] == "string" &&
187 (arguments[1] instanceof AExpression || arguments[1] instanceof AQLClause)) {
188
189 this._properties["function"] = arguments[0];
190 this._properties["expression"] = arguments[1];
191
192 }
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700193
194 // Return object
195 return this;
196}
197
198
199FunctionExpression.prototype = Object.create(AExpression.prototype);
200FunctionExpression.prototype.constructor = FunctionExpression;
201
202
genia.likes.science@gmail.com18f3bf22013-06-12 07:45:02 -0700203FunctionExpression.prototype.fn = function(fnName) {
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700204
genia.likes.science@gmail.com18f3bf22013-06-12 07:45:02 -0700205 if (typeof fnName == "string") {
206 this._properties["function"] = fnName;
207 }
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700208
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700209 return this;
210};
211
genia.likes.science@gmail.com18f3bf22013-06-12 07:45:02 -0700212
213FunctionExpression.prototype.expression = function(expression) {
214 if (expression instanceof AExpression || expression instanceof AQLClause) {
215 this._properties["expression"] = expression;
216 }
217
218 return this;
219};
220
221
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700222FunctionExpression.prototype.val = function () {
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700223 return this._properties["function"] + "(" + this._properties["expression"].val() + ")";
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700224};
225
genia.likes.science@gmail.com73dcc702013-05-28 04:21:28 -0700226
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700227// FLWOGRExpression
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700228//
229// FLWOGRExpression ::= ( ForClause | LetClause ) ( Clause )* "return" Expression
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700230function FLWOGRExpression (options) {
231 // Initialize superclass
232 AExpression.call(this);
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700233
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700234 this._properties["clauses"] = [];
genia.likes.science@gmail.com79e603a2013-05-31 10:03:23 -0700235 this._properties["minSize"] = 0;
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700236
237 // Bind options and return
238 this.bind(options);
239 return this;
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700240}
241
242
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700243FLWOGRExpression.prototype = Object.create(AExpression.prototype);
244FLWOGRExpression.prototype.constructor = FLWOGRExpression;
245
246
247FLWOGRExpression.prototype.bind = function(options) {
248 AExpression.prototype.bind.call(this, options);
249
250 var options = options || {};
251
genia.likes.science@gmail.com79e603a2013-05-31 10:03:23 -0700252 if (options instanceof SetStatement) {
253 this._properties["clauses"].push(options);
254 this._properties["minSize"] += 1;
255 }
256
257 if (this._properties["clauses"].length <= this._properties["minSize"]) {
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700258 // Needs to start with for or let clause
259 if (options instanceof ForClause || options instanceof LetClause) {
260 this._properties["clauses"].push(options);
261 }
262 } else {
263 if (options instanceof AQLClause) {
264 this._properties["clauses"].push(options);
265 }
266 }
267
268 return this;
269};
270
271
272FLWOGRExpression.prototype.val = function() {
273 var value = AExpression.prototype.val.call(this);
274
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700275 var clauseValues = [];
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700276 for (var c in this._properties["clauses"]) {
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700277 clauseValues.push(this._properties["clauses"][c].val());
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700278 }
279
genia.likes.science@gmail.com79e603a2013-05-31 10:03:23 -0700280 return value + clauseValues.join("\n");// + ";";
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700281};
282
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700283// Pretty Expression Shorthand
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700284
genia.likes.science@gmail.com5f425342013-06-13 13:51:26 -0700285FLWOGRExpression.prototype.ReturnClause = function(expression) {
286 return this.bind(new ReturnClause(expression));
287};
288
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700289FLWOGRExpression.prototype.ForClause = function() {
290 return this.bind(new ForClause(Array.prototype.slice.call(arguments)));
291};
292
293FLWOGRExpression.prototype.LetClause = function() {
294 return this.bind(new LetClause(Array.prototype.slice.call(arguments)));
295};
296
297FLWOGRExpression.prototype.WhereClause = function() {
298 return this.bind(new WhereClause(Array.prototype.slice.call(arguments)));
299};
300
301FLWOGRExpression.prototype.and = function() {
302 var args = Array.prototype.slice.call(arguments);
303 args.push(true);
304 return this.bind(new WhereClause().and(args));
305};
306
307FLWOGRExpression.prototype.or = function() {
308 var args = Array.prototype.slice.call(arguments);
309 args.push(true);
310 return this.bind(new WhereClause().or(args));
311};
312
313FLWOGRExpression.prototype.OrderbyClause = function() {
314 return this.bind(new OrderbyClause(Array.prototype.slice.call(arguments)));
315};
316
317
318FLWOGRExpression.prototype.GroupClause = function() {
319 return this.bind(new GroupClause(Array.prototype.slice.call(arguments)));
320};
321
322FLWOGRExpression.prototype.LimitClause = function() {
323 return this.bind(new LimitClause(Array.prototype.slice.call(arguments)));
324};
325
326FLWOGRExpression.prototype.DistinctClause = function() {
327 return this.bind(new DistinctClause(Array.prototype.slice.call(arguments)));
328};
329
330FLWOGRExpression.prototype.AQLClause = function() {
331 return this.bind(new AQLClause(Array.prototype.slice.call(arguments)));
332};
333
334
genia.likes.science@gmail.com73dcc702013-05-28 04:21:28 -0700335// AQLClause
336//
337// Base Clause ::= ForClause | LetClause | WhereClause | OrderbyClause | GroupClause | LimitClause | DistinctClause
338function AQLClause() {
339 this._properties = {};
340 this._properties["clause"] = "";
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700341 this._properties["stack"] = [];
342 if (typeof arguments[0] == 'string') {
343 this._properties["clause"] = arguments[0];
344 }
345 return this;
genia.likes.science@gmail.com73dcc702013-05-28 04:21:28 -0700346}
347
348AQLClause.prototype.val = function() {
349 var value = this._properties["clause"];
genia.likes.science@gmail.com73dcc702013-05-28 04:21:28 -0700350
351 return value;
352};
353
354AQLClause.prototype.bind = function(options) {
genia.likes.science@gmail.com73dcc702013-05-28 04:21:28 -0700355
genia.likes.science@gmail.com18f3bf22013-06-12 07:45:02 -0700356 if (options instanceof AQLClause) {
357 this._properties["clause"] += " " + options.val();
genia.likes.science@gmail.com73dcc702013-05-28 04:21:28 -0700358 }
359
360 return this;
361};
362
genia.likes.science@gmail.com39578302013-05-31 04:42:26 -0700363AQLClause.prototype.set = function(value) {
364 this._properties["clause"] = value;
365 return this;
366};
367
genia.likes.science@gmail.com73dcc702013-05-28 04:21:28 -0700368
369// ForClause
370//
371// Grammar:
372// "for" Variable ( "at" Variable )? "in" ( Expression )
373//
374// @param for_variable [String], REQUIRED, first variable in clause
375// @param at_variable [String], NOT REQUIRED, first variable in clause
376// @param expression [AsterixExpression], REQUIRED, expression to evaluate
genia.likes.science@gmail.com73dcc702013-05-28 04:21:28 -0700377function ForClause(for_variable, at_variable, expression) {
378 AQLClause.call(this);
379
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700380 var parameters = [];
381 if (arguments[0] instanceof Array) {
382 parameters = arguments[0];
383 } else {
384 parameters = arguments;
385 }
386
387 this._properties["clause"] = "for " + parameters[0];
genia.likes.science@gmail.comf8cca192013-06-13 15:45:53 -0700388
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700389 if (parameters.length == 3) {
390 this._properties["clause"] += " at " + parameters[1];
391 this._properties["clause"] += " in " + parameters[2].val();
392 } else if (parameters.length == 2) {
393 this._properties["clause"] += " in " + parameters[1].val();
genia.likes.science@gmail.com73dcc702013-05-28 04:21:28 -0700394 }
genia.likes.science@gmail.comf8cca192013-06-13 15:45:53 -0700395
genia.likes.science@gmail.com73dcc702013-05-28 04:21:28 -0700396 return this;
397}
398
399ForClause.prototype = Object.create(AQLClause.prototype);
400ForClause.prototype.constructor = ForClause;
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700401
402
403// LetClause
404//
405// Grammar:
406// LetClause ::= "let" Variable ":=" Expression
407//
408// @param let_variable [String]
409// @param expression [AExpression]
410//
411// TODO Vigorous error checking
412function LetClause(let_variable, expression) {
413 AQLClause.call(this);
414
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700415 var parameters = [];
416 if (arguments[0] instanceof Array) {
417 parameters = arguments[0];
418 } else {
419 parameters = arguments;
420 }
421
422 this._properties["clause"] = "let " + parameters[0] + " := ";
423 this._properties["clause"] += parameters[1].val();
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700424
425 return this;
426}
427
428LetClause.prototype = Object.create(AQLClause.prototype);
429LetClause.prototype.constructor = LetClause;
430
431
genia.likes.science@gmail.com5ca104c2013-05-29 05:39:26 -0700432// ReturnClause
433//
434// Grammar:
435// return [AQLExpression]
436function ReturnClause(expression) {
437 AQLClause.call(this);
438
439 this._properties["clause"] = "return ";
genia.likes.science@gmail.com5749ef92013-06-12 07:59:25 -0700440
genia.likes.science@gmail.com79e603a2013-05-31 10:03:23 -0700441 if (expression instanceof AExpression || expression instanceof AQLClause) {
genia.likes.science@gmail.com5ca104c2013-05-29 05:39:26 -0700442 this._properties["clause"] += expression.val();
genia.likes.science@gmail.com5749ef92013-06-12 07:59:25 -0700443
444 } else if ( typeof expression == "object" && Object.getPrototypeOf( expression ) === Object.prototype ) {
445
446 // TODO Null object check
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700447
448 this._properties["clause"] += "{";
449 var returnStatements = [];
450 for (returnValue in expression) {
451
452 if (expression[returnValue] instanceof AExpression) {
genia.likes.science@gmail.comfba7cc82013-05-31 04:04:08 -0700453 returnStatements.push('"' + returnValue + '" ' + " : " + expression[returnValue].val());
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700454 } else if (typeof expression[returnValue] == "string") {
genia.likes.science@gmail.comfba7cc82013-05-31 04:04:08 -0700455 returnStatements.push('"' + returnValue + '" ' + " : " + expression[returnValue]);
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700456 }
457 }
458 this._properties["clause"] += returnStatements.join(",\n");
genia.likes.science@gmail.comf7929d82013-06-12 11:30:01 -0700459 this._properties["clause"] += "\n}";
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700460
genia.likes.science@gmail.com5ca104c2013-05-29 05:39:26 -0700461 } else {
genia.likes.science@gmail.com5749ef92013-06-12 07:59:25 -0700462 this._properties["clause"] += new AQLClause().set(expression).val();
genia.likes.science@gmail.com5ca104c2013-05-29 05:39:26 -0700463 }
464
465 return this;
466}
467
genia.likes.science@gmail.com5749ef92013-06-12 07:59:25 -0700468
genia.likes.science@gmail.com5ca104c2013-05-29 05:39:26 -0700469ReturnClause.prototype = Object.create(AQLClause.prototype);
470ReturnClause.prototype.constructor = ReturnClause;
471
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700472
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700473// WhereClause
474//
475// Grammar:
476// ::= "where" Expression
477//
478// @param expression [BooleanExpression], pushes this expression onto the stack
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700479function WhereClause(expression) {
480 AQLClause.call(this);
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700481
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700482 this._properties["stack"] = [];
483
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700484 if (expression instanceof Array) {
485 this.bind(expression[0]);
486 } else {
487 this.bind(expression);
488 }
489
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700490 return this;
491}
492
493
494WhereClause.prototype = Object.create(AQLClause.prototype);
495WhereClause.prototype.constructor = WhereClause;
496
497
498WhereClause.prototype.bind = function(expression) {
genia.likes.science@gmail.com64ca88e2013-05-31 10:40:39 -0700499 if (expression instanceof AExpression) {
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700500 this._properties["stack"].push(expression);
501 }
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700502 return this;
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700503};
504
505
506WhereClause.prototype.val = function() {
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700507 var value = "";
508
509 if (this._properties["stack"].length == 0) {
510 return value;
511 }
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700512
513 var count = this._properties["stack"].length - 1;
514 while (count >= 0) {
515 value += this._properties["stack"][count].val() + " ";
516 count -= 1;
517 }
518
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700519 return "where " + value;
genia.likes.science@gmail.com64ca88e2013-05-31 10:40:39 -0700520};
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700521
522
genia.likes.science@gmail.comd2f753e2013-06-12 13:51:03 -0700523WhereClause.prototype.and = function() {
524
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700525 var parameters = [];
526 if (arguments[0] instanceof Array) {
527 parameters = arguments[0];
528 } else {
529 parameters = arguments;
530 }
531
genia.likes.science@gmail.comd2f753e2013-06-12 13:51:03 -0700532 var andClauses = [];
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700533 for (var expression in parameters) {
genia.likes.science@gmail.comd2f753e2013-06-12 13:51:03 -0700534
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700535 if (parameters[expression] instanceof AExpression) {
536 andClauses.push(parameters[expression].val());
genia.likes.science@gmail.comd2f753e2013-06-12 13:51:03 -0700537 }
538 }
539
540 if (andClauses.length > 0) {
541 this._properties["stack"].push(new AExpression().set(andClauses.join(" and ")));
542 }
543
544 return this;
545};
546
547
548WhereClause.prototype.or = function() {
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700549
550 var parameters = [];
551 if (arguments[0] instanceof Array) {
552 parameters = arguments[0];
553 } else {
554 parameters = arguments;
555 }
556
genia.likes.science@gmail.comd2f753e2013-06-12 13:51:03 -0700557 var orClauses = [];
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700558 for (var expression in parameters) {
genia.likes.science@gmail.comd2f753e2013-06-12 13:51:03 -0700559
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700560 if (parameters[expression] instanceof AExpression) {
561 orClauses.push(parameters[expression].val());
genia.likes.science@gmail.comd2f753e2013-06-12 13:51:03 -0700562 }
563 }
564
565 if (andClauses.length > 0) {
566 this._properties["stack"].push(new AExpression().set(orClauses.join(" and ")));
567 }
568
569 return this;
570};
571
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700572// LimitClause
573// Grammar:
574// LimitClause ::= "limit" Expression ( "offset" Expression )?
575//
576// @param limitExpression [REQUIRED, AQLExpression]
577// @param offsetExpression [OPTIONAL, AQLExpression]
578function LimitClause(limitExpression, offsetExpression) {
579
580 AQLClause.call(this);
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700581
582 var parameters = [];
583 if (arguments[0] instanceof Array) {
584 parameters = arguments[0];
585 } else {
586 parameters = arguments;
587 }
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700588
589 // limitExpression required
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700590 this._properties["clause"] = "limit " + parameters[0].val();
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700591
592 // Optional: Offset
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700593 if (parameters.length == 2) {
594 this._properties["clause"] += " offset " + parameters[1].val();
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700595 }
596
597 return this;
598}
599
600LimitClause.prototype = Object.create(AQLClause.prototype);
601LimitClause.prototype.constructor = LimitClause;
602
603
604// OrderbyClause
605//
606// Grammar:
607// OrderbyClause ::= "order" "by" Expression ( ( "asc" ) | ( "desc" ) )? ( "," Expression ( ( "asc" ) | ( "desc" ) )? )*
608//
609// @params AQLExpressions and asc/desc strings, in any quantity. At least one required.
610function OrderbyClause() {
611
612 AQLClause.call(this);
613
614 // At least one argument expression is required, and first should be expression
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700615 if (arguments.length == 0) {
genia.likes.science@gmail.com18f3bf22013-06-12 07:45:02 -0700616
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700617 alert("Order By Error");
618 this._properties["clause"] = null;
619 return this;
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700620 }
621
622 var parameters = [];
623 if (arguments[0] instanceof Array) {
624 parameters = arguments[0];
625 } else {
626 parameters = arguments;
627 }
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700628
629 var expc = 0;
630 var expressions = [];
631
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700632 while (expc < parameters.length) {
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700633
634 var expression = "";
635
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700636 if (parameters[expc] instanceof AExpression) {
637 expression += parameters[expc].val();
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700638 }
639
640 var next = expc + 1;
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700641 if (next < parameters.length && (parameters[next] == "asc" || parameters[next] == "desc")) {
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700642 expc++;
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700643 expression += " " + parameters[expc];
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700644 }
645
646 expressions.push(expression);
647
648 expc++;
649 }
650
651 this._properties["clause"] = "order by " + expressions.join(", ");
652 return this;
653}
654
655OrderbyClause.prototype = Object.create(AQLClause.prototype);
656OrderbyClause.prototype.constructor = OrderbyClause;
657
658
genia.likes.science@gmail.com6e392912013-05-31 03:46:59 -0700659// GroupClause
660//
661// Grammar:
662// GroupClause ::= "group" "by" ( Variable ":=" )? Expression ( "," ( Variable ":=" )? Expression )* ( "decor" Variable ":=" Expression ( "," "decor" Variable ":=" Expression )* )? "with" VariableRef ( "," VariableRef )*
663function GroupClause() {
664 AQLClause.call(this);
665
666 if (arguments.length == 0) {
667 // TODO Not sure which error to throw for an empty GroupBy but this should fail.
668 alert("Group Error");
669 this._properties["clause"] = null;
670 return this;
671 }
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700672
673 var parameters = [];
674 if (arguments[0] instanceof Array) {
675 parameters = arguments[0];
676 } else {
677 parameters = arguments;
678 }
genia.likes.science@gmail.com6e392912013-05-31 03:46:59 -0700679
680 var expc = 0;
681 var expressions = [];
682 var variableRefs = [];
683 var isDecor = false;
genia.likes.science@gmail.com5f425342013-06-13 13:51:26 -0700684
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700685 while (expc < parameters.length) {
genia.likes.science@gmail.com6e392912013-05-31 03:46:59 -0700686
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700687 if (parameters[expc] instanceof AExpression) {
genia.likes.science@gmail.com6e392912013-05-31 03:46:59 -0700688
689 isDecor = false;
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700690 expressions.push(parameters[expc].val());
genia.likes.science@gmail.com6e392912013-05-31 03:46:59 -0700691
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700692 } else if (typeof parameters[expc] == "string") {
genia.likes.science@gmail.com6e392912013-05-31 03:46:59 -0700693
694 // Special keywords, decor & with
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700695 if (parameters[expc] == "decor") {
genia.likes.science@gmail.com6e392912013-05-31 03:46:59 -0700696 isDecor = true;
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700697 } else if (parameters[expc] == "with") {
genia.likes.science@gmail.com6e392912013-05-31 03:46:59 -0700698 isDecor = false;
699 expc++;
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700700 while (expc < parameters.length) {
701 variableRefs.push(parameters[expc]);
genia.likes.science@gmail.com6e392912013-05-31 03:46:59 -0700702 expc++;
703 }
704
705 // Variables and variable refs
706 } else {
707
708 var nextc = expc + 1;
709 var expression = "";
710
711 if (isDecor) {
712 expression += "decor ";
713 isDecor = false;
714 }
715
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700716 expression += parameters[expc] + " := " + parameters[nextc].val();
genia.likes.science@gmail.com6e392912013-05-31 03:46:59 -0700717 expressions.push(expression);
718 expc++;
719 }
720 }
721
722 expc++;
723 }
724
725 this._properties["clause"] = "group by " + expressions.join(", ") + " with " + variableRefs.join(", ");
726 return this;
727}
728
729GroupClause.prototype = Object.create(AQLClause.prototype);
730GroupClause.prototype.constructor = GroupClause;
731
genia.likes.science@gmail.com79e603a2013-05-31 10:03:23 -0700732
733// SetStatement
734//
735// Grammar
736// "set" Identifier StringLiteral
737function SetStatement (identifier, stringLiteral) {
738 AExpression.call(this);
739
740 var statement = "set " + identifier + ' "' + stringLiteral + '";';
741
742 AExpression.prototype.set.call(this, statement);
743
744 return this;
745}
746
747SetStatement.prototype = Object.create(AExpression.prototype);
748SetStatement.prototype.constructor = SetStatement;
genia.likes.science@gmail.come34c57b2013-05-31 10:15:49 -0700749
750
751// Quantified Expression
752//
753// Grammar
754// QuantifiedExpression ::= ( ( "some" ) | ( "every" ) ) Variable "in" Expression ( "," Variable "in" Expression )* "satisfies" Expression
755//
756// @param String some/every
757// @param [AExpression]
758// @param [Aexpression] satisfiesExpression
759function QuantifiedExpression (keyword, expressions, satisfiesExpression) {
760 AExpression.call(this);
761
762 var expression = keyword + " ";
763 var varsInExpressions = [];
764
765 for (var varInExpression in expressions) {
766 varsInExpressions.push(varInExpression + " in " + expressions[varInExpression].val());
767 }
768 expression += varsInExpressions.join(", ") + " satisfies " + satisfiesExpression.val();
769
770 AExpression.prototype.set.call(this, expression);
771
772 return this;
773}
774
775QuantifiedExpression.prototype = Object.create(AExpression.prototype);
776QuantifiedExpression.prototype.constructor = QuantifiedExpression;
genia.likes.science@gmail.com64ca88e2013-05-31 10:40:39 -0700777
778QuantifiedExpression.prototype.val = function() {
779 var value = AExpression.prototype.val.call(this);
780 return "(" + value + ")";
781};