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

Commit be10f0bc by Sascha Depold

pg support for eager loading

1 parent f096d9be
...@@ -31,7 +31,9 @@ module.exports = (function() { ...@@ -31,7 +31,9 @@ module.exports = (function() {
} }
var query = new Query(this.client, this.sequelize, callee, options || {}) var query = new Query(this.client, this.sequelize, callee, options || {})
self.pendingQueries += 1 self.pendingQueries += 1
return query.run(sql) return query.run(sql)
.success(function() { self.endQuery.call(self) }) .success(function() { self.endQuery.call(self) })
.error(function() { self.endQuery.call(self) }) .error(function() { self.endQuery.call(self) })
......
...@@ -233,49 +233,27 @@ module.exports = (function() { ...@@ -233,49 +233,27 @@ module.exports = (function() {
if (options.include) { if (options.include) {
var optAttributes = [options.table + '.*'] var optAttributes = [options.table + '.*']
for (var daoName in options.include) { options.include.forEach(function(include) {
if (options.include.hasOwnProperty(daoName)) { var attributes = Object.keys(include.daoFactory.attributes).map(function(attr) {
var dao = options.include[daoName] var template = Utils._.template('"<%= table %>"."<%= attr %>" AS "<%= as %>.<%= attr %>"')
, daoFactory = dao.daoFactoryManager.getDAO(tableName, { return template({
attribute: 'tableName' table: include.daoFactory.tableName,
as: include.as,
attr: attr
})
}) })
, _tableName = addQuotes(dao.tableName)
, association = dao.getAssociation(daoFactory)
if (association.connectorDAO) {
var foreignIdentifier = Object.keys(association.connectorDAO.rawAttributes).filter(function(attrName) {
return (!!attrName.match(/.+Id$/) || !!attrName.match(/.+_id$/)) && (attrName !== association.identifier)
})[0]
query += ' LEFT OUTER JOIN ' + addQuotes(association.connectorDAO.tableName) + ' ON '
query += addQuotes(association.connectorDAO.tableName) + '.'
query += addQuotes(foreignIdentifier) + '='
query += addQuotes(table) + '.' + addQuotes('id')
query += ' LEFT OUTER JOIN ' + addQuotes(dao.tableName) + ' ON '
query += addQuotes(dao.tableName) + '.'
query += addQuotes('id') + '='
query += addQuotes(association.connectorDAO.tableName) + '.' + addQuotes(association.identifier)
} else {
query += ' LEFT OUTER JOIN ' + addQuotes(dao.tableName) + ' ON '
query += addQuotes(association.associationType === 'BelongsTo' ? dao.tableName : tableName) + '.'
query += addQuotes(association.identifier) + '='
query += addQuotes(association.associationType === 'BelongsTo' ? tableName : dao.tableName) + '.' + addQuotes('id')
}
var aliasAssoc = daoFactory.getAssociationByAlias(daoName) optAttributes = optAttributes.concat(attributes)
, aliasName = !!aliasAssoc ? addQuotes(daoName) : _tableName
optAttributes = optAttributes.concat( var joinQuery = ' LEFT OUTER JOIN "<%= table %>" ON "<%= tableLeft %>"."<%= attrLeft %>" = "<%= tableRight %>"."<%= attrRight %>"'
Object.keys(dao.attributes).map(function(attr) { query += Utils._.template(joinQuery)({
return '' + table: include.daoFactory.tableName,
[_tableName, addQuotes(attr)].join('.') + tableLeft: ((include.association.associationType === 'BelongsTo') ? include.daoFactory.tableName : tableName),
' AS "' + attrLeft: 'id',
removeQuotes([aliasName, attr].join('.')) + '"' tableRight: ((include.association.associationType === 'BelongsTo') ? tableName : include.daoFactory.tableName),
attrRight: include.association.identifier
})
}) })
)
}
}
options.attributes = optAttributes.join(', ') options.attributes = optAttributes.join(', ')
} }
......
if(typeof require === 'function') { if (typeof require === 'function') {
const buster = require("buster") const buster = require("buster")
, Helpers = require('./buster-helpers') , Helpers = require('./buster-helpers')
, dialect = Helpers.getTestDialect() , dialect = Helpers.getTestDialect()
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!