Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
public
/
sequelize
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
不要怂,就是干,撸起袖子干!
Commit 5e833715
authored
May 02, 2018
by
Sushant
Committed by
GitHub
May 02, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix(query-generator/deleteQuery): remove auto limit (#9377)
1 parent
7b163084
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
62 additions
and
85 deletions
lib/dialects/mssql/query-generator.js
lib/dialects/mysql/query-generator.js
lib/dialects/postgres/query-generator.js
lib/dialects/sqlite/query-generator.js
lib/query-interface.js
test/unit/sql/delete.test.js
lib/dialects/mssql/query-generator.js
View file @
5e83371
...
@@ -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
)
+
')'
;
...
...
lib/dialects/mysql/query-generator.js
View file @
5e83371
...
@@ -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
)
{
...
...
lib/dialects/postgres/query-generator.js
View file @
5e83371
...
@@ -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
:
t
ableName
,
table
:
t
his
.
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.'
);
...
...
lib/dialects/sqlite/query-generator.js
View file @
5e83371
...
@@ -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
||
{};
return
`DELETE FROM
${
this
.
quoteTable
(
tableName
)}
`
;
_
.
defaults
(
options
,
this
.
options
);
}
if
(
options
.
truncate
===
true
)
{
// Truncate does not allow LIMIT and WHERE
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
}
`
;
}
}
...
...
lib/query-interface.js
View file @
5e83371
...
@@ -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
)
{
...
...
test/unit/sql/delete.test.js
View file @
5e83371
...
@@ -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
.
delet
eQuery
(
sql
.
truncateTabl
eQuery
(
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
.
delet
eQuery
(
sql
.
truncateTabl
eQuery
(
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'
}
}
);
);
});
});
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment