Skip to content

Commit

Permalink
Work on query builder
Browse files Browse the repository at this point in the history
  • Loading branch information
atruskie committed Sep 3, 2014
1 parent 52ce0f8 commit 8a39953
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 18 deletions.
82 changes: 64 additions & 18 deletions src/components/services/queryBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,91 @@ var qb = angular.module("bawApp.services.queryBuilder", ["bawApp.configuration"
function Query() {

this.filter = {};
this.sort = {};
this.paging = {};

this.combinator = function combinator(type, arguments) {
this.graph[type] = [];

var that = this;
arguments.forEach(function(value, key) {
that.graph[key] = {};
value.call(that.graph[key]);

this.combinator = function combinator(type, functions) {
if (!functions){
return this;
}

this.filter[type] = new Query();

var that = this.filter[type];
functions.forEach(function(value, key) {
if (!(value instanceof Query)){
throw "A combinator only accepts Query objects";
}

that.filter[key] = value.filter;
});

return this;
};

this.operator = function operator(operation, field, value) {
this.graph[field] = this.graph[field] || {};
var that = this;
if (this instanceof RootQuery) {
that = new Query();
}

this.graph[field][operation] = value;
}
that.filter[field] = that.filter[field] || {};

that.filter[field][operation] = value;

return that;
};

}

Query.mergeFilter = function mergeFilter(rootQuery, childQuery) {
if (rootQuery instanceof Query && childQuery instanceof Query) {

}
else {
throw "Query objects not passed in";
}
};


Query.prototype.eq = function eq(field, value) {
this.operator("eq", field, value);
return this.operator("eq", field, value);
};

Query.prototype.and = function and(functions) {
this.combinator("and", functions);
Query.prototype.and = function and() {
var args = Array.prototype.slice.call(arguments, 0);
return this.combinator("and", args);
};

Query.prototype.or = function or(functions) {
this.combinator("or", functions);
Query.prototype.or = function or() {
var args = Array.prototype.slice.call(arguments, 0);
return this.combinator("or", args);
};


function RootQuery() {
Query.call(this);

this.sort = {};
this.paging = {};

this.toJSON = function toJSON(spaces) {
return JSON.stringify({
filter: this.filter

}, null, spaces);
};
}
RootQuery.prototype = Object.create(Query.prototype);





qb.factory("QueryBuilder", [function() {
return {
create: function() {
return new Query();
return new RootQuery();
}
}
};
}]);
74 changes: 74 additions & 0 deletions src/components/services/queryBuilder.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
describe("The QueryBuilder", function () {

var queryBuilder, q;

var spaces = 2;
function j(obj) {
JSON.stringify(obj, null, spaces);
}

beforeEach(module("bawApp.services.queryBuilder"));

beforeEach(inject(["QueryBuilder", function (QueryBuilder) {
queryBuilder = QueryBuilder;
q = queryBuilder.create();

}]));


it("should be able to be created", function() {
var q = queryBuilder.create();

expect(q instanceof RootQuery).toBeTrue();
});

it("a query combinator should return itself", function() {
var actual = q.and(q.eq("field", 3.0));

expect(actual).toBe(q);
});

it("will throw if a combinator is passed a non-query object", function() {
expect(function() {
q.combinator("and", [{}]);
}).toThrow("A combinator only accepts Query objects");
});

it("a query operator should return a new instance of a Query", function() {
var actual = q.eq("field", 3.0);

expect(actual instanceof Query).toBeTrue();
expect(q instanceof RootQuery).toBeTrue();
expect(actual instanceof RootQuery).toBeFalse();


expect(actual).not.toBe(q);
});

it("should be able to do basic equality", function() {
var expected = {
filter: {
and: {
field: {
eq: 3.0
}
}
}
};

var actual = q.and(q.eq("field", 3.0));


expect(actual.toJSON(spaces)).toBe(j(expected));
});




// var actual = q.and(
// q.eq("fieldA", 3.0),
// q.or(
// q.lt("fieldA", 30.0).ge("fieldB", 0.0)
// )
// );
});

0 comments on commit 8a39953

Please sign in to comment.