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

Commit 6497a489 by Mick Hansen

should be possible use limit and a where on a hasMany with additional includes

1 parent bb5fca4e
......@@ -539,7 +539,11 @@ module.exports = (function() {
})
if (subQuery && (include.hasIncludeRequired || include.required)) {
subQueryAttributes = subQueryAttributes.concat(attributes)
if (include.association.isMultiAssociation) {
mainAttributes = mainAttributes.concat(attributes)
} else {
subQueryAttributes = subQueryAttributes.concat(attributes)
}
} else {
mainAttributes = mainAttributes.concat(attributes)
}
......@@ -554,7 +558,11 @@ module.exports = (function() {
if (options.includeIgnoreAttributes !== false) {
if (subQuery && (include.hasIncludeRequired || include.required)) {
subQueryAttributes = subQueryAttributes.concat(attributes)
if (include.association.isMultiAssociation) {
mainAttributes = mainAttributes.concat(throughAttributes)
} else {
subQueryAttributes = subQueryAttributes.concat(throughAttributes)
}
} else {
mainAttributes = mainAttributes.concat(throughAttributes)
}
......@@ -587,13 +595,20 @@ module.exports = (function() {
, attrLeft = ((primaryKeysLeft.length !== 1) ? 'id' : primaryKeysLeft[0])
, tableRight = ((association.associationType === 'BelongsTo') ? parentTable : as)
, attrRight = association.identifier
, where
where = self.quoteIdentifier(tableLeft) + "." + self.quoteIdentifier(attrLeft) + " = "
where += self.quoteIdentifier(tableRight) + "." + self.quoteIdentifier(attrRight)
joinQueryItem += joinType + self.quoteIdentifier(table) + " AS " + self.quoteIdentifier(as) + " ON "
joinQueryItem += self.quoteIdentifier(tableLeft) + "." + self.quoteIdentifier(attrLeft) + " = "
joinQueryItem += self.quoteIdentifier(tableRight) + "." + self.quoteIdentifier(attrRight)
joinQueryItem += where
if (include.where) {
joinQueryItem += " AND "+self.hashToWhereConditions(includeWhere, include.daoFactory, whereOptions)
if (subQuery && association.isMultiAssociation) {
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")
}
}
}
......@@ -611,7 +626,11 @@ module.exports = (function() {
var joinQueryItem = generateJoinQuery(include, tableName)
if (subQuery && (include.hasIncludeWhere || include.where)) {
subJoinQueries.push(joinQueryItem)
if (include.association.isMultiAssociation) {
mainJoinQueries.push(joinQueryItem)
} else {
subJoinQueries.push(joinQueryItem)
}
} else {
mainJoinQueries.push(joinQueryItem)
}
......@@ -911,11 +930,15 @@ module.exports = (function() {
options = options || {}
for (var key in hash) {
var value = hash[key]
, _key
// Closures are nice
Utils._.each(hash, function (value, key) {
var _key
, _value = null
if (value instanceof Utils.asIs) {
result.push(value.toString(this))
return
}
if (options.keysEscaped) {
_key = key
......@@ -927,8 +950,6 @@ module.exports = (function() {
}
}
//handle qualified key names
if (Array.isArray(value)) {
result.push(this.arrayValue(value, key, _key, dao))
} else if ((value) && (typeof value == 'object') && !(value instanceof Date) && !Buffer.isBuffer(value)) {
......@@ -961,9 +982,10 @@ module.exports = (function() {
} else {
_value = this.escape(value)
}
result.push((_value == 'NULL') ? _key + " IS NULL" : [_key, _value].join("="))
}
}
}.bind(this))
return result.join(" AND ")
},
......
......@@ -5,10 +5,11 @@ var Utils = require(__dirname + '/utils')
module.exports = (function() {
var QueryInterface = function(sequelize) {
this.sequelize = sequelize
this.QueryGenerator = require('./dialects/' + this.sequelize.options.dialect + '/query-generator')
this.QueryGenerator.options = this.sequelize.options
this.QueryGenerator._dialect = this.sequelize.dialect
this.sequelize = sequelize
this.QueryGenerator = require('./dialects/' + this.sequelize.options.dialect + '/query-generator')
this.QueryGenerator.options = this.sequelize.options
this.QueryGenerator._dialect = this.sequelize.dialect
this.QueryGenerator.sequelize = this.sequelize
}
Utils.addEventEmitter(QueryInterface)
......
......@@ -395,22 +395,26 @@ module.exports = (function() {
}).run()
}
Sequelize.prototype.fn = function (fn) {
Sequelize.fn = Sequelize.prototype.fn = function (fn) {
return new Utils.fn(fn, Array.prototype.slice.call(arguments, 1))
}
Sequelize.prototype.col = function (col) {
Sequelize.col = Sequelize.prototype.col = function (col) {
return new Utils.col(col)
}
Sequelize.prototype.cast = function (val, type) {
Sequelize.cast = Sequelize.prototype.cast = function (val, type) {
return new Utils.cast(val, type)
}
Sequelize.prototype.literal = function (val) {
Sequelize.literal = Sequelize.prototype.literal = function (val) {
return new Utils.literal(val)
}
Sequelize.asIs = Sequelize.prototype.asIs = function (val) {
return new Utils.asIs(val)
}
Sequelize.prototype.transaction = function(_options, _callback) {
var options = (typeof _options === 'function') ? {} : _options
, callback = (typeof _options === 'function') ? _options : _callback
......
......@@ -450,7 +450,9 @@ var Utils = module.exports = {
var _hash = {}
for (var key in hash) {
if (key.indexOf('.') === -1) {
if (key instanceof Utils.literal) {
_hash[key] = hash[key]
} else if (key.indexOf('.') === -1) {
_hash[tableName + '.' + key] = hash[key]
} else {
_hash[key] = hash[key]
......@@ -540,6 +542,10 @@ var Utils = module.exports = {
this.val = val
},
asIs: function(val) {
this.val = val
},
generateUUID: function() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8)
......@@ -558,6 +564,7 @@ var Utils = module.exports = {
Utils.literal.prototype.toString = function() {
return this.val
}
Utils.asIs.prototype = Utils.literal.prototype
Utils.cast.prototype.toString = function(queryGenerator) {
if (!this.val instanceof Utils.fn && !this.val instanceof Utils.col && !this.val instanceof Utils.literal) {
......
......@@ -1165,13 +1165,11 @@ describe(Support.getTestDialectTeaser("Include"), function () {
expect(product.tags.length).to.be.ok
expect(product.prices.length).to.be.ok
product.tags.forEach(function (tag) {
//expect(tag.name).to.equal('A')
product.prices.forEach(function (price) {
expect(price.value).to.be.above(5)
})
})
done()
}).on('sql', function (sql) {
console.log(sql)
})
})
})
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!