Skip to content

Commit

Permalink
fix(mysql): cannot bind LIMIT or OFFSET
Browse files Browse the repository at this point in the history
  • Loading branch information
lohart13 committed Feb 7, 2024
1 parent 260c227 commit 6c58304
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
Expand Up @@ -172,7 +172,8 @@ export class MySqlQueryGeneratorTypeScript extends AbstractQueryGenerator {
return 'UUID()';
}

protected _addLimitAndOffset(options: AddLimitOffsetOptions) {
// Due to https://github.com/sidorares/node-mysql2/issues/1239 we cannot use bind parameters for LIMIT and OFFSET
protected _addLimitAndOffset({ bindParam: _, ...options }: AddLimitOffsetOptions) {
let fragment = '';
if (options.limit != null) {
fragment += ` LIMIT ${this.escape(options.limit, options)}`;
Expand Down
6 changes: 6 additions & 0 deletions packages/core/test/unit/query-generator/update-query.test.ts
Expand Up @@ -39,6 +39,7 @@ describe('QueryGenerator#updateQuery', () => {
it('generates an update query with a where clause and limit', () => {
expectsql(() => queryGenerator.updateQuery('myTable', { status: 'bar' }, { where: { id: 2 }, limit: 1 }).query, {
default: 'UPDATE [myTable] SET [status]=$sequelize_1 WHERE [id] = $sequelize_2 LIMIT $sequelize_3',
mysql: 'UPDATE `myTable` SET `status`=$sequelize_1 WHERE `id` = $sequelize_2 LIMIT 1',
sqlite: 'UPDATE `myTable` SET `status`=$sequelize_1 WHERE rowid IN (SELECT rowid FROM `myTable` WHERE `id` = $sequelize_2 LIMIT $sequelize_3)',
'db2 ibmi': 'SELECT * FROM FINAL TABLE (UPDATE "myTable" SET "status"=$sequelize_1 WHERE "id" = $sequelize_2 FETCH NEXT $sequelize_3 ROWS ONLY)',
'mssql postgres snowflake': limitWithoutModelError,
Expand All @@ -48,6 +49,7 @@ describe('QueryGenerator#updateQuery', () => {
it('generates an update query with a where clause, limit and offset', () => {
expectsql(() => queryGenerator.updateQuery('myTable', { status: 'bar' }, { where: { id: 2 }, limit: 1, offset: 2 }).query, {
default: 'UPDATE [myTable] SET [status]=$sequelize_1 WHERE [id] = $sequelize_2 LIMIT $sequelize_3 OFFSET $sequelize_4',
mysql: 'UPDATE `myTable` SET `status`=$sequelize_1 WHERE `id` = $sequelize_2 LIMIT 1 OFFSET 2',
sqlite: 'UPDATE `myTable` SET `status`=$sequelize_1 WHERE rowid IN (SELECT rowid FROM `myTable` WHERE `id` = $sequelize_2 LIMIT $sequelize_3 OFFSET $sequelize_4)',
'db2 ibmi': 'SELECT * FROM FINAL TABLE (UPDATE "myTable" SET "status"=$sequelize_1 WHERE "id" = $sequelize_2 OFFSET $sequelize_3 ROWS FETCH NEXT $sequelize_4 ROWS ONLY)',
'mssql postgres snowflake': limitWithoutModelError,
Expand All @@ -59,13 +61,15 @@ describe('QueryGenerator#updateQuery', () => {
query: {
default: 'UPDATE [Users] SET [firstName]=$sequelize_1 WHERE [id] = $sequelize_2 LIMIT $sequelize_3',
mssql: 'UPDATE [Users] SET [firstName]=$sequelize_1 WHERE [id] IN (SELECT [id] FROM [Users] WHERE [id] = $sequelize_2 ORDER BY [id] OFFSET $sequelize_3 ROWS FETCH NEXT $sequelize_4 ROWS ONLY)',
mysql: 'UPDATE `Users` SET `firstName`=$sequelize_1 WHERE `id` = $sequelize_2 LIMIT 1',
sqlite: 'UPDATE `Users` SET `firstName`=$sequelize_1 WHERE rowid IN (SELECT rowid FROM `Users` WHERE `id` = $sequelize_2 LIMIT $sequelize_3)',
'db2 ibmi': 'SELECT * FROM FINAL TABLE (UPDATE "Users" SET "firstName"=$sequelize_1 WHERE "id" = $sequelize_2 FETCH NEXT $sequelize_3 ROWS ONLY)',
'postgres snowflake': 'UPDATE [Users] SET [firstName]=$sequelize_1 WHERE [id] IN (SELECT [id] FROM [Users] WHERE [id] = $sequelize_2 ORDER BY [id] LIMIT $sequelize_3)',
},
bind: {
default: { sequelize_1: 'bar', sequelize_2: 2, sequelize_3: 1 },
mssql: { sequelize_1: 'bar', sequelize_2: 2, sequelize_3: 0, sequelize_4: 1 },
mysql: { sequelize_1: 'bar', sequelize_2: 2 },
},
});
});
Expand All @@ -75,6 +79,7 @@ describe('QueryGenerator#updateQuery', () => {
query: {
default: 'UPDATE [Users] SET [firstName]=$sequelize_1 WHERE [id] = $sequelize_2 LIMIT $sequelize_3 OFFSET $sequelize_4',
mssql: 'UPDATE [Users] SET [firstName]=$sequelize_1 WHERE [id] IN (SELECT [id] FROM [Users] WHERE [id] = $sequelize_2 ORDER BY [id] OFFSET $sequelize_3 ROWS FETCH NEXT $sequelize_4 ROWS ONLY)',
mysql: 'UPDATE `Users` SET `firstName`=$sequelize_1 WHERE `id` = $sequelize_2 LIMIT 1 OFFSET 2',
sqlite: 'UPDATE `Users` SET `firstName`=$sequelize_1 WHERE rowid IN (SELECT rowid FROM `Users` WHERE `id` = $sequelize_2 LIMIT $sequelize_3 OFFSET $sequelize_4)',
'db2 ibmi': 'SELECT * FROM FINAL TABLE (UPDATE "Users" SET "firstName"=$sequelize_1 WHERE "id" = $sequelize_2 OFFSET $sequelize_3 ROWS FETCH NEXT $sequelize_4 ROWS ONLY)',
'postgres snowflake': 'UPDATE [Users] SET [firstName]=$sequelize_1 WHERE [id] IN (SELECT [id] FROM [Users] WHERE [id] = $sequelize_2 ORDER BY [id] LIMIT $sequelize_3 OFFSET $sequelize_4)',
Expand All @@ -84,6 +89,7 @@ describe('QueryGenerator#updateQuery', () => {
db2: { sequelize_1: 'bar', sequelize_2: 2, sequelize_3: 2, sequelize_4: 1 },
ibmi: { sequelize_1: 'bar', sequelize_2: 2, sequelize_3: 2, sequelize_4: 1 },
mssql: { sequelize_1: 'bar', sequelize_2: 2, sequelize_3: 2, sequelize_4: 1 },
mysql: { sequelize_1: 'bar', sequelize_2: 2 },
},
});
});
Expand Down

0 comments on commit 6c58304

Please sign in to comment.