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

Commit dda41926 by Mick Hansen

Merge pull request #1715 from cusspvz/master

Paranoid Clause: Run always recursively + SubQuery 1:M fix
2 parents d81e4937 e34da0d4
...@@ -765,11 +765,11 @@ module.exports = (function() { ...@@ -765,11 +765,11 @@ module.exports = (function() {
, attrLeft = primaryKeysLeft[0] , attrLeft = primaryKeysLeft[0]
, tableRight = association.associationType === 'BelongsTo' ? parentTable : as , tableRight = association.associationType === 'BelongsTo' ? parentTable : as
, attrRight = association.identifier , attrRight = association.identifier
, where , joinOn
// Filter statement // Filter statement
// Used by both join and subquery where // Used by both join and subquery where
where = joinOn =
// Left side // Left side
( (
( subQuery && !include.subQuery && include.parent.subQuery && !( include.hasParentRequired && include.hasParentWhere ) ) && self.quoteIdentifier(tableLeft + "." + attrLeft) || ( subQuery && !include.subQuery && include.parent.subQuery && !( include.hasParentRequired && include.hasParentWhere ) ) && self.quoteIdentifier(tableLeft + "." + attrLeft) ||
...@@ -784,21 +784,28 @@ module.exports = (function() { ...@@ -784,21 +784,28 @@ module.exports = (function() {
self.quoteTable(tableRight) + "." + self.quoteIdentifier(attrRight) self.quoteTable(tableRight) + "." + self.quoteIdentifier(attrRight)
) )
// Generate join SQL
joinQueryItem += joinType + self.quoteTable(table, as) + " ON "
joinQueryItem += where
if (include.where) { if (include.where) {
joinQueryItem += " AND "+self.getWhereConditions(include.where, self.sequelize.literal(self.quoteIdentifier(as)), include.model, whereOptions) joinOn += " AND " + self.getWhereConditions(include.where, self.sequelize.literal(self.quoteIdentifier(as)), include.model, whereOptions)
// If its a multi association we need to add a where query to the main where (executed in the subquery) // If its a multi association we need to add a where query to the main where (executed in the subquery)
if (subQuery && association.isMultiAssociation && include.required) { if (subQuery && association.isMultiAssociation && include.required) {
if (!options.where) options.where = {} if (!options.where) options.where = {}
// Creating the as-is where for the subQuery, checks that the required association exists // Creating the as-is where for the subQuery, checks that the required association exists
options.where["__"+as] = self.sequelize.asIs("(SELECT "+self.quoteIdentifier(attrRight)+" FROM " + self.quoteTable(table, as) + " WHERE " + where + " LIMIT 1) IS NOT NULL") options.where["__"+as] = self.sequelize.asIs([ '(',
"SELECT " + self.quoteIdentifier(attrRight),
"FROM " + self.quoteTable(table, as),
"WHERE " + joinOn,
"LIMIT 1",
')', 'IS NOT NULL'].join(' '))
} }
} }
// Generate join SQL
joinQueryItem += joinType + self.quoteTable(table, as) + " ON " + joinOn
} }
if (include.subQuery && subQuery) { if (include.subQuery && subQuery) {
......
...@@ -1534,6 +1534,17 @@ module.exports = (function() { ...@@ -1534,6 +1534,17 @@ module.exports = (function() {
options = options || {} options = options || {}
options.where = options.where || {} options.where = options.where || {}
// Apply on each include
// This should be handled before handling where conditions because of logic with returns
// 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 ){
paranoidClause.call( include.model, include )
}
})
}
var deletedAtCol = this._timestampAttributes.deletedAt var deletedAtCol = this._timestampAttributes.deletedAt
, quoteIdentifiedDeletedAtCol = this.QueryInterface.quoteIdentifier(deletedAtCol) , quoteIdentifiedDeletedAtCol = this.QueryInterface.quoteIdentifier(deletedAtCol)
...@@ -1560,15 +1571,6 @@ module.exports = (function() { ...@@ -1560,15 +1571,6 @@ module.exports = (function() {
options.where[deletedAtCol] = null options.where[deletedAtCol] = null
} }
// Apply on each include
if ( options.include && options.include.length ) {
options.include.forEach(function(include) {
if( typeof include == 'object' && include.model || include.model ){
paranoidClause.call( include.model || include.model, include )
}
})
}
return options; return options;
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!