Skip to content

Commit

Permalink
Accept Raw on onIn joins (#4830)
Browse files Browse the repository at this point in the history
  • Loading branch information
OlivierCavadenti committed Nov 16, 2021
1 parent 27ade6f commit 672c269
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 14 deletions.
26 changes: 18 additions & 8 deletions lib/query/querycompiler.js
Expand Up @@ -448,6 +448,23 @@ class QueryCompiler {

onIn(statement) {
if (Array.isArray(statement.column)) return this.multiOnIn(statement);

let values;
if (statement.value instanceof Raw) {
values = this.client.parameter(
statement.value,
this.builder,
this.formatter
);
} else {
values = this.client.parameterize(
statement.value,
undefined,
this.builder,
this.bindingsHolder
);
}

return (
wrap_(
statement.column,
Expand All @@ -458,14 +475,7 @@ class QueryCompiler {
) +
' ' +
this._not(statement, 'in ') +
this.wrap(
this.client.parameterize(
statement.value,
undefined,
this.builder,
this.bindingsHolder
)
)
this.wrap(values)
);
}

Expand Down
24 changes: 24 additions & 0 deletions test/unit/query/builder.js
Expand Up @@ -4719,6 +4719,30 @@ describe('QueryBuilder', () => {
);
});

it('or on in with raw', () => {
testsql(
qb()
.select('*')
.from('users')
.join('contacts', (qb) => {
qb.on('users.id', '=', 'contacts.id')
.onIn('contacts.id', [7, 15, 23, 41])
.orOnIn('users.id', raw('select id from users where age > 18'));
}),
{
mysql:
'select * from `users` inner join `contacts` on `users`.`id` = `contacts`.`id` and `contacts`.`id` in (?, ?, ?, ?) or `users`.`id` in (select id from users where age > 18)',
mssql:
'select * from [users] inner join [contacts] on [users].[id] = [contacts].[id] and [contacts].[id] in (?, ?, ?, ?) or [users].[id] in (select id from users where age > 18)',
pg: 'select * from "users" inner join "contacts" on "users"."id" = "contacts"."id" and "contacts"."id" in (?, ?, ?, ?) or "users"."id" in (select id from users where age > 18)',
'pg-redshift':
'select * from "users" inner join "contacts" on "users"."id" = "contacts"."id" and "contacts"."id" in (?, ?, ?, ?) or "users"."id" in (select id from users where age > 18)',
oracledb:
'select * from "users" inner join "contacts" on "users"."id" = "contacts"."id" and "contacts"."id" in (?, ?, ?, ?) or "users"."id" in (select id from users where age > 18)',
}
);
});

it('on not in', () => {
testsql(
qb()
Expand Down
12 changes: 6 additions & 6 deletions types/index.d.ts
Expand Up @@ -1304,12 +1304,12 @@ export declare namespace Knex {
andOnVal(column1: string, operator: string, value: Value): JoinClause;
orOnVal(column1: string, value: Value): JoinClause;
orOnVal(column1: string, operator: string, value: Value): JoinClause;
onIn(column1: string, values: readonly any[]): JoinClause;
andOnIn(column1: string, values: readonly any[]): JoinClause;
orOnIn(column1: string, values: readonly any[]): JoinClause;
onNotIn(column1: string, values: readonly any[]): JoinClause;
andOnNotIn(column1: string, values: readonly any[]): JoinClause;
orOnNotIn(column1: string, values: readonly any[]): JoinClause;
onIn(column1: string, values: readonly any[] | Raw): JoinClause;
andOnIn(column1: string, values: readonly any[] | Raw): JoinClause;
orOnIn(column1: string, values: readonly any[] | Raw): JoinClause;
onNotIn(column1: string, values: readonly any[] | Raw): JoinClause;
andOnNotIn(column1: string, values: readonly any[] | Raw): JoinClause;
orOnNotIn(column1: string, values: readonly any[] | Raw): JoinClause;
onNull(column1: string): JoinClause;
andOnNull(column1: string): JoinClause;
orOnNull(column1: string): JoinClause;
Expand Down

0 comments on commit 672c269

Please sign in to comment.