Skip to content

Commit

Permalink
refactor quote function to use getAssociation
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Feb 9, 2014
1 parent 5cbd245 commit 8210dde
Showing 1 changed file with 22 additions and 36 deletions.
58 changes: 22 additions & 36 deletions lib/dialects/abstract/query-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,58 +356,44 @@ module.exports = (function() {
} else if (Array.isArray(obj)) {
// loop through array, adding table names of models to quoted
// (checking associations to see if names should be singularised or not)
var quoted = []
, i
var tableNames = []
, parentAssociation
, len = obj.length
for (i = 0; i < len - 1; i++) {
for (var i = 0; i < len - 1; i++) {
var item = obj[i]
if (Utils._.isString(item) || item instanceof Utils.fn || item instanceof Utils.col || item instanceof Utils.literal || item instanceof Utils.cast || 'raw' in item) {
break
}

var model, as
if (item instanceof daoFactory) {
item = {model: item}
model = item
} else {
model = item.model
as = item.as
}

// find applicable association for linking parent to this model
var model = item.model
, as
, associations = parent.associations
, association
if (item.hasOwnProperty('as')) {
as = item.as
association = Utils._.find(associations, function(association, associationName) {
return association.target === model && associationName === as
})
// check if model provided is through table
var association
if (!as && parentAssociation && parentAssociation.through === model) {
association = {as: Utils.singularize(model.tableName, model.options.language)}
} else {
association = Utils._.find(associations, function(association, associationName) {
return association.target === model ?
associationName === (
association.doubleLinked ?
association.combinedName:
(
association.isSingleAssociation ?
Utils.singularize(model.tableName, model.options.language) :
parent.tableName + model.tableName
)
) :
association.targetAssociation && association.targetAssociation.through === model
})
// NB association.target !== model clause below is to singularize names of through tables in hasMany-hasMany joins
as = (association && (association.isSingleAssociation || association.target !== model)) ? Utils.singularize(model.tableName, model.options.language) : model.tableName
// find applicable association for linking parent to this model
association = parent.getAssociation(model, as)
}

quoted[i] = as

if (!association) {
throw new Error('\'' + quoted.join('.') + '\' in order / group clause is not valid association')
if (association) {
tableNames[i] = association.as
parent = model
parentAssociation = association
} else {
tableNames[i] = model.tableName
throw new Error('\'' + tableNames.join('.') + '\' in order / group clause is not valid association')
}

parent = model
}

// add 1st string as quoted, 2nd as unquoted raw
var sql = (i > 0 ? this.quoteIdentifier(quoted.join('.')) + '.' : '') + this.quote(obj[i], parent, force)
var sql = (i > 0 ? this.quoteIdentifier(tableNames.join('.')) + '.' : '') + this.quote(obj[i], parent, force)
if (i < len - 1) {
sql += ' ' + obj[i + 1]
}
Expand Down

0 comments on commit 8210dde

Please sign in to comment.