Skip to content

Commit 55f56a8

Browse files
authored
validate where clause during update or delete (#127)
* validate where clause during update or delete * 2.6.75
1 parent a2be5c0 commit 55f56a8

File tree

3 files changed

+25
-12
lines changed

3 files changed

+25
-12
lines changed

formatter.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ SqlFormatter.prototype.escape = function(value,unquoted)
150150

151151
if (typeof value === 'object')
152152
{
153-
if (value instanceof JSONArray || value instanceof JSONObject) {
153+
if ((value instanceof JSONArray) || (value instanceof JSONObject)) {
154154
return SqlUtils.escape(value.toString());
155155
}
156156
//add an exception for Date object
@@ -992,7 +992,7 @@ SqlFormatter.prototype.formatSelect = function(obj)
992992
if (Object.prototype.hasOwnProperty.call(item[key], '$jsonEach')) {
993993
sqlSelect = $this.escape(item);
994994
sqlAlias = key;
995-
} else if (Object.prototype.hasOwnProperty.call(item, '$select')) {
995+
} else if (Object.prototype.hasOwnProperty.call(item, '$select') && typeof item.$select === 'object') {
996996
/**
997997
* parse a sub-query expression
998998
* @type {QueryExpression}
@@ -1308,15 +1308,22 @@ SqlFormatter.prototype.formatUpdate = function(obj)
13081308
for(var prop in obj1)
13091309
if (Object.prototype.hasOwnProperty.call(obj1, prop))
13101310
props.push(prop);
1311-
//add basic INSERT statement
1311+
//add basic UPDATE statement
13121312
sql = sql.concat('UPDATE ', self.escapeName(entity), ' SET ',
13131313
_.map(props, function(x)
13141314
{
13151315
var value = obj1[x];
13161316
return self.escapeName(x).concat('=', self.escape(value!==null ? value: null));
13171317
}).join(', '));
1318-
if (_.isObject(obj.$where))
1319-
sql = sql.concat(' WHERE ',this.formatWhere(obj.$where));
1318+
if (obj.$where == null) {
1319+
throw new Error('Update expression must have a where clause.');
1320+
}
1321+
1322+
var whereClause = this.formatWhere(obj.$where);
1323+
Args.check(whereClause != null, new Error('Update expression must have a valid "where" clause.'));
1324+
Args.check(typeof whereClause === 'string' && whereClause.trim().length > 0, new Error('"Where" clause cannot be empty at the context of an update expression.'));
1325+
sql = sql.concat(' WHERE ', whereClause);
1326+
13201327
return sql;
13211328
};
13221329

@@ -1334,8 +1341,14 @@ SqlFormatter.prototype.formatDelete = function(obj)
13341341
var entity = obj.$delete;
13351342
//add basic INSERT statement
13361343
sql = sql.concat('DELETE FROM ', this.escapeName(entity));
1337-
if (_.isObject(obj.$where))
1338-
sql = sql.concat(' WHERE ',this.formatWhere(obj.$where));
1344+
if (obj.$where == null) {
1345+
throw new Error('Delete expression must have a where clause.');
1346+
}
1347+
var whereClause = this.formatWhere(obj.$where);
1348+
Args.check(whereClause != null, new Error('Delete expression must have a valid "where" clause.'));
1349+
Args.check(typeof whereClause === 'string' && whereClause.trim().length > 0, new Error('"Where" clause cannot be empty at the context of a delete expression.'));
1350+
sql = sql.concat(' WHERE ', whereClause);
1351+
13391352
return sql;
13401353
};
13411354

@@ -1486,9 +1499,9 @@ SqlFormatter.prototype.format = function(obj, s)
14861499
}
14871500
else if (typeof query.$insert === 'object')
14881501
return this.formatInsert(query);
1489-
else if (typeof query.$update === 'object')
1502+
else if (typeof query.$update === 'object' || typeof query.$update === 'string')
14901503
return this.formatUpdate(query);
1491-
else if (typeof query.$delete === 'object')
1504+
else if (typeof query.$delete === 'object' || typeof query.$delete === 'string')
14921505
return this.formatDelete(query);
14931506
else if (typeof query.$where === 'object')
14941507
return this.formatWhere(query.$where);

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@themost/query",
3-
"version": "2.6.74",
3+
"version": "2.6.75",
44
"description": "@themost/query is a query builder for SQL. It includes a wide variety of helper functions for building complex SQL queries under node.js.",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)