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

Commit 44b268bd by Sascha Depold

fixed prefetching + actually implemented it for pg

1 parent 4fd57bf5
...@@ -158,7 +158,7 @@ module.exports = (function() { ...@@ -158,7 +158,7 @@ module.exports = (function() {
query += ' LEFT OUTER JOIN ' + Utils.addTicks(dao.tableName) + ' ON ' query += ' LEFT OUTER JOIN ' + Utils.addTicks(dao.tableName) + ' ON '
query += Utils.addTicks(association.associationType === 'BelongsTo' ? dao.tableName : tableName) + '.' query += Utils.addTicks(association.associationType === 'BelongsTo' ? dao.tableName : tableName) + '.'
query += Utils.addTicks(association.identifier) + '=' query += Utils.addTicks(association.identifier) + '='
query += Utils.addTicks(table) + '.' + Utils.addTicks('id') query += Utils.addTicks(association.associationType === 'BelongsTo' ? tableName : dao.tableName) + '.' + Utils.addTicks('id')
} }
optAttributes = optAttributes.concat( optAttributes = optAttributes.concat(
......
...@@ -10,7 +10,10 @@ function removeQuotes(s, quoteChar) { ...@@ -10,7 +10,10 @@ function removeQuotes(s, quoteChar) {
function addQuotes(s, quoteChar) { function addQuotes(s, quoteChar) {
quoteChar = quoteChar || '"' quoteChar = quoteChar || '"'
return s.split('.').map(function(e) { return quoteChar + String(e) + quoteChar }).join('.') return removeQuotes(s, quoteChar)
.split('.')
.map(function(e) { return quoteChar + String(e) + quoteChar })
.join('.')
} }
function pgEscape(val) { function pgEscape(val) {
...@@ -185,9 +188,10 @@ module.exports = (function() { ...@@ -185,9 +188,10 @@ module.exports = (function() {
selectQuery: function(tableName, options) { selectQuery: function(tableName, options) {
var query = "SELECT <%= attributes %> FROM <%= table %>" var query = "SELECT <%= attributes %> FROM <%= table %>"
, table = null
options = options || {} options = options || {}
options.table = Array.isArray(tableName) ? tableName.map(function(t){return addQuotes(t);}).join(", ") : addQuotes(tableName) options.table = table = Array.isArray(tableName) ? tableName.map(function(t){return addQuotes(t);}).join(", ") : addQuotes(tableName)
options.attributes = options.attributes && options.attributes.map(function(attr){ options.attributes = options.attributes && options.attributes.map(function(attr){
if(Array.isArray(attr) && attr.length == 2) { if(Array.isArray(attr) && attr.length == 2) {
return [attr[0], addQuotes(removeQuotes(attr[1], '`'))].join(' as ') return [attr[0], addQuotes(removeQuotes(attr[1], '`'))].join(' as ')
...@@ -200,26 +204,48 @@ module.exports = (function() { ...@@ -200,26 +204,48 @@ module.exports = (function() {
options.attributes = options.attributes || '*' options.attributes = options.attributes || '*'
if (options.include) { if (options.include) {
var tableNames = [options.table] var optAttributes = [options.table + '.*']
, optAttributes = [options.table + '.*']
for (var daoName in options.include) { for (var daoName in options.include) {
if (options.include.hasOwnProperty(daoName)) { if (options.include.hasOwnProperty(daoName)) {
var dao = options.include[daoName] var dao = options.include[daoName]
, _tableName = Utils.addTicks(dao.tableName) , daoFactory = dao.daoFactoryManager.getDAO(tableName, {
attribute: 'tableName'
})
, _tableName = addQuotes(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 ' + 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')
}
tableNames.push(_tableName)
optAttributes = optAttributes.concat( optAttributes = optAttributes.concat(
Utils._.keys(dao.attributes).map(function(attr) { Utils._.keys(dao.attributes).map(function(attr) {
var identifier = [_tableName, Utils.addTicks(attr)] var identifier = [_tableName, addQuotes(attr)]
return identifier.join('.') + ' AS ' + Utils.addTicks(identifier.join('.')) return identifier.join('.') + ' AS "' + removeQuotes(identifier.join('.')) + '"'
}) })
) )
} }
} }
options.table = tableNames.join(', ').replace(/`/g, '"') options.attributes = optAttributes.join(', ')
options.attributes = optAttributes.join(', ').replace(/`/g, '"')
} }
if(options.where) { if(options.where) {
......
...@@ -426,20 +426,20 @@ describe("[" + dialect.toUpperCase() + "] DAOFactory", function() { ...@@ -426,20 +426,20 @@ describe("[" + dialect.toUpperCase() + "] DAOFactory", function() {
this.sequelize.sync({ force: true }).success(function() { this.sequelize.sync({ force: true }).success(function() {
this.User.create({ name: 'barfooz' }).success(function(user) { this.User.create({ name: 'barfooz' }).success(function(user) {
this.User.create({ name: 'another user' }).success(function(another_user) { this.User.create({ name: 'another user' }).success(function(another_user) {
this.Task.create({ title: 'task' }).success(function(task) { this.Task.create({ title: 'task' }).success(function(task) {
user.setTask(task).success(function() { user.setTask(task).success(function() {
this.Task.find({ this.Task.find({
where: { 'Tasks.id': 1 }, where: { 'Tasks.id': 1 },
include: [ 'UserWithName' ] include: [ 'UserWithName' ]
}).success(function(task) { }).success(function(task) {
expect(task.userWithName).toBeDefined() expect(task.userWithName).toBeDefined()
expect(task.userWithName.id).toEqual(user.id) expect(task.userWithName.id).toEqual(user.id)
done() done()
}) })
}.bind(this)) //- setTask }.bind(this)) //- setTask
}.bind(this)) //- Task.create }.bind(this)) //- Task.create
}.bind(this)) //- User.create }.bind(this)) //- User.create
}.bind(this)) //- User.create }.bind(this)) //- User.create
}.bind(this)) //- sequelize.sync }.bind(this)) //- sequelize.sync
}) })
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!