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

Commit be10f0bc by Sascha Depold

pg support for eager loading

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