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

Commit 2dbdbc47 by Jan Aagaard Meier

Merge pull request #2257 from findhit/patch/paranoid-where-clause

paranoidClause: should handle sequelize.[and|or] correctly
2 parents 8eb98ece 330318a6
......@@ -1613,7 +1613,7 @@ module.exports = (function() {
// private
var paranoidClause = function(options ) {
var paranoidClause = function(options) {
if (! this.options.timestamps || ! this.options.paranoid || options.paranoid === false) return options || {};
options = options || {};
......@@ -1624,53 +1624,27 @@ module.exports = (function() {
// otherwise this code will never run on includes of a already conditionable where
if (options.include && options.include.length) {
options.include.forEach(function(include) {
if (typeof include === 'object' && include.model) {
if (Utils._.isPlainObject(include) && include.model) {
paranoidClause.call(include.model, include);
}
});
}
var deletedAtCol = this._timestampAttributes.deletedAt
, quoteIdentifiedDeletedAtCol = this.QueryInterface.quoteIdentifier(deletedAtCol);
, deletedAtObject = {};
// Don't overwrite our explicit deletedAt search value if we provide one
if (!!options.where[deletedAtCol]) {
return options;
}
if (this.name || this.tableName) {
quoteIdentifiedDeletedAtCol = this.QueryInterface.quoteIdentifier(this.name || this.tableName) + '.' + quoteIdentifiedDeletedAtCol;
}
deletedAtObject[ deletedAtCol ] = null;
if (typeof options.where === 'string') {
options.where += ' AND ' + quoteIdentifiedDeletedAtCol + ' IS NULL ';
}
else if (Array.isArray(options.where)) {
// Need to check how the array is being used, as it could be an array
// of string-likes/Dates or an array with objects, hashes, or other
// complex data types
if (Utils.canTreatArrayAsAnd(options.where)) {
// Don't overwrite our explicit deletedAt search value if we provide one
var whereClause = this.QueryGenerator.getWhereConditions(options.where, this.getTableName(), this, options);
if (whereClause.indexOf(deletedAtCol) !== -1) {
return options;
}
var whereObj = {};
whereObj[quoteIdentifiedDeletedAtCol] = null;
options.where.push(whereObj);
} else if (options.where.length) {
if (options.where[0].indexOf(deletedAtCol) !== -1) {
return options;
}
// Don't overwrite our explicit deletedAt search value if we provide one
options.where[0] += ' AND ' + quoteIdentifiedDeletedAtCol + ' IS NULL ';
} else {
options.where[0] = quoteIdentifiedDeletedAtCol + ' IS NULL ';
}
} else {
options.where[deletedAtCol] = null;
// detect emptiness
if(
Utils._.isObject(options.where) && Object.keys( options.where ).length === 0 ||
( Utils._.isString(options.where) || Utils._.isArray(options.where) ) && options.where.length === 0
) {
options.where = deletedAtObject;
return options;
}
options.where = this.sequelize.and( deletedAtObject, options.where );
return options;
};
......
......@@ -2250,12 +2250,13 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
})
})
it('should not overwrite a specified deletedAt', function(done) {
it('should not overwrite a specified deletedAt by setting paranoid: false', function(done) {
var tableName = ''
if(this.User.name) {
tableName = this.sequelize.queryInterface.QueryGenerator.quoteIdentifier(this.User.name) + '.'
}
this.User.findAll({
paranoid: false,
where: [
tableName + this.sequelize.queryInterface.QueryGenerator.quoteIdentifier('deletedAt') + ' IS NOT NULL '
],
......@@ -2274,8 +2275,9 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
}).error(done)
})
it('should not overwrite a specified deletedAt (complex query)', function (done) {
it('should not overwrite a specified deletedAt (complex query) by setting paranoid: false', function (done) {
this.User.findAll({
paranoid: false,
where: [
this.sequelize.or({ username: 'leia' }, { username: 'luke' }),
this.sequelize.and(
......
......@@ -1305,6 +1305,90 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
})
})
it("sql should have paranoid condition", function ( done ) {
var self = this;
self.ParanoidUser.create({ username: 'cuss' })
.then(function () {
return self.ParanoidUser.findAll();
})
.then(function ( users ) {
expect( users ).to.have.length( 1 );
return users[ 0 ].destroy();
})
.then(function () {
return self.ParanoidUser.findAll();
})
.then(function ( users ) {
expect( users ).to.have.length( 0 );
})
.done( done )
.catch( done );
});
it("sequelize.and as where should include paranoid condition", function ( done ) {
var self = this;
self.ParanoidUser.create({ username: 'cuss' })
.then(function () {
return self.ParanoidUser.findAll({
where: self.sequelize.and({
username: 'cuss'
})
});
})
.then(function ( users ) {
expect( users ).to.have.length( 1 );
return users[ 0 ].destroy();
})
.then(function () {
return self.ParanoidUser.findAll({
where: self.sequelize.and({
username: 'cuss'
})
});
})
.then(function ( users ) {
expect( users ).to.have.length( 0 );
})
.done( done )
.catch( done );
});
it("sequelize.or as where should include paranoid condition", function ( done ) {
var self = this;
self.ParanoidUser.create({ username: 'cuss' })
.then(function () {
return self.ParanoidUser.findAll({
where: self.sequelize.or({
username: 'cuss'
})
});
})
.then(function ( users ) {
expect( users ).to.have.length( 1 );
return users[ 0 ].destroy();
})
.then(function () {
return self.ParanoidUser.findAll({
where: self.sequelize.or({
username: 'cuss'
})
});
})
.then(function ( users ) {
expect( users ).to.have.length( 0 );
})
.done( done )
.catch( done );
});
it("escapes a single single quotes properly in where clauses", function(done) {
var self = this
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!