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

Commit 995d9819 by Mick Hansen

Merge pull request #1205 from mickhansen/include-subquery-refactor

Include subquery refactor
2 parents 9cb959c2 a6aef16a
......@@ -1280,11 +1280,15 @@ module.exports = (function() {
options.include = options.include.map(function(include) {
include = validateIncludedElement.call(this, include, options.daoFactory)
options.includeMap[include.as] = include
options.includeNames.push(include.as)
if (include.association.isMultiAssociation) options.hasMultiAssociation = true
if (include.association.isSingleAssociation) options.hasSingleAssociation = true
options.includeMap[include.as] = include
options.includeNames.push(include.as)
options.hasIncludeWhere = options.hasIncludeWhere || include.hasIncludeWhere || !!include.where
options.hasIncludeRequired = options.hasIncludeRequired || include.hasIncludeRequired || !!include.required
return include
}.bind(this))
};
......
......@@ -364,6 +364,7 @@ module.exports = (function() {
})
delete result.__children
})
return results
}
......
......@@ -374,6 +374,7 @@ module.exports = (function() {
},
addLimitAndOffset: function(options, query){
query = query || ""
if (options.offset && !options.limit) {
query += " LIMIT " + options.offset + ", " + 18440000000000000000;
} else if (options.limit && !(options.include && (options.limit === 1))) {
......
......@@ -389,6 +389,7 @@ module.exports = (function() {
},
addLimitAndOffset: function(options, query){
query = query || ""
if (!(options.include && (options.limit === 1))) {
if (options.limit) {
query += " LIMIT " + options.limit
......
......@@ -132,6 +132,7 @@ module.exports = (function() {
},
addLimitAndOffset: function(options, query){
query = query || ""
if (options.offset && !options.limit) {
query += " LIMIT " + options.offset + ", " + 10000000000000;
} else if (options.limit && !(options.include && (options.limit === 1))) {
......
......@@ -5,10 +5,11 @@ var Utils = require(__dirname + '/utils')
module.exports = (function() {
var QueryInterface = function(sequelize) {
this.sequelize = sequelize
this.QueryGenerator = require('./dialects/' + this.sequelize.options.dialect + '/query-generator')
this.QueryGenerator.options = this.sequelize.options
this.QueryGenerator._dialect = this.sequelize.dialect
this.sequelize = sequelize
this.QueryGenerator = require('./dialects/' + this.sequelize.options.dialect + '/query-generator')
this.QueryGenerator.options = this.sequelize.options
this.QueryGenerator._dialect = this.sequelize.dialect
this.QueryGenerator.sequelize = this.sequelize
}
Utils.addEventEmitter(QueryInterface)
......
......@@ -395,22 +395,26 @@ module.exports = (function() {
}).run()
}
Sequelize.prototype.fn = function (fn) {
Sequelize.fn = Sequelize.prototype.fn = function (fn) {
return new Utils.fn(fn, Array.prototype.slice.call(arguments, 1))
}
Sequelize.prototype.col = function (col) {
Sequelize.col = Sequelize.prototype.col = function (col) {
return new Utils.col(col)
}
Sequelize.prototype.cast = function (val, type) {
Sequelize.cast = Sequelize.prototype.cast = function (val, type) {
return new Utils.cast(val, type)
}
Sequelize.prototype.literal = function (val) {
Sequelize.literal = Sequelize.prototype.literal = function (val) {
return new Utils.literal(val)
}
Sequelize.asIs = Sequelize.prototype.asIs = function (val) {
return new Utils.asIs(val)
}
Sequelize.prototype.transaction = function(_options, _callback) {
var options = (typeof _options === 'function') ? {} : _options
, callback = (typeof _options === 'function') ? _options : _callback
......
......@@ -450,7 +450,9 @@ var Utils = module.exports = {
var _hash = {}
for (var key in hash) {
if (key.indexOf('.') === -1) {
if (key instanceof Utils.literal) {
_hash[key] = hash[key]
} else if (key.indexOf('.') === -1) {
_hash[tableName + '.' + key] = hash[key]
} else {
_hash[key] = hash[key]
......@@ -540,6 +542,10 @@ var Utils = module.exports = {
this.val = val
},
asIs: function(val) {
this.val = val
},
generateUUID: function() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8)
......@@ -558,6 +564,7 @@ var Utils = module.exports = {
Utils.literal.prototype.toString = function() {
return this.val
}
Utils.asIs.prototype = Utils.literal.prototype
Utils.cast.prototype.toString = function(queryGenerator) {
if (!this.val instanceof Utils.fn && !this.val instanceof Utils.col && !this.val instanceof Utils.literal) {
......
......@@ -47,12 +47,12 @@ describe(Support.getTestDialectTeaser("Multiple Level Filters"), function() {
Task.findAll({
where: {
'project.user.username': 'leia'
}/*,
},
include: [
{model: Project, include: [
User
]}
]*/
]
}).done(function(err, tasks){
expect(err).not.to.be.ok
......@@ -113,12 +113,12 @@ describe(Support.getTestDialectTeaser("Multiple Level Filters"), function() {
where: {
'project.user.username': 'leia',
'project.user.id': 1
}/*,
},
include: [
{model: Project, include: [
User
]}
]*/
]
}).success(function(tasks){
try{
expect(tasks.length).to.be.equal(2);
......@@ -175,12 +175,12 @@ describe(Support.getTestDialectTeaser("Multiple Level Filters"), function() {
User.findAll({
where: {
'projects.tasks.title': 'fight empire'
}/*,
},
include: [
{model: Project, include: [
Task
]}
]*/
]
}).done(function(err, users){
try{
expect(users.length).to.be.equal(1);
......@@ -224,10 +224,10 @@ describe(Support.getTestDialectTeaser("Multiple Level Filters"), function() {
User.findAll({
where: {
'projects.title': 'republic'
}/*,
},
include: [
{model: Project}
]*/
]
}).success(function(users){
try{
expect(users.length).to.be.equal(1);
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!