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

Commit fd080235 by Gal Jozsef Committed by Sushant

fix: sequelize.where col not null (#11447) (#11454)

1 parent 22d874c3
......@@ -2070,7 +2070,16 @@ class QueryGenerator {
if (value && value instanceof Utils.SequelizeMethod) {
value = this.getWhereConditions(value, tableName, factory, options, prepend);
return value === 'NULL' ? `${key} IS NULL` : [key, value].join(` ${smth.comparator} `);
if (value === 'NULL') {
if (smth.comparator === '=') {
smth.comparator = 'IS';
}
if (smth.comparator === '!=') {
smth.comparator = 'IS NOT';
}
}
return [key, value].join(` ${smth.comparator} `);
}
if (_.isPlainObject(value)) {
return this.whereItemQuery(smth.attribute, value, {
......@@ -2083,7 +2092,16 @@ class QueryGenerator {
value = this.escape(value);
}
return value === 'NULL' ? `${key} IS NULL` : [key, value].join(` ${smth.comparator} `);
if (value === 'NULL') {
if (smth.comparator === '=') {
smth.comparator = 'IS';
}
if (smth.comparator === '!=') {
smth.comparator = 'IS NOT';
}
}
return [key, value].join(` ${smth.comparator} `);
}
if (smth instanceof Utils.Literal) {
return smth.val;
......
......@@ -239,7 +239,7 @@ function isColString(value) {
exports.isColString = isColString;
function canTreatArrayAsAnd(arr) {
return arr.some(arg => _.isPlainObject(arg));
return arr.some(arg => _.isPlainObject(arg) || arg instanceof Where);
}
exports.canTreatArrayAsAnd = canTreatArrayAsAnd;
......
......@@ -91,6 +91,12 @@ describe('QueryGenerator', () => {
const QG = getAbstractQueryGenerator(this.sequelize);
QG.handleSequelizeMethod(this.sequelize.where(this.sequelize.col('foo'), 'LIKE', this.sequelize.col('bar')))
.should.be.equal('foo LIKE bar');
QG.handleSequelizeMethod(this.sequelize.where(this.sequelize.col('foo'), Op.ne, null))
.should.be.equal('foo IS NOT NULL');
QG.handleSequelizeMethod(this.sequelize.where(this.sequelize.col('foo'), Op.not, null))
.should.be.equal('foo IS NOT NULL');
});
});
......
......@@ -1193,5 +1193,18 @@ describe(Support.getTestDialectTeaser('SQL'), () => {
testsql(current.where(current.fn('SUM', current.col('hours')), Op.gt, 0), {
default: 'SUM([hours]) > 0'
});
testsql(current.where(current.fn('lower', current.col('name')), Op.ne, null), {
default: 'lower([name]) IS NOT NULL'
});
testsql(current.where(current.fn('lower', current.col('name')), Op.not, null), {
default: 'lower([name]) IS NOT NULL'
});
testsql([current.where(current.fn('SUM', current.col('hours')), Op.gt, 0),
current.where(current.fn('lower', current.col('name')), null)], {
default: '(SUM([hours]) > 0 AND lower([name]) IS NULL)'
});
});
});
......@@ -20,6 +20,20 @@ describe(Support.getTestDialectTeaser('Utils'), () => {
});
});
describe('canTreatArrayAsAnd', () => {
it('Array can be treated as and', () => {
expect(Utils.canTreatArrayAsAnd([{ 'uuid': 1 }])).to.equal(true);
expect(Utils.canTreatArrayAsAnd([{ 'uuid': 1 }, { 'uuid': 2 }, 1])).to.equal(true);
expect(Utils.canTreatArrayAsAnd([new Utils.Where('uuid', 1)])).to.equal(true);
expect(Utils.canTreatArrayAsAnd([new Utils.Where('uuid', 1), new Utils.Where('uuid', 2)])).to.equal(true);
expect(Utils.canTreatArrayAsAnd([new Utils.Where('uuid', 1), { 'uuid': 2 }, 1])).to.equal(true);
});
it('Array cannot be treated as and', () => {
expect(Utils.canTreatArrayAsAnd([1, 'uuid'])).to.equal(false);
expect(Utils.canTreatArrayAsAnd([1])).to.equal(false);
});
});
describe('toDefaultValue', () => {
it('return plain data types', () => {
expect(Utils.toDefaultValue(DataTypes.UUIDV4)).to.equal('UUIDV4');
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!