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

Commit bb2c1615 by Meg Sharkey

construct join query for many-to-many association getter -- currently won't work…

… on self many-to-many
1 parent 87dbb148
...@@ -10,22 +10,19 @@ HasManyDoubleLinked.prototype.injectGetter = function() { ...@@ -10,22 +10,19 @@ HasManyDoubleLinked.prototype.injectGetter = function() {
var customEventEmitter = new Utils.CustomEventEmitter(function() { var customEventEmitter = new Utils.CustomEventEmitter(function() {
var where = {} var where = {}
where[self.__definition.identifier] = self.instance.id //connectorModel = join table
//fully qualify
where[self.__definition.connectorModel.tableName+"."+self.__definition.identifier] = self.instance.id
var primaryKeys = Utils._.keys(self.__definition.connectorModel.rawAttributes) var primaryKeys = Utils._.keys(self.__definition.connectorModel.rawAttributes)
, foreignKey = primaryKeys.filter(function(pk) { return pk != self.__definition.identifier })[0] , foreignKey = primaryKeys.filter(function(pk) { return pk != self.__definition.identifier })[0]
self.__definition.connectorModel.findAll({where: where}).on('success', function(associatedObjects) { where[self.__definition.connectorModel.tableName+"."+foreignKey] = {join: self.__definition.target.tableName+".id"}
var ids = associatedObjects.map(function(obj) { return obj[foreignKey] })
if (ids.length == 0) { self.__definition.target.findAllJoin(self.__definition.connectorModel.tableName, {where: where})
customEventEmitter.emit('success', []) .on('success', function(objects) { customEventEmitter.emit('success', objects) })
} else { .on('failure', function(err){ customEventEmitter.emit('failure', err) })
self.__definition.target.findAll({where: 'id in (' + ids.join(", ") + ')'})
.on('success', function(objects) { customEventEmitter.emit('success', objects) })
.on('failure', function(err){ customEventEmitter.emit('failure', err) })
}
})
}) })
return customEventEmitter.run() return customEventEmitter.run()
......
...@@ -131,6 +131,14 @@ ModelDefinition.prototype.findAll = function(options) { ...@@ -131,6 +131,14 @@ ModelDefinition.prototype.findAll = function(options) {
return this.query(QueryGenerator.selectQuery(this.tableName, options)) return this.query(QueryGenerator.selectQuery(this.tableName, options))
} }
//right now, the caller (has-many-double-linked) is in charge of the where clause
ModelDefinition.prototype.findAllJoin = function(joinTableName, options) {
optcpy = Utils._.clone(options)
optcpy.attributes = optcpy.attributes || [Utils.addTicks(this.tableName)+".*"]
return this.query(QueryGenerator.selectQuery([this.tableName, joinTableName], optcpy))
}
ModelDefinition.prototype.find = function(options) { ModelDefinition.prototype.find = function(options) {
// options is not a hash but an id // options is not a hash but an id
if(typeof options == 'number') if(typeof options == 'number')
......
...@@ -55,8 +55,8 @@ var QueryGenerator = module.exports = { ...@@ -55,8 +55,8 @@ var QueryGenerator = module.exports = {
*/ */
selectQuery: function(tableName, options) { selectQuery: function(tableName, options) {
options = options || {} options = options || {}
options.table = Utils.addTicks(tableName) options.table = Array.isArray(tableName) ? tableName.map(function(tbl){return Utils.addTicks(tbl)}).join(", ") : Utils.addTicks(tableName)
options.attributes = options.attributes && options.attributes.map(function(attr){return Utils.addTicks(attr)}).join(", ") options.attributes = options.attributes && options.attributes.map(function(attr){return attr.indexOf(Utils.TICK_CHAR)<0 ? Utils.addTicks(attr) : attr}).join(", ")
options.attributes = options.attributes || '*' options.attributes = options.attributes || '*'
var query = "SELECT <%= attributes %> FROM <%= table %>" var query = "SELECT <%= attributes %> FROM <%= table %>"
...@@ -178,8 +178,9 @@ var QueryGenerator = module.exports = { ...@@ -178,8 +178,9 @@ var QueryGenerator = module.exports = {
*/ */
hashToWhereConditions: function(hash) { hashToWhereConditions: function(hash) {
return Utils._.map(hash, function(value, key) { return Utils._.map(hash, function(value, key) {
var _key = Utils.addTicks(key) //handle qualified key names
, _value = null var _key = key.split('.').map(function(col){return Utils.addTicks(col)}).join(".")
var _value = null
if(Array.isArray(value)) { if(Array.isArray(value)) {
_value = "(" + Utils._.map(value, function(subvalue) { _value = "(" + Utils._.map(value, function(subvalue) {
...@@ -187,6 +188,11 @@ var QueryGenerator = module.exports = { ...@@ -187,6 +188,11 @@ var QueryGenerator = module.exports = {
}).join(',') + ")" }).join(',') + ")"
return [_key, _value].join(" IN ") return [_key, _value].join(" IN ")
}
else if (typeof value == 'object') {
//using as sentinel for join column => value
_value = value.join.split('.').map(function(col){return Utils.addTicks(col)}).join(".")
return [_key, _value].join("=")
} else { } else {
_value = Utils.escape(value) _value = Utils.escape(value)
return (_value == 'NULL') ? _key + " IS NULL" : [_key, _value].join("=") return (_value == 'NULL') ? _key + " IS NULL" : [_key, _value].join("=")
......
...@@ -22,6 +22,7 @@ var Utils = module.exports = { ...@@ -22,6 +22,7 @@ var Utils = module.exports = {
addEventEmitter: function(_class) { addEventEmitter: function(_class) {
require("sys").inherits(_class, require('events').EventEmitter) require("sys").inherits(_class, require('events').EventEmitter)
}, },
TICK_CHAR: '`',
addTicks: function(s) { addTicks: function(s) {
return '`' + Utils.removeTicks(s) + '`' return '`' + Utils.removeTicks(s) + '`'
}, },
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!