blob: f8bed0e872ab01f0ec974130e1cbf1699c1fd2c8 [file] [log] [blame]
Ian Maxon032a1782015-06-30 17:10:51 -07001/*
Ian Maxon928bbd12015-09-14 17:12:48 -07002 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
Ian Maxon032a1782015-06-30 17:10:51 -07009 *
Ian Maxon928bbd12015-09-14 17:12:48 -070010 * http://www.apache.org/licenses/LICENSE-2.0
Ian Maxon032a1782015-06-30 17:10:51 -070011 *
Ian Maxon928bbd12015-09-14 17:12:48 -070012 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
Ian Maxon032a1782015-06-30 17:10:51 -070018 */
19
genia.likes.science@gmail.comb91b577a2013-06-13 14:36:06 -070020function AsterixDBConnection(configuration) {
21 this._properties = {};
22 this._properties["dataverse"] = "";
23 this._properties["mode"] = "synchronous";
24
25 var configuration = arguments || {};
26
27 for (var key in configuration) {
28 this._properties[key] = configuration[key];
29 }
30
31 return this;
32}
33
34
35AsterixDBConnection.prototype.dataverse = function(dataverseName) {
36 this._properties["dataverse"] = dataverseName;
37
38 return this;
39};
40
41
genia.likes.science@gmail.com4a5dfe12013-07-05 07:10:00 -070042AsterixDBConnection.prototype.query = function(statements, successFn, mode) {
genia.likes.science@gmail.com3395d5b2013-07-05 00:47:11 -070043
genia.likes.science@gmail.comb91b577a2013-06-13 14:36:06 -070044 if ( typeof statements === 'string') {
45 statements = [ statements ];
46 }
47
genia.likes.science@gmail.com212a6442013-07-13 20:40:11 -070048 var m = typeof mode ? mode : "synchronous";
49
genia.likes.science@gmail.com78593d72013-07-02 06:33:39 -070050 var query = "use dataverse " + this._properties["dataverse"] + ";\n" + statements.join("\n");
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -070051
genia.likes.science@gmail.com3395d5b2013-07-05 00:47:11 -070052 this._api(
53 {
genia.likes.science@gmail.comb91b577a2013-06-13 14:36:06 -070054 "query" : query,
genia.likes.science@gmail.com4a5dfe12013-07-05 07:10:00 -070055 "mode" : m
genia.likes.science@gmail.comb91b577a2013-06-13 14:36:06 -070056 },
genia.likes.science@gmail.com3395d5b2013-07-05 00:47:11 -070057 successFn,
58 "http://localhost:19002/query"
59 );
genia.likes.science@gmail.comb91b577a2013-06-13 14:36:06 -070060
61 return this;
62};
63
64
genia.likes.science@gmail.com4a5dfe12013-07-05 07:10:00 -070065AsterixDBConnection.prototype.query_status = function(data, successFn) {
66
67 this._api(
68 data,
69 successFn,
70 "http://localhost:19002/query/status"
71 );
72
73 return this;
74};
75
76
77AsterixDBConnection.prototype.query_result = function(data, successFn) {
78 this._api(
79 data,
80 successFn,
81 "http://localhost:19002/query/result"
82 );
83
84 return this;
85};
86
genia.likes.science@gmail.com6686c452013-07-05 08:39:35 -070087
88AsterixDBConnection.prototype.ddl = function(statements, successFn) {
89 if ( typeof statements === 'string') {
90 statements = [ statements ];
91 }
92
93 this._api(
94 {
95 "ddl" : "use dataverse " + this._properties["dataverse"] + ";\n" + statements.join("\n")
96 },
97 successFn,
98 "http://localhost:19002/ddl"
99 );
100}
101
102
103AsterixDBConnection.prototype.update = function(statements, successFn) {
104 if ( typeof statements === 'string') {
105 statements = [ statements ];
106 }
107
108 this._api(
109 {
110 "statements" : "use dataverse " + this._properties["dataverse"] + ";\n" + statements.join("\n")
111 },
112 successFn,
113 "http://localhost:19002/update"
114 );
115}
116
117
genia.likes.science@gmail.com3395d5b2013-07-05 00:47:11 -0700118AsterixDBConnection.prototype._api = function(json, onSuccess, endpoint) {
119 var success_fn = onSuccess;
120
121 $.ajax({
122 type: 'GET',
123 url: endpoint,
124 data : json,
125 dataType: "json",
126 success: function(data) {
127 success_fn(data);
128 }
129 // TODO error:
130 });
131
132 return this;
133};
134
135
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700136function AExpression () {
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700137
genia.likes.science@gmail.com512454d2013-05-28 03:32:20 -0700138 this._properties = {};
139 this._success = function() {};
140
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700141 if (typeof arguments[0] == 'string') {
142 this._properties["value"] = arguments[0];
143 }
144
genia.likes.science@gmail.com512454d2013-05-28 03:32:20 -0700145 return this;
146}
147
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700148
149AExpression.prototype.bind = function(options) {
genia.likes.science@gmail.com512454d2013-05-28 03:32:20 -0700150 var options = options || {};
151
genia.likes.science@gmail.com512454d2013-05-28 03:32:20 -0700152 if (options.hasOwnProperty("success")) {
153 this._success = options["success"];
154 }
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700155
156 if (options.hasOwnProperty("return")) {
157 this._properties["return"] = " return " + options["return"].val();
158 }
genia.likes.science@gmail.com512454d2013-05-28 03:32:20 -0700159};
160
genia.likes.science@gmail.com512454d2013-05-28 03:32:20 -0700161
genia.likes.science@gmail.com18f3bf22013-06-12 07:45:02 -0700162AExpression.prototype.run = function(successFn) {
genia.likes.science@gmail.com512454d2013-05-28 03:32:20 -0700163 return this;
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700164};
165
166
167AExpression.prototype.val = function() {
168
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700169 var value = "";
170
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700171 // If there is a dataverse defined, provide it.
172 if (this._properties.hasOwnProperty("dataverse")) {
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700173 value += "use dataverse " + this._properties["dataverse"] + ";\n";
174 };
175
176 if (this._properties.hasOwnProperty("value")) {
177 value += this._properties["value"];
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700178 }
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700179
180 return value;
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700181};
182
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700183// @param expressionValue [String]
184AExpression.prototype.set = function(expressionValue) {
185 this._properties["value"] = expressionValue;
186 return this;
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700187};
188
genia.likes.science@gmail.com73dcc702013-05-28 04:21:28 -0700189
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700190// FunctionExpression
191// Parent: AsterixExpression
192//
193// @param options [Various],
194// @key function [String], a function to be applid to the expression
genia.likes.science@gmail.com18f3bf22013-06-12 07:45:02 -0700195// @key expression [AsterixExpression or AQLClause] an AsterixExpression/Clause to which the fn will be applied
196function FunctionExpression() {
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700197
198 // Initialize superclass
199 AExpression.call(this);
genia.likes.science@gmail.com18f3bf22013-06-12 07:45:02 -0700200
201 this._properties["function"] = "";
202 this._properties["expression"] = new AExpression().set("");
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700203
genia.likes.science@gmail.com18f3bf22013-06-12 07:45:02 -0700204 // Check for fn/expression input
205 if (arguments.length == 2 && typeof arguments[0] == "string" &&
206 (arguments[1] instanceof AExpression || arguments[1] instanceof AQLClause)) {
207
208 this._properties["function"] = arguments[0];
209 this._properties["expression"] = arguments[1];
210
211 }
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700212
213 // Return object
214 return this;
215}
216
217
218FunctionExpression.prototype = Object.create(AExpression.prototype);
219FunctionExpression.prototype.constructor = FunctionExpression;
220
221
genia.likes.science@gmail.com18f3bf22013-06-12 07:45:02 -0700222FunctionExpression.prototype.fn = function(fnName) {
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700223
genia.likes.science@gmail.com18f3bf22013-06-12 07:45:02 -0700224 if (typeof fnName == "string") {
225 this._properties["function"] = fnName;
226 }
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700227
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700228 return this;
229};
230
genia.likes.science@gmail.com18f3bf22013-06-12 07:45:02 -0700231
232FunctionExpression.prototype.expression = function(expression) {
233 if (expression instanceof AExpression || expression instanceof AQLClause) {
234 this._properties["expression"] = expression;
235 }
236
237 return this;
238};
239
240
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700241FunctionExpression.prototype.val = function () {
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700242 return this._properties["function"] + "(" + this._properties["expression"].val() + ")";
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700243};
244
genia.likes.science@gmail.com73dcc702013-05-28 04:21:28 -0700245
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700246// FLWOGRExpression
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700247//
248// FLWOGRExpression ::= ( ForClause | LetClause ) ( Clause )* "return" Expression
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700249function FLWOGRExpression (options) {
250 // Initialize superclass
251 AExpression.call(this);
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700252
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700253 this._properties["clauses"] = [];
genia.likes.science@gmail.com79e603a2013-05-31 10:03:23 -0700254 this._properties["minSize"] = 0;
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700255
256 // Bind options and return
257 this.bind(options);
258 return this;
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700259}
260
261
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700262FLWOGRExpression.prototype = Object.create(AExpression.prototype);
263FLWOGRExpression.prototype.constructor = FLWOGRExpression;
264
265
266FLWOGRExpression.prototype.bind = function(options) {
267 AExpression.prototype.bind.call(this, options);
268
269 var options = options || {};
270
genia.likes.science@gmail.com79e603a2013-05-31 10:03:23 -0700271 if (options instanceof SetStatement) {
272 this._properties["clauses"].push(options);
273 this._properties["minSize"] += 1;
274 }
275
276 if (this._properties["clauses"].length <= this._properties["minSize"]) {
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700277 // Needs to start with for or let clause
278 if (options instanceof ForClause || options instanceof LetClause) {
279 this._properties["clauses"].push(options);
280 }
281 } else {
282 if (options instanceof AQLClause) {
283 this._properties["clauses"].push(options);
284 }
285 }
286
287 return this;
288};
289
290
291FLWOGRExpression.prototype.val = function() {
292 var value = AExpression.prototype.val.call(this);
293
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700294 var clauseValues = [];
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700295 for (var c in this._properties["clauses"]) {
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700296 clauseValues.push(this._properties["clauses"][c].val());
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700297 }
298
genia.likes.science@gmail.com79e603a2013-05-31 10:03:23 -0700299 return value + clauseValues.join("\n");// + ";";
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700300};
301
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700302// Pretty Expression Shorthand
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700303
genia.likes.science@gmail.com5f425342013-06-13 13:51:26 -0700304FLWOGRExpression.prototype.ReturnClause = function(expression) {
305 return this.bind(new ReturnClause(expression));
306};
307
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700308FLWOGRExpression.prototype.ForClause = function() {
309 return this.bind(new ForClause(Array.prototype.slice.call(arguments)));
310};
311
312FLWOGRExpression.prototype.LetClause = function() {
313 return this.bind(new LetClause(Array.prototype.slice.call(arguments)));
314};
315
316FLWOGRExpression.prototype.WhereClause = function() {
317 return this.bind(new WhereClause(Array.prototype.slice.call(arguments)));
318};
319
320FLWOGRExpression.prototype.and = function() {
321 var args = Array.prototype.slice.call(arguments);
322 args.push(true);
323 return this.bind(new WhereClause().and(args));
324};
325
326FLWOGRExpression.prototype.or = function() {
327 var args = Array.prototype.slice.call(arguments);
328 args.push(true);
329 return this.bind(new WhereClause().or(args));
330};
331
332FLWOGRExpression.prototype.OrderbyClause = function() {
333 return this.bind(new OrderbyClause(Array.prototype.slice.call(arguments)));
334};
335
336
337FLWOGRExpression.prototype.GroupClause = function() {
338 return this.bind(new GroupClause(Array.prototype.slice.call(arguments)));
339};
340
341FLWOGRExpression.prototype.LimitClause = function() {
342 return this.bind(new LimitClause(Array.prototype.slice.call(arguments)));
343};
344
345FLWOGRExpression.prototype.DistinctClause = function() {
346 return this.bind(new DistinctClause(Array.prototype.slice.call(arguments)));
347};
348
349FLWOGRExpression.prototype.AQLClause = function() {
350 return this.bind(new AQLClause(Array.prototype.slice.call(arguments)));
351};
352
353
genia.likes.science@gmail.com73dcc702013-05-28 04:21:28 -0700354// AQLClause
355//
356// Base Clause ::= ForClause | LetClause | WhereClause | OrderbyClause | GroupClause | LimitClause | DistinctClause
357function AQLClause() {
358 this._properties = {};
359 this._properties["clause"] = "";
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700360 this._properties["stack"] = [];
361 if (typeof arguments[0] == 'string') {
362 this._properties["clause"] = arguments[0];
363 }
364 return this;
genia.likes.science@gmail.com73dcc702013-05-28 04:21:28 -0700365}
366
367AQLClause.prototype.val = function() {
368 var value = this._properties["clause"];
genia.likes.science@gmail.com73dcc702013-05-28 04:21:28 -0700369
370 return value;
371};
372
373AQLClause.prototype.bind = function(options) {
genia.likes.science@gmail.com73dcc702013-05-28 04:21:28 -0700374
genia.likes.science@gmail.com18f3bf22013-06-12 07:45:02 -0700375 if (options instanceof AQLClause) {
376 this._properties["clause"] += " " + options.val();
genia.likes.science@gmail.com73dcc702013-05-28 04:21:28 -0700377 }
378
379 return this;
380};
381
genia.likes.science@gmail.com39578302013-05-31 04:42:26 -0700382AQLClause.prototype.set = function(value) {
383 this._properties["clause"] = value;
384 return this;
385};
386
genia.likes.science@gmail.com73dcc702013-05-28 04:21:28 -0700387
388// ForClause
389//
390// Grammar:
391// "for" Variable ( "at" Variable )? "in" ( Expression )
392//
393// @param for_variable [String], REQUIRED, first variable in clause
394// @param at_variable [String], NOT REQUIRED, first variable in clause
395// @param expression [AsterixExpression], REQUIRED, expression to evaluate
genia.likes.science@gmail.com73dcc702013-05-28 04:21:28 -0700396function ForClause(for_variable, at_variable, expression) {
397 AQLClause.call(this);
398
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700399 var parameters = [];
400 if (arguments[0] instanceof Array) {
401 parameters = arguments[0];
402 } else {
403 parameters = arguments;
404 }
405
406 this._properties["clause"] = "for " + parameters[0];
genia.likes.science@gmail.comf8cca192013-06-13 15:45:53 -0700407
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700408 if (parameters.length == 3) {
409 this._properties["clause"] += " at " + parameters[1];
410 this._properties["clause"] += " in " + parameters[2].val();
411 } else if (parameters.length == 2) {
412 this._properties["clause"] += " in " + parameters[1].val();
genia.likes.science@gmail.com73dcc702013-05-28 04:21:28 -0700413 }
genia.likes.science@gmail.comf8cca192013-06-13 15:45:53 -0700414
genia.likes.science@gmail.com73dcc702013-05-28 04:21:28 -0700415 return this;
416}
417
418ForClause.prototype = Object.create(AQLClause.prototype);
419ForClause.prototype.constructor = ForClause;
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700420
421
422// LetClause
423//
424// Grammar:
425// LetClause ::= "let" Variable ":=" Expression
426//
427// @param let_variable [String]
428// @param expression [AExpression]
429//
430// TODO Vigorous error checking
431function LetClause(let_variable, expression) {
432 AQLClause.call(this);
433
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700434 var parameters = [];
435 if (arguments[0] instanceof Array) {
436 parameters = arguments[0];
437 } else {
438 parameters = arguments;
439 }
440
441 this._properties["clause"] = "let " + parameters[0] + " := ";
442 this._properties["clause"] += parameters[1].val();
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700443
444 return this;
445}
446
447LetClause.prototype = Object.create(AQLClause.prototype);
448LetClause.prototype.constructor = LetClause;
449
450
genia.likes.science@gmail.com5ca104c2013-05-29 05:39:26 -0700451// ReturnClause
452//
453// Grammar:
454// return [AQLExpression]
455function ReturnClause(expression) {
456 AQLClause.call(this);
457
458 this._properties["clause"] = "return ";
genia.likes.science@gmail.com5749ef92013-06-12 07:59:25 -0700459
genia.likes.science@gmail.com79e603a2013-05-31 10:03:23 -0700460 if (expression instanceof AExpression || expression instanceof AQLClause) {
genia.likes.science@gmail.com5ca104c2013-05-29 05:39:26 -0700461 this._properties["clause"] += expression.val();
genia.likes.science@gmail.com5749ef92013-06-12 07:59:25 -0700462
463 } else if ( typeof expression == "object" && Object.getPrototypeOf( expression ) === Object.prototype ) {
464
465 // TODO Null object check
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700466
467 this._properties["clause"] += "{";
468 var returnStatements = [];
469 for (returnValue in expression) {
470
471 if (expression[returnValue] instanceof AExpression) {
genia.likes.science@gmail.comfba7cc82013-05-31 04:04:08 -0700472 returnStatements.push('"' + returnValue + '" ' + " : " + expression[returnValue].val());
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700473 } else if (typeof expression[returnValue] == "string") {
genia.likes.science@gmail.comfba7cc82013-05-31 04:04:08 -0700474 returnStatements.push('"' + returnValue + '" ' + " : " + expression[returnValue]);
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700475 }
476 }
477 this._properties["clause"] += returnStatements.join(",\n");
genia.likes.science@gmail.comf7929d82013-06-12 11:30:01 -0700478 this._properties["clause"] += "\n}";
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700479
genia.likes.science@gmail.com5ca104c2013-05-29 05:39:26 -0700480 } else {
genia.likes.science@gmail.com5749ef92013-06-12 07:59:25 -0700481 this._properties["clause"] += new AQLClause().set(expression).val();
genia.likes.science@gmail.com5ca104c2013-05-29 05:39:26 -0700482 }
483
484 return this;
485}
486
genia.likes.science@gmail.com5749ef92013-06-12 07:59:25 -0700487
genia.likes.science@gmail.com5ca104c2013-05-29 05:39:26 -0700488ReturnClause.prototype = Object.create(AQLClause.prototype);
489ReturnClause.prototype.constructor = ReturnClause;
490
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700491
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700492// WhereClause
493//
494// Grammar:
495// ::= "where" Expression
496//
497// @param expression [BooleanExpression], pushes this expression onto the stack
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700498function WhereClause(expression) {
499 AQLClause.call(this);
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700500
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700501 this._properties["stack"] = [];
502
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700503 if (expression instanceof Array) {
504 this.bind(expression[0]);
505 } else {
506 this.bind(expression);
507 }
508
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700509 return this;
510}
511
512
513WhereClause.prototype = Object.create(AQLClause.prototype);
514WhereClause.prototype.constructor = WhereClause;
515
516
517WhereClause.prototype.bind = function(expression) {
genia.likes.science@gmail.com64ca88e2013-05-31 10:40:39 -0700518 if (expression instanceof AExpression) {
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700519 this._properties["stack"].push(expression);
520 }
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700521 return this;
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700522};
523
524
525WhereClause.prototype.val = function() {
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700526 var value = "";
527
528 if (this._properties["stack"].length == 0) {
529 return value;
530 }
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700531
532 var count = this._properties["stack"].length - 1;
533 while (count >= 0) {
534 value += this._properties["stack"][count].val() + " ";
535 count -= 1;
536 }
537
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700538 return "where " + value;
genia.likes.science@gmail.com64ca88e2013-05-31 10:40:39 -0700539};
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700540
541
genia.likes.science@gmail.comd2f753e2013-06-12 13:51:03 -0700542WhereClause.prototype.and = function() {
543
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700544 var parameters = [];
545 if (arguments[0] instanceof Array) {
546 parameters = arguments[0];
547 } else {
548 parameters = arguments;
549 }
550
genia.likes.science@gmail.comd2f753e2013-06-12 13:51:03 -0700551 var andClauses = [];
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700552 for (var expression in parameters) {
genia.likes.science@gmail.comd2f753e2013-06-12 13:51:03 -0700553
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700554 if (parameters[expression] instanceof AExpression) {
555 andClauses.push(parameters[expression].val());
genia.likes.science@gmail.comd2f753e2013-06-12 13:51:03 -0700556 }
557 }
558
559 if (andClauses.length > 0) {
560 this._properties["stack"].push(new AExpression().set(andClauses.join(" and ")));
561 }
562
563 return this;
564};
565
566
567WhereClause.prototype.or = function() {
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700568
569 var parameters = [];
570 if (arguments[0] instanceof Array) {
571 parameters = arguments[0];
572 } else {
573 parameters = arguments;
574 }
575
genia.likes.science@gmail.comd2f753e2013-06-12 13:51:03 -0700576 var orClauses = [];
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700577 for (var expression in parameters) {
genia.likes.science@gmail.comd2f753e2013-06-12 13:51:03 -0700578
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700579 if (parameters[expression] instanceof AExpression) {
580 orClauses.push(parameters[expression].val());
genia.likes.science@gmail.comd2f753e2013-06-12 13:51:03 -0700581 }
582 }
583
584 if (andClauses.length > 0) {
585 this._properties["stack"].push(new AExpression().set(orClauses.join(" and ")));
586 }
587
588 return this;
589};
590
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700591// LimitClause
592// Grammar:
593// LimitClause ::= "limit" Expression ( "offset" Expression )?
594//
595// @param limitExpression [REQUIRED, AQLExpression]
596// @param offsetExpression [OPTIONAL, AQLExpression]
597function LimitClause(limitExpression, offsetExpression) {
598
599 AQLClause.call(this);
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700600
601 var parameters = [];
602 if (arguments[0] instanceof Array) {
603 parameters = arguments[0];
604 } else {
605 parameters = arguments;
606 }
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700607
608 // limitExpression required
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700609 this._properties["clause"] = "limit " + parameters[0].val();
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700610
611 // Optional: Offset
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700612 if (parameters.length == 2) {
613 this._properties["clause"] += " offset " + parameters[1].val();
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700614 }
615
616 return this;
617}
618
619LimitClause.prototype = Object.create(AQLClause.prototype);
620LimitClause.prototype.constructor = LimitClause;
621
622
623// OrderbyClause
624//
625// Grammar:
626// OrderbyClause ::= "order" "by" Expression ( ( "asc" ) | ( "desc" ) )? ( "," Expression ( ( "asc" ) | ( "desc" ) )? )*
627//
628// @params AQLExpressions and asc/desc strings, in any quantity. At least one required.
629function OrderbyClause() {
630
631 AQLClause.call(this);
632
633 // At least one argument expression is required, and first should be expression
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700634 if (arguments.length == 0) {
genia.likes.science@gmail.com18f3bf22013-06-12 07:45:02 -0700635
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700636 alert("Order By Error");
637 this._properties["clause"] = null;
638 return this;
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700639 }
640
641 var parameters = [];
642 if (arguments[0] instanceof Array) {
643 parameters = arguments[0];
644 } else {
645 parameters = arguments;
646 }
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700647
648 var expc = 0;
649 var expressions = [];
650
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700651 while (expc < parameters.length) {
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700652
653 var expression = "";
654
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700655 if (parameters[expc] instanceof AExpression) {
656 expression += parameters[expc].val();
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700657 }
658
659 var next = expc + 1;
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700660 if (next < parameters.length && (parameters[next] == "asc" || parameters[next] == "desc")) {
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700661 expc++;
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700662 expression += " " + parameters[expc];
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700663 }
664
665 expressions.push(expression);
666
667 expc++;
668 }
669
670 this._properties["clause"] = "order by " + expressions.join(", ");
671 return this;
672}
673
674OrderbyClause.prototype = Object.create(AQLClause.prototype);
675OrderbyClause.prototype.constructor = OrderbyClause;
676
677
genia.likes.science@gmail.com6e392912013-05-31 03:46:59 -0700678// GroupClause
679//
680// Grammar:
681// GroupClause ::= "group" "by" ( Variable ":=" )? Expression ( "," ( Variable ":=" )? Expression )* ( "decor" Variable ":=" Expression ( "," "decor" Variable ":=" Expression )* )? "with" VariableRef ( "," VariableRef )*
682function GroupClause() {
683 AQLClause.call(this);
684
685 if (arguments.length == 0) {
686 // TODO Not sure which error to throw for an empty GroupBy but this should fail.
687 alert("Group Error");
688 this._properties["clause"] = null;
689 return this;
690 }
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700691
692 var parameters = [];
693 if (arguments[0] instanceof Array) {
694 parameters = arguments[0];
695 } else {
696 parameters = arguments;
697 }
genia.likes.science@gmail.com6e392912013-05-31 03:46:59 -0700698
699 var expc = 0;
700 var expressions = [];
701 var variableRefs = [];
702 var isDecor = false;
genia.likes.science@gmail.com5f425342013-06-13 13:51:26 -0700703
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700704 while (expc < parameters.length) {
genia.likes.science@gmail.com6e392912013-05-31 03:46:59 -0700705
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700706 if (parameters[expc] instanceof AExpression) {
genia.likes.science@gmail.com6e392912013-05-31 03:46:59 -0700707
708 isDecor = false;
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700709 expressions.push(parameters[expc].val());
genia.likes.science@gmail.com6e392912013-05-31 03:46:59 -0700710
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700711 } else if (typeof parameters[expc] == "string") {
genia.likes.science@gmail.com6e392912013-05-31 03:46:59 -0700712
713 // Special keywords, decor & with
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700714 if (parameters[expc] == "decor") {
genia.likes.science@gmail.com6e392912013-05-31 03:46:59 -0700715 isDecor = true;
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700716 } else if (parameters[expc] == "with") {
genia.likes.science@gmail.com6e392912013-05-31 03:46:59 -0700717 isDecor = false;
718 expc++;
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700719 while (expc < parameters.length) {
720 variableRefs.push(parameters[expc]);
genia.likes.science@gmail.com6e392912013-05-31 03:46:59 -0700721 expc++;
722 }
723
724 // Variables and variable refs
725 } else {
726
727 var nextc = expc + 1;
728 var expression = "";
729
730 if (isDecor) {
731 expression += "decor ";
732 isDecor = false;
733 }
734
genia.likes.science@gmail.comd2795fb2013-07-14 15:48:04 -0700735 expression += parameters[expc] + " := " + parameters[nextc].val();
genia.likes.science@gmail.com6e392912013-05-31 03:46:59 -0700736 expressions.push(expression);
737 expc++;
738 }
739 }
740
741 expc++;
742 }
743
744 this._properties["clause"] = "group by " + expressions.join(", ") + " with " + variableRefs.join(", ");
745 return this;
746}
747
748GroupClause.prototype = Object.create(AQLClause.prototype);
749GroupClause.prototype.constructor = GroupClause;
750
genia.likes.science@gmail.com79e603a2013-05-31 10:03:23 -0700751
752// SetStatement
753//
754// Grammar
755// "set" Identifier StringLiteral
756function SetStatement (identifier, stringLiteral) {
757 AExpression.call(this);
758
759 var statement = "set " + identifier + ' "' + stringLiteral + '";';
760
761 AExpression.prototype.set.call(this, statement);
762
763 return this;
764}
765
766SetStatement.prototype = Object.create(AExpression.prototype);
767SetStatement.prototype.constructor = SetStatement;
genia.likes.science@gmail.come34c57b2013-05-31 10:15:49 -0700768
769
770// Quantified Expression
771//
772// Grammar
773// QuantifiedExpression ::= ( ( "some" ) | ( "every" ) ) Variable "in" Expression ( "," Variable "in" Expression )* "satisfies" Expression
774//
775// @param String some/every
776// @param [AExpression]
777// @param [Aexpression] satisfiesExpression
778function QuantifiedExpression (keyword, expressions, satisfiesExpression) {
779 AExpression.call(this);
780
781 var expression = keyword + " ";
782 var varsInExpressions = [];
783
784 for (var varInExpression in expressions) {
785 varsInExpressions.push(varInExpression + " in " + expressions[varInExpression].val());
786 }
787 expression += varsInExpressions.join(", ") + " satisfies " + satisfiesExpression.val();
788
789 AExpression.prototype.set.call(this, expression);
790
791 return this;
792}
793
794QuantifiedExpression.prototype = Object.create(AExpression.prototype);
795QuantifiedExpression.prototype.constructor = QuantifiedExpression;
genia.likes.science@gmail.com64ca88e2013-05-31 10:40:39 -0700796
797QuantifiedExpression.prototype.val = function() {
798 var value = AExpression.prototype.val.call(this);
799 return "(" + value + ")";
800};