Skip to content

Commit

Permalink
Merge pull request #13 from brandsExclusive/is-null-support
Browse files Browse the repository at this point in the history
Add is null support
  • Loading branch information
mastfissh authored Apr 26, 2018
2 parents 3c7b732 + 9941598 commit 0a26a86
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
13 changes: 7 additions & 6 deletions lib/queryBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ function queryBuilder(initial_state) {
placeholder = "$" + bound_vars.push(`%${value}%`);
statements.push(` ${column} ${operator} ${placeholder} `)


} else if ((operator == 'IS') && (value == 'NULL')) {
statements.push(`${column} IS NULL`)
} else {
placeholder = "$" + bound_vars.push(value);
statements.push(` ${column} ${operator} ${placeholder} `)
Expand All @@ -68,9 +69,9 @@ function queryBuilder(initial_state) {
if(orderParams.function == 'similarity'){
column = similarity(column, orderParams.values.pop());
}
}
}

ordersBy.push(`${column} ${direction}`)
ordersBy.push(`${column} ${direction}`)
}

sql = sql + " ORDER BY " + ordersBy.join(", ");
Expand Down Expand Up @@ -101,7 +102,7 @@ function queryBuilder(initial_state) {
return this;
};
out.select = select.bind(out);

let addSelect = function (field) {
this.state.select = this.state.select || [];
this.state.select.push(field);
Expand All @@ -124,7 +125,7 @@ function queryBuilder(initial_state) {
return this;
};
out.addOrderBy = addOrderBy.bind(out);

let addOrderByFunction = function (functionName, field, values, direction) {
delete this.state.count;
this.state.orderBy = this.state.orderBy || [];
Expand All @@ -139,7 +140,7 @@ function queryBuilder(initial_state) {
let where = function (field, operator, value) {
this.state.wheres = this.state.wheres || [];
this.state.wheres.push([field, operator, value]);

return this;
};
out.where = where.bind(out);
Expand Down
10 changes: 9 additions & 1 deletion test/queryBuilder_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,14 @@ describe('queryBuilder', function() {
,["%Football%"]]).to.eql(query.finalize());
});

it('should compose where IS NULL', function() {
query = queryBuilder();
query.from("sports");
query.where("name","IS", "NULL");
expect([
" SELECT * FROM sports WHERE name IS NULL",[]]).to.eql(query.finalize());
});

it('should compose multiple where in with where like and where equals', function() {
query = queryBuilder();
query.from("banana");
Expand Down Expand Up @@ -196,7 +204,7 @@ describe('queryBuilder', function() {
" SELECT first_name, last_name FROM banana WHERE species in ($1,$2) AND feeder = $3 "
,["cavendish","alchemist","ape"]]).to.eql(query.finalize());
});

it('should compose custom select fields and several orders', function() {
query = queryBuilder();
query.from("banana");
Expand Down

0 comments on commit 0a26a86

Please sign in to comment.