blob: ac7e26c051a007fbc9f2b86edbbf7520b15d8d58 [file] [log] [blame]
Eugenia Gabrielova12de0302013-10-18 02:25:39 -07001/**
2* Asterix SDK - Beta Version
3* @author Eugenia Gabrielov <genia.likes.science@gmail.com>
4*
5* This is a Javascript helper file for generating AQL queries for AsterixDB (https://code.google.com/p/asterixdb/)
6*/
7
8/**
9* AsterixDBConnection
10*
11* This is a handler for connections to a local AsterixDB REST API Endpoint.
12* This initialization takes as input a configuraiton object, and initializes
13* same basic functionality.
14*/
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -070015function AsterixDBConnection(configuration) {
Eugenia Gabrielovadbd50a42013-10-19 00:30:27 -070016 // Initialize AsterixDBConnection properties
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -070017 this._properties = {};
Eugenia Gabrielovadbd50a42013-10-19 00:30:27 -070018
19 // Set dataverse as null for now, this needs to be set by the user.
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -070020 this._properties["dataverse"] = "";
Eugenia Gabrielovadbd50a42013-10-19 00:30:27 -070021
22 // By default, we will wait for calls to the REST API to complete. The query method
23 // sends a different setting when executed asynchronously. Calls that do not specify a mode
24 // will be executed synchronously.
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -070025 this._properties["mode"] = "synchronous";
Eugenia Gabrielovadbd50a42013-10-19 00:30:27 -070026
27 // These are the default error behaviors for Asterix and ajax errors, respectively.
28 // They can be overridden by calling initializing your AsterixDBConnection like so:
29 // adb = new AsterixDBConnection({
30 // "error" : function(data) {
31 // // override here...
32 // });
33 // and similarly for ajax_error, just pass in the configuration as a json object.
34 this._properties["error"] = function(data) {
35 alert("Asterix REST API Error:\n" + data["error-code"][0] + "\n" + data["error-code"][1]);
36 };
37
38 this._properties["ajax_error"] = function(message) {
39 alert("[Ajax Error]\n" + message);
40 };
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -070041
Eugenia Gabrielovadbd50a42013-10-19 00:30:27 -070042 // This is the default path to the local Asterix REST API. Can be overwritten for remote configurations
43 // or for demo setup purposes (such as with a proxy handler with Python or PHP.
44 this._properties["endpoint_root"] = "http://localhost:19002/";
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -070045
Eugenia Gabrielovadbd50a42013-10-19 00:30:27 -070046 // If we have passed in a configuration, we will update the internal properties
47 // using that configuration. You can do things such as include a new endpoint_root,
48 // a new error function, a new dataverse, etc. You can even store extra info.
49 //
50 // NOTE Long-term, this should have more strict limits.
51 var configuration = configuration || {};
52
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -070053 for (var key in configuration) {
54 this._properties[key] = configuration[key];
55 }
56
57 return this;
58}
59
60
Eugenia Gabrielova12de0302013-10-18 02:25:39 -070061/**
62* dataverse
63*
64* Sets dataverse for execution for the AsterixDBConnection.
65*/
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -070066AsterixDBConnection.prototype.dataverse = function(dataverseName) {
67 this._properties["dataverse"] = dataverseName;
68
69 return this;
70};
71
72
Eugenia Gabrielova12de0302013-10-18 02:25:39 -070073/**
74* query (http://asterix.ics.uci.edu/documentation/api.html#QueryApi)
75*
76* @param statements, statements of an AQL query
77* @param successFn, a function to execute if this query is run successfully
78* @param mode, a string either "synchronous" or "asynchronous", depending on preferred
79* execution mode.
80*/
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -070081AsterixDBConnection.prototype.query = function(statements, successFn, mode) {
82
83 if ( typeof statements === 'string') {
84 statements = [ statements ];
85 }
86
87 var m = typeof mode ? mode : "synchronous";
Eugenia Gabrielova12de0302013-10-18 02:25:39 -070088
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -070089 var query = "use dataverse " + this._properties["dataverse"] + ";\n" + statements.join("\n");
90
91 this._api(
92 {
93 "query" : query,
94 "mode" : m
95 },
96 successFn,
97 "query"
98 );
99
100 return this;
101};
102
Eugenia Gabrielova12de0302013-10-18 02:25:39 -0700103/**
104* query_status (http://asterix.ics.uci.edu/documentation/api.html#QueryStatusApi)
105*
106* @param handle, a json object of the form {"handle" : handleObject}, where
107* the handle object is an opaque handle previously returned
108* from an asynchronous call.
109* @param successFn, a function to call on successful execution of this API call.
110*/
111AsterixDBConnection.prototype.query_status = function(handle, successFn) {
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700112 this._api(
Eugenia Gabrielova12de0302013-10-18 02:25:39 -0700113 handle,
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700114 successFn,
115 "query/status"
116 );
117
118 return this;
119};
120
121
Eugenia Gabrielova12de0302013-10-18 02:25:39 -0700122/**
123* query_result (http://asterix.ics.uci.edu/documentation/api.html#AsynchronousResultApi)
124*
125* handle, a json object of the form {"handle" : handleObject}, where
126* the handle object is an opaque handle previously returned
127* from an asynchronous call.
128* successFn, a function to call on successful execution of this API call.
129*/
130AsterixDBConnection.prototype.query_result = function(handle, successFn) {
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700131 this._api(
Eugenia Gabrielova12de0302013-10-18 02:25:39 -0700132 handle,
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700133 successFn,
134 "query/result"
135 );
136
137 return this;
138};
139
140
Eugenia Gabrielova12de0302013-10-18 02:25:39 -0700141/**
142* ddl (http://asterix.ics.uci.edu/documentation/api.html#DdlApi)
143*
144* @param statements, statements to run through ddl api
145* @param successFn, a function to execute if they are successful
146*/
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700147AsterixDBConnection.prototype.ddl = function(statements, successFn) {
148 if ( typeof statements === 'string') {
149 statements = [ statements ];
150 }
151
152 this._api(
153 {
154 "ddl" : "use dataverse " + this._properties["dataverse"] + ";\n" + statements.join("\n")
155 },
156 successFn,
157 "ddl"
158 );
159}
160
161
Eugenia Gabrielova12de0302013-10-18 02:25:39 -0700162/**
163* update (http://asterix.ics.uci.edu/documentation/api.html#UpdateApi)
164*
165* @param statements, statement(s) for an update API call
166* @param successFn, a function to run if this is executed successfully.
167*
168* This is an AsterixDBConnection handler for the update API. It passes statements provided
169* to the internal API endpoint handler.
170*/
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700171AsterixDBConnection.prototype.update = function(statements, successFn) {
172 if ( typeof statements === 'string') {
173 statements = [ statements ];
174 }
175
176 this._api(
177 {
178 "statements" : "use dataverse " + this._properties["dataverse"] + ";\n" + statements.join("\n")
179 },
180 successFn,
181 "update"
182 );
183}
184
185
Eugenia Gabrielova12de0302013-10-18 02:25:39 -0700186/**
187* meta
188* @param statements, a string or a list of strings representing an Asterix query object
189* @param successFn, a function to execute if call succeeds
190*
191* Queries without a dataverse. This is a work-around for an Asterix REST API behavior
192* that sometiems throws an error. This is handy for Asterix Metadata queries.
193*/
194AsterixDBConnection.prototype.meta = function(statements, successFn) {
195
196 if ( typeof statements === 'string') {
197 statements = [ statements ];
198 }
199
200 var query = statements.join("\n");
201
202 this._api(
203 {
204 "query" : query,
205 "mode" : "synchronous"
206 },
207 successFn,
208 "query"
209 );
210
211 return this;
212}
213
214
215/**
216* _api
217*
218* @param json, the data to be passed with the request
219* @param onSuccess, the success function to be run if this succeeds
220* @param endpoint, a string representing one of the Asterix API endpoints
221*
222* Documentation of endpoints is here:
223* http://asterix.ics.uci.edu/documentation/api.html
224*
225* This is treated as an internal method for making the actual call to the API.
Eugenia Gabrielova12de0302013-10-18 02:25:39 -0700226*/
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700227AsterixDBConnection.prototype._api = function(json, onSuccess, endpoint) {
Eugenia Gabrielovadbd50a42013-10-19 00:30:27 -0700228
229 // The success function is called if the response is successful and returns data,
230 // or is just OK.
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700231 var success_fn = onSuccess;
Eugenia Gabrielovadbd50a42013-10-19 00:30:27 -0700232
233 // This is the error function. Called if something breaks either on the Asterix side
234 // or in the Ajax call.
Eugenia Gabrielova12de0302013-10-18 02:25:39 -0700235 var error_fn = this._properties["error"];
Eugenia Gabrielovadbd50a42013-10-19 00:30:27 -0700236 var ajax_error_fn = this._properties["ajax_error"];
237
238 // This is the target endpoint from the REST api, called as a string.
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700239 var endpoint_url = this._properties["endpoint_root"] + endpoint;
240
Eugenia Gabrielovadbd50a42013-10-19 00:30:27 -0700241 // This SDK does not rely on jQuery, but utilizes its Ajax capabilities when present.
242 if (window.jQuery) {
243 $.ajax({
244
245 // The Asterix API does not accept post requests.
246 type : 'GET',
247
248 // This is the endpoint url provided by combining the default
249 // or reconfigured endpoint root along with the appropriate api endpoint
250 // such as "query" or "update".
251 url : endpoint_url,
252
253 // This is the data in the format specified on the API documentation.
254 data : json,
255
256 // We send out the json datatype to make sure our data is parsed correctly.
257 dataType : "json",
258
259 // The success option calls a function on success, which in this case means
260 // something was returned from the API. However, this does not mean the call succeeded
261 // on the REST API side, it just means we got something back. This also contains the
262 // error return codes, which need to be handled before we call th success function.
263 success : function(data) {
Eugenia Gabrielovaf9fcd712013-10-20 02:37:35 -0700264
Eugenia Gabrielovadbd50a42013-10-19 00:30:27 -0700265 // Check Asterix Response for errors
266 // See http://asterix.ics.uci.edu/documentation/api.html#ErrorCodes
267 if (data["error-code"]) {
268 error_fn(data);
269
270 // Otherwise, run our provided success function
271 } else {
272 success_fn(data);
273 }
274 },
275
276 // This is the function that gets called if there is an ajax-related (non-Asterix)
277 // error. Network errors, empty response bodies, syntax errors, and a number of others
278 // can pop up.
279 error : function(data) {
Eugenia Gabrielovaf9fcd712013-10-20 02:37:35 -0700280
Eugenia Gabrielovadbd50a42013-10-19 00:30:27 -0700281 // Some of the Asterix API endpoints return empty responses on success.
282 // However, the ajax function treats these as errors while reporting a
283 // 200 OK code with no payload. So we will check for that, otherwise
284 // alert of an error. An example response is as follows:
285 // {"readyState":4,"responseText":"","status":200,"statusText":"OK"}
286 if (data["status"] == 200 && data["responseText"] == "") {
287 success_fn(data);
288 } else {
Eugenia Gabrielovaf9fcd712013-10-20 02:37:35 -0700289 alert("[Ajax Error]\n" + JSON.stringify(data));
Eugenia Gabrielovadbd50a42013-10-19 00:30:27 -0700290 }
291 }
292 });
Eugenia Gabrielova3d7bfe52013-10-28 02:36:44 -0700293
Eugenia Gabrielovadbd50a42013-10-19 00:30:27 -0700294 } else {
Eugenia Gabrielova3d7bfe52013-10-28 02:36:44 -0700295
296 // First, we encode the parameters of the query to create a new url.
297 api_endpoint = endpoint_url + "?" + Object.keys(json).map(function(k) {
298 return encodeURIComponent(k) + '=' + encodeURIComponent(json[k])
299 }).join('&');
300
301 // Now, create an XMLHttp object to carry our request. We will call the
302 // UI callback function on ready.
Eugenia Gabrielovadbd50a42013-10-19 00:30:27 -0700303 var xmlhttp;
Eugenia Gabrielova3d7bfe52013-10-28 02:36:44 -0700304 xmlhttp = new XMLHttpRequest();
305 xmlhttp.open("GET", endpoint_url, true);
306 xmlhttp.send(null);
307
308 xmlhttp.onreadystatechange = function(){
309 if (xmlhttp.readyState == 4) {
310 if (xmlhttp.status === 200) {
311 alert(xmlhttp.responseText);
312 //success.call(null, xmlHttp.responseText);
313 } else {
314 //error.call(null, xmlHttp.responseText);
315 }
316 } else {
317 // Still processing
318 }
319 };
Eugenia Gabrielovadbd50a42013-10-19 00:30:27 -0700320 }
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700321 return this;
322};
323
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700324// Asterix Expressions - Base
325function AExpression () {
326
327 this._properties = {};
328 this._success = function() {};
329
330 if (arguments.length == 1) {
331 this._properties["value"] = arguments[0];
332 }
333
334 return this;
335}
336
337
338AExpression.prototype.bind = function(options) {
339 var options = options || {};
340
341 if (options.hasOwnProperty("success")) {
342 this._success = options["success"];
343 }
344
345 if (options.hasOwnProperty("return")) {
346 this._properties["return"] = " return " + options["return"].val();
347 }
348};
349
350
351AExpression.prototype.run = function(successFn) {
352 return this;
353};
354
355
356AExpression.prototype.val = function() {
357
358 var value = "";
359
360 // If there is a dataverse defined, provide it.
361 if (this._properties.hasOwnProperty("dataverse")) {
362 value += "use dataverse " + this._properties["dataverse"] + ";\n";
363 };
364
365 if (this._properties.hasOwnProperty("value")) {
366 value += this._properties["value"].toString();
367 }
368
369 return value;
370};
371
372
373// @param expressionValue [String]
374AExpression.prototype.set = function(expressionValue) {
375 this._properties["value"] = expressionValue;
376 return this;
377};
378
379
380// AQL Statements
381// SingleStatement ::= DataverseDeclaration
382// | FunctionDeclaration
383// | CreateStatement
384// | DropStatement
385// | LoadStatement
386// | SetStatement
387// | InsertStatement
388// | DeleteStatement
389// | Query
390function InsertStatement(quantifiedName, query) {
391 AExpression.call(this);
392
393 var innerQuery = "";
394 if (query instanceof AExpression) {
395 innerQuery = query.val();
396 } else if (typeof query == "object" && Object.getPrototypeOf( query ) === Object.prototype ) {
397
398 var insertStatements = [];
399 for (querykey in query) {
400 if (query[querykey] instanceof AExpression) {
401 insertStatements.push('"' + querykey + '" : ' + query[querykey].val());
402 } else if (typeof query[querykey] == "string") {
403 insertStatements.push('"' + querykey + '" : ' + query[querykey]);
404 } else {
405 insertStatements.push('"' + querykey + '" : ' + query[querykey].toString());
406 }
407 }
408
409 innerQuery = "{" + insertStatements.join(', ') + "}";
410 }
411
412 var statement = "insert into dataset " + quantifiedName + "(" + innerQuery + ");";
413
414 AExpression.prototype.set.call(this, statement);
415
416 return this;
417}
418
419InsertStatement.prototype = Object.create(AExpression.prototype);
420InsertStatement.prototype.constructor = InsertStatement;
421
422
423// Delete Statement
424// DeleteStatement ::= "delete" Variable "from" "dataset" QualifiedName ( "where" Expression )?
425function DeleteStatement (variable, quantifiedName, whereExpression) {
426 AExpression.call(this);
427
428 var statement = "delete " + variable + " from dataset " + quantifiedName;
429
430 if (whereExpression instanceof AExpression) {
431 statement += " where " + whereExpression.val();
432 }
433
434 AExpression.prototype.set.call(this, statement);
435
436 return this;
437}
438
439DeleteStatement.prototype = Object.create(AExpression.prototype);
440DeleteStatement.prototype.constructor = DeleteStatement;
441
442// SetStatement
443//
444// Grammar
445// "set" Identifier StringLiteral
446function SetStatement (identifier, stringLiteral) {
447 AExpression.call(this);
448
449 var statement = "set " + identifier + ' "' + stringLiteral + '";';
450
451 AExpression.prototype.set.call(this, statement);
452
453 return this;
454}
455
456SetStatement.prototype = Object.create(AExpression.prototype);
457SetStatement.prototype.constructor = SetStatement;
458
459
460// Other Expressions
461
462// FunctionExpression
463// Parent: AsterixExpression
464//
465// @param options [Various],
466// @key function [String], a function to be applid to the expression
467// @key expression [AsterixExpression or AQLClause] an AsterixExpression/Clause to which the fn will be applied
468function FunctionExpression() {
469
470 // Initialize superclass
471 AExpression.call(this);
472
473 this._properties["function"] = "";
474 this._properties["expressions"] = [];
475
476 // Check for fn/expression input
477 if (arguments.length >= 2 && typeof arguments[0] == "string") {
478
479 this._properties["function"] = arguments[0];
480
481 for (i = 1; i < arguments.length; i++) {
482 if (arguments[i] instanceof AExpression || arguments[i] instanceof AQLClause) {
483 this._properties["expressions"].push(arguments[i]);
484 } else {
485 this._properties["expressions"].push(new AExpression(arguments[i]));
486 }
487 }
488 }
489
490 // Return FunctionCallExpression object
491 return this;
492}
493
494
495FunctionExpression.prototype = Object.create(AExpression.prototype);
496FunctionExpression.prototype.constructor = FunctionExpression;
497
498
499FunctionExpression.prototype.val = function () {
500 var fn_args = [];
501 for (var i = 0; i < this._properties["expressions"].length; i++) {
502 fn_args.push(this._properties["expressions"][i].val());
503 }
504
505 return this._properties["function"] + "(" + fn_args.join(", ") + ")";
506};
507
508
509// FLWOGRExpression
510//
511// FLWOGRExpression ::= ( ForClause | LetClause ) ( Clause )* "return" Expression
512function FLWOGRExpression (options) {
513 // Initialize superclass
514 AExpression.call(this);
515
516 this._properties["clauses"] = [];
517 this._properties["minSize"] = 0;
518
519 // Bind options and return
520 this.bind(options);
521 return this;
522}
523
524
525FLWOGRExpression.prototype = Object.create(AExpression.prototype);
526FLWOGRExpression.prototype.constructor = FLWOGRExpression;
527
528
529FLWOGRExpression.prototype.bind = function(options) {
530 AExpression.prototype.bind.call(this, options);
531
532 var options = options || {};
533
534 if (options instanceof SetStatement) {
535 this._properties["clauses"].push(options);
536 this._properties["minSize"] += 1;
537 }
538
539 if (this._properties["clauses"].length <= this._properties["minSize"]) {
540 // Needs to start with for or let clause
541 if (options instanceof ForClause || options instanceof LetClause) {
542 this._properties["clauses"].push(options);
543 }
544 } else {
545 if (options instanceof AQLClause) {
546 this._properties["clauses"].push(options);
547 }
548 }
549
550 return this;
551};
552
553
554FLWOGRExpression.prototype.val = function() {
555 var value = AExpression.prototype.val.call(this);
556
557 var clauseValues = [];
558 for (var c in this._properties["clauses"]) {
559 clauseValues.push(this._properties["clauses"][c].val());
560 }
561
562 return value + clauseValues.join("\n");// + ";";
563};
564
565// Pretty Expression Shorthand
566
567FLWOGRExpression.prototype.ReturnClause = function(expression) {
568 return this.bind(new ReturnClause(expression));
569};
570
571FLWOGRExpression.prototype.ForClause = function() {
572 return this.bind(new ForClause(Array.prototype.slice.call(arguments)));
573};
574
575FLWOGRExpression.prototype.LetClause = function() {
576 return this.bind(new LetClause(Array.prototype.slice.call(arguments)));
577};
578
579FLWOGRExpression.prototype.WhereClause = function() {
580 return this.bind(new WhereClause(Array.prototype.slice.call(arguments)));
581};
582
583FLWOGRExpression.prototype.and = function() {
584 var args = Array.prototype.slice.call(arguments);
585 args.push(true);
586 return this.bind(new WhereClause().and(args));
587};
588
589FLWOGRExpression.prototype.or = function() {
590 var args = Array.prototype.slice.call(arguments);
591 args.push(true);
592 return this.bind(new WhereClause().or(args));
593};
594
595FLWOGRExpression.prototype.OrderbyClause = function() {
596 return this.bind(new OrderbyClause(Array.prototype.slice.call(arguments)));
597};
598
599
600FLWOGRExpression.prototype.GroupClause = function() {
601 return this.bind(new GroupClause(Array.prototype.slice.call(arguments)));
602};
603
604FLWOGRExpression.prototype.LimitClause = function() {
605 return this.bind(new LimitClause(Array.prototype.slice.call(arguments)));
606};
607
608FLWOGRExpression.prototype.DistinctClause = function() {
609 return this.bind(new DistinctClause(Array.prototype.slice.call(arguments)));
610};
611
612FLWOGRExpression.prototype.AQLClause = function() {
613 return this.bind(new AQLClause(Array.prototype.slice.call(arguments)));
614};
615
616
617// AQLClause
618//
619// Base Clause ::= ForClause | LetClause | WhereClause | OrderbyClause | GroupClause | LimitClause | DistinctClause
620function AQLClause() {
621 this._properties = {};
622 this._properties["clause"] = "";
623 this._properties["stack"] = [];
624 if (typeof arguments[0] == 'string') {
625 this._properties["clause"] = arguments[0];
626 }
627 return this;
628}
629
630AQLClause.prototype.val = function() {
631 var value = this._properties["clause"];
632
633 return value;
634};
635
636AQLClause.prototype.bind = function(options) {
637
638 if (options instanceof AQLClause) {
639 this._properties["clause"] += " " + options.val();
640 }
641
642 return this;
643};
644
645AQLClause.prototype.set = function(value) {
646 this._properties["clause"] = value;
647 return this;
648};
649
650
651// ForClause
652//
653// Grammar:
654// "for" Variable ( "at" Variable )? "in" ( Expression )
655//
656// @param for_variable [String], REQUIRED, first variable in clause
657// @param at_variable [String], NOT REQUIRED, first variable in clause
658// @param expression [AsterixExpression], REQUIRED, expression to evaluate
659function ForClause(for_variable, at_variable, expression) {
660 AQLClause.call(this);
661
662 var parameters = [];
663 if (arguments[0] instanceof Array) {
664 parameters = arguments[0];
665 } else {
666 parameters = arguments;
667 }
668
669 this._properties["clause"] = "for " + parameters[0];
670
671 if (parameters.length == 3) {
672 this._properties["clause"] += " at " + parameters[1];
673 this._properties["clause"] += " in " + parameters[2].val();
674 } else if (parameters.length == 2) {
675 this._properties["clause"] += " in " + parameters[1].val();
676 }
677
678 return this;
679}
680
681ForClause.prototype = Object.create(AQLClause.prototype);
682ForClause.prototype.constructor = ForClause;
683
684
685// LetClause
686//
687// Grammar:
688// LetClause ::= "let" Variable ":=" Expression
689//
690// @param let_variable [String]
691// @param expression [AExpression]
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700692function LetClause(let_variable, expression) {
693 AQLClause.call(this);
694
695 var parameters = [];
696 if (arguments[0] instanceof Array) {
697 parameters = arguments[0];
698 } else {
699 parameters = arguments;
700 }
701
702 this._properties["clause"] = "let " + parameters[0] + " := ";
703 this._properties["clause"] += parameters[1].val();
704
705 return this;
706}
707
708LetClause.prototype = Object.create(AQLClause.prototype);
709LetClause.prototype.constructor = LetClause;
710
711
712// ReturnClause
713//
714// Grammar:
715// return [AQLExpression]
716function ReturnClause(expression) {
717 AQLClause.call(this);
718
719 this._properties["clause"] = "return ";
720
721 if (expression instanceof AExpression || expression instanceof AQLClause) {
722 this._properties["clause"] += expression.val();
723
724 } else if ( typeof expression == "object" && Object.getPrototypeOf( expression ) === Object.prototype ) {
725
726 this._properties["clause"] += "\n{\n";
727 var returnStatements = [];
728 for (returnValue in expression) {
729
730 if (expression[returnValue] instanceof AExpression) {
731 returnStatements.push('"' + returnValue + '" ' + " : " + expression[returnValue].val());
732 } else if (typeof expression[returnValue] == "string") {
733 returnStatements.push('"' + returnValue + '" ' + " : " + expression[returnValue]);
734 }
735 }
736 this._properties["clause"] += returnStatements.join(",\n");
737 this._properties["clause"] += "\n}";
738
739 } else {
740 this._properties["clause"] += new AQLClause().set(expression).val();
741 }
742
743 return this;
744}
745
746
747ReturnClause.prototype = Object.create(AQLClause.prototype);
748ReturnClause.prototype.constructor = ReturnClause;
749
750
751// WhereClause
752//
753// Grammar:
754// ::= "where" Expression
755//
756// @param expression [BooleanExpression], pushes this expression onto the stack
757function WhereClause(expression) {
758 AQLClause.call(this);
759
760 this._properties["stack"] = [];
761
762 if (expression instanceof Array) {
763 this.bind(expression[0]);
764 } else {
765 this.bind(expression);
766 }
767
768 return this;
769}
770
771
772WhereClause.prototype = Object.create(AQLClause.prototype);
773WhereClause.prototype.constructor = WhereClause;
774
775
776WhereClause.prototype.bind = function(expression) {
777 if (expression instanceof AExpression) {
778 this._properties["stack"].push(expression);
779 }
780 return this;
781};
782
783
784WhereClause.prototype.val = function() {
785 var value = "";
786
787 if (this._properties["stack"].length == 0) {
788 return value;
789 }
790
791 var count = this._properties["stack"].length - 1;
792 while (count >= 0) {
793 value += this._properties["stack"][count].val() + " ";
794 count -= 1;
795 }
796
797 return "where " + value;
798};
799
800
801WhereClause.prototype.and = function() {
802
803 var parameters = [];
804 if (arguments[0] instanceof Array) {
805 parameters = arguments[0];
806 } else {
807 parameters = arguments;
808 }
809
810 var andClauses = [];
811 for (var expression in parameters) {
812
813 if (parameters[expression] instanceof AExpression) {
814 andClauses.push(parameters[expression].val());
815 }
816 }
817
818 if (andClauses.length > 0) {
819 this._properties["stack"].push(new AExpression().set(andClauses.join(" and ")));
820 }
821
822 return this;
823};
824
825
826WhereClause.prototype.or = function() {
827
828 var parameters = [];
829 if (arguments[0] instanceof Array) {
830 parameters = arguments[0];
831 } else {
832 parameters = arguments;
833 }
834
835 var orClauses = [];
836 for (var expression in parameters) {
837
838 if (parameters[expression] instanceof AExpression) {
839 orClauses.push(parameters[expression].val());
840 }
841 }
842
843 if (andClauses.length > 0) {
844 this._properties["stack"].push(new AExpression().set(orClauses.join(" and ")));
845 }
846
847 return this;
848};
849
850// LimitClause
851// Grammar:
852// LimitClause ::= "limit" Expression ( "offset" Expression )?
853//
854// @param limitExpression [REQUIRED, AQLExpression]
855// @param offsetExpression [OPTIONAL, AQLExpression]
856function LimitClause(limitExpression, offsetExpression) {
857
858 AQLClause.call(this);
859
860 var parameters = [];
861 if (arguments[0] instanceof Array) {
862 parameters = arguments[0];
863 } else {
864 parameters = arguments;
865 }
866
867 // limitExpression required
868 this._properties["clause"] = "limit " + parameters[0].val();
869
870 // Optional: Offset
871 if (parameters.length == 2) {
872 this._properties["clause"] += " offset " + parameters[1].val();
873 }
874
875 return this;
876}
877
878LimitClause.prototype = Object.create(AQLClause.prototype);
879LimitClause.prototype.constructor = LimitClause;
880
881
882// OrderbyClause
883//
884// Grammar:
885// OrderbyClause ::= "order" "by" Expression ( ( "asc" ) | ( "desc" ) )? ( "," Expression ( ( "asc" ) | ( "desc" ) )? )*
886//
887// @params AQLExpressions and asc/desc strings, in any quantity. At least one required.
888function OrderbyClause() {
889
890 AQLClause.call(this);
891
892 // At least one argument expression is required, and first should be expression
893 if (arguments.length == 0) {
894 this._properties["clause"] = null;
895 return this;
896 }
897
898 var parameters = [];
899 if (arguments[0] instanceof Array) {
900 parameters = arguments[0];
901 } else {
902 parameters = arguments;
903 }
904
905 var expc = 0;
906 var expressions = [];
907
908 while (expc < parameters.length) {
909
910 var expression = "";
911
912 if (parameters[expc] instanceof AExpression) {
913 expression += parameters[expc].val();
914 }
915
916 var next = expc + 1;
917 if (next < parameters.length && (parameters[next] == "asc" || parameters[next] == "desc")) {
918 expc++;
919 expression += " " + parameters[expc];
920 }
921
922 expressions.push(expression);
923
924 expc++;
925 }
926
927 this._properties["clause"] = "order by " + expressions.join(", ");
928 return this;
929}
930
931OrderbyClause.prototype = Object.create(AQLClause.prototype);
932OrderbyClause.prototype.constructor = OrderbyClause;
933
934
935// GroupClause
936//
937// Grammar:
938// GroupClause ::= "group" "by" ( Variable ":=" )? Expression ( "," ( Variable ":=" )? Expression )* ( "decor" Variable ":=" Expression ( "," "decor" Variable ":=" Expression )* )? "with" VariableRef ( "," VariableRef )*
939function GroupClause() {
940 AQLClause.call(this);
941
942 if (arguments.length == 0) {
943 this._properties["clause"] = null;
944 return this;
945 }
946
947 var parameters = [];
948 if (arguments[0] instanceof Array) {
949 parameters = arguments[0];
950 } else {
951 parameters = arguments;
952 }
953
954 var expc = 0;
955 var expressions = [];
956 var variableRefs = [];
957 var isDecor = false;
958
959 while (expc < parameters.length) {
960
961 if (parameters[expc] instanceof AExpression) {
962
963 isDecor = false;
964 expressions.push(parameters[expc].val());
965
966 } else if (typeof parameters[expc] == "string") {
967
968 // Special keywords, decor & with
969 if (parameters[expc] == "decor") {
970 isDecor = true;
971 } else if (parameters[expc] == "with") {
972 isDecor = false;
973 expc++;
974 while (expc < parameters.length) {
975 variableRefs.push(parameters[expc]);
976 expc++;
977 }
978
979 // Variables and variable refs
980 } else {
981
982 var nextc = expc + 1;
983 var expression = "";
984
985 if (isDecor) {
986 expression += "decor ";
987 isDecor = false;
988 }
989
990 expression += parameters[expc] + " := " + parameters[nextc].val();
991 expressions.push(expression);
992 expc++;
993 }
994 }
995
996 expc++;
997 }
998
999 this._properties["clause"] = "group by " + expressions.join(", ") + " with " + variableRefs.join(", ");
1000 return this;
1001}
1002
1003GroupClause.prototype = Object.create(AQLClause.prototype);
1004GroupClause.prototype.constructor = GroupClause;
1005
1006
1007// Quantified Expression
1008//
1009// Grammar
1010// QuantifiedExpression ::= ( ( "some" ) | ( "every" ) ) Variable "in" Expression ( "," Variable "in" Expression )* "satisfies" Expression
1011//
1012// @param String some/every
1013// @param [AExpression]
1014// @param [Aexpression] satisfiesExpression
1015function QuantifiedExpression (keyword, expressions, satisfiesExpression) {
1016 AExpression.call(this);
1017
1018 var expression = keyword + " ";
1019 var varsInExpressions = [];
1020
1021 for (var varInExpression in expressions) {
1022 varsInExpressions.push(varInExpression + " in " + expressions[varInExpression].val());
1023 }
1024 expression += varsInExpressions.join(", ") + " satisfies " + satisfiesExpression.val();
1025
1026 AExpression.prototype.set.call(this, expression);
1027
1028 return this;
1029}
1030
1031QuantifiedExpression.prototype = Object.create(AExpression.prototype);
1032QuantifiedExpression.prototype.constructor = QuantifiedExpression;
1033
1034QuantifiedExpression.prototype.val = function() {
1035 var value = AExpression.prototype.val.call(this);
1036 return "(" + value + ")";
1037};