Skip to content
New issue

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

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

Already on GitHub? # to your account

[feat] inArray and notInArray accept empty list #2502

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions drizzle-orm/src/sql/expressions/conditions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ export function inArray(
): SQL {
if (Array.isArray(values)) {
if (values.length === 0) {
throw new Error('inArray requires at least one value');
return sql`false`;
}
return sql`${column} in ${values.map((v) => bindIfParam(v, column))}`;
}
Expand Down Expand Up @@ -335,7 +335,7 @@ export function notInArray(
): SQL {
if (Array.isArray(values)) {
if (values.length === 0) {
throw new Error('notInArray requires at least one value');
return sql`true`;
}
return sql`${column} not in ${values.map((v) => bindIfParam(v, column))}`;
}
Expand Down
29 changes: 29 additions & 0 deletions integration-tests/tests/mysql/mysql-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
max,
min,
Name,
notInArray,
placeholder,
sql,
sum,
Expand Down Expand Up @@ -534,6 +535,34 @@ export function tests(driver?: string) {
expect(users).toEqual([{ name: 'JOHN' }]);
});

test('select with empty array in inArray', async (ctx) => {
const { db } = ctx.mysql;

await db.insert(usersTable).values([{ name: 'John' }, { name: 'Jane' }, { name: 'Jane' }]);
const result = await db
.select({
name: sql`upper(${usersTable.name})`,
})
.from(usersTable)
.where(inArray(usersTable.id, []));

expect(result).toEqual([]);
});

test('select with empty array in notInArray', async (ctx) => {
const { db } = ctx.mysql;

await db.insert(usersTable).values([{ name: 'John' }, { name: 'Jane' }, { name: 'Jane' }]);
const result = await db
.select({
name: sql`upper(${usersTable.name})`,
})
.from(usersTable)
.where(notInArray(usersTable.id, []));

expect(result).toEqual([{ name: 'JOHN' }, { name: 'JANE' }, { name: 'JANE' }]);
});

test('select distinct', async (ctx) => {
const { db } = ctx.mysql;

Expand Down
28 changes: 27 additions & 1 deletion integration-tests/tests/pg/awsdatapi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'dotenv/config';

import { RDSDataClient } from '@aws-sdk/client-rds-data';
import * as dotenv from 'dotenv';
import { asc, eq, sql, TransactionRollbackError } from 'drizzle-orm';
import { asc, eq, inArray, notInArray, sql, TransactionRollbackError } from 'drizzle-orm';
import type { AwsDataApiPgDatabase } from 'drizzle-orm/aws-data-api/pg';
import { drizzle } from 'drizzle-orm/aws-data-api/pg';
import { migrate } from 'drizzle-orm/aws-data-api/pg/migrator';
Expand Down Expand Up @@ -105,6 +105,32 @@ test('select sql', async () => {
expect(users).toEqual([{ name: 'JOHN' }]);
});

test('select with empty array in inArray', async () => {
await db.insert(usersTable).values([
{ name: 'John' },
{ name: 'Jane' },
{ name: 'Jane' },
]);
const users = await db.select({
name: sql`upper(${usersTable.name})`,
}).from(usersTable).where(inArray(usersTable.id, []));

expect(users).toEqual([]);
});

test('select with empty array in notInArray', async () => {
await db.insert(usersTable).values([
{ name: 'John' },
{ name: 'Jane' },
{ name: 'Jane' },
]);
const result = await db.select({
name: sql`upper(${usersTable.name})`,
}).from(usersTable).where(notInArray(usersTable.id, []));

expect(result).toEqual([{ name: 'John' }, { name: 'Jane' }, { name: 'Jane' }]);
});

test('select typed sql', async () => {
await db.insert(usersTable).values({ name: 'John' });
const users = await db.select({
Expand Down
29 changes: 29 additions & 0 deletions integration-tests/tests/pg/pg-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
lt,
max,
min,
notInArray,
or,
SQL,
sql,
Expand Down Expand Up @@ -539,6 +540,34 @@ export function tests() {
expect(users).toEqual([{ name: 'JOHN' }]);
});

test('select with empty array in inArray', async (ctx) => {
const { db } = ctx.pg;

await db.insert(usersTable).values([{ name: 'John' }, { name: 'Jane' }, { name: 'Jane' }]);
const result = await db
.select({
name: sql`upper(${usersTable.name})`,
})
.from(usersTable)
.where(inArray(usersTable.id, []));

expect(result).toEqual([]);
});

test('select with empty array in notInArray', async (ctx) => {
const { db } = ctx.pg;

await db.insert(usersTable).values([{ name: 'John' }, { name: 'Jane' }, { name: 'Jane' }]);
const result = await db
.select({
name: sql`upper(${usersTable.name})`,
})
.from(usersTable)
.where(notInArray(usersTable.id, []));

expect(result).toEqual([{ name: 'JOHN' }, { name: 'JANE' }, { name: 'JANE' }]);
});

test('$default function', async (ctx) => {
const { db } = ctx.pg;

Expand Down
29 changes: 29 additions & 0 deletions integration-tests/tests/sqlite/sqlite-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
max,
min,
Name,
notInArray,
sql,
sum,
sumDistinct,
Expand Down Expand Up @@ -371,6 +372,34 @@ export function tests() {
expect(users).toEqual([{ name: 'JOHN' }]);
});

test('select with empty array in inArray', async (ctx) => {
const { db } = ctx.sqlite;

await db.insert(usersTable).values([{ name: 'John' }, { name: 'Jane' }, { name: 'Jane' }]);
const result = await db
.select({
name: sql`upper(${usersTable.name})`,
})
.from(usersTable)
.where(inArray(usersTable.id, []));

expect(result).toEqual([]);
});

test('select with empty array in notInArray', async (ctx) => {
const { db } = ctx.sqlite;

await db.insert(usersTable).values([{ name: 'John' }, { name: 'Jane' }, { name: 'Jane' }]);
const result = await db
.select({
name: sql`upper(${usersTable.name})`,
})
.from(usersTable)
.where(notInArray(usersTable.id, []));

expect(result).toEqual([{ name: 'JOHN' }, { name: 'JANE' }, { name: 'JANE' }]);
});

test('select distinct', async (ctx) => {
const { db } = ctx.sqlite;

Expand Down