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

Commit b64b3541 by Jan Aagaard Meier

merge #527

1 parent 3416639b
...@@ -290,7 +290,8 @@ module.exports = (function() { ...@@ -290,7 +290,8 @@ module.exports = (function() {
deleteQuery: function(tableName, where, options) { deleteQuery: function(tableName, where, options) {
options = options || {} options = options || {}
var table = QueryGenerator.addQuotes(tableName) var query = options && options.truncate === true ? "TRUNCATE " : "DELETE FROM "
, table = QueryGenerator.addQuotes(tableName)
where = QueryGenerator.getWhereConditions(where) where = QueryGenerator.getWhereConditions(where)
var limit = "" var limit = ""
...@@ -302,7 +303,7 @@ module.exports = (function() { ...@@ -302,7 +303,7 @@ module.exports = (function() {
limit = " LIMIT " + Utils.escape(options.limit) limit = " LIMIT " + Utils.escape(options.limit)
} }
var query = "DELETE FROM " + table + " WHERE " + where + limit query += table + " WHERE " + where + limit
return query return query
}, },
......
...@@ -369,7 +369,8 @@ module.exports = (function() { ...@@ -369,7 +369,8 @@ module.exports = (function() {
primaryKeys[tableName] = primaryKeys[tableName] || []; primaryKeys[tableName] = primaryKeys[tableName] || [];
var query = "DELETE FROM <%= table %> WHERE <%= primaryKeys %> IN (SELECT <%= primaryKeysSelection %> FROM <%= table %> WHERE <%= where %><%= limit %>)" var query = options && options.truncate === true ? "TRUNCATE <%= table %>" : "DELETE FROM <%= table %>"
query += " WHERE <%= primaryKeys %> IN (SELECT <%= primaryKeysSelection %> FROM <%= table %> WHERE <%= where %><%= limit %>)"
var pks; var pks;
if (primaryKeys[tableName] && primaryKeys[tableName].length > 0) { if (primaryKeys[tableName] && primaryKeys[tableName].length > 0) {
......
...@@ -150,6 +150,10 @@ module.exports = (function() { ...@@ -150,6 +150,10 @@ module.exports = (function() {
If you use a string, you have to escape it on your own. If you use a string, you have to escape it on your own.
Options: Options:
- limit -> Maximaum count of lines to delete - limit -> Maximaum count of lines to delete
- truncate -> boolean - whether to use an 'optimized' mechanism (i.e. TRUNCATE) if available,
note that this should not be the default behaviour because TRUNCATE does not
always play nicely (e.g. InnoDB tables with FK constraints)
(@see http://dev.mysql.com/doc/refman/5.6/en/truncate-table.html)
*/ */
deleteQuery: function(tableName, where, options) { deleteQuery: function(tableName, where, options) {
throwMethodUndefined('deleteQuery') throwMethodUndefined('deleteQuery')
......
...@@ -300,6 +300,9 @@ describe('QueryGenerator', function() { ...@@ -300,6 +300,9 @@ describe('QueryGenerator', function() {
}, { }, {
arguments: ['myTable', 1], arguments: ['myTable', 1],
expectation: "DELETE FROM `myTable` WHERE `id`=1 LIMIT 1" expectation: "DELETE FROM `myTable` WHERE `id`=1 LIMIT 1"
},{
arguments: ['myTable', 1, {truncate: true}],
expectation: "TRUNCATE `myTable` WHERE `id`=1 LIMIT 1"
}, { }, {
arguments: ['myTable', 1, {limit: 10}], arguments: ['myTable', 1, {limit: 10}],
expectation: "DELETE FROM `myTable` WHERE `id`=1 LIMIT 10" expectation: "DELETE FROM `myTable` WHERE `id`=1 LIMIT 10"
......
...@@ -293,6 +293,9 @@ describe('QueryGenerator', function() { ...@@ -293,6 +293,9 @@ describe('QueryGenerator', function() {
arguments: ['myTable', 1], arguments: ['myTable', 1],
expectation: "DELETE FROM \"myTable\" WHERE \"id\" IN (SELECT \"id\" FROM \"myTable\" WHERE \"id\"=1 LIMIT 1)" expectation: "DELETE FROM \"myTable\" WHERE \"id\" IN (SELECT \"id\" FROM \"myTable\" WHERE \"id\"=1 LIMIT 1)"
}, { }, {
arguments: ['myTable', 1, {truncate: true}],
expectation: "TRUNCATE \"myTable\" WHERE \"id\" IN (SELECT \"id\" FROM \"myTable\" WHERE \"id\"=1 LIMIT 1)"
}, {
arguments: ['myTable', 1, {limit: 10}], arguments: ['myTable', 1, {limit: 10}],
expectation: "DELETE FROM \"myTable\" WHERE \"id\" IN (SELECT \"id\" FROM \"myTable\" WHERE \"id\"=1 LIMIT 10)" expectation: "DELETE FROM \"myTable\" WHERE \"id\" IN (SELECT \"id\" FROM \"myTable\" WHERE \"id\"=1 LIMIT 10)"
}, { }, {
......
...@@ -199,6 +199,9 @@ describe('QueryGenerator', function() { ...@@ -199,6 +199,9 @@ describe('QueryGenerator', function() {
arguments: ['myTable', 1], arguments: ['myTable', 1],
expectation: "DELETE FROM `myTable` WHERE `id`=1" expectation: "DELETE FROM `myTable` WHERE `id`=1"
}, { }, {
arguments: ['myTable', 1, {truncate: true}],
expectation: "DELETE FROM `myTable` WHERE `id`=1"
}, {
arguments: ['myTable', 1, {limit: 10}], arguments: ['myTable', 1, {limit: 10}],
expectation: "DELETE FROM `myTable` WHERE `id`=1" expectation: "DELETE FROM `myTable` WHERE `id`=1"
}, { }, {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!