From ba10473fc7344e5a5d50224275748d54c617175c Mon Sep 17 00:00:00 2001 From: Lemon Date: Thu, 29 Aug 2013 01:22:30 +0100 Subject: [PATCH] Non-'id' primary key include support - MySQL Plus cleaning up many-many join to be more understandable --- lib/dialects/mysql/query-generator.js | 30 +++++++++++++++------------ 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/lib/dialects/mysql/query-generator.js b/lib/dialects/mysql/query-generator.js index 87f3e1d31878..2605605f59e0 100644 --- a/lib/dialects/mysql/query-generator.js +++ b/lib/dialects/mysql/query-generator.js @@ -188,26 +188,30 @@ module.exports = (function() { optAttributes = optAttributes.concat(attributes) + var table = include.daoFactory.tableName + var as = include.as + if (!include.association.connectorDAO) { - var table = include.daoFactory.tableName - var as = include.as + var primaryKeysLeft = ((include.association.associationType === 'BelongsTo') ? Object.keys(include.association.target.primaryKeys) : Object.keys(include.association.source.primaryKeys)); var tableLeft = ((include.association.associationType === 'BelongsTo') ? include.as : tableName) - var attrLeft = 'id' + var attrLeft = ((primaryKeysLeft.length !== 1) ? 'id' : primaryKeysLeft[0]) var tableRight = ((include.association.associationType === 'BelongsTo') ? tableName : include.as) 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) } 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 primaryKeysSource = Object.keys(include.association.source.primaryKeys); + var tableSource = tableName + var identSource = include.association.identifier + var attrSource = ((!include.association.source.hasPrimaryKeys || primaryKeysSource.length !== 1) ? 'id' : primaryKeysSource[0]) + + var primaryKeysTarget = Object.keys(include.association.target.primaryKeys); + var tableTarget = include.as + var identTarget = include.association.foreignIdentifier + var attrTarget = ((!include.association.target.hasPrimaryKeys || primaryKeysTarget.length !== 1) ? 'id' : primaryKeysTarget[0]) + 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) + joinQuery += " LEFT OUTER JOIN " + this.quoteIdentifier(tableJunction) + " ON " + this.quoteIdentifier(tableSource) + "." + this.quoteIdentifier(attrSource) + " = " + this.quoteIdentifier(tableJunction) + "." + this.quoteIdentifier(identSource) + joinQuery += " LEFT OUTER JOIN " + this.quoteIdentifier(table) + " AS " + this.quoteIdentifier(as) + " ON " + this.quoteIdentifier(tableTarget) + "." + this.quoteIdentifier(attrTarget) + " = " + this.quoteIdentifier(tableJunction) + "." + this.quoteIdentifier(identTarget) } }.bind(this))