Skip to content

Commit

Permalink
Merge pull request drizzle-team#2255 from sillvva/main
Browse files Browse the repository at this point in the history
feat: add support for "limit 0"
  • Loading branch information
AndriiSherman committed Jul 23, 2024
2 parents bdbadeb + bd273a9 commit 026b9bb
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 6 deletions.
8 changes: 6 additions & 2 deletions drizzle-orm/src/mysql-core/dialect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,9 @@ 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;

Expand Down Expand Up @@ -403,7 +405,9 @@ 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 ' : ''}`);

Expand Down
8 changes: 6 additions & 2 deletions drizzle-orm/src/pg-core/dialect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,9 @@ 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;

Expand Down Expand Up @@ -443,7 +445,9 @@ 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 ' : ''}`);

Expand Down
8 changes: 6 additions & 2 deletions drizzle-orm/src/sqlite-core/dialect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,9 @@ 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;

Expand Down Expand Up @@ -362,7 +364,9 @@ 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 ' : ''}`);

Expand Down
24 changes: 24 additions & 0 deletions integration-tests/tests/mysql/mysql-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3515,4 +3515,28 @@ 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([]);
});

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);
});
}
24 changes: 24 additions & 0 deletions integration-tests/tests/pg/pg-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4457,5 +4457,29 @@ 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([]);
});

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);
});
});
}
24 changes: 24 additions & 0 deletions integration-tests/tests/sqlite/sqlite-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2710,4 +2710,28 @@ 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([]);
});

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);
});
}

0 comments on commit 026b9bb

Please sign in to comment.