不要怂,就是干,撸起袖子干!

Commit 5e833715 by Sushant Committed by GitHub

fix(query-generator/deleteQuery): remove auto limit (#9377)

1 parent 7b163084
...@@ -475,23 +475,17 @@ class MSSQLQueryGenerator extends AbstractQueryGenerator { ...@@ -475,23 +475,17 @@ class MSSQLQueryGenerator extends AbstractQueryGenerator {
return query; return query;
} }
deleteQuery(tableName, where, options) { truncateTableQuery(tableName) {
options = options || {}; return `TRUNCATE TABLE ${this.quoteTable(tableName)}`;
}
deleteQuery(tableName, where, options = {}) {
const table = this.quoteTable(tableName); const table = this.quoteTable(tableName);
if (options.truncate === true) { const query = 'DELETE<%= limit %> FROM <%= table %><%= where %>; SELECT @@ROWCOUNT AS AFFECTEDROWS;';
// Truncate does not allow LIMIT and WHERE
return 'TRUNCATE TABLE ' + table;
}
where = this.getWhereConditions(where); where = this.getWhereConditions(where);
let limit = '';
const query = 'DELETE<%= limit %> FROM <%= table %><%= where %>; ' +
'SELECT @@ROWCOUNT AS AFFECTEDROWS;';
if (_.isUndefined(options.limit)) { let limit = '';
options.limit = 1;
}
if (options.limit) { if (options.limit) {
limit = ' TOP(' + this.escape(options.limit) + ')'; limit = ' TOP(' + this.escape(options.limit) + ')';
......
...@@ -244,31 +244,25 @@ class MySQLQueryGenerator extends AbstractQueryGenerator { ...@@ -244,31 +244,25 @@ class MySQLQueryGenerator extends AbstractQueryGenerator {
return this.insertQuery(tableName, insertValues, model.rawAttributes, options); return this.insertQuery(tableName, insertValues, model.rawAttributes, options);
} }
deleteQuery(tableName, where, options, model) { truncateTableQuery(tableName) {
options = options || {}; return `TRUNCATE ${this.quoteTable(tableName)}`;
const table = this.quoteTable(tableName);
if (options.truncate === true) {
// Truncate does not allow LIMIT and WHERE
return 'TRUNCATE ' + table;
} }
where = this.getWhereConditions(where, null, model, options); deleteQuery(tableName, where, options = {}, model) {
let limit = ''; let limit = '';
let query = 'DELETE FROM ' + this.quoteTable(tableName);
if (_.isUndefined(options.limit)) {
options.limit = 1;
}
if (options.limit) { if (options.limit) {
limit = ' LIMIT ' + this.escape(options.limit); limit = ' LIMIT ' + this.escape(options.limit);
} }
let query = 'DELETE FROM ' + table; where = this.getWhereConditions(where, null, model, options);
if (where) query += ' WHERE ' + where;
query += limit; if (where) {
query += ' WHERE ' + where;
}
return query; return query + limit;
} }
showIndexesQuery(tableName, options) { showIndexesQuery(tableName, options) {
......
...@@ -359,37 +359,23 @@ class PostgresQueryGenerator extends AbstractQueryGenerator { ...@@ -359,37 +359,23 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {
); );
} }
deleteQuery(tableName, where, options, model) { truncateTableQuery(tableName, options = {}) {
let query; return [
`TRUNCATE ${this.quoteTable(tableName)}`,
options = options || {}; options.restartIdentity ? ' RESTART IDENTITY' : '',
options.cascade ? ' CASCADE' : ''
tableName = this.quoteTable(tableName); ].join('');
if (options.truncate === true) {
query = 'TRUNCATE ' + tableName;
if (options.restartIdentity) {
query += ' RESTART IDENTITY';
}
if (options.cascade) {
query += ' CASCADE';
}
return query;
}
if (_.isUndefined(options.limit)) {
options.limit = 1;
} }
deleteQuery(tableName, where, options = {}, model) {
const replacements = { const replacements = {
table: tableName, table: this.quoteTable(tableName),
where: this.getWhereConditions(where, null, model, options), where: this.getWhereConditions(where, null, model, options),
limit: options.limit ? ' LIMIT ' + this.escape(options.limit) : '' limit: options.limit ? ' LIMIT ' + this.escape(options.limit) : ''
}; };
let query;
if (options.limit) { if (options.limit) {
if (!model) { if (!model) {
throw new Error('Cannot LIMIT delete without a model.'); throw new Error('Cannot LIMIT delete without a model.');
......
...@@ -235,20 +235,15 @@ class SQLiteQueryGenerator extends MySqlQueryGenerator { ...@@ -235,20 +235,15 @@ class SQLiteQueryGenerator extends MySqlQueryGenerator {
} }
} }
deleteQuery(tableName, where, options, model) { truncateTableQuery(tableName) {
options = options || {};
_.defaults(options, this.options);
if (options.truncate === true) {
// Truncate does not allow LIMIT and WHERE
return `DELETE FROM ${this.quoteTable(tableName)}`; return `DELETE FROM ${this.quoteTable(tableName)}`;
} }
if (_.isUndefined(options.limit)) { deleteQuery(tableName, where, options = {}, model) {
options.limit = 1; _.defaults(options, this.options);
}
let whereClause = this.getWhereConditions(where, null, model, options); let whereClause = this.getWhereConditions(where, null, model, options);
if (whereClause) { if (whereClause) {
whereClause = `WHERE ${whereClause}`; whereClause = `WHERE ${whereClause}`;
} }
......
...@@ -1047,7 +1047,7 @@ class QueryInterface { ...@@ -1047,7 +1047,7 @@ class QueryInterface {
delete(instance, tableName, identifier, options) { delete(instance, tableName, identifier, options) {
const cascades = []; const cascades = [];
const sql = this.QueryGenerator.deleteQuery(tableName, identifier, null, instance.constructor); const sql = this.QueryGenerator.deleteQuery(tableName, identifier, {}, instance.constructor);
options = _.clone(options) || {}; options = _.clone(options) || {};
...@@ -1087,18 +1087,30 @@ class QueryInterface { ...@@ -1087,18 +1087,30 @@ class QueryInterface {
/** /**
* Delete records from a table * Delete records from a table
* *
* @param {String} tableName Table name from where to delete records * @param {String} tableName table name from where to delete records
* @param {Object} identifier Where conditions to find records to delete * @param {Object} where where conditions to find records to delete
* @param {Object} [options] options
* @param {Boolean} [options.truncate]
* *
* @return {Promise} * @return {Promise}
*/ */
bulkDelete(tableName, identifier, options, model) { bulkDelete(tableName, where, options, model) {
options = Utils.cloneDeep(options); options = Utils.cloneDeep(options);
options = _.defaults(options, {limit: null}); options = _.defaults(options, { limit: null });
if (typeof identifier === 'object') identifier = Utils.cloneDeep(identifier);
const sql = this.QueryGenerator.deleteQuery(tableName, identifier, options, model); if (options.truncate === true) {
return this.sequelize.query(sql, options); return this.sequelize.query(
this.QueryGenerator.truncateTableQuery(tableName, options),
options
);
}
if (typeof identifier === 'object') where = Utils.cloneDeep(where);
return this.sequelize.query(
this.QueryGenerator.deleteQuery(tableName, where, options, model),
options
);
} }
select(model, tableName, options) { select(model, tableName, options) {
......
...@@ -27,11 +27,9 @@ suite(Support.getTestDialectTeaser('SQL'), () => { ...@@ -27,11 +27,9 @@ suite(Support.getTestDialectTeaser('SQL'), () => {
test(util.inspect(options, {depth: 2}), () => { test(util.inspect(options, {depth: 2}), () => {
return expectsql( return expectsql(
sql.deleteQuery( sql.truncateTableQuery(
options.table, options.table,
options.where, options
options,
User
), { ), {
postgres: 'TRUNCATE "public"."test_users" CASCADE', postgres: 'TRUNCATE "public"."test_users" CASCADE',
mssql: 'TRUNCATE TABLE [public].[test_users]', mssql: 'TRUNCATE TABLE [public].[test_users]',
...@@ -54,11 +52,9 @@ suite(Support.getTestDialectTeaser('SQL'), () => { ...@@ -54,11 +52,9 @@ suite(Support.getTestDialectTeaser('SQL'), () => {
test(util.inspect(options, {depth: 2}), () => { test(util.inspect(options, {depth: 2}), () => {
return expectsql( return expectsql(
sql.deleteQuery( sql.truncateTableQuery(
options.table, options.table,
options.where, options
options,
User
), { ), {
postgres: 'TRUNCATE "public"."test_users" RESTART IDENTITY CASCADE', postgres: 'TRUNCATE "public"."test_users" RESTART IDENTITY CASCADE',
mssql: 'TRUNCATE TABLE [public].[test_users]', mssql: 'TRUNCATE TABLE [public].[test_users]',
...@@ -173,10 +169,10 @@ suite(Support.getTestDialectTeaser('SQL'), () => { ...@@ -173,10 +169,10 @@ suite(Support.getTestDialectTeaser('SQL'), () => {
options, options,
User User
), { ), {
postgres: 'DELETE FROM "test_user" WHERE "test_user_id" IN (SELECT "test_user_id" FROM "test_user" WHERE "test_user_id" = 100 LIMIT 1)', postgres: 'DELETE FROM "test_user" WHERE "test_user_id" = 100',
sqlite: 'DELETE FROM `test_user` WHERE rowid IN (SELECT rowid FROM `test_user` WHERE `test_user_id` = 100 LIMIT 1)', sqlite: 'DELETE FROM `test_user` WHERE `test_user_id` = 100',
mssql: 'DELETE TOP(1) FROM [test_user] WHERE [test_user_id] = 100; SELECT @@ROWCOUNT AS AFFECTEDROWS;', mssql: 'DELETE FROM [test_user] WHERE [test_user_id] = 100; SELECT @@ROWCOUNT AS AFFECTEDROWS;',
default: 'DELETE FROM [test_user] WHERE [test_user_id] = 100 LIMIT 1' default: 'DELETE FROM [test_user] WHERE [test_user_id] = 100'
} }
); );
}); });
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!