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

Commit 281a5871 by Sascha Depold

correctly join tables

1 parent 4242e849
Showing with 33 additions and 10 deletions
......@@ -115,9 +115,10 @@ module.exports = (function() {
selectQuery: function(tableName, options) {
var query = "SELECT <%= attributes %> FROM <%= table %>"
, table = null
options = options || {}
options.table = Array.isArray(tableName) ? tableName.map(function(tbl){ return Utils.addTicks(tbl) }).join(", ") : Utils.addTicks(tableName)
options = options || {}
options.table = table = Array.isArray(tableName) ? tableName.map(function(tbl){ return Utils.addTicks(tbl) }).join(", ") : Utils.addTicks(tableName)
options.attributes = options.attributes && options.attributes.map(function(attr){
if(Array.isArray(attr) && attr.length == 2) {
return [attr[0], Utils.addTicks(attr[1])].join(' as ')
......@@ -128,25 +129,47 @@ module.exports = (function() {
options.attributes = options.attributes || '*'
if (options.include) {
var tableNames = [options.table]
, optAttributes = [options.table + '.*']
var optAttributes = [options.table + '.*']
for (var daoName in options.include) {
if (options.include.hasOwnProperty(daoName)) {
var dao = options.include[daoName]
, _tableName = Utils.addTicks(dao.tableName)
var dao = options.include[daoName]
, daoFactory = dao.daoFactoryManager.getDAO(tableName, {
attribute: 'tableName'
})
, _tableName = Utils.addTicks(dao.tableName)
, association = dao.getAssociation(daoFactory)
if (association.connectorDAO) {
var foreignIdentifier = Utils._.keys(association.connectorDAO.rawAttributes).filter(function(attrName) {
return (!!attrName.match(/.+Id$/) || !!attrName.match(/.+_id$/)) && (attrName !== association.identifier)
})[0]
query += ' LEFT OUTER JOIN ' + Utils.addTicks(association.connectorDAO.tableName) + ' ON '
query += Utils.addTicks(association.connectorDAO.tableName) + '.'
query += Utils.addTicks(foreignIdentifier) + '='
query += Utils.addTicks(table) + '.' + Utils.addTicks('id')
query += ' LEFT OUTER JOIN ' + Utils.addTicks(dao.tableName) + ' ON '
query += Utils.addTicks(dao.tableName) + '.'
query += Utils.addTicks('id') + '='
query += Utils.addTicks(association.connectorDAO.tableName) + '.' + Utils.addTicks(association.identifier)
} else {
query += ' LEFT OUTER JOIN ' + Utils.addTicks(dao.tableName) + ' ON '
query += Utils.addTicks(association.associationType === 'BelongsTo' ? dao.tableName : tableName) + '.'
query += Utils.addTicks(association.identifier) + '='
query += Utils.addTicks(table) + '.' + Utils.addTicks('id')
}
tableNames.push(_tableName)
optAttributes = optAttributes.concat(
Utils._.keys(dao.attributes).map(function(attr) {
var identifer = [_tableName, Utils.addTicks(attr)]
return identifer.join('.') + ' AS ' + Utils.addTicks(identifer.join('.'))
var identifier = [_tableName, Utils.addTicks(attr)]
return identifier.join('.') + ' AS ' + Utils.addTicks(identifier.join('.'))
})
)
}
}
options.table = tableNames.join(', ')
options.attributes = optAttributes.join(', ')
}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!