blob: ad8421d6eac0a0b433ade17a14ec048863d12590 [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
375
376// ForClause
377//
378// Grammar:
379// "for" Variable ( "at" Variable )? "in" ( Expression )
380//
381// @param for_variable [String], REQUIRED, first variable in clause
382// @param at_variable [String], NOT REQUIRED, first variable in clause
383// @param expression [AsterixExpression], REQUIRED, expression to evaluate
384//
385// TODO Error Checking
386function ForClause(for_variable, at_variable, expression) {
387 AQLClause.call(this);
388
389 // at_variable is optional, check if defined
390 var at = typeof at_variable ? at_variable : null;
391
392 // Prepare clause
393 this._properties["clause"] = "for $" + for_variable;
394 if (at != null) {
395 this._properties["clause"] += " at $" + at_variable;
396 }
397 this._properties["clause"] += " in " + expression.val();
398 return this;
399}
400
401ForClause.prototype = Object.create(AQLClause.prototype);
402ForClause.prototype.constructor = ForClause;
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700403
404
405// LetClause
406//
407// Grammar:
408// LetClause ::= "let" Variable ":=" Expression
409//
410// @param let_variable [String]
411// @param expression [AExpression]
412//
413// TODO Vigorous error checking
414function LetClause(let_variable, expression) {
415 AQLClause.call(this);
416
417 this._properties["clause"] = "let $" + let_variable + " := ";
418 this._properties["clause"] += expression.val();
419
420 return this;
421}
422
423LetClause.prototype = Object.create(AQLClause.prototype);
424LetClause.prototype.constructor = LetClause;
425
426
genia.likes.science@gmail.com5ca104c2013-05-29 05:39:26 -0700427// ReturnClause
428//
429// Grammar:
430// return [AQLExpression]
431function ReturnClause(expression) {
432 AQLClause.call(this);
433
434 this._properties["clause"] = "return ";
435 if (expression instanceof AExpression) {
436 this._properties["clause"] += expression.val();
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700437 } else if ( Object.getPrototypeOf( expression ) === Object.prototype ) {
438
439 this._properties["clause"] += "{";
440 var returnStatements = [];
441 for (returnValue in expression) {
442
443 if (expression[returnValue] instanceof AExpression) {
genia.likes.science@gmail.comfba7cc82013-05-31 04:04:08 -0700444 returnStatements.push('"' + returnValue + '" ' + " : " + expression[returnValue].val());
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700445 } else if (typeof expression[returnValue] == "string") {
genia.likes.science@gmail.comfba7cc82013-05-31 04:04:08 -0700446 returnStatements.push('"' + returnValue + '" ' + " : " + expression[returnValue]);
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700447 }
448 }
449 this._properties["clause"] += returnStatements.join(",\n");
450 this._properties["clause"] += "}";
451
genia.likes.science@gmail.com5ca104c2013-05-29 05:39:26 -0700452 } else {
453 this._properties["clause"] += new AsterixExpression().set([expression]).val();
454 }
455
456 return this;
457}
458
459ReturnClause.prototype = Object.create(AQLClause.prototype);
460ReturnClause.prototype.constructor = ReturnClause;
461
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700462ReturnClause.prototype.val = function () {
463 return this._properties["clause"];
464};
465
466
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700467// WhereClause
468//
469// Grammar:
470// ::= "where" Expression
471//
472// @param expression [BooleanExpression], pushes this expression onto the stack
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700473function WhereClause(expression) {
474 AQLClause.call(this);
475
476 this._properties["stack"] = [];
477
478 this.bind(expression);
479
480 return this;
481}
482
483
484WhereClause.prototype = Object.create(AQLClause.prototype);
485WhereClause.prototype.constructor = WhereClause;
486
487
488WhereClause.prototype.bind = function(expression) {
489 if (expression instanceof BooleanExpression) {
490 this._properties["stack"].push(expression);
491 }
492};
493
494
495WhereClause.prototype.val = function() {
496 var value = "where ";
497
498 var count = this._properties["stack"].length - 1;
499 while (count >= 0) {
500 value += this._properties["stack"][count].val() + " ";
501 count -= 1;
502 }
503
504 return value;
505}
506
507
genia.likes.science@gmail.com2ba32242013-05-31 03:10:48 -0700508// LimitClause
509// Grammar:
510// LimitClause ::= "limit" Expression ( "offset" Expression )?
511//
512// @param limitExpression [REQUIRED, AQLExpression]
513// @param offsetExpression [OPTIONAL, AQLExpression]
514function LimitClause(limitExpression, offsetExpression) {
515
516 AQLClause.call(this);
517
518 // limitExpression required
519 this._properties["clause"] = "limit " + limitExpression.val();
520
521 // Optional: Offset
522 var offset = typeof offsetExpression ? offsetExpression : null;
523 if (offset != null) {
524 this._properties["clause"] += " offset " + offsetExpression.val();
525 }
526
527 return this;
528}
529
530LimitClause.prototype = Object.create(AQLClause.prototype);
531LimitClause.prototype.constructor = LimitClause;
532
533
534// OrderbyClause
535//
536// Grammar:
537// OrderbyClause ::= "order" "by" Expression ( ( "asc" ) | ( "desc" ) )? ( "," Expression ( ( "asc" ) | ( "desc" ) )? )*
538//
539// @params AQLExpressions and asc/desc strings, in any quantity. At least one required.
540function OrderbyClause() {
541
542 AQLClause.call(this);
543
544 // At least one argument expression is required, and first should be expression
545 if (arguments.length == 0 || !(arguments[0] instanceof AExpression)) {
546 // TODO Not sure which error to throw for an empty OrderBy but this should fail.
547 alert("Order By Error");
548 this._properties["clause"] = null;
549 return this;
550 }
551
552 var expc = 0;
553 var expressions = [];
554
555 while (expc < arguments.length) {
556
557 var expression = "";
558
559 if (arguments[expc] instanceof AExpression) {
560 expression += arguments[expc].val();
561 }
562
563 var next = expc + 1;
564 if (next < arguments.length && (arguments[next] == "asc" || arguments[next] == "desc")) {
565 expc++;
566 expression += " " + arguments[expc];
567 }
568
569 expressions.push(expression);
570
571 expc++;
572 }
573
574 this._properties["clause"] = "order by " + expressions.join(", ");
575 return this;
576}
577
578OrderbyClause.prototype = Object.create(AQLClause.prototype);
579OrderbyClause.prototype.constructor = OrderbyClause;
580
581
genia.likes.science@gmail.com6e392912013-05-31 03:46:59 -0700582// GroupClause
583//
584// Grammar:
585// GroupClause ::= "group" "by" ( Variable ":=" )? Expression ( "," ( Variable ":=" )? Expression )* ( "decor" Variable ":=" Expression ( "," "decor" Variable ":=" Expression )* )? "with" VariableRef ( "," VariableRef )*
586function GroupClause() {
587 AQLClause.call(this);
588
589 if (arguments.length == 0) {
590 // TODO Not sure which error to throw for an empty GroupBy but this should fail.
591 alert("Group Error");
592 this._properties["clause"] = null;
593 return this;
594 }
595
596 var expc = 0;
597 var expressions = [];
598 var variableRefs = [];
599 var isDecor = false;
600
601 while (expc < arguments.length) {
602
603 if (arguments[expc] instanceof AExpression) {
604
605 isDecor = false;
606 expressions.push(arguments[expc].val());
607
608 } else if (typeof arguments[expc] == "string") {
609
610 // Special keywords, decor & with
611 if (arguments[expc] == "decor") {
612 isDecor = true;
613 } else if (arguments[expc] == "with") {
614 isDecor = false;
615 expc++;
616 while (expc < arguments.length) {
617 variableRefs.push("$" + arguments[expc]);
618 expc++;
619 }
620
621 // Variables and variable refs
622 } else {
623
624 var nextc = expc + 1;
625 var expression = "";
626
627 if (isDecor) {
628 expression += "decor ";
629 isDecor = false;
630 }
631
632 expression += "$" + arguments[expc] + " := " + arguments[nextc].val();
633 expressions.push(expression);
634 expc++;
635 }
636 }
637
638 expc++;
639 }
640
641 this._properties["clause"] = "group by " + expressions.join(", ") + " with " + variableRefs.join(", ");
642 return this;
643}
644
645GroupClause.prototype = Object.create(AQLClause.prototype);
646GroupClause.prototype.constructor = GroupClause;
647
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700648// BooleanExpression
649//
650// TODO
651function BooleanExpression(expression) {
652 this.value = expression;
genia.likes.science@gmail.com6fd7b2e2013-05-31 00:40:28 -0700653 alert("Debugging Bool: " + arguments.length + " " + expression);
genia.likes.science@gmail.com38612632013-05-28 13:11:52 -0700654}
655
656BooleanExpression.prototype.val = function() {
657 return this.value;
658}