blob: 870f93bc6d96f59ba87a35ede89a39313b1c5c8b [file] [log] [blame]
genia.likes.science@gmail.com512454d2013-05-28 03:32:20 -07001function AsterixSDK() {
2
3 // Asterix SDK => send
4 // Posts a message containing an API endpoint, json data,
5 // and a UI callback function.
6 //
7 // @param handler [Asterix REST Controller], a handler object
8 // that provides REST request information.
9 //
10 // Anticipated Usage:
11 //
12 // var a = AsterixSDK();
13 // var e = Expression;
14 // var h = AsterixRestController.bind(e);
15 // a.send(h);
16 myThis = this;
17 this.callbacks = {
18 "sync" : function() { alert("default sync"); },
19 "async" : function() {}
20 };
21 this.send = function(handler, cb) {
22 myThis.callbacks = cb;
23 this.handler = handler;
24 this.extras = handler["extras"];
25 this.xhr.post(
26 handler["endpoint"],
27 handler["apiData"],
28 this.branch
29 );
30 };
31
32 this.branch = function(response) {
33 if (response && response["error-code"]) {
34
35 alert("Error [Code" + response["error-code"][0] + "]: " + response["error-code"][1]);
36
37 } else if (response && response["results"]) {
38 var fn_callback = myThis.callbacks["sync"];
39 fn_callback(response, myThis.extras);
40
41 } else if (response["handle"]) {
42
43 var fn_callback = this.callbacks["async"];
44 fn_callback(response, extra);
45
46 } else if (response["status"]) {
47
48 var fn_callback = this.callbacks["sync"];
49 fn_callback(response, extra);
50 }
51 }
52
53 // Asterix SDK => bindingHandler
54 // AsterixExpression form handler where a new REST API point is bound. Takes as input any
55 // AsterixExpression, each of which is bindable.
56 this.bindingHandler = new AsterixExpression();
57 this.bind = this.bindingHandler.bind;
58}
59
60function AsterixExpression() {
61 this.init();
62 return this;
63}
64
65AsterixExpression.prototype.init = function () {
66 this.dataverse = ""; // TODO This shouldn't make it to send
67 this.boundTo = {};
68 this.clauses = [];
69 this.ui_callback_on_success = function() {};
70 this.ui_callback_on_success_async = function() {};
71};
72
73AsterixExpression.prototype.bind = function(expression) {
74 // If expression is an AsterixExpression, it becomes base
75 if (expression instanceof AsterixExpression) {
76 this.boundTo = expression;
77 } else if (expression instanceof AsterixClause) {
78 this.clauses.push(expression.val());
79 }
80 return this;
81};
82
83AsterixExpression.prototype.send = function(arc) {
84 // Hackiest of hacks
85 var g = new AsterixSDK();
86 g.send(arc, arc["callback"]);
87};
88
89AsterixExpression.prototype.clear = function() {
90 this.clauses.length = 0;
91 return this;
92};
93
94AsterixExpression.prototype.val = function() {
95 return this.clauses.join("\n");
96};
97
98AsterixExpression.prototype.success = function(fn, isSync) {
99 if (isSync) {
100 this.ui_callback_on_success = fn;
101 } else {
102 this.ui_callback_on_success_async = fn;
103 }
104 return this;
105};
106
107AsterixExpression.prototype.set = function(statements_arr) {
108 for (var i = 0; i < statements_arr.length; i++) {
109 this.clauses.push(statements_arr[i]);
110 }
111 return this;
112};
113
114AsterixExpression.prototype.use_dataverse = function(dv) {
115 this.dataverse = dv;
116 this.clauses.push("use dataverse " + dv + ";");
117 return this;
118};
119
120AsterixExpression.prototype.return = function(return_object) {
121 var components = [];
122 for (var key in return_object) {
123 components.push('"' + key + '" : ' + return_object[key]);
124 }
125
126 var return_expression = 'return { ' + components.join(', ') + ' }';
127 this.clauses.push(return_expression);
128 return this;
129};
130
genia.likes.science@gmail.com512454d2013-05-28 03:32:20 -0700131
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700132// Functions that can be used to call core expressions/clauses more cleanly
133function AFLWOGR () {
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700134
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700135}
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700136
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700137function AClause () {
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700138
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700139}
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700140
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700141function ALetClause () {
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700142
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700143}
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700144
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700145function AWhereClause () {
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700146
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700147}
148
149function AOrderbyClause () {
150
151}
152
153function AGroupClause () {
154
155}
156
157function ALimitClause () {
158
159}
160
161function ADistinctClause () {
162
163}
164
165function AVariable () {
166
167}
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700168
169// Temporary AsterixExpression Placeholder
170function AExpression () {
genia.likes.science@gmail.com512454d2013-05-28 03:32:20 -0700171 this._properties = {};
172 this._success = function() {};
173
genia.likes.science@gmail.com512454d2013-05-28 03:32:20 -0700174 return this;
175}
176
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700177
178AExpression.prototype.bind = function(options) {
genia.likes.science@gmail.com512454d2013-05-28 03:32:20 -0700179 var options = options || {};
180
genia.likes.science@gmail.com512454d2013-05-28 03:32:20 -0700181 if (options.hasOwnProperty("dataverse")) {
182 this._properties["dataverse"] = options["dataverse"];
183 }
184
185 if (options.hasOwnProperty("success")) {
186 this._success = options["success"];
187 }
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700188
189 if (options.hasOwnProperty("return")) {
190 this._properties["return"] = " return " + options["return"].val();
191 }
genia.likes.science@gmail.com512454d2013-05-28 03:32:20 -0700192};
193
genia.likes.science@gmail.com512454d2013-05-28 03:32:20 -0700194
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700195AExpression.prototype.run = function() {
genia.likes.science@gmail.com512454d2013-05-28 03:32:20 -0700196 var success_fn = this._success;
197
198 $.ajax({
199 type : 'GET',
200 url : "http://localhost:19101/query",
201 data : {"query" : this.val()},
202 dataType : "json",
203 success : function(data) {
204 success_fn(data);
205 }
206 });
207
208 return this;
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700209};
210
211
212AExpression.prototype.val = function() {
213
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700214 var value = "";
215
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700216 // If there is a dataverse defined, provide it.
217 if (this._properties.hasOwnProperty("dataverse")) {
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700218 value += "use dataverse " + this._properties["dataverse"] + ";\n";
219 };
220
221 if (this._properties.hasOwnProperty("value")) {
222 value += this._properties["value"];
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700223 }
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700224
225 return value;
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700226};
227
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700228// @param expressionValue [String]
229AExpression.prototype.set = function(expressionValue) {
230 this._properties["value"] = expressionValue;
231 return this;
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700232};
233
genia.likes.science@gmail.com73dcc702013-05-28 04:21:28 -0700234
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700235AExpression.prototype.error = function(msg) {
236 return "Asterix FunctionExpression Error: " + msg;
237};
238
239
240// FunctionExpression
241// Parent: AsterixExpression
242//
243// @param options [Various],
244// @key function [String], a function to be applid to the expression
245// @key expression [AsterixExpression or AsterixClause] an AsterixExpression/Clause to which the fn will be applied
246function FunctionExpression(options) {
247
248 // Initialize superclass
249 AExpression.call(this);
250
251 // Possible to initialize a function epxression without inputs, or with them
252 this.bind(options);
253
254 // Return object
255 return this;
256}
257
258
259FunctionExpression.prototype = Object.create(AExpression.prototype);
260FunctionExpression.prototype.constructor = FunctionExpression;
261
262
263FunctionExpression.prototype.bind = function(options) {
264
265 AExpression.prototype.bind.call(this, options);
266
267 var options = options || {};
268
269 if (options.hasOwnProperty("function")) {
270 this._properties["function"] = options["function"];
271 }
272
273 if (options.hasOwnProperty("expression")) {
274 this._properties["expression"] = options["expression"];
275 }
276
277 return this;
278};
279
280FunctionExpression.prototype.val = function () {
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700281 return this._properties["function"] + "(" + this._properties["expression"].val() + ")";
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700282};
283
genia.likes.science@gmail.com73dcc702013-05-28 04:21:28 -0700284
genia.likes.science@gmail.com73dcc702013-05-28 04:21:28 -0700285// FLWOGR ::= ( ForClause | LetClause ) ( Clause )* "return" Expression
286// Clause ::= ForClause | LetClause | WhereClause | OrderbyClause | GroupClause | LimitClause | DistinctClause
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700287//
genia.likes.science@gmail.com73dcc702013-05-28 04:21:28 -0700288// WhereClause ::= "where" Expression
289// OrderbyClause ::= "order" "by" Expression ( ( "asc" ) | ( "desc" ) )? ( "," Expression ( ( "asc" ) | ( "desc" ) )? )*
genia.likes.science@gmail.comb4cb6b32013-05-29 04:30:38 -0700290//
genia.likes.science@gmail.com73dcc702013-05-28 04:21:28 -0700291// GroupClause ::= "group" "by" ( Variable ":=" )? Expression ( "," ( Variable ":=" )? Expression )* ( "decor" Variable ":=" Expression ( "," "decor" Variable ":=" Expression )* )? "with" VariableRef ( "," VariableRef )*
292// LimitClause ::= "limit" Expression ( "offset" Expression )?
293// DistinctClause ::= "distinct" "by" Expression ( "," Expression )*
genia.likes.science@gmail.com73dcc702013-05-28 04:21:28 -0700294
295
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700296// FLWOGRExpression
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700297//
298// FLWOGRExpression ::= ( ForClause | LetClause ) ( Clause )* "return" Expression
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700299function FLWOGRExpression (options) {
300 // Initialize superclass
301 AExpression.call(this);
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700302
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700303 this._properties["clauses"] = [];
304
305 // Bind options and return
306 this.bind(options);
307 return this;
genia.likes.science@gmail.com90d08722013-05-28 04:40:12 -0700308}
309
310
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700311FLWOGRExpression.prototype = Object.create(AExpression.prototype);
312FLWOGRExpression.prototype.constructor = FLWOGRExpression;
313
314
315FLWOGRExpression.prototype.bind = function(options) {
316 AExpression.prototype.bind.call(this, options);
317
318 var options = options || {};
319
320 if (this._properties["clauses"].length == 0) {
321 // Needs to start with for or let clause
322 if (options instanceof ForClause || options instanceof LetClause) {
323 this._properties["clauses"].push(options);
324 }
325 } else {
326 if (options instanceof AQLClause) {
327 this._properties["clauses"].push(options);
328 }
329 }
330
331 return this;
332};
333
334
335FLWOGRExpression.prototype.val = function() {
336 var value = AExpression.prototype.val.call(this);
337
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700338 var clauseValues = [];
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700339 for (var c in this._properties["clauses"]) {
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700340 clauseValues.push(this._properties["clauses"][c].val());
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700341 }
342
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700343 return value + clauseValues.join("\n") + ";";
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700344};
345
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700346
genia.likes.science@gmail.com73dcc702013-05-28 04:21:28 -0700347// AQLClause
348//
349// Base Clause ::= ForClause | LetClause | WhereClause | OrderbyClause | GroupClause | LimitClause | DistinctClause
350function AQLClause() {
351 this._properties = {};
352 this._properties["clause"] = "";
353}
354
355AQLClause.prototype.val = function() {
356 var value = this._properties["clause"];
357
358 if (this._properties.hasOwnProperty("return")) {
359 value += " return " + this._properties["return"].val();
360 }
361
362 return value;
363};
364
365AQLClause.prototype.bind = function(options) {
366 var options = options || {};
367
368 if (options.hasOwnProperty("return")) {
369 this._properties["return"] = options["return"];
370 }
371
372 return this;
373};
374
genia.likes.science@gmail.com39578302013-05-31 04:42:26 -0700375AQLClause.prototype.set = function(value) {
376 this._properties["clause"] = value;
377 return this;
378};
379
genia.likes.science@gmail.com73dcc702013-05-28 04:21:28 -0700380
381// ForClause
382//
383// Grammar:
384// "for" Variable ( "at" Variable )? "in" ( Expression )
385//
386// @param for_variable [String], REQUIRED, first variable in clause
387// @param at_variable [String], NOT REQUIRED, first variable in clause
388// @param expression [AsterixExpression], REQUIRED, expression to evaluate
389//
390// TODO Error Checking
391function ForClause(for_variable, at_variable, expression) {
392 AQLClause.call(this);
393
394 // at_variable is optional, check if defined
395 var at = typeof at_variable ? at_variable : null;
396
397 // Prepare clause
398 this._properties["clause"] = "for $" + for_variable;
399 if (at != null) {
400 this._properties["clause"] += " at $" + at_variable;
401 }
402 this._properties["clause"] += " in " + expression.val();
403 return this;
404}
405
406ForClause.prototype = Object.create(AQLClause.prototype);
407ForClause.prototype.constructor = ForClause;
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700408
409
410// LetClause
411//
412// Grammar:
413// LetClause ::= "let" Variable ":=" Expression
414//
415// @param let_variable [String]
416// @param expression [AExpression]
417//
418// TODO Vigorous error checking
419function LetClause(let_variable, expression) {
420 AQLClause.call(this);
421
422 this._properties["clause"] = "let $" + let_variable + " := ";
423 this._properties["clause"] += expression.val();
424
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 ";
440 if (expression instanceof AExpression) {
441 this._properties["clause"] += expression.val();
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700442 } else if ( Object.getPrototypeOf( expression ) === Object.prototype ) {
443
444 this._properties["clause"] += "{";
445 var returnStatements = [];
446 for (returnValue in expression) {
447
448 if (expression[returnValue] instanceof AExpression) {
genia.likes.science@gmail.comfba7cc82013-05-31 04:04:08 -0700449 returnStatements.push('"' + returnValue + '" ' + " : " + expression[returnValue].val());
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700450 } else if (typeof expression[returnValue] == "string") {
genia.likes.science@gmail.comfba7cc82013-05-31 04:04:08 -0700451 returnStatements.push('"' + returnValue + '" ' + " : " + expression[returnValue]);
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700452 }
453 }
454 this._properties["clause"] += returnStatements.join(",\n");
455 this._properties["clause"] += "}";
456
genia.likes.science@gmail.com5ca104c2013-05-29 05:39:26 -0700457 } else {
458 this._properties["clause"] += new AsterixExpression().set([expression]).val();
459 }
460
461 return this;
462}
463
464ReturnClause.prototype = Object.create(AQLClause.prototype);
465ReturnClause.prototype.constructor = ReturnClause;
466
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700467ReturnClause.prototype.val = function () {
468 return this._properties["clause"];
469};
470
471
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700472// WhereClause
473//
474// Grammar:
475// ::= "where" Expression
476//
477// @param expression [BooleanExpression], pushes this expression onto the stack
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700478function WhereClause(expression) {
479 AQLClause.call(this);
480
481 this._properties["stack"] = [];
482
483 this.bind(expression);
484
485 return this;
486}
487
488
489WhereClause.prototype = Object.create(AQLClause.prototype);
490WhereClause.prototype.constructor = WhereClause;
491
492
493WhereClause.prototype.bind = function(expression) {
494 if (expression instanceof BooleanExpression) {
495 this._properties["stack"].push(expression);
496 }
497};
498
499
500WhereClause.prototype.val = function() {
501 var value = "where ";
502
503 var count = this._properties["stack"].length - 1;
504 while (count >= 0) {
505 value += this._properties["stack"][count].val() + " ";
506 count -= 1;
507 }
508
509 return value;
510}
511
512
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700513// LimitClause
514// Grammar:
515// LimitClause ::= "limit" Expression ( "offset" Expression )?
516//
517// @param limitExpression [REQUIRED, AQLExpression]
518// @param offsetExpression [OPTIONAL, AQLExpression]
519function LimitClause(limitExpression, offsetExpression) {
520
521 AQLClause.call(this);
522
523 // limitExpression required
524 this._properties["clause"] = "limit " + limitExpression.val();
525
526 // Optional: Offset
527 var offset = typeof offsetExpression ? offsetExpression : null;
528 if (offset != null) {
529 this._properties["clause"] += " offset " + offsetExpression.val();
530 }
531
532 return this;
533}
534
535LimitClause.prototype = Object.create(AQLClause.prototype);
536LimitClause.prototype.constructor = LimitClause;
537
538
539// OrderbyClause
540//
541// Grammar:
542// OrderbyClause ::= "order" "by" Expression ( ( "asc" ) | ( "desc" ) )? ( "," Expression ( ( "asc" ) | ( "desc" ) )? )*
543//
544// @params AQLExpressions and asc/desc strings, in any quantity. At least one required.
545function OrderbyClause() {
546
547 AQLClause.call(this);
548
549 // At least one argument expression is required, and first should be expression
550 if (arguments.length == 0 || !(arguments[0] instanceof AExpression)) {
551 // TODO Not sure which error to throw for an empty OrderBy but this should fail.
552 alert("Order By Error");
553 this._properties["clause"] = null;
554 return this;
555 }
556
557 var expc = 0;
558 var expressions = [];
559
560 while (expc < arguments.length) {
561
562 var expression = "";
563
564 if (arguments[expc] instanceof AExpression) {
565 expression += arguments[expc].val();
566 }
567
568 var next = expc + 1;
569 if (next < arguments.length && (arguments[next] == "asc" || arguments[next] == "desc")) {
570 expc++;
571 expression += " " + arguments[expc];
572 }
573
574 expressions.push(expression);
575
576 expc++;
577 }
578
579 this._properties["clause"] = "order by " + expressions.join(", ");
580 return this;
581}
582
583OrderbyClause.prototype = Object.create(AQLClause.prototype);
584OrderbyClause.prototype.constructor = OrderbyClause;
585
586
genia.likes.science@gmail.com6e392912013-05-31 03:46:59 -0700587// GroupClause
588//
589// Grammar:
590// GroupClause ::= "group" "by" ( Variable ":=" )? Expression ( "," ( Variable ":=" )? Expression )* ( "decor" Variable ":=" Expression ( "," "decor" Variable ":=" Expression )* )? "with" VariableRef ( "," VariableRef )*
591function GroupClause() {
592 AQLClause.call(this);
593
594 if (arguments.length == 0) {
595 // TODO Not sure which error to throw for an empty GroupBy but this should fail.
596 alert("Group Error");
597 this._properties["clause"] = null;
598 return this;
599 }
600
601 var expc = 0;
602 var expressions = [];
603 var variableRefs = [];
604 var isDecor = false;
605
606 while (expc < arguments.length) {
607
608 if (arguments[expc] instanceof AExpression) {
609
610 isDecor = false;
611 expressions.push(arguments[expc].val());
612
613 } else if (typeof arguments[expc] == "string") {
614
615 // Special keywords, decor & with
616 if (arguments[expc] == "decor") {
617 isDecor = true;
618 } else if (arguments[expc] == "with") {
619 isDecor = false;
620 expc++;
621 while (expc < arguments.length) {
622 variableRefs.push("$" + arguments[expc]);
623 expc++;
624 }
625
626 // Variables and variable refs
627 } else {
628
629 var nextc = expc + 1;
630 var expression = "";
631
632 if (isDecor) {
633 expression += "decor ";
634 isDecor = false;
635 }
636
637 expression += "$" + arguments[expc] + " := " + arguments[nextc].val();
638 expressions.push(expression);
639 expc++;
640 }
641 }
642
643 expc++;
644 }
645
646 this._properties["clause"] = "group by " + expressions.join(", ") + " with " + variableRefs.join(", ");
647 return this;
648}
649
650GroupClause.prototype = Object.create(AQLClause.prototype);
651GroupClause.prototype.constructor = GroupClause;
652
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700653// BooleanExpression
654//
655// TODO
656function BooleanExpression(expression) {
657 this.value = expression;
genia.likes.science@gmail.com6fd7b2e2013-05-31 00:40:28 -0700658 alert("Debugging Bool: " + arguments.length + " " + expression);
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700659}
660
661BooleanExpression.prototype.val = function() {
662 return this.value;
663}