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

Commit e59b9b62 by Sascha Depold

delete query

1 parent 6394c5b6
...@@ -104,14 +104,29 @@ var QueryGenerator = module.exports = { ...@@ -104,14 +104,29 @@ var QueryGenerator = module.exports = {
return Utils._.template(query)(replacements) return Utils._.template(query)(replacements)
}, },
// TODO /*
deleteQuery: function(tableName, where) { Returns a deletion query.
// if(Sequelize.Helper.Hash.isHash(values.where)) Parameters:
// values.where = Sequelize.Helper.SQL.hashToWhereConditions(values.where) - tableName -> Name of the table
// - where -> A hash with conditions (e.g. {name: 'foo'})
// query = "DELETE FROM `%{table}` WHERE %{where}" OR an ID as integer
// if(typeof values.limit == 'undefined') query += " LIMIT 1" OR a string with conditions (e.g. 'name="foo"').
// else if(values.limit != null) query += " LIMIT " + values.limit If you use a string, you have to escape it on your own.
Options:
- limit -> Maximaum count of lines to delete
*/
deleteQuery: function(tableName, where, options) {
options = options || {}
options.limit = options.limit || 1
var query = "DELETE FROM <%= table %> WHERE <%= where %> LIMIT <%= limit %>"
var replacements = {
table: Utils.addTicks(tableName),
where: QueryGenerator.getWhereConditions(where),
limit: client.escape(options.limit)
}
return Utils._.template(query)(replacements)
}, },
getWhereConditions: function(smth) { getWhereConditions: function(smth) {
......
...@@ -52,5 +52,11 @@ module.exports = { ...@@ -52,5 +52,11 @@ module.exports = {
) )
eql(QueryGenerator.updateQuery('myTable', {bar: 2}, {name: 'foo'}), "UPDATE `myTable` SET `bar`=2 WHERE `name`='foo'") eql(QueryGenerator.updateQuery('myTable', {bar: 2}, {name: 'foo'}), "UPDATE `myTable` SET `bar`=2 WHERE `name`='foo'")
eql(QueryGenerator.updateQuery('myTable', {name: "foo';DROP TABLE myTable;"}, {name: 'foo'}), "UPDATE `myTable` SET `name`='foo\\';DROP TABLE myTable;' WHERE `name`='foo'") eql(QueryGenerator.updateQuery('myTable', {name: "foo';DROP TABLE myTable;"}, {name: 'foo'}), "UPDATE `myTable` SET `name`='foo\\';DROP TABLE myTable;' WHERE `name`='foo'")
},
'deleteQuery': function() {
eql(QueryGenerator.deleteQuery('myTable', {name: 'foo'}), "DELETE FROM `myTable` WHERE `name`='foo' LIMIT 1")
eql(QueryGenerator.deleteQuery('myTable', 1), "DELETE FROM `myTable` WHERE `id`=1 LIMIT 1")
eql(QueryGenerator.deleteQuery('myTable', 1, {limit: 10}), "DELETE FROM `myTable` WHERE `id`=1 LIMIT 10")
eql(QueryGenerator.deleteQuery('myTable', {name: "foo';DROP TABLE myTable;"}, {limit: 10}), "DELETE FROM `myTable` WHERE `name`='foo\\';DROP TABLE myTable;' LIMIT 10")
} }
} }
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!