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

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,
, _tableName = addQuotes(dao.tableName) attr: attr
, association = dao.getAssociation(daoFactory) })
})
if (association.connectorDAO) {
var foreignIdentifier = Object.keys(association.connectorDAO.rawAttributes).filter(function(attrName) { optAttributes = optAttributes.concat(attributes)
return (!!attrName.match(/.+Id$/) || !!attrName.match(/.+_id$/)) && (attrName !== association.identifier)
})[0] var joinQuery = ' LEFT OUTER JOIN "<%= table %>" ON "<%= tableLeft %>"."<%= attrLeft %>" = "<%= tableRight %>"."<%= attrRight %>"'
query += Utils._.template(joinQuery)({
query += ' LEFT OUTER JOIN ' + addQuotes(association.connectorDAO.tableName) + ' ON ' table: include.daoFactory.tableName,
query += addQuotes(association.connectorDAO.tableName) + '.' tableLeft: ((include.association.associationType === 'BelongsTo') ? include.daoFactory.tableName : tableName),
query += addQuotes(foreignIdentifier) + '=' attrLeft: 'id',
query += addQuotes(table) + '.' + addQuotes('id') tableRight: ((include.association.associationType === 'BelongsTo') ? tableName : include.daoFactory.tableName),
attrRight: include.association.identifier
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)
, aliasName = !!aliasAssoc ? addQuotes(daoName) : _tableName
optAttributes = optAttributes.concat(
Object.keys(dao.attributes).map(function(attr) {
return '' +
[_tableName, addQuotes(attr)].join('.') +
' AS "' +
removeQuotes([aliasName, attr].join('.')) + '"'
})
)
}
}
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!