From a21735f85f3a9de533212151bee8df55810b25b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ad=C3=A1mek?= Date: Wed, 4 Nov 2020 22:28:23 +0100 Subject: [PATCH] fix(sql): inline array parameters when formatting queries Closes #1021 --- packages/core/src/types/ArrayType.ts | 6 +++++- packages/knex/src/AbstractSqlPlatform.ts | 2 +- packages/postgresql/src/PostgreSqlPlatform.ts | 2 -- packages/sqlite/src/SqlitePlatform.ts | 2 +- tests/EntityManager.mysql.test.ts | 7 +++++++ tests/EntityManager.postgre.test.ts | 7 +++++++ tests/EntityManager.sqlite2.test.ts | 9 ++++++++- 7 files changed, 29 insertions(+), 6 deletions(-) diff --git a/packages/core/src/types/ArrayType.ts b/packages/core/src/types/ArrayType.ts index a86cccf8f9a3..b6d28d9cc181 100644 --- a/packages/core/src/types/ArrayType.ts +++ b/packages/core/src/types/ArrayType.ts @@ -11,7 +11,7 @@ export class ArrayType extends Type extends Type { }); }); + test('raw query with array param', async () => { + const q1 = await orm.em.getPlatform().formatQuery(`select * from author2 where id in (?) limit ?`, [[1, 2, 3], 3]); + expect(q1).toBe('select * from author2 where id in (1, 2, 3) limit 3'); + const q2 = await orm.em.getPlatform().formatQuery(`select * from author2 where id in (?) limit ?`, [['1', '2', '3'], 3]); + expect(q2).toBe(`select * from author2 where id in ('1', '2', '3') limit 3`); + }); + test('should return mysql driver', async () => { const driver = orm.em.getDriver(); expect(driver).toBeInstanceOf(MySqlDriver); diff --git a/tests/EntityManager.postgre.test.ts b/tests/EntityManager.postgre.test.ts index 8c9a8cd5a780..0542772c30c2 100644 --- a/tests/EntityManager.postgre.test.ts +++ b/tests/EntityManager.postgre.test.ts @@ -61,6 +61,13 @@ describe('EntityManagerPostgre', () => { }); }); + test('raw query with array param', async () => { + const q1 = await orm.em.getPlatform().formatQuery(`select * from author2 where id in (?) limit ?`, [[1, 2, 3], 3]); + expect(q1).toBe('select * from author2 where id in (1, 2, 3) limit 3'); + const q2 = await orm.em.getPlatform().formatQuery(`select * from author2 where id in (?) limit ?`, [['1', '2', '3'], 3]); + expect(q2).toBe(`select * from author2 where id in ('1', '2', '3') limit 3`); + }); + test('should return postgre driver', async () => { const driver = orm.em.getDriver(); expect(driver).toBeInstanceOf(PostgreSqlDriver); diff --git a/tests/EntityManager.sqlite2.test.ts b/tests/EntityManager.sqlite2.test.ts index 8c0d3745bcda..a48ddff20def 100644 --- a/tests/EntityManager.sqlite2.test.ts +++ b/tests/EntityManager.sqlite2.test.ts @@ -1,4 +1,4 @@ -import { Collection, EntityManager, LockMode, MikroORM, QueryOrder, Logger, ValidationError, wrap, ArrayCollection } from '@mikro-orm/core'; +import { ArrayCollection, Collection, EntityManager, LockMode, Logger, MikroORM, QueryOrder, ValidationError, wrap } from '@mikro-orm/core'; import { SqliteDriver } from '@mikro-orm/sqlite'; import { initORMSqlite2, wipeDatabaseSqlite2 } from './bootstrap'; import { Author4, Book4, BookTag4, FooBar4, IAuthor4, IPublisher4, Publisher4, PublisherType, Test4 } from './entities-schema'; @@ -31,6 +31,13 @@ describe('EntityManagerSqlite2', () => { expect(authors[0]).toBe(author); }); + test('raw query with array param', async () => { + const q1 = await orm.em.getPlatform().formatQuery(`select * from author4 where id in (?) limit ?`, [[1, 2, 3], 3]); + expect(q1).toBe('select * from author4 where id in (1, 2, 3) limit 3'); + const q2 = await orm.em.getPlatform().formatQuery(`select * from author4 where id in (?) limit ?`, [['1', '2', '3'], 3]); + expect(q2).toBe(`select * from author4 where id in ('1', '2', '3') limit 3`); + }); + test('transactions', async () => { const god1 = orm.em.create(Author4, { name: 'God1', email: 'hello@heaven1.god' });