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

Commit 8210ddef by Overlook Motel

refactor quote function to use getAssociation

1 parent 5cbd2452
Showing with 22 additions and 36 deletions
...@@ -356,58 +356,44 @@ module.exports = (function() { ...@@ -356,58 +356,44 @@ module.exports = (function() {
} else if (Array.isArray(obj)) { } else if (Array.isArray(obj)) {
// loop through array, adding table names of models to quoted // loop through array, adding table names of models to quoted
// (checking associations to see if names should be singularised or not) // (checking associations to see if names should be singularised or not)
var quoted = [] var tableNames = []
, i , parentAssociation
, len = obj.length , len = obj.length
for (i = 0; i < len - 1; i++) { for (var i = 0; i < len - 1; i++) {
var item = obj[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) { if (Utils._.isString(item) || item instanceof Utils.fn || item instanceof Utils.col || item instanceof Utils.literal || item instanceof Utils.cast || 'raw' in item) {
break break
} }
var model, as
if (item instanceof daoFactory) { if (item instanceof daoFactory) {
item = {model: item} model = item
}
// 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
})
} else { } else {
association = Utils._.find(associations, function(association, associationName) { model = item.model
return association.target === model ? as = item.as
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
} }
quoted[i] = as // check if model provided is through table
var association
if (!association) { if (!as && parentAssociation && parentAssociation.through === model) {
throw new Error('\'' + quoted.join('.') + '\' in order / group clause is not valid association') association = {as: Utils.singularize(model.tableName, model.options.language)}
} else {
// find applicable association for linking parent to this model
association = parent.getAssociation(model, as)
} }
if (association) {
tableNames[i] = association.as
parent = model parent = model
parentAssociation = association
} else {
tableNames[i] = model.tableName
throw new Error('\'' + tableNames.join('.') + '\' in order / group clause is not valid association')
}
} }
// add 1st string as quoted, 2nd as unquoted raw // 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) { if (i < len - 1) {
sql += ' ' + obj[i + 1] sql += ' ' + obj[i + 1]
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!