Skip to content

Commit

Permalink
fix(sql): inline array parameters when formatting queries
Browse files Browse the repository at this point in the history
Closes #1021
  • Loading branch information
B4nan committed Nov 4, 2020
1 parent fe4b7bd commit a21735f
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 6 deletions.
6 changes: 5 additions & 1 deletion packages/core/src/types/ArrayType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@ export class ArrayType<T extends string | number = string> extends Type<T[] | nu
}

convertToDatabaseValue(value: T[] | null, platform: Platform, fromQuery?: boolean): string | null {
if (!value || fromQuery) {
if (!value) {
return value as null;
}

if (Array.isArray(value)) {
return platform.marshallArray(value as string[]);
}

if (fromQuery) {
return value;
}

throw ValidationError.invalidType(ArrayType, value, 'JS');
}

Expand Down
2 changes: 1 addition & 1 deletion packages/knex/src/AbstractSqlPlatform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export abstract class AbstractSqlPlatform extends Platform {

quoteValue(value: any): string {
/* istanbul ignore if */
if (Utils.isPlainObject(value) || Array.isArray(value)) {
if (Utils.isPlainObject(value)) {
return escape(JSON.stringify(value));
}

Expand Down
2 changes: 0 additions & 2 deletions packages/postgresql/src/PostgreSqlPlatform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ export class PostgreSqlPlatform extends AbstractSqlPlatform {
/* istanbul ignore if */
if (Utils.isPlainObject(value)) {
value = JSON.stringify(value);
} else if (Array.isArray(value)) {
value = this.marshallArray(value);
}

if (typeof value === 'string') {
Expand Down
2 changes: 1 addition & 1 deletion packages/sqlite/src/SqlitePlatform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class SqlitePlatform extends AbstractSqlPlatform {

quoteValue(value: any): string {
/* istanbul ignore if */
if (Utils.isPlainObject(value) || Array.isArray(value)) {
if (Utils.isPlainObject(value)) {
return escape(JSON.stringify(value), true, this.timezone);
}

Expand Down
7 changes: 7 additions & 0 deletions tests/EntityManager.mysql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ describe('EntityManagerMySql', () => {
});
});

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);
Expand Down
7 changes: 7 additions & 0 deletions tests/EntityManager.postgre.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 8 additions & 1 deletion tests/EntityManager.sqlite2.test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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' });

Expand Down

0 comments on commit a21735f

Please sign in to comment.