From 392d4e99ce02cdcccf2b76d61f5e31f8fb7d52a8 Mon Sep 17 00:00:00 2001 From: Matt DeKok Date: Sat, 4 May 2024 14:28:01 +0000 Subject: [PATCH 1/4] Add support for "limit 0" --- drizzle-orm/src/mysql-core/dialect.ts | 10 ++++++++-- drizzle-orm/src/pg-core/dialect.ts | 10 ++++++++-- drizzle-orm/src/sqlite-core/dialect.ts | 10 ++++++++-- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/drizzle-orm/src/mysql-core/dialect.ts b/drizzle-orm/src/mysql-core/dialect.ts index 4c0db87ef..d572e22d0 100644 --- a/drizzle-orm/src/mysql-core/dialect.ts +++ b/drizzle-orm/src/mysql-core/dialect.ts @@ -323,7 +323,10 @@ export class MySqlDialect { groupBySql = sql` group by ${sql.join(groupBy, sql`, `)}`; } - const limitSql = limit ? sql` limit ${limit}` : undefined; + const limitSql = + typeof limit === "object" || (typeof limit === "number" && limit >= 0) + ? sql` limit ${limit}` + : undefined; const offsetSql = offset ? sql` offset ${offset}` : undefined; @@ -400,7 +403,10 @@ export class MySqlDialect { orderBySql = sql` order by ${sql.join(orderByValues, sql`, `)} `; } - const limitSql = limit ? sql` limit ${limit}` : undefined; + const limitSql = + typeof limit === "object" || (typeof limit === "number" && limit >= 0) + ? sql` limit ${limit}` + : undefined; const operatorChunk = sql.raw(`${type} ${isAll ? 'all ' : ''}`); diff --git a/drizzle-orm/src/pg-core/dialect.ts b/drizzle-orm/src/pg-core/dialect.ts index 7fed65c4c..1b88ecae8 100644 --- a/drizzle-orm/src/pg-core/dialect.ts +++ b/drizzle-orm/src/pg-core/dialect.ts @@ -357,7 +357,10 @@ export class PgDialect { groupBySql = sql` group by ${sql.join(groupBy, sql`, `)}`; } - const limitSql = limit ? sql` limit ${limit}` : undefined; + const limitSql = + typeof limit === "object" || (typeof limit === "number" && limit >= 0) + ? sql` limit ${limit}` + : undefined; const offsetSql = offset ? sql` offset ${offset}` : undefined; @@ -443,7 +446,10 @@ export class PgDialect { orderBySql = sql` order by ${sql.join(orderByValues, sql`, `)} `; } - const limitSql = limit ? sql` limit ${limit}` : undefined; + const limitSql = + typeof limit === "object" || (typeof limit === "number" && limit >= 0) + ? sql` limit ${limit}` + : undefined; const operatorChunk = sql.raw(`${type} ${isAll ? 'all ' : ''}`); diff --git a/drizzle-orm/src/sqlite-core/dialect.ts b/drizzle-orm/src/sqlite-core/dialect.ts index aa229d231..908c6f28b 100644 --- a/drizzle-orm/src/sqlite-core/dialect.ts +++ b/drizzle-orm/src/sqlite-core/dialect.ts @@ -295,7 +295,10 @@ export abstract class SQLiteDialect { const orderBySql = orderByList.length > 0 ? sql` order by ${sql.join(orderByList)}` : undefined; - const limitSql = limit ? sql` limit ${limit}` : undefined; + const limitSql = + typeof limit === "object" || (typeof limit === "number" && limit >= 0) + ? sql` limit ${limit}` + : undefined; const offsetSql = offset ? sql` offset ${offset}` : undefined; @@ -362,7 +365,10 @@ export abstract class SQLiteDialect { orderBySql = sql` order by ${sql.join(orderByValues, sql`, `)}`; } - const limitSql = limit ? sql` limit ${limit}` : undefined; + const limitSql = + typeof limit === "object" || (typeof limit === "number" && limit >= 0) + ? sql` limit ${limit}` + : undefined; const operatorChunk = sql.raw(`${type} ${isAll ? 'all ' : ''}`); From 261728b29a86dd62260396eec740f97daffb727a Mon Sep 17 00:00:00 2001 From: Matt DeKok <5138384+sillvva@users.noreply.github.com> Date: Fri, 19 Jul 2024 15:50:44 +0000 Subject: [PATCH 2/4] Add Tests: Limit 0 --- integration-tests/tests/mysql/mysql-common.ts | 12 ++++++++++++ integration-tests/tests/pg/pg-common.ts | 12 ++++++++++++ integration-tests/tests/sqlite/sqlite-common.ts | 12 ++++++++++++ 3 files changed, 36 insertions(+) diff --git a/integration-tests/tests/mysql/mysql-common.ts b/integration-tests/tests/mysql/mysql-common.ts index db1486270..2a3b5d8b0 100644 --- a/integration-tests/tests/mysql/mysql-common.ts +++ b/integration-tests/tests/mysql/mysql-common.ts @@ -3486,4 +3486,16 @@ export function tests(driver?: string) { await db.execute(sql`drop view ${newYorkers1}`); }); }); + + test('limit 0', async (ctx) => { + const { db } = ctx.mysql; + + await db.insert(usersTable).values({ name: 'John' }); + const users = await db + .select() + .from(usersTable) + .limit(0); + + expect(users).toEqual([]); + }); } diff --git a/integration-tests/tests/pg/pg-common.ts b/integration-tests/tests/pg/pg-common.ts index b668238f2..b30fb78ef 100644 --- a/integration-tests/tests/pg/pg-common.ts +++ b/integration-tests/tests/pg/pg-common.ts @@ -4428,5 +4428,17 @@ export function tests() { await db.execute(sql`drop materialized view ${newYorkers1}`); }); + + test('limit 0', async (ctx) => { + const { db } = ctx.pg; + + await db.insert(usersTable).values({ name: 'John' }); + const users = await db + .select() + .from(usersTable) + .limit(0); + + expect(users).toEqual([]); + }); }); } diff --git a/integration-tests/tests/sqlite/sqlite-common.ts b/integration-tests/tests/sqlite/sqlite-common.ts index a816d8cca..6361e126c 100644 --- a/integration-tests/tests/sqlite/sqlite-common.ts +++ b/integration-tests/tests/sqlite/sqlite-common.ts @@ -2681,4 +2681,16 @@ export function tests() { expect(columnField?.isUnique).toBeTruthy(); expect(columnField?.uniqueName).toBe(uniqueKeyName(cities1Table, [columnField!.name])); }); + + test('limit 0', async (ctx) => { + const { db } = ctx.sqlite; + + await db.insert(usersTable).values({ name: 'John' }); + const users = await db + .select() + .from(usersTable) + .limit(0); + + expect(users).toEqual([]); + }); } From 300ab574b73d1ab086c7fbd5fb2935f25b8f106c Mon Sep 17 00:00:00 2001 From: Matt DeKok <5138384+sillvva@users.noreply.github.com> Date: Mon, 22 Jul 2024 13:23:01 +0000 Subject: [PATCH 3/4] Add Tests: Limit -1 --- integration-tests/tests/mysql/mysql-common.ts | 12 ++++++++++++ integration-tests/tests/pg/pg-common.ts | 12 ++++++++++++ integration-tests/tests/sqlite/sqlite-common.ts | 12 ++++++++++++ 3 files changed, 36 insertions(+) diff --git a/integration-tests/tests/mysql/mysql-common.ts b/integration-tests/tests/mysql/mysql-common.ts index 2a3b5d8b0..9a8532eb6 100644 --- a/integration-tests/tests/mysql/mysql-common.ts +++ b/integration-tests/tests/mysql/mysql-common.ts @@ -3498,4 +3498,16 @@ export function tests(driver?: string) { expect(users).toEqual([]); }); + + test('limit -1', async (ctx) => { + const { db } = ctx.mysql; + + await db.insert(usersTable).values({ name: 'John' }); + const users = await db + .select() + .from(usersTable) + .limit(-1); + + expect(users.length).toBeGreaterThan(0); + }); } diff --git a/integration-tests/tests/pg/pg-common.ts b/integration-tests/tests/pg/pg-common.ts index b30fb78ef..4e76fa055 100644 --- a/integration-tests/tests/pg/pg-common.ts +++ b/integration-tests/tests/pg/pg-common.ts @@ -4440,5 +4440,17 @@ export function tests() { expect(users).toEqual([]); }); + + test('limit -1', async (ctx) => { + const { db } = ctx.pg; + + await db.insert(usersTable).values({ name: 'John' }); + const users = await db + .select() + .from(usersTable) + .limit(-1); + + expect(users.length).toBeGreaterThan(0); + }); }); } diff --git a/integration-tests/tests/sqlite/sqlite-common.ts b/integration-tests/tests/sqlite/sqlite-common.ts index 6361e126c..5a96342bd 100644 --- a/integration-tests/tests/sqlite/sqlite-common.ts +++ b/integration-tests/tests/sqlite/sqlite-common.ts @@ -2693,4 +2693,16 @@ export function tests() { expect(users).toEqual([]); }); + + test('limit -1', async (ctx) => { + const { db } = ctx.sqlite; + + await db.insert(usersTable).values({ name: 'John' }); + const users = await db + .select() + .from(usersTable) + .limit(-1); + + expect(users.length).toBeGreaterThan(0); + }); } From ede059603e44018d48fd6ea9831c769818a80b34 Mon Sep 17 00:00:00 2001 From: Matt DeKok <5138384+sillvva@users.noreply.github.com> Date: Mon, 22 Jul 2024 17:46:33 +0000 Subject: [PATCH 4/4] dprint format --- drizzle-orm/src/mysql-core/dialect.ts | 10 ++++------ drizzle-orm/src/pg-core/dialect.ts | 10 ++++------ drizzle-orm/src/sqlite-core/dialect.ts | 10 ++++------ 3 files changed, 12 insertions(+), 18 deletions(-) diff --git a/drizzle-orm/src/mysql-core/dialect.ts b/drizzle-orm/src/mysql-core/dialect.ts index f3ede7d0d..4a72d9c5f 100644 --- a/drizzle-orm/src/mysql-core/dialect.ts +++ b/drizzle-orm/src/mysql-core/dialect.ts @@ -326,9 +326,8 @@ export class MySqlDialect { groupBySql = sql` group by ${sql.join(groupBy, sql`, `)}`; } - const limitSql = - typeof limit === "object" || (typeof limit === "number" && limit >= 0) - ? sql` limit ${limit}` + const limitSql = typeof limit === 'object' || (typeof limit === 'number' && limit >= 0) + ? sql` limit ${limit}` : undefined; const offsetSql = offset ? sql` offset ${offset}` : undefined; @@ -406,9 +405,8 @@ export class MySqlDialect { orderBySql = sql` order by ${sql.join(orderByValues, sql`, `)} `; } - const limitSql = - typeof limit === "object" || (typeof limit === "number" && limit >= 0) - ? sql` limit ${limit}` + const limitSql = typeof limit === 'object' || (typeof limit === 'number' && limit >= 0) + ? sql` limit ${limit}` : undefined; const operatorChunk = sql.raw(`${type} ${isAll ? 'all ' : ''}`); diff --git a/drizzle-orm/src/pg-core/dialect.ts b/drizzle-orm/src/pg-core/dialect.ts index 65a1a53b4..fff2ce65a 100644 --- a/drizzle-orm/src/pg-core/dialect.ts +++ b/drizzle-orm/src/pg-core/dialect.ts @@ -357,9 +357,8 @@ export class PgDialect { groupBySql = sql` group by ${sql.join(groupBy, sql`, `)}`; } - const limitSql = - typeof limit === "object" || (typeof limit === "number" && limit >= 0) - ? sql` limit ${limit}` + const limitSql = typeof limit === 'object' || (typeof limit === 'number' && limit >= 0) + ? sql` limit ${limit}` : undefined; const offsetSql = offset ? sql` offset ${offset}` : undefined; @@ -446,9 +445,8 @@ export class PgDialect { orderBySql = sql` order by ${sql.join(orderByValues, sql`, `)} `; } - const limitSql = - typeof limit === "object" || (typeof limit === "number" && limit >= 0) - ? sql` limit ${limit}` + const limitSql = typeof limit === 'object' || (typeof limit === 'number' && limit >= 0) + ? sql` limit ${limit}` : undefined; const operatorChunk = sql.raw(`${type} ${isAll ? 'all ' : ''}`); diff --git a/drizzle-orm/src/sqlite-core/dialect.ts b/drizzle-orm/src/sqlite-core/dialect.ts index 3e4e44932..645e15592 100644 --- a/drizzle-orm/src/sqlite-core/dialect.ts +++ b/drizzle-orm/src/sqlite-core/dialect.ts @@ -295,9 +295,8 @@ export abstract class SQLiteDialect { const orderBySql = orderByList.length > 0 ? sql` order by ${sql.join(orderByList)}` : undefined; - const limitSql = - typeof limit === "object" || (typeof limit === "number" && limit >= 0) - ? sql` limit ${limit}` + const limitSql = typeof limit === 'object' || (typeof limit === 'number' && limit >= 0) + ? sql` limit ${limit}` : undefined; const offsetSql = offset ? sql` offset ${offset}` : undefined; @@ -365,9 +364,8 @@ export abstract class SQLiteDialect { orderBySql = sql` order by ${sql.join(orderByValues, sql`, `)}`; } - const limitSql = - typeof limit === "object" || (typeof limit === "number" && limit >= 0) - ? sql` limit ${limit}` + const limitSql = typeof limit === 'object' || (typeof limit === 'number' && limit >= 0) + ? sql` limit ${limit}` : undefined; const operatorChunk = sql.raw(`${type} ${isAll ? 'all ' : ''}`);