X-Git-Url: http://git.indexdata.com/?p=cql-js-moved-to-github.git;a=blobdiff_plain;f=cql.js;h=0a1cc75da057edb05885b256d88941579cad70e9;hp=3e4f6d177cb399db8f714f1684119fabc187c0f1;hb=2b8022d29d91ccf1a2b5ab66dbe5aec025906433;hpb=ef1c473eb97976c5839851e06195915de6da6897
diff --git a/cql.js b/cql.js
index 3e4f6d1..0a1cc75 100644
--- a/cql.js
+++ b/cql.js
@@ -12,6 +12,10 @@ var CQLModifier = function () {
}
CQLModifier.prototype = {
+ toString: function () {
+ return this.name + this.relation + this.value;
+ },
+
toXCQL: function (n) {
var s = indent(n+1) + "\n";
s = s + indent(n+2) + "" + this.name + "\n";
@@ -23,8 +27,17 @@ CQLModifier.prototype = {
+ "" + this.value +"\n";
s = s + indent(n+1) + "\n";
return s;
+ },
+
+ toFQ: function () {
+ //we ignore modifier relation symbol, for value-less modifiers
+ //we assume 'true'
+ var value = this.value.length > 0 ? this.value : "true";
+ var s = '"'+this.name+'": "'+value+'"';
+ return s;
}
}
+
// CQLSearchClause
var CQLSearchClause = function (field, fielduri, relation, relationuri,
modifiers, term) {
@@ -37,6 +50,21 @@ var CQLSearchClause = function (field, fielduri, relation, relationuri,
}
CQLSearchClause.prototype = {
+ toString: function () {
+ var field = this.field;
+ var relation = this.relation;
+ if (field == 'cql.serverChoice' && relation == 'scr') {
+ //avoid redundant field/relation
+ field = null;
+ relation = null;
+ }
+ return (field ? field + ' ' : '') +
+ (relation ? relation : '') +
+ (this.modifiers.length > 0 ? '/' + this.modifiers.join('/') : '') +
+ (relation || this.modifiers.length ? ' ' : '') +
+ '"' + this.term + '"';
+ },
+
toXCQL: function (n) {
var s = indent(n) + "\n";
if (this.fielduri.length > 0)
@@ -65,7 +93,50 @@ CQLSearchClause.prototype = {
s = s + indent(n+1) + "" + this.term + "\n";
s = s + indent(n) + "\n";
return s;
+ },
+
+ toFQ: function () {
+ var s = '{ "term": "'+this.term+'"';
+ if (this.field.length > 0 && this.field != 'cql.serverChoice')
+ s+= ', "field": "'+this.field+'"';
+ if (this.relation.length > 0 && this.relation != 'scr')
+ s+= ', "relation": "'+this._mapRelation(this.relation)+'"';
+ for (var i = 0; i < this.modifiers.length; i++) {
+ //since modifiers are mapped to keys, ignore the reserved ones
+ if (this.modifiers[i].name == "term"
+ ||this.modifiers[i].name == "field"
+ ||this.modifiers[i].name == "relation")
+ continue;
+ s += ', ' + this.modifiers[i].toFQ();
+ }
+ s += ' }';
+ return s;
+ },
+
+ _mapRelation: function (rel) {
+ switch(rel) {
+ case "<" : return "lt";
+ case ">" : return "gt";
+ case "=" : return "eq";
+ case "<>" : return "ne";
+ case ">=" : return "ge";
+ case "<=" : return "le";
+ default: return rel;
+ }
+ },
+
+ _remapRelation: function (rel) {
+ switch(rel) {
+ case "lt" : return "<";
+ case "gt" : return ">";
+ case "eq" : return "=";
+ case "ne" : return "<>";
+ case "ge" : return ">=";
+ case "le" : return "<=";
+ default: return rel;
+ }
}
+
}
// CQLBoolean
var CQLBoolean = function() {
@@ -76,6 +147,12 @@ var CQLBoolean = function() {
}
CQLBoolean.prototype = {
+ toString: function () {
+ return (this.left.op ? '(' + this.left + ')' : this.left) + ' ' +
+ this.op.toUpperCase() +
+ (this.modifiers.length > 0 ? '/' + this.modifiers.join('/') : '') +
+ ' ' + (this.right.op ? '(' + this.right + ')' : this.right);;
+ },
toXCQL: function (n) {
var s = indent(n) + "\n";
s = s + indent(n+1) + "\n" +
@@ -94,7 +171,19 @@ CQLBoolean.prototype = {
this.right.toXCQL(n+2) + indent(n+1) + "\n";
s = s + indent(n) + "\n";
return s;
+ },
+
+ toFQ: function () {
+ var s = ' { "op": "'+this.op+'"';
+ //proximity modifiers
+ for (var i = 0; i < this.modifiers.length; i++)
+ s += ', ' + this.modifiers[i].toFQ();
+ s += ', "s1": '+this.left.toFQ();
+ s += ', "s2": '+this.right.toFQ();
+ s += ' }'
+ return s;
}
+
}
// CQLParser
var CQLParser = function () {
@@ -121,9 +210,69 @@ CQLParser.prototype = {
if (this.look != "")
throw new Error("EOF expected");
},
+ parseFromFQ: function (query) {
+ if (!query)
+ throw new Error("The query to be parsed cannot be empty");
+ if (typeof query == 'string')
+ query = JSON.parse(query);
+ this.tree = this._parseFromFQ(query);
+ },
+ _parseFromFQ: function (fq) {
+ //op-node
+ if (fq.hasOwnProperty('op')
+ && fq.hasOwnProperty('s1')
+ && fq.hasOwnProperty('s2')) {
+ var node = new CQLBoolean();
+ node.op = fq.op;
+ node.left = this._parseFromFQ(fq.s1);
+ node.right = this._parseFromFQ(fq.s2);
+ //include all other members as modifiers
+ node.modifiers = [];
+ for (var key in fq) {
+ if (key == 'op' || key == 's1' || key == 's2')
+ continue;
+ var mod = new CQLModifier();
+ mod.name = key;
+ mod.relation = '=';
+ mod.value = fq[key];
+ node.modifiers.push(mod);
+ }
+ return node;
+ }
+ //search-clause node
+ if (fq.hasOwnProperty('term')) {
+ var node = new CQLSearchClause();
+ node.term = fq.term;
+ node.field = fq.hasOwnProperty('field')
+ ? fq.field : 'cql.serverChoice';
+ node.relation = fq.hasOwnProperty('relation')
+ ? node._remapRelation(fq.relation) : 'scr';
+ //include all other members as modifiers
+ node.relationuri = '';
+ node.fielduri = '';
+ node.modifiers = [];
+ for (var key in fq) {
+ if (key == 'term' || key == 'field' || key == 'relation')
+ continue;
+ var mod = new CQLModifier();
+ mod.name = key;
+ mod.relation = '=';
+ mod.value = fq[key];
+ node.modifiers.push(mod);
+ }
+ return node;
+ }
+ throw new Error('Unknow node type; '+JSON.stringify(fq));
+ },
toXCQL: function () {
return this.tree.toXCQL();
},
+ toFQ: function () {
+ return this.tree.toFQ();
+ },
+ toString: function () {
+ return this.tree.toString();
+ },
_parseQuery: function(field, relation, modifiers) {
var left = this._parseSearchClause(field, relation, modifiers);
while (this.look == "s" && (