Skip to content

Commit

Permalink
Adds support for Boolean types and support for '<>' operators (#17)
Browse files Browse the repository at this point in the history
* Adds support for Boolean types and support for '<>' operators

* Fixed an error with the '<>' operator implementation and associated test
  • Loading branch information
hogpilot committed Nov 25, 2020
1 parent 1a19cfe commit 61ad1e5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/mongoParser/whereClause.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ function parseWhere(whereTree) {
return whereTree.value.replace(/['"]+/g, "");
case "Number":
return +whereTree.value;
case "Boolean":
return whereTree.value.toLowerCase() === "true";
default:
throw new Error(`Unsupported expression type: ${whereTree.type}`);
}
Expand Down Expand Up @@ -137,6 +139,7 @@ function handleComparisonOperator(whereTree) {
case "<=":
case ">":
case ">=":
case "<>":
case "!=":
return {
[whereTree.left.value]: {
Expand Down Expand Up @@ -169,6 +172,7 @@ function getEqulityOperator(sqlOperator) {
return "$lt";
case "<=":
return "$lte";
case "<>":
case "!=":
return "$ne";
default:
Expand Down
28 changes: 27 additions & 1 deletion test/lib/mongoParser/whereClause.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ describe("parseWhere", () => {
});
});

describe("type is Boolean", () => {
it("should return a boolean", () => {
const whereTree = { type: "Boolean", value: "TRUE" };

const result = parseWhere(whereTree);

expect(result).toBe(true);
});
});

describe("type is String", () => {
it("should return a string", () => {
const whereTree = { type: "String", value: "1337" };
Expand Down Expand Up @@ -258,6 +268,22 @@ describe("parseWhere", () => {
});
});

describe("operator is <>", () => {
it("should return a valid $ne object", () => {
const whereTree = {
type: "ComparisonBooleanPrimary",
left: { type: "Identifier", value: "city" },
operator: "<>",
right: { type: "String", value: "'Rio'" }
};
const expected = { city: { $ne: "Rio" } };

const result = parseWhere(whereTree);

expect(result).toEqual(expected);
});
});

describe("operator is >", () => {
it("should return a valid $gt object", () => {
const whereTree = {
Expand Down Expand Up @@ -326,7 +352,7 @@ describe("parseWhere", () => {
const whereTree = {
type: "ComparisonBooleanPrimary",
left: { type: "Identifier", value: "age" },
operator: "<>",
operator: "|",
right: { type: "Number", value: "18" }
};

Expand Down

0 comments on commit 61ad1e5

Please sign in to comment.