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

Commit 07698882 by Mick Hansen

Merge pull request #1698 from cusspvz/master

Paranoid: don't required non conditional queries
2 parents e1ce0e21 2ed8ea0d
......@@ -737,11 +737,11 @@ module.exports = (function() {
joinQueryItem += sourceJoinOn
// Generate join SQL for right side of through
joinQueryItem += joinType + self.quoteTable( table, as ) + " ON "
joinQueryItem += joinType + self.quoteTable(table, as) + " ON "
joinQueryItem += targetJoinOn
if ( include.where ) {
if (include.where) {
targetWhere = self.getWhereConditions(include.where, self.sequelize.literal(self.quoteIdentifier(as)), include.daoFactory, whereOptions)
joinQueryItem += " AND "+ targetWhere
if (subQuery) {
......@@ -750,9 +750,9 @@ module.exports = (function() {
// Creating the as-is where for the subQuery, checks that the required association exists
options.where["__"+throughAs] = self.sequelize.asIs([ '(',
"SELECT " + self.quoteIdentifier( throughAs ) + "." + self.quoteIdentifier( identSource ) + " FROM " + self.quoteTable(throughTable, throughAs),
! include.required && joinType + self.quoteTable( association.source.tableName, tableSource ) + " ON " + sourceJoinOn || '',
joinType + self.quoteTable( table, as ) + " ON " + targetJoinOn,
"SELECT " + self.quoteIdentifier(throughAs) + "." + self.quoteIdentifier(identSource) + " FROM " + self.quoteTable(throughTable, throughAs),
! include.required && joinType + self.quoteTable(association.source.tableName, tableSource) + " ON " + sourceJoinOn || '',
joinType + self.quoteTable(table, as) + " ON " + targetJoinOn,
"WHERE " + ( ! include.required && targetWhere || sourceJoinOn + " AND " + targetWhere ),
"LIMIT 1",
......
/* jshint camelcase: false */
/* jshint expr: true */
var chai = require('chai')
, expect = chai.expect
, Support = require(__dirname + '/../support')
, DataTypes = require(__dirname + "/../../lib/data-types")
, datetime = require('chai-datetime')
chai.use(datetime)
chai.config.includeStack = true
describe(Support.getTestDialectTeaser("Paranoid"), function () {
beforeEach(function ( done ) {
var S = this.sequelize,
DT = DataTypes,
A = this.A = S.define( 'A', { name: DT.STRING }, { paranoid: true }),
B = this.B = S.define( 'B', { name: DT.STRING }, { paranoid: true }),
C = this.C = S.define( 'C', { name: DT.STRING }, { paranoid: true }),
D = this.D = S.define( 'D', { name: DT.STRING }, { paranoid: true })
A
.belongsTo( B )
.hasMany( D )
.hasMany( C )
B
.hasMany( A )
.hasMany( C )
C
.belongsTo( A )
.belongsTo( B )
D
.hasMany( A )
S.sync({ force: true }).done(function ( err ) {
expect( err ).not.to.be.ok
done()
})
})
it( 'test if default required behavior is marked as false', function ( done ) {
var A = this.A,
B = this.B,
options = {
include: [
{
model: B,
}
],
}
A.find( options ).done(function ( err ) {
expect( err ).not.to.be.ok
expect( options.include[0].required ).to.be.equal( false )
done()
})
})
it( 'test if non required is marked as false', function ( done ) {
var A = this.A,
B = this.B,
options = {
include: [
{
model: B,
required: false,
}
],
}
A.find( options ).done(function ( err ) {
expect( err ).not.to.be.ok
expect( options.include[0].required ).to.be.equal( false )
done()
})
})
it( 'test if required is marked as true', function ( done ) {
var A = this.A,
B = this.B,
options = {
include: [
{
model: B,
required: true,
}
],
}
A.find( options ).done(function ( err ) {
expect( err ).not.to.be.ok
expect( options.include[0].required ).to.be.equal( true )
done()
})
})
})
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!