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 {
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
)
+
')'
;
...
...
lib/dialects/mysql/query-generator.js
View file @
5e83371
...
...
@@ -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
)
{
...
...
lib/dialects/postgres/query-generator.js
View file @
5e83371
...
...
@@ -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
:
t
ableName
,
table
:
t
his
.
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.'
);
...
...
lib/dialects/sqlite/query-generator.js
View file @
5e83371
...
...
@@ -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
}
`
;
}
...
...
lib/query-interface.js
View file @
5e83371
...
...
@@ -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
)
{
...
...
test/unit/sql/delete.test.js
View file @
5e83371
...
...
@@ -27,11 +27,9 @@ suite(Support.getTestDialectTeaser('SQL'), () => {
test
(
util
.
inspect
(
options
,
{
depth
:
2
}),
()
=>
{
return
expectsql
(
sql
.
delet
eQuery
(
sql
.
truncateTabl
eQuery
(
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
.
delet
eQuery
(
sql
.
truncateTabl
eQuery
(
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'
}
);
});
...
...
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