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

Commit 528d8439 by Mick Hansen

more fixdes

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