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

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