Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept Raw on onIn joins #4285 #4830

Merged
merged 1 commit into from Nov 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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