Skip to content

Commit

Permalink
1227: handle raw where clause binding
Browse files Browse the repository at this point in the history
  • Loading branch information
littlemaneuver committed Dec 22, 2022
1 parent a228f32 commit 6f7c51b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
6 changes: 6 additions & 0 deletions lib/query/querybuilder.js
Expand Up @@ -532,6 +532,12 @@ class Builder extends EventEmitter {
// Adds a raw `where` clause to the query.
whereRaw(sql, bindings) {
const raw = sql.isRawInstance ? sql : this.client.raw(sql, bindings);

assert(
!raw.bindings.some(isObject),
'The values in where clause must not be object or array.'
);

this._statements.push({
grouping: 'where',
type: 'whereRaw',
Expand Down
46 changes: 39 additions & 7 deletions test/unit/query/builder.js
Expand Up @@ -848,13 +848,34 @@ describe('QueryBuilder', () => {
});
});

it('basic wheres should not accept array or object as a value', () => {
it('basic wheres should not accept array or object as a value #1227', () => {
testquery(qb().select('*').from('users').where('id', '=', 1), {
mysql: 'select * from `users` where `id` = 1',
pg: 'select * from "users" where "id" = 1',
'pg-redshift': 'select * from "users" where "id" = 1',
mssql: 'select * from [users] where [id] = 1',
});
testquery(qb().select('*').from('users').where({ id: 1 }), {
mysql: 'select * from `users` where `id` = 1',
pg: 'select * from "users" where "id" = 1',
'pg-redshift': 'select * from "users" where "id" = 1',
mssql: 'select * from [users] where [id] = 1',
});

try {
clientsWithCustomLoggerForTestWarnings.pg
.queryBuilder()
qb().select('*').from('users').where('id', '=', [0]);
throw new Error('Should not reach this point');
} catch (error) {
expect(error.message).to.equal(
'The values in where clause must not be object or array.'
);
}

try {
qb()
.select('*')
.from('users')
.where('id', '=', [0]);
.where({ id: { test: 'test' } });
throw new Error('Should not reach this point');
} catch (error) {
expect(error.message).to.equal(
Expand All @@ -863,11 +884,22 @@ describe('QueryBuilder', () => {
}

try {
clientsWithCustomLoggerForTestWarnings.pg
.queryBuilder()
qb()
.select('*')
.from('users')
.where({ id: { test: 'test' } });
.where(raw('?? = ?', ['id', [0]]));
throw new Error('Should not reach this point');
} catch (error) {
expect(error.message).to.equal(
'The values in where clause must not be object or array.'
);
}

try {
qb()
.select('*')
.from('users')
.whereRaw('?? = ?', ['id', { test: 'test' }]);
throw new Error('Should not reach this point');
} catch (error) {
expect(error.message).to.equal(
Expand Down

0 comments on commit 6f7c51b

Please sign in to comment.