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

Commit 9419770e by Sascha Depold

fixed formatting of join query results

1 parent 0afc6f94
Showing with 62 additions and 6 deletions
...@@ -122,13 +122,37 @@ module.exports = (function() { ...@@ -122,13 +122,37 @@ module.exports = (function() {
// private // // private //
///////////// /////////////
var findTableNameInAttribute = function(attribute) {
var tableName = null
this.sequelize.daoFactoryManager.daos.forEach(function(daoFactory) {
if (!!tableName) {
return
} else {
var regExp = new RegExp("^" + daoFactory.tableName + "\\.")
, match = attribute.match(regExp)
if (!!match) {
tableName = daoFactory.tableName
}
}
})
return tableName
}
var queryResultHasJoin = function(results) { var queryResultHasJoin = function(results) {
var hasJoin = !!results[0] if (!!results[0]) {
var keys = Utils._.keys(results[0])
hasJoin = hasJoin && (Utils._.keys(results[0]).length > 1) for (var i = 0; i < keys.length; i++) {
hasJoin = hasJoin && (Utils.isHash(results[0][Utils._.keys(results[0])[0]])) if (!!findTableNameInAttribute.call(this, keys[i])) {
return true
}
}
}
return hasJoin return false
} }
var isInsertQuery = function(results) { var isInsertQuery = function(results) {
...@@ -169,8 +193,9 @@ module.exports = (function() { ...@@ -169,8 +193,9 @@ module.exports = (function() {
if (this.options.raw) { if (this.options.raw) {
result = results result = results
} else if (queryResultHasJoin(results)) { } else if (queryResultHasJoin.call(this, results)) {
result = groupDataByCalleeFactory.call(this, results).map(function(result) { result = prepareJoinData.call(this, results)
result = groupDataByCalleeFactory.call(this, result).map(function(result) {
// let's build the actual dao instance first... // let's build the actual dao instance first...
var dao = this.callee.build(result[this.callee.tableName], { isNewRecord: false }) var dao = this.callee.build(result[this.callee.tableName], { isNewRecord: false })
...@@ -287,5 +312,36 @@ module.exports = (function() { ...@@ -287,5 +312,36 @@ module.exports = (function() {
return result return result
} }
/**
* This function will prepare the result of select queries with joins.
*
* @param {Array} data This array contains objects.
* @return {Array} The array will have the needed format for groupDataByCalleeFactory.
*/
var prepareJoinData = function(data) {
var result = data.map(function(row) {
var nestedRow = {}
for (var key in row) {
if (row.hasOwnProperty(key)) {
var tableName = findTableNameInAttribute.call(this, key)
if (!!tableName) {
nestedRow[tableName] = nestedRow[tableName] || {}
nestedRow[tableName][key.replace(tableName + '.', '')] = row[key]
} else {
nestedRow[this.callee.tableName] = nestedRow[this.callee.tableName] || {}
nestedRow[this.callee.tableName][key] = row[key]
}
}
}
return nestedRow
}.bind(this))
return result
}
return AbstractQuery return AbstractQuery
})() })()
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!