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

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