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

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() { ...@@ -1280,11 +1280,15 @@ module.exports = (function() {
options.include = options.include.map(function(include) { options.include = options.include.map(function(include) {
include = validateIncludedElement.call(this, include, options.daoFactory) 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.isMultiAssociation) options.hasMultiAssociation = true
if (include.association.isSingleAssociation) options.hasSingleAssociation = true if (include.association.isSingleAssociation) options.hasSingleAssociation = true
options.includeMap[include.as] = include options.hasIncludeWhere = options.hasIncludeWhere || include.hasIncludeWhere || !!include.where
options.includeNames.push(include.as) options.hasIncludeRequired = options.hasIncludeRequired || include.hasIncludeRequired || !!include.required
return include return include
}.bind(this)) }.bind(this))
}; };
......
...@@ -364,6 +364,7 @@ module.exports = (function() { ...@@ -364,6 +364,7 @@ module.exports = (function() {
}) })
delete result.__children delete result.__children
}) })
return results return results
} }
......
...@@ -374,6 +374,7 @@ module.exports = (function() { ...@@ -374,6 +374,7 @@ module.exports = (function() {
}, },
addLimitAndOffset: function(options, query){ addLimitAndOffset: function(options, query){
query = query || ""
if (options.offset && !options.limit) { if (options.offset && !options.limit) {
query += " LIMIT " + options.offset + ", " + 18440000000000000000; query += " LIMIT " + options.offset + ", " + 18440000000000000000;
} else if (options.limit && !(options.include && (options.limit === 1))) { } else if (options.limit && !(options.include && (options.limit === 1))) {
......
...@@ -389,6 +389,7 @@ module.exports = (function() { ...@@ -389,6 +389,7 @@ module.exports = (function() {
}, },
addLimitAndOffset: function(options, query){ addLimitAndOffset: function(options, query){
query = query || ""
if (!(options.include && (options.limit === 1))) { if (!(options.include && (options.limit === 1))) {
if (options.limit) { if (options.limit) {
query += " LIMIT " + options.limit query += " LIMIT " + options.limit
......
...@@ -132,6 +132,7 @@ module.exports = (function() { ...@@ -132,6 +132,7 @@ module.exports = (function() {
}, },
addLimitAndOffset: function(options, query){ addLimitAndOffset: function(options, query){
query = query || ""
if (options.offset && !options.limit) { if (options.offset && !options.limit) {
query += " LIMIT " + options.offset + ", " + 10000000000000; query += " LIMIT " + options.offset + ", " + 10000000000000;
} else if (options.limit && !(options.include && (options.limit === 1))) { } else if (options.limit && !(options.include && (options.limit === 1))) {
......
...@@ -9,6 +9,7 @@ module.exports = (function() { ...@@ -9,6 +9,7 @@ module.exports = (function() {
this.QueryGenerator = require('./dialects/' + this.sequelize.options.dialect + '/query-generator') this.QueryGenerator = require('./dialects/' + this.sequelize.options.dialect + '/query-generator')
this.QueryGenerator.options = this.sequelize.options this.QueryGenerator.options = this.sequelize.options
this.QueryGenerator._dialect = this.sequelize.dialect this.QueryGenerator._dialect = this.sequelize.dialect
this.QueryGenerator.sequelize = this.sequelize
} }
Utils.addEventEmitter(QueryInterface) Utils.addEventEmitter(QueryInterface)
......
...@@ -395,22 +395,26 @@ module.exports = (function() { ...@@ -395,22 +395,26 @@ module.exports = (function() {
}).run() }).run()
} }
Sequelize.prototype.fn = function (fn) { Sequelize.fn = Sequelize.prototype.fn = function (fn) {
return new Utils.fn(fn, Array.prototype.slice.call(arguments, 1)) 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) 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) return new Utils.cast(val, type)
} }
Sequelize.prototype.literal = function (val) { Sequelize.literal = Sequelize.prototype.literal = function (val) {
return new Utils.literal(val) return new Utils.literal(val)
} }
Sequelize.asIs = Sequelize.prototype.asIs = function (val) {
return new Utils.asIs(val)
}
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
......
...@@ -450,7 +450,9 @@ var Utils = module.exports = { ...@@ -450,7 +450,9 @@ var Utils = module.exports = {
var _hash = {} var _hash = {}
for (var key in 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] _hash[tableName + '.' + key] = hash[key]
} else { } else {
_hash[key] = hash[key] _hash[key] = hash[key]
...@@ -540,6 +542,10 @@ var Utils = module.exports = { ...@@ -540,6 +542,10 @@ var Utils = module.exports = {
this.val = val this.val = val
}, },
asIs: function(val) {
this.val = val
},
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)
...@@ -558,6 +564,7 @@ var Utils = module.exports = { ...@@ -558,6 +564,7 @@ var Utils = module.exports = {
Utils.literal.prototype.toString = function() { Utils.literal.prototype.toString = function() {
return this.val return this.val
} }
Utils.asIs.prototype = Utils.literal.prototype
Utils.cast.prototype.toString = function(queryGenerator) { Utils.cast.prototype.toString = function(queryGenerator) {
if (!this.val instanceof Utils.fn && !this.val instanceof Utils.col && !this.val instanceof Utils.literal) { 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() { ...@@ -47,12 +47,12 @@ describe(Support.getTestDialectTeaser("Multiple Level Filters"), function() {
Task.findAll({ Task.findAll({
where: { where: {
'project.user.username': 'leia' 'project.user.username': 'leia'
}/*, },
include: [ include: [
{model: Project, include: [ {model: Project, include: [
User User
]} ]}
]*/ ]
}).done(function(err, tasks){ }).done(function(err, tasks){
expect(err).not.to.be.ok expect(err).not.to.be.ok
...@@ -113,12 +113,12 @@ describe(Support.getTestDialectTeaser("Multiple Level Filters"), function() { ...@@ -113,12 +113,12 @@ describe(Support.getTestDialectTeaser("Multiple Level Filters"), function() {
where: { where: {
'project.user.username': 'leia', 'project.user.username': 'leia',
'project.user.id': 1 'project.user.id': 1
}/*, },
include: [ include: [
{model: Project, include: [ {model: Project, include: [
User User
]} ]}
]*/ ]
}).success(function(tasks){ }).success(function(tasks){
try{ try{
expect(tasks.length).to.be.equal(2); expect(tasks.length).to.be.equal(2);
...@@ -175,12 +175,12 @@ describe(Support.getTestDialectTeaser("Multiple Level Filters"), function() { ...@@ -175,12 +175,12 @@ describe(Support.getTestDialectTeaser("Multiple Level Filters"), function() {
User.findAll({ User.findAll({
where: { where: {
'projects.tasks.title': 'fight empire' 'projects.tasks.title': 'fight empire'
}/*, },
include: [ include: [
{model: Project, include: [ {model: Project, include: [
Task Task
]} ]}
]*/ ]
}).done(function(err, users){ }).done(function(err, users){
try{ try{
expect(users.length).to.be.equal(1); expect(users.length).to.be.equal(1);
...@@ -224,10 +224,10 @@ describe(Support.getTestDialectTeaser("Multiple Level Filters"), function() { ...@@ -224,10 +224,10 @@ describe(Support.getTestDialectTeaser("Multiple Level Filters"), function() {
User.findAll({ User.findAll({
where: { where: {
'projects.title': 'republic' 'projects.title': 'republic'
}/*, },
include: [ include: [
{model: Project} {model: Project}
]*/ ]
}).success(function(users){ }).success(function(users){
try{ try{
expect(users.length).to.be.equal(1); 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!