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

Commit 143e0881 by Mick Hansen

Fix tests

1 parent 34fe92c6
...@@ -461,6 +461,8 @@ module.exports = (function() { ...@@ -461,6 +461,8 @@ module.exports = (function() {
- offset -> An offset value to start from. Only useable with limit! - offset -> An offset value to start from. Only useable with limit!
*/ */
selectQuery: function(tableName, options, factory) { selectQuery: function(tableName, options, factory) {
options = options || {}
var table = null var table = null
, self = this , self = this
, query , query
...@@ -474,7 +476,6 @@ module.exports = (function() { ...@@ -474,7 +476,6 @@ module.exports = (function() {
, subQueryAttributes = null , subQueryAttributes = null
, subJoinQueries = [] , subJoinQueries = []
options = options || {}
options.table = table = !Array.isArray(tableName) ? this.quoteIdentifiers(tableName) : tableName.map(function(t) { options.table = table = !Array.isArray(tableName) ? this.quoteIdentifiers(tableName) : tableName.map(function(t) {
return this.quoteIdentifiers(t) return this.quoteIdentifiers(t)
}.bind(this)).join(", ") }.bind(this)).join(", ")
...@@ -538,12 +539,9 @@ module.exports = (function() { ...@@ -538,12 +539,9 @@ module.exports = (function() {
return self.quoteIdentifier(as) + "." + self.quoteIdentifier(attr) + " AS " + self.quoteIdentifier(as + "." + attr) return self.quoteIdentifier(as) + "." + self.quoteIdentifier(attr) + " AS " + self.quoteIdentifier(as + "." + attr)
}) })
if (subQuery && (include.hasIncludeRequired || include.required)) { // If not many to many, and we're doing a subquery, add attributes to the subquery
if (include.association.isMultiAssociation) { if (!include.association.isMultiAssociation && subQuery && (include.hasIncludeRequired || include.required)) {
mainAttributes = mainAttributes.concat(attributes) subQueryAttributes = subQueryAttributes.concat(attributes)
} else {
subQueryAttributes = subQueryAttributes.concat(attributes)
}
} else { } else {
mainAttributes = mainAttributes.concat(attributes) mainAttributes = mainAttributes.concat(attributes)
} }
...@@ -555,20 +553,7 @@ module.exports = (function() { ...@@ -555,20 +553,7 @@ module.exports = (function() {
, throughAttributes = through.attributes.map(function(attr) { , throughAttributes = through.attributes.map(function(attr) {
return self.quoteIdentifier(throughAs) + "." + self.quoteIdentifier(attr) + " AS " + self.quoteIdentifier(throughAs + "." + attr) return self.quoteIdentifier(throughAs) + "." + self.quoteIdentifier(attr) + " AS " + self.quoteIdentifier(throughAs + "." + attr)
}) })
, primaryKeysSource = Object.keys(association.source.primaryKeys)
if (options.includeIgnoreAttributes !== false) {
if (subQuery && (include.hasIncludeRequired || include.required)) {
if (include.association.isMultiAssociation) {
mainAttributes = mainAttributes.concat(throughAttributes)
} else {
subQueryAttributes = subQueryAttributes.concat(throughAttributes)
}
} else {
mainAttributes = mainAttributes.concat(throughAttributes)
}
}
var primaryKeysSource = Object.keys(association.source.primaryKeys)
, tableSource = parentTable , tableSource = parentTable
, identSource = association.identifier , identSource = association.identifier
, attrSource = ((!association.source.hasPrimaryKeys || primaryKeysSource.length !== 1) ? 'id' : primaryKeysSource[0]) , attrSource = ((!association.source.hasPrimaryKeys || primaryKeysSource.length !== 1) ? 'id' : primaryKeysSource[0])
...@@ -583,6 +568,11 @@ module.exports = (function() { ...@@ -583,6 +568,11 @@ module.exports = (function() {
, targetJoinOn , targetJoinOn
, targetWhere , targetWhere
if (options.includeIgnoreAttributes !== false) {
// Through includes are always hasMany, so we need to add the attributes to the mainAttributes no matter what (Real join will never be executed in subquery)
mainAttributes = mainAttributes.concat(throughAttributes)
}
sourceJoinOn = self.quoteIdentifier(tableSource) + "." + self.quoteIdentifier(attrSource) + " = " sourceJoinOn = self.quoteIdentifier(tableSource) + "." + self.quoteIdentifier(attrSource) + " = "
sourceJoinOn += self.quoteIdentifier(throughAs) + "." + self.quoteIdentifier(identSource) sourceJoinOn += self.quoteIdentifier(throughAs) + "." + self.quoteIdentifier(identSource)
...@@ -627,6 +617,8 @@ module.exports = (function() { ...@@ -627,6 +617,8 @@ module.exports = (function() {
if (include.where) { if (include.where) {
joinQueryItem += " AND "+self.hashToWhereConditions(includeWhere, include.daoFactory, whereOptions) joinQueryItem += " AND "+self.hashToWhereConditions(includeWhere, include.daoFactory, whereOptions)
// If its a multi association we need to add a where query to the main where (executed in the subquery)
if (subQuery && association.isMultiAssociation) { if (subQuery && association.isMultiAssociation) {
if (!options.where) options.where = {} if (!options.where) options.where = {}
options.where["__"+as] = self.sequelize.asIs("(SELECT "+self.quoteIdentifier(attrRight)+" FROM " + self.quoteIdentifier(tableRight) + " WHERE " + where + " LIMIT 1) IS NOT NULL") options.where["__"+as] = self.sequelize.asIs("(SELECT "+self.quoteIdentifier(attrRight)+" FROM " + self.quoteIdentifier(tableRight) + " WHERE " + where + " LIMIT 1) IS NOT NULL")
...@@ -660,10 +652,10 @@ module.exports = (function() { ...@@ -660,10 +652,10 @@ module.exports = (function() {
} }
if (subQuery) { if (subQuery) {
subQueryItems.push("SELECT " + subQueryAttributes.join(',') + " FROM " + options.table) subQueryItems.push("SELECT " + subQueryAttributes.join(', ') + " FROM " + options.table)
subQueryItems.push(subJoinQueries.join('')) subQueryItems.push(subJoinQueries.join(''))
} else { } else {
mainQueryItems.push("SELECT " + mainAttributes.join(',') + " FROM " + options.table) mainQueryItems.push("SELECT " + mainAttributes.join(', ') + " FROM " + options.table)
mainQueryItems.push(mainJoinQueries.join('')) mainQueryItems.push(mainJoinQueries.join(''))
} }
...@@ -707,7 +699,7 @@ module.exports = (function() { ...@@ -707,7 +699,7 @@ module.exports = (function() {
} }
if (subQuery) { if (subQuery) {
query = "SELECT " + mainAttributes.join(',') + " FROM (" query = "SELECT " + mainAttributes.join(', ') + " FROM ("
query += subQueryItems.join('') query += subQueryItems.join('')
query += ") AS "+options.table query += ") AS "+options.table
query += mainJoinQueries.join('') query += mainJoinQueries.join('')
......
...@@ -112,7 +112,8 @@ describe(Support.getTestDialectTeaser("Include"), function () { ...@@ -112,7 +112,8 @@ describe(Support.getTestDialectTeaser("Include"), function () {
{name: 'Bonanza'}, {name: 'Bonanza'},
{name: 'NYSE'}, {name: 'NYSE'},
{name: 'Coshopr'} {name: 'Coshopr'}
]).done(function () { ]).done(function (err) {
if (err) return callback(err);
Company.findAll().done(callback) Company.findAll().done(callback)
}) })
}, },
...@@ -169,7 +170,8 @@ describe(Support.getTestDialectTeaser("Include"), function () { ...@@ -169,7 +170,8 @@ describe(Support.getTestDialectTeaser("Include"), function () {
{title: 'Bed'}, {title: 'Bed'},
{title: 'Pen'}, {title: 'Pen'},
{title: 'Monitor'} {title: 'Monitor'}
]).done(function () { ]).done(function (err) {
if (err) return callback(err);
Product.findAll().done(callback) Product.findAll().done(callback)
}) })
}, },
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!