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

Commit 356cd4fe by Mick Hansen

doing work

1 parent 4aa6a21e
......@@ -48,21 +48,21 @@ module.exports = (function() {
if (!options.attributes) {
options.attributes = [
self.QueryInterface.quoteTable(self.association.target.getTableName())+".*"
self.QueryInterface.quoteTable(self.association.target.name)+".*"
]
}
if (options.joinTableAttributes) {
options.joinTableAttributes.forEach(function (elem) {
options.attributes.push(
self.QueryInterface.quoteTable(through.getTableName()) + '.' + self.QueryInterface.quoteIdentifier(elem) + ' as ' +
self.QueryInterface.quoteTable(through.name) + '.' + self.QueryInterface.quoteIdentifier(elem) + ' as ' +
self.QueryInterface.quoteIdentifier(through.name + '.' + elem, true)
)
})
} else {
Utils._.forOwn(through.rawAttributes, function (elem, key) {
options.attributes.push(
self.QueryInterface.quoteTable(through.getTableName()) + '.' + self.QueryInterface.quoteIdentifier(key) + ' as ' +
self.QueryInterface.quoteTable(through.name) + '.' + self.QueryInterface.quoteIdentifier(key) + ' as ' +
self.QueryInterface.quoteIdentifier(through.name + '.' + key, true)
)
})
......
......@@ -261,6 +261,7 @@ module.exports = (function() {
Utils._.each(self.rawAttributes, function(options, attribute) {
options.Model = self
options.fieldName = attribute
options._modelAttribute = true
if (options.hasOwnProperty(type)) {
_custom[attribute] = options[type]
......@@ -470,7 +471,7 @@ module.exports = (function() {
//right now, the caller (has-many-double-linked) is in charge of the where clause
DAOFactory.prototype.findAllJoin = function(joinTableName, options, queryOptions) {
var optcpy = Utils._.clone(options)
optcpy.attributes = optcpy.attributes || [this.QueryInterface.quoteIdentifier(this.getTableName())+".*"]
optcpy.attributes = optcpy.attributes || [this.QueryInterface.quoteTable(this.name)+".*"]
// whereCollection is used for non-primary key updates
this.options.whereCollection = optcpy.where || null;
......
......@@ -14,6 +14,8 @@ module.exports = (function() {
return {
tableName: param.tableName || param,
table: param.tableName || param,
name: param.name || param,
schema: schema,
delimiter: schemaDelimiter || '.',
toString: function() {
......@@ -350,10 +352,14 @@ module.exports = (function() {
},
quoteTable: function(param) {
quoteTable: function(param, as) {
if (_.isObject(param)) {
var table = '';
if (as === true) {
as = param.as || param.name
}
if (this._dialect.supports.schemas) {
if (param.schema) {
table += this.quoteIdentifier(param.schema) + '.'
......@@ -369,6 +375,10 @@ module.exports = (function() {
table = this.quoteIdentifier(table)
}
if (as) {
table += " AS " + this.quoteIdentifier(as)
}
return table
}
return this.quoteIdentifier(param)
......@@ -406,7 +416,7 @@ module.exports = (function() {
, len = obj.length
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) {
if (item._modelAttribute || Utils._.isString(item) || item instanceof Utils.fn || item instanceof Utils.col || item instanceof Utils.literal || item instanceof Utils.cast || 'raw' in item) {
break
}
......@@ -443,6 +453,8 @@ module.exports = (function() {
sql += ' ' + obj[i + 1]
}
return sql
} else if (obj._modelAttribute) {
return this.quoteTable(obj.Model.name)+'.'+obj.fieldName
} else if (obj instanceof Utils.fn || obj instanceof Utils.col || obj instanceof Utils.literal || obj instanceof Utils.cast) {
return obj.toString(this)
} else if (Utils._.isObject(obj) && 'raw' in obj) {
......@@ -579,9 +591,13 @@ module.exports = (function() {
, subQueryItems = []
, subQueryAttributes = null
, subJoinQueries = []
, mainTableAs = null
if (!Array.isArray(tableName)) {
options.tableAs = mainTableAs = this.quoteTable(Model.name)
}
options.table = table = !Array.isArray(tableName) ? this.quoteTable(tableName) : tableName.map(function(t) {
return this.quoteTable(t)
return this.quoteTable(t, true)
}.bind(this)).join(", ")
if (subQuery && mainAttributes) {
......@@ -619,20 +635,20 @@ module.exports = (function() {
}
if (options.include && attr.indexOf('.') === -1 && addTable) {
attr = this.quoteTable(options.table) + '.' + attr
attr = mainTableAs + '.' + attr
}
return attr
}.bind(this))
// If no attributes specified, use *
mainAttributes = mainAttributes || (options.include ? [options.table+'.*'] : ['*'])
mainAttributes = mainAttributes || (options.include ? [mainTableAs+'.*'] : ['*'])
// If subquery, we ad the mainAttributes to the subQuery and set the mainAttributes to select * from subquery
if (subQuery) {
// We need primary keys
subQueryAttributes = mainAttributes
mainAttributes = [options.table+'.*']
mainAttributes = mainTableAs[mainTableAs+'.*']
}
if (options.include) {
......@@ -653,7 +669,7 @@ module.exports = (function() {
whereOptions.keysEscaped = true
if (tableName !== parentTable) {
if (tableName !== parentTable && mainTableAs !== parentTable) {
as = parentTable+'.'+include.as
}
......@@ -707,11 +723,11 @@ module.exports = (function() {
targetJoinOn += self.quoteIdentifier(throughAs) + "." + self.quoteIdentifier(identTarget)
// Generate join SQL for left side of through
joinQueryItem += joinType + self.quoteTable(throughTable) + " AS " + self.quoteIdentifier(throughAs) + " ON "
joinQueryItem += joinType + self.quoteTable(throughTable, throughAs) + " ON "
joinQueryItem += sourceJoinOn
// Generate join SQL for right side of through
joinQueryItem += joinType + self.quoteTable(table) + " AS " + self.quoteIdentifier(as) + " ON "
joinQueryItem += joinType + self.quoteTable(table, as) + " ON "
joinQueryItem += targetJoinOn
......@@ -723,8 +739,8 @@ module.exports = (function() {
// Creating the as-is where for the subQuery, checks that the required association exists
var _where = "(";
_where += "SELECT "+self.quoteIdentifier(identSource)+" FROM " + self.quoteTable(throughTable) + " AS " + self.quoteIdentifier(throughAs);
_where += joinType + self.quoteTable(table) + " AS " + self.quoteIdentifier(as) + " ON "+targetJoinOn;
_where += "SELECT "+self.quoteIdentifier(identSource)+" FROM " + self.quoteTable(throughTable, throughAs);
_where += joinType + self.quoteTable(table, as) + " ON "+targetJoinOn;
_where += " WHERE " + sourceJoinOn + " AND " + targetWhere + " LIMIT 1"
_where += ")";
_where += " IS NOT NULL"
......@@ -751,7 +767,7 @@ module.exports = (function() {
where += self.quoteTable(tableRight) + "." + self.quoteIdentifier(attrRight)
// Generate join SQL
joinQueryItem += joinType + self.quoteTable(table) + " AS " + self.quoteIdentifier(as) + " ON "
joinQueryItem += joinType + self.quoteTable(table, as) + " ON "
joinQueryItem += where
if (include.where) {
......@@ -762,7 +778,7 @@ module.exports = (function() {
if (!options.where) options.where = {}
// Creating the as-is where for the subQuery, checks that the required association exists
options.where["__"+as] = self.sequelize.asIs("(SELECT "+self.quoteIdentifier(attrRight)+" FROM " + self.quoteTable(table) + " as " + self.quoteTable(as) + " WHERE " + where + " LIMIT 1) IS NOT NULL")
options.where["__"+as] = self.sequelize.asIs("(SELECT "+self.quoteIdentifier(attrRight)+" FROM " + self.quoteTable(table, as) + " WHERE " + where + " LIMIT 1) IS NOT NULL")
}
}
}
......@@ -790,7 +806,7 @@ module.exports = (function() {
// Loop through includes and generate subqueries
options.include.forEach(function(include) {
var joinQueries = generateJoinQueries(include, tableName)
var joinQueries = generateJoinQueries(include, options.tableAs)
subJoinQueries = subJoinQueries.concat(joinQueries.subQuery)
mainJoinQueries = mainJoinQueries.concat(joinQueries.mainQuery)
......@@ -805,6 +821,9 @@ module.exports = (function() {
// Else do it the reguar way
} else {
mainQueryItems.push("SELECT " + mainAttributes.join(', ') + " FROM " + options.table)
if (options.tableAs) {
mainQueryItems.push(" AS "+options.tableAs)
}
mainQueryItems.push(mainJoinQueries.join(''))
}
......@@ -876,33 +895,9 @@ module.exports = (function() {
// If using subQuery, select attributes from wrapped subQuery and join out join tables
if (subQuery) {
//the subquery "AS" uses the tableName at the front of the SELECT (out of the subquery)
//as the "AS", when this is the case, force quoting on the mainAttributes "options.table"
//that will be used in the "AS".
var asTable = options.table
var aliasAttrs = mainAttributes[0].split('.')
if(aliasAttrs.length > 1) {
var baseName = aliasAttrs[0]+'.'+aliasAttrs[1]
//here is the match from subquery to mainAttributes
if(baseName === options.table) {
var aliasAttrs = mainAttributes[0].split('.')
var remainingVals = Utils._.rest(aliasAttrs, 2)
//only add back the rest if it exists
if(remainingVals.length) {
mainAttributes[0] = self.quoteIdentifier(baseName, true)+'.'+remainingVals.join('.')
} else {
mainAttributes[0] = self.quoteIdentifier(baseName, true)
}
//quote the "as"
asTable = self.quoteIdentifier(options.table, true)
}
}
query = "SELECT " + mainAttributes.join(', ') + " FROM ("
query += subQueryItems.join('')
query += ") AS "+asTable
query += ") AS "+options.tableAs
query += mainJoinQueries.join('')
query += mainQueryItems.join('')
} else {
......@@ -1003,7 +998,7 @@ module.exports = (function() {
result = "(" + result + ")"
} else if (smth instanceof Utils.where) {
var value = smth.logic
, key = this.quoteTable(smth.attribute.Model.getTableName())+'.'+this.quoteIdentifier(smth.attribute.fieldName)
, key = this.quoteTable(smth.attribute.Model.name)+'.'+this.quoteIdentifier(smth.attribute.fieldName)
, logic
, _result = []
, _value
......
......@@ -245,7 +245,7 @@ describe(Support.getTestDialectTeaser("Includes with schemas"), function () {
}
})
it('should support an include with multiple different association types', function (done) {
it.only('should support an include with multiple different association types', function (done) {
var self = this
self.sequelize.dropAllSchemas().success(function(){
......@@ -388,6 +388,8 @@ describe(Support.getTestDialectTeaser("Includes with schemas"), function () {
}, callback)
},
function (err) {
console.log(err.sql);
console.log(err);
expect(err).not.to.be.ok
AccUser.findAll({
......@@ -403,11 +405,12 @@ describe(Support.getTestDialectTeaser("Includes with schemas"), function () {
]}
],
order: [
['account.AccUsers.id', 'ASC']
[AccUser.rawAttributes.id, 'ASC']
]
}).done(function (err, users) {
expect(err).not.to.be.ok
users.forEach(function (user, i) {
expect(user.memberships).to.be.ok
user.memberships.sort(sortById)
expect(user.memberships.length).to.equal(2)
......@@ -433,7 +436,9 @@ describe(Support.getTestDialectTeaser("Includes with schemas"), function () {
)
}]
}, done)
}).error(done)
}).error(done).on('sql', function (sql) {
console.log(sql)
})
})
})
})
......@@ -1404,7 +1409,7 @@ it('should be possible to extend the on clause with a where option on a hasOne i
],
limit: 3,
order: [
[self.sequelize.col(self.models.Product.getTableName()+'.id'), 'ASC']
[self.sequelize.literal(self.models.Product.getTableName()+'.id'), 'ASC']
]
}).done(function (err, products) {
expect(err).not.to.be.ok
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!