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

Commit 54d2e58a by Sascha Depold

Merge branch 'eagermany' of https://github.com/lemon-tree/sequelize into lemon-tree-eagermany

2 parents f2701381 f2bcc282
...@@ -47,7 +47,9 @@ module.exports = (function() { ...@@ -47,7 +47,9 @@ module.exports = (function() {
this.foreignIdentifier = Utils._.underscoredIf((this.options.as || this.target.tableName) + 'Id', this.options.underscored) this.foreignIdentifier = Utils._.underscoredIf((this.options.as || this.target.tableName) + 'Id', this.options.underscored)
} else { } else {
this.foreignIdentifier = this.target.associations[this.associationAccessor].identifier this.foreignIdentifier = this.target.associations[this.associationAccessor].identifier
this.target.associations[this.associationAccessor].foreignIdentifier = this.identifier
delete this.source.rawAttributes[this.foreignIdentifier] delete this.source.rawAttributes[this.foreignIdentifier]
delete this.target.associations[this.associationAccessor].source.rawAttributes[this.identifier]
} }
// define a new model, which connects the models // define a new model, which connects the models
......
...@@ -187,6 +187,7 @@ module.exports = (function() { ...@@ -187,6 +187,7 @@ module.exports = (function() {
optAttributes = optAttributes.concat(attributes) optAttributes = optAttributes.concat(attributes)
if (!include.association.connectorDAO) {
var table = include.daoFactory.tableName var table = include.daoFactory.tableName
var as = include.as var as = include.as
var tableLeft = ((include.association.associationType === 'BelongsTo') ? include.as : tableName) var tableLeft = ((include.association.associationType === 'BelongsTo') ? include.as : tableName)
...@@ -194,7 +195,19 @@ module.exports = (function() { ...@@ -194,7 +195,19 @@ module.exports = (function() {
var tableRight = ((include.association.associationType === 'BelongsTo') ? tableName : include.as) var tableRight = ((include.association.associationType === 'BelongsTo') ? tableName : include.as)
var attrRight = include.association.identifier var attrRight = include.association.identifier
joinQuery += " LEFT OUTER JOIN " + this.quoteIdentifier(table) + " AS " + this.quoteIdentifier(as) + " ON " + this.quoteIdentifier(tableLeft) + "." + this.quoteIdentifier(attrLeft) + " = " + this.quoteIdentifier(tableRight) + "." + this.quoteIdentifier(attrRight) joinQuery += " LEFT OUTER JOIN " + this.quoteIdentifier(table) + " AS " + this.quoteIdentifier(as) + " ON " + this.quoteIdentifier(tableLeft) + "." + this.quoteIdentifier(attrLeft) + " = " + this.quoteIdentifier(tableRight) + "." + this.quoteIdentifier(attrRight)
} else {
var table = include.daoFactory.tableName
var as = include.as
var tableLeft = tableName
var identLeft = include.association.identifier
var attrLeft = 'id'
var tableRight = include.as
var identRight = include.association.foreignIdentifier
var attrRight = 'id'
var tableJunction = include.association.connectorDAO.tableName
joinQuery += " LEFT OUTER JOIN " + this.quoteIdentifier(tableJunction) + " ON " + this.quoteIdentifier(tableLeft) + "." + this.quoteIdentifier(attrLeft) + " = " + this.quoteIdentifier(tableJunction) + "." + this.quoteIdentifier(identLeft)
joinQuery += " LEFT OUTER JOIN " + this.quoteIdentifier(table) + " AS " + this.quoteIdentifier(as) + " ON " + this.quoteIdentifier(tableRight) + "." + this.quoteIdentifier(attrRight) + " = " + this.quoteIdentifier(tableJunction) + "." + this.quoteIdentifier(identRight)
}
}.bind(this)) }.bind(this))
options.attributes = optAttributes.join(', ') options.attributes = optAttributes.join(', ')
......
...@@ -260,6 +260,7 @@ module.exports = (function() { ...@@ -260,6 +260,7 @@ module.exports = (function() {
var joinQuery = ' LEFT OUTER JOIN <%= table %> AS <%= as %> ON <%= tableLeft %>.<%= attrLeft %> = <%= tableRight %>.<%= attrRight %>' var joinQuery = ' LEFT OUTER JOIN <%= table %> AS <%= as %> ON <%= tableLeft %>.<%= attrLeft %> = <%= tableRight %>.<%= attrRight %>'
if (!include.association.connectorDAO) {
query += Utils._.template(joinQuery)({ query += Utils._.template(joinQuery)({
table: this.quoteIdentifiers(include.daoFactory.tableName), table: this.quoteIdentifiers(include.daoFactory.tableName),
as: this.quoteIdentifier(include.as), as: this.quoteIdentifier(include.as),
...@@ -268,6 +269,25 @@ module.exports = (function() { ...@@ -268,6 +269,25 @@ module.exports = (function() {
tableRight: this.quoteIdentifiers((include.association.associationType === 'BelongsTo') ? tableName : include.as), tableRight: this.quoteIdentifiers((include.association.associationType === 'BelongsTo') ? tableName : include.as),
attrRight: this.quoteIdentifier(include.association.identifier) attrRight: this.quoteIdentifier(include.association.identifier)
}) })
} else {
query += Utils._.template(joinQuery)({
table: this.quoteIdentifiers(include.association.connectorDAO.tableName),
as: this.quoteIdentifier(include.association.connectorDAO.tableName),
tableLeft: this.quoteIdentifiers(tableName),
attrLeft: this.quoteIdentifier('id'),
tableRight: this.quoteIdentifiers(include.association.connectorDAO.tableName),
attrRight: this.quoteIdentifier(include.association.identifier)
})
query += Utils._.template(joinQuery)({
table: this.quoteIdentifiers(include.daoFactory.tableName),
as: this.quoteIdentifier(include.as),
tableLeft: this.quoteIdentifiers(include.as),
attrLeft: this.quoteIdentifier('id'),
tableRight: this.quoteIdentifiers(include.association.connectorDAO.tableName),
attrRight: this.quoteIdentifier(include.association.foreignIdentifier)
})
}
}.bind(this)) }.bind(this))
options.attributes = optAttributes.join(', ') options.attributes = optAttributes.join(', ')
......
...@@ -169,13 +169,27 @@ module.exports = (function() { ...@@ -169,13 +169,27 @@ module.exports = (function() {
optAttributes = optAttributes.concat(attributes) optAttributes = optAttributes.concat(attributes)
if (!include.association.connectorDAO) {
var table = include.daoFactory.tableName var table = include.daoFactory.tableName
var as = include.as var as = include.as
var tableLeft = ((include.association.associationType === 'BelongsTo') ? include.as : tableName) var tableLeft = ((include.association.associationType === 'BelongsTo') ? include.as : tableName)
var attrLeft = 'id' var attrLeft = 'id'
var tableRight = ((include.association.associationType === 'BelongsTo') ? tableName : include.as) var tableRight = ((include.association.associationType === 'BelongsTo') ? tableName : include.as)
var attrRight = include.association.identifier var attrRight = include.association.identifier
joinQuery += " LEFT OUTER JOIN " + this.quoteIdentifier(table) + " AS " + this.quoteIdentifier(as) + " ON " + this.quoteIdentifier(tableLeft) + "." + this.quoteIdentifier(attrLeft) + " = " + this.quoteIdentifier(tableRight) + "." + this.quoteIdentifier(attrRight) + "" joinQuery += " LEFT OUTER JOIN " + this.quoteIdentifier(table) + " AS " + this.quoteIdentifier(as) + " ON " + this.quoteIdentifier(tableLeft) + "." + this.quoteIdentifier(attrLeft) + " = " + this.quoteIdentifier(tableRight) + "." + this.quoteIdentifier(attrRight)
} else {
var table = include.daoFactory.tableName
var as = include.as
var tableLeft = tableName
var identLeft = include.association.identifier
var attrLeft = 'id'
var tableRight = include.as
var identRight = include.association.foreignIdentifier
var attrRight = 'id'
var tableJunction = include.association.connectorDAO.tableName
joinQuery += " LEFT OUTER JOIN " + this.quoteIdentifier(tableJunction) + " ON " + this.quoteIdentifier(tableLeft) + "." + this.quoteIdentifier(attrLeft) + " = " + this.quoteIdentifier(tableJunction) + "." + this.quoteIdentifier(identLeft)
joinQuery += " LEFT OUTER JOIN " + this.quoteIdentifier(table) + " AS " + this.quoteIdentifier(as) + " ON " + this.quoteIdentifier(tableRight) + "." + this.quoteIdentifier(attrRight) + " = " + this.quoteIdentifier(tableJunction) + "." + this.quoteIdentifier(identRight)
}
}.bind(this)) }.bind(this))
......
...@@ -381,6 +381,13 @@ describe(Support.getTestDialectTeaser("HasMany"), function() { ...@@ -381,6 +381,13 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
}) })
}) })
}) })
it("get associated objects with an eager load", function(done) {
this.User.find({where: {username: 'John'}, include: [ this.Task ]}).success(function (john) {
expect(john.tasks).to.have.length(2);
done();
})
})
}) })
it("removes the reference id, which was added in the first place", function(done) { it("removes the reference id, which was added in the first place", function(done) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!