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

Commit 8b44643b by Mick Hansen

yay, some logic works

1 parent 872f66ef
...@@ -2,32 +2,26 @@ var Utils = require('./../utils') ...@@ -2,32 +2,26 @@ var Utils = require('./../utils')
, Transaction = require('./../transaction') , Transaction = require('./../transaction')
module.exports = (function() { module.exports = (function() {
var HasManySingleLinked = function(definition, instance) { var HasManySingleLinked = function(association, instance) {
this.__factory = definition this.__factory = association
this.association = association
this.instance = instance this.instance = instance
this.target = this.association.target
this.source = this.association.source
} }
HasManySingleLinked.prototype.injectGetter = function(options) { HasManySingleLinked.prototype.injectGetter = function(options) {
var self = this
, where = {}
, smart
options = options || {} options = options || {}
var primaryKey = Object.keys(this.instance.rawAttributes).filter(function(k) { return self.instance.rawAttributes[k].primaryKey === true }) options.where = new Utils.and([
primaryKey = primaryKey.length === 1 ? primaryKey[0] : 'id' new Utils.where(
where[this.__factory.identifier] = this.instance[primaryKey] this.target.rawAttributes[this.association.identifier],
this.instance[this.source.primaryKeyAttribute])
if (options.where) { ,
smart = Utils.smartWhere([where, options.where], this.__factory.target.daoFactoryManager.sequelize.options.dialect) options.where
smart = Utils.compileSmartWhere.call(this.__factory.target, smart, this.__factory.target.daoFactoryManager.sequelize.options.dialect) ])
if (smart.length > 0) {
options.where = smart
}
} else {
options.where = where
}
return this.__factory.target.all(options) return this.association.target.all(options)
} }
HasManySingleLinked.prototype.injectSetter = function(emitter, oldAssociations, newAssociations, defaultAttributes) { HasManySingleLinked.prototype.injectSetter = function(emitter, oldAssociations, newAssociations, defaultAttributes) {
......
...@@ -294,23 +294,20 @@ module.exports = (function() { ...@@ -294,23 +294,20 @@ module.exports = (function() {
}).run() }).run()
} }
obj[this.accessors.add] = function(newAssociatedObject, additionalAttributes) { obj[this.accessors.add] = function(newInstance, additionalAttributes) {
var instance = this var instance = this
, primaryKeys = Object.keys(newAssociatedObject.Model.primaryKeys || {}) , primaryKey = newInstance.Model.primaryKeyAttribute
, primaryKey = primaryKeys.length === 1 ? primaryKeys[0] : 'id' , where = new Utils.where(self.target.rawAttributes[primaryKey], newInstance[primaryKey])
, where = {}
where[newAssociatedObject.Model.getTableName()+'.'+primaryKey] = newAssociatedObject[primaryKey]
return new Utils.CustomEventEmitter(function(emitter) { return new Utils.CustomEventEmitter(function(emitter) {
instance[self.accessors.get]({ where: where }) instance[self.accessors.get]({ where: where })
.proxy(emitter, {events: ['error', 'sql']}) .proxy(emitter, {events: ['error', 'sql']})
.success(function(currentAssociatedObjects) { .success(function(currentAssociatedObjects) {
if (currentAssociatedObjects.length === 0 || Object(self.through) === self.through) { if (currentAssociatedObjects.length === 0 || Object(self.through) === self.through) {
var Class = Object(self.through) === self.through ? HasManyDoubleLinked : HasManySingleLinked var Class = Object(self.through) === self.through ? HasManyDoubleLinked : HasManySingleLinked
new Class(self, instance).injectAdder(emitter, newAssociatedObject, additionalAttributes, !!currentAssociatedObjects.length) new Class(self, instance).injectAdder(emitter, newInstance, additionalAttributes, !!currentAssociatedObjects.length)
} else { } else {
emitter.emit('success', newAssociatedObject); emitter.emit('success', newInstance);
} }
}) })
}).run() }).run()
......
...@@ -259,6 +259,9 @@ module.exports = (function() { ...@@ -259,6 +259,9 @@ module.exports = (function() {
}) })
Utils._.each(self.rawAttributes, function(options, attribute) { Utils._.each(self.rawAttributes, function(options, attribute) {
options.Model = self
options.fieldName = attribute
if (options.hasOwnProperty(type)) { if (options.hasOwnProperty(type)) {
_custom[attribute] = options[type] _custom[attribute] = options[type]
} }
......
...@@ -799,6 +799,7 @@ module.exports = (function() { ...@@ -799,6 +799,7 @@ module.exports = (function() {
// Add WHERE to sub or main query // Add WHERE to sub or main query
if (options.hasOwnProperty('where')) { if (options.hasOwnProperty('where')) {
options.where = this.getWhereConditions(options.where, tableName, Model, options) options.where = this.getWhereConditions(options.where, tableName, Model, options)
if (subQuery) { if (subQuery) {
subQueryItems.push(" WHERE " + options.where) subQueryItems.push(" WHERE " + options.where)
...@@ -954,11 +955,35 @@ module.exports = (function() { ...@@ -954,11 +955,35 @@ module.exports = (function() {
if ((smth instanceof Utils.and) || (smth instanceof Utils.or)) { if ((smth instanceof Utils.and) || (smth instanceof Utils.or)) {
var connector = (smth instanceof Utils.and) ? ' AND ' : ' OR ' var connector = (smth instanceof Utils.and) ? ' AND ' : ' OR '
result = smth.args.map(function(arg) { result = smth.args.filter(function (arg) {
return arg !== undefined
}).map(function(arg) {
return self.getWhereConditions(arg, tableName, factory, options, prepend) return self.getWhereConditions(arg, tableName, factory, options, prepend)
}).join(connector) }).join(connector)
result = "(" + result + ")" result = "(" + result + ")"
} else if (smth instanceof Utils.where) {
var value = smth.logic
, key = this.quoteTable(smth.attribute.Model.getTableName())+'.'+this.quoteIdentifier(smth.attribute.fieldName)
, logic
, _result = []
, _value
if (typeof value === 'object') {
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);
} else {
value = this.escape(value)
}
result = (value == 'NULL') ? key + " IS NULL" : [key, value].join("=")
}
} else if (Utils.isHash(smth)) { } else if (Utils.isHash(smth)) {
if (prepend) { if (prepend) {
options.keysEscaped = true options.keysEscaped = true
......
...@@ -467,6 +467,14 @@ module.exports = (function() { ...@@ -467,6 +467,14 @@ module.exports = (function() {
return new Utils.or(Array.prototype.slice.call(arguments)) return new Utils.or(Array.prototype.slice.call(arguments))
} }
Sequelize.where = Sequelize.prototype.where = function() {
return new Utils.where(Array.prototype.slice.call(arguments))
}
Sequelize.condition = Sequelize.prototype.condition = function() {
return new Utils.condition(Array.prototype.slice.call(arguments))
}
Sequelize.prototype.transaction = function(_options, _callback) { Sequelize.prototype.transaction = function(_options, _callback) {
var options = (typeof _options === 'function') ? {} : _options var options = (typeof _options === 'function') ? {} : _options
, callback = (typeof _options === 'function') ? _options : _callback , callback = (typeof _options === 'function') ? _options : _callback
......
...@@ -541,6 +541,11 @@ var Utils = module.exports = { ...@@ -541,6 +541,11 @@ var Utils = module.exports = {
this.args = args this.args = args
}, },
where: function(attribute, logic) {
this.attribute = attribute
this.logic = logic
},
generateUUID: function() { generateUUID: function() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8) var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8)
...@@ -553,6 +558,8 @@ var Utils = module.exports = { ...@@ -553,6 +558,8 @@ var Utils = module.exports = {
} }
} }
Utils.condition = Utils.where
// I know this may seem silly, but this gives us the ability to recognize whether // I know this may seem silly, but this gives us the ability to recognize whether
// or not we should be escaping or if we should trust the user. Basically, it // or not we should be escaping or if we should trust the user. Basically, it
// keeps things in perspective and organized. // keeps things in perspective and organized.
......
...@@ -104,7 +104,8 @@ describe(Support.getTestDialectTeaser("HasMany"), function() { ...@@ -104,7 +104,8 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
]) ])
chainer.runSerially() chainer.runSerially()
.success(function(_, label1, hasLabel1, hasLabel2) { .done(function(err, _, label1, hasLabel1, hasLabel2) {
expect(err).not.to.be.ok
expect(hasLabel1).to.be.true expect(hasLabel1).to.be.true
expect(hasLabel2).to.be.false expect(hasLabel2).to.be.false
done() done()
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!