Skip to content

Commit

Permalink
Merge 972f528 into 51cb2a8
Browse files Browse the repository at this point in the history
  • Loading branch information
ngdaniels committed Nov 11, 2019
2 parents 51cb2a8 + 972f528 commit 476ceff
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
21 changes: 15 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,26 @@ class QueryBuilder {
let whereString = '';

if (typeof where.column === 'string') {
whereString = where.column;
if (typeof where.value === 'string') {
whereString = `${where.column} ${where.operator} '${where.value}'`;
}
else {
whereString = `${where.column} ${where.operator} ${where.value}`;
}
}
else if (typeof where.column === 'object') {
whereString = `${where.column.tableName}."${where.column.columnName}"`
if (where.column.columnType === "string" && !where.caseSensitive) {
whereString = `LOWER (${where.column.tableName}."${where.column.columnName}") ${where.operator} LOWER ('${where.value}')`
}
else {
whereString = `${where.column.tableName}."${where.column.columnName}" ${where.operator} '${where.value}'`
}
}

if (index === this.whereField.length - 1) {
queryString += `${whereString} ${where.operator} '${where.value}' `;
if (index < this.whereField.length - 1) {
queryString += `${whereString} AND `;
}
else {
queryString += `${whereString} ${where.operator} '${where.value}' AND `;
queryString += `${whereString} `;
}
});
}
Expand Down
19 changes: 19 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,23 @@ describe('Query String Builder', function () {
.generateQuery();
expect(queryString).to.equal(`SELECT firstName FROM user WHERE firstName = 'Agung' AND lastName = 'Hercules';`);
});

it(`Test 9. Test with multiple where (2), operator is 'greater than' and value is an 'integer'`, function() {
let queryClass = new QueryBuilder;
let queryString = queryClass
.addSelect('firstName')
.setFirstTable('user')
.addWhere({
column: 'age',
operator: '>',
value: 25
})
.addWhere({
column: 'status',
operator: '=',
value: 'Single'
})
.generateQuery();
expect(queryString).to.equal(`SELECT firstName FROM user WHERE age > 25 AND status = 'Single';`);
})
});

0 comments on commit 476ceff

Please sign in to comment.