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

Commit 528d8439 by Mick Hansen

more fixdes

1 parent 8b44643b
......@@ -16,23 +16,31 @@ module.exports = (function() {
, smart
var customEventEmitter = new Utils.CustomEventEmitter(function() {
var where = {}
var where = []
, through = self.association.through
, options = _options || {}
, queryOptions = {}
, targetAssociation = self.association.targetAssociation
//fully qualify
var instancePrimaryKeys = Object.keys(self.instance.Model.primaryKeys)
, instancePrimaryKey = instancePrimaryKeys.length > 0 ? instancePrimaryKeys[0] : 'id'
, foreignPrimary = Object.keys(self.association.target.primaryKeys)
foreignPrimary = foreignPrimary.length === 1 ? foreignPrimary[0] : 'id'
where[self.QueryInterface.quoteIdentifiers(through.getTableName())+"."+self.association.identifier] = self.instance[instancePrimaryKey]
where[self.QueryInterface.quoteIdentifiers(through.getTableName())+"."+self.association.foreignIdentifier] = {
join: self.QueryInterface.quoteIdentifiers(self.association.target.getTableName())+"."+foreignPrimary
}
var instancePrimaryKey = self.instance.Model.primaryKeyAttribute
, foreignPrimaryKey = self.association.target.primaryKeyAttribute
options.where = new Utils.and([
new Utils.where(
through.rawAttributes[self.association.identifier],
self.instance[instancePrimaryKey]
),
new Utils.where(
through.rawAttributes[self.association.foreignIdentifier], {
join: new Utils.literal([
self.QueryInterface.quoteTable(self.association.target.getTableName()),
self.QueryInterface.quoteIdentifier(foreignPrimaryKey)
].join('.'))
}
),
options.where
])
if (Object(targetAssociation.through) === targetAssociation.through) {
queryOptions.hasJoinTableModel = true
......@@ -40,45 +48,27 @@ module.exports = (function() {
if (!options.attributes) {
options.attributes = [
self.QueryInterface.quoteIdentifiers(self.association.target.getTableName())+".*"
self.QueryInterface.quoteTable(self.association.target.getTableName())+".*"
]
}
if (options.joinTableAttributes) {
options.joinTableAttributes.forEach(function (elem) {
options.attributes.push(
self.QueryInterface.quoteIdentifiers(through.getTableName() + '.' + elem) + ' as ' +
self.QueryInterface.quoteTable(through.getTableName()) + '.' + 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.quoteIdentifiers(through.getTableName() + '.' + key) + ' as ' +
self.QueryInterface.quoteTable(through.getTableName()) + '.' + self.QueryInterface.quoteIdentifier(key) + ' as ' +
self.QueryInterface.quoteIdentifier(through.name + '.' + key, true)
)
})
}
}
if (options.where) {
if (Array.isArray(options.where)) {
smart = Utils.smartWhere([where, options.where], self.association.target.daoFactoryManager.sequelize.options.dialect)
smart = Utils.compileSmartWhere.call(self.association.target, smart, self.association.target.daoFactoryManager.sequelize.options.dialect)
if (smart.length > 0) {
options.where = smart
}
} else {
smart = Utils.smartWhere([where, options.where], self.association.target.daoFactoryManager.sequelize.options.dialect)
smart = Utils.compileSmartWhere.call(self.association.target, smart, self.association.target.daoFactoryManager.sequelize.options.dialect)
if (smart.length > 0) {
options.where = smart
}
}
} else {
options.where = where;
}
self.association.target.findAllJoin(through.getTableName(), options, queryOptions)
.on('success', function(objects) { customEventEmitter.emit('success', objects) })
.on('error', function(err){ customEventEmitter.emit('error', err) })
......
......@@ -9,6 +9,8 @@ module.exports = (function() {
var schema = (param.options && param.options.schema ? param.options.schema : undefined)
, schemaDelimiter = (param.options && param.options.schemaDelimiter ? param.options.schemaDelimiter : undefined)
if (!schema) return param.tableName || param;
return {
tableName: param.tableName || param,
schema: schema,
......@@ -500,7 +502,12 @@ module.exports = (function() {
Split an identifier into .-separated tokens and quote each part
*/
quoteIdentifiers: function(identifiers, force) {
throwMethodUndefined('quoteIdentifiers')
return identifiers.split('.').map(function(t) {
if (t.tableName) {
return this.quoteTable(t)
}
return this.quoteIdentifier(t, force)
}.bind(this)).join('.')
},
/*
......@@ -946,6 +953,10 @@ module.exports = (function() {
, where = {}
, self = this
if (Array.isArray(tableName)) {
tableName = tableName[0]
}
options = options || {}
if (typeof prepend === 'undefined') {
......@@ -970,11 +981,16 @@ module.exports = (function() {
, _value
if (typeof value === 'object') {
if (value.join) {
//using as sentinel for join column => value
result = [key, value.join].join("=")
} else {
for (logic in value) {
_result.push([key, this.escape(value[logic])].join(' ' + Utils.getWhereLogic(logic, value[logic]) + ' '))
}
result = _result.join(" AND ")
}
} else {
if (typeof value === 'boolean') {
value = this.booleanValue(value);
......@@ -989,6 +1005,7 @@ module.exports = (function() {
options.keysEscaped = true
smth = this.prependTableNameToHash(tableName, smth)
}
result = this.hashToWhereConditions(smth, factory, options)
} else if (typeof smth === 'number') {
var primaryKeys = !!factory ? Object.keys(factory.primaryKeys) : []
......
......@@ -391,11 +391,6 @@ module.exports = (function() {
if (identifier === '*') return identifier
return Utils.addTicks(identifier, "`")
},
quoteIdentifiers: function(identifiers, force) {
return identifiers.split('.').map(function(v) { return this.quoteIdentifier(v, force) }.bind(this)).join('.')
},
/**
* Generates an SQL query that returns all foreign keys of a table.
*
......
......@@ -10,17 +10,6 @@ module.exports = (function() {
options: {},
dialect: 'postgres',
addSchema: function(param) {
var schema = (param.options && param.options.schema ? param.options.schema : undefined)
, schemaDelimiter = (param.options && param.options.schemaDelimiter ? param.options.schemaDelimiter : undefined)
return {
tableName: param.tableName || param,
schema: schema,
delimiter: schemaDelimiter || '.'
}
},
createSchema: function(schema) {
var query = "CREATE SCHEMA <%= schema%>;"
return Utils._.template(query)({schema: schema})
......@@ -805,10 +794,6 @@ module.exports = (function() {
}
},
quoteIdentifiers: function(identifiers, force) {
return identifiers.split('.').map(function(t) { return this.quoteIdentifier(t, force) }.bind(this)).join('.')
},
/**
* Generates an SQL query that returns all foreign keys of a table.
*
......
......@@ -15,17 +15,6 @@ module.exports = (function() {
options: {},
dialect: 'sqlite',
addSchema: function(param) {
var schema = (param.options && param.options.schema ? param.options.schema : undefined)
, schemaDelimiter = (param.options && param.options.schemaDelimiter ? param.options.schemaDelimiter : undefined)
return {
tableName: param.tableName || param,
schema: schema,
delimiter: schemaDelimiter || '.'
}
},
createSchema: function() {
var query = "SELECT name FROM `sqlite_master` WHERE type='table' and name!='sqlite_sequence';"
return Utils._.template(query)({})
......@@ -412,10 +401,6 @@ module.exports = (function() {
return Utils.addTicks(identifier, "`")
},
quoteIdentifiers: function(identifiers, force) {
return identifiers.split('.').map(function(v) { return this.quoteIdentifier(v, force) }.bind(this)).join('.')
},
/**
* Generates an SQL query that returns all foreign keys of a table.
*
......
......@@ -838,6 +838,11 @@ module.exports = (function() {
return this.QueryGenerator.quoteIdentifier(identifier, force)
}
QueryInterface.prototype.quoteTable = function(identifier) {
return this.QueryGenerator.quoteTable(identifier)
}
/**
* Split an identifier into .-separated tokens and quote each part.
* If force is true, the identifier will be quoted even if the
......
......@@ -546,7 +546,8 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
])
chainer.run().success(function (results, john, task1, task2) {
john.setTasks([task1, task2]).success(function() {
john.setTasks([task1, task2]).done(function(err) {
expect(err).not.to.be.ok
done()
})
})
......@@ -599,6 +600,8 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
john.getTasks({where: {active: true}}).success(function (tasks) {
expect(tasks).to.have.length(1)
done()
}).on('sql', function (sql) {
console.log(sql)
})
})
})
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!