From 672c2692b9d45493fd7d257658ffb53443fc0016 Mon Sep 17 00:00:00 2001 From: Olivier Cavadenti Date: Tue, 16 Nov 2021 09:25:29 +0100 Subject: [PATCH] Accept Raw on onIn joins (#4830) --- lib/query/querycompiler.js | 26 ++++++++++++++++++-------- test/unit/query/builder.js | 24 ++++++++++++++++++++++++ types/index.d.ts | 12 ++++++------ 3 files changed, 48 insertions(+), 14 deletions(-) diff --git a/lib/query/querycompiler.js b/lib/query/querycompiler.js index 79a36fb7de..5fd6dd930a 100644 --- a/lib/query/querycompiler.js +++ b/lib/query/querycompiler.js @@ -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, @@ -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) ); } diff --git a/test/unit/query/builder.js b/test/unit/query/builder.js index 68f5f765bc..c46f9d4cb8 100644 --- a/test/unit/query/builder.js +++ b/test/unit/query/builder.js @@ -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() diff --git a/types/index.d.ts b/types/index.d.ts index 57d474c3b5..fbd82073bb 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -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;