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

Commit 356cd4fe by Mick Hansen

doing work

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