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

Commit ac1882e2 by Mick Hansen

Merge pull request #1231 from unlooped/master

better check if 'IS NOT' or '!=' should be used
2 parents b3186857 4e849116
...@@ -299,7 +299,7 @@ var Utils = module.exports = { ...@@ -299,7 +299,7 @@ var Utils = module.exports = {
case 'eq': case 'eq':
return '=' return '='
case 'ne': case 'ne':
return val ? '!=' : 'IS NOT' return val === null ? 'IS NOT' : '!='
case 'between': case 'between':
case '..': case '..':
return 'BETWEEN' return 'BETWEEN'
...@@ -451,7 +451,7 @@ var Utils = module.exports = { ...@@ -451,7 +451,7 @@ var Utils = module.exports = {
for (var key in hash) { for (var key in hash) {
if (key instanceof Utils.literal) { if (key instanceof Utils.literal) {
_hash[key] = hash[key] _hash[key] = hash[key]
} else if (key.indexOf('.') === -1) { } else if (key.indexOf('.') === -1) {
_hash[tableName + '.' + key] = hash[key] _hash[tableName + '.' + key] = hash[key]
} else { } else {
......
...@@ -273,6 +273,16 @@ if (Support.dialectIsMySQL()) { ...@@ -273,6 +273,16 @@ if (Support.dialectIsMySQL()) {
arguments: ['myTable', {where: { field: new Buffer("Sequelize")}}], arguments: ['myTable', {where: { field: new Buffer("Sequelize")}}],
expectation: "SELECT * FROM `myTable` WHERE `myTable`.`field`=X'53657175656c697a65';", expectation: "SELECT * FROM `myTable` WHERE `myTable`.`field`=X'53657175656c697a65';",
context: QueryGenerator context: QueryGenerator
}, {
title: 'use != if ne !== null',
arguments: ['myTable', {where: {field: {ne: 0}}}],
expectation: "SELECT * FROM `myTable` WHERE `myTable`.`field` != 0;",
context: QueryGenerator
}, {
title: 'use IS NOT if ne === null',
arguments: ['myTable', {where: {field: {ne: null}}}],
expectation: "SELECT * FROM `myTable` WHERE `myTable`.`field` IS NOT NULL;",
context: QueryGenerator
} }
], ],
......
...@@ -268,8 +268,8 @@ if (dialect.match(/^postgres/)) { ...@@ -268,8 +268,8 @@ if (dialect.match(/^postgres/)) {
}, { }, {
title: 'functions can take functions as arguments', title: 'functions can take functions as arguments',
arguments: ['myTable', function (sequelize) { arguments: ['myTable', function (sequelize) {
return { return {
order: [[sequelize.fn('f1', sequelize.fn('f2', sequelize.col('id'))), 'DESC']] order: [[sequelize.fn('f1', sequelize.fn('f2', sequelize.col('id'))), 'DESC']]
} }
}], }],
expectation: 'SELECT * FROM "myTable" ORDER BY f1(f2("id")) DESC;', expectation: 'SELECT * FROM "myTable" ORDER BY f1(f2("id")) DESC;',
...@@ -280,7 +280,7 @@ if (dialect.match(/^postgres/)) { ...@@ -280,7 +280,7 @@ if (dialect.match(/^postgres/)) {
arguments: ['myTable', function (sequelize) { arguments: ['myTable', function (sequelize) {
return { return {
order: [ order: [
[sequelize.fn('f1', sequelize.col('myTable.id')), 'DESC'], [sequelize.fn('f1', sequelize.col('myTable.id')), 'DESC'],
[sequelize.fn('f2', 12, 'lalala', new Date(Date.UTC(2011, 2, 27, 10, 1, 55))), 'ASC'] [sequelize.fn('f2', 12, 'lalala', new Date(Date.UTC(2011, 2, 27, 10, 1, 55))), 'ASC']
] ]
} }
...@@ -410,6 +410,16 @@ if (dialect.match(/^postgres/)) { ...@@ -410,6 +410,16 @@ if (dialect.match(/^postgres/)) {
arguments: ['mySchema.myTable', {where: {name: "foo';DROP TABLE mySchema.myTable;"}}], arguments: ['mySchema.myTable', {where: {name: "foo';DROP TABLE mySchema.myTable;"}}],
expectation: "SELECT * FROM mySchema.myTable WHERE mySchema.myTable.name='foo'';DROP TABLE mySchema.myTable;';", expectation: "SELECT * FROM mySchema.myTable WHERE mySchema.myTable.name='foo'';DROP TABLE mySchema.myTable;';",
context: {options: {quoteIdentifiers: false}} context: {options: {quoteIdentifiers: false}}
}, {
title: 'use != if ne !== null',
arguments: ['myTable', {where: {field: {ne: 0}}}],
expectation: "SELECT * FROM myTable WHERE myTable.field != 0;",
context: {options: {quoteIdentifiers: false}}
}, {
title: 'use IS NOT if ne === null',
arguments: ['myTable', {where: {field: {ne: null}}}],
expectation: "SELECT * FROM myTable WHERE myTable.field IS NOT NULL;",
context: {options: {quoteIdentifiers: false}}
} }
], ],
......
...@@ -165,8 +165,8 @@ if (dialect === 'sqlite') { ...@@ -165,8 +165,8 @@ if (dialect === 'sqlite') {
}, { }, {
title: 'functions can take functions as arguments', title: 'functions can take functions as arguments',
arguments: ['myTable', function (sequelize) { arguments: ['myTable', function (sequelize) {
return { return {
order: [[sequelize.fn('f1', sequelize.fn('f2', sequelize.col('id'))), 'DESC']] order: [[sequelize.fn('f1', sequelize.fn('f2', sequelize.col('id'))), 'DESC']]
} }
}], }],
expectation: "SELECT * FROM `myTable` ORDER BY f1(f2(`id`)) DESC;", expectation: "SELECT * FROM `myTable` ORDER BY f1(f2(`id`)) DESC;",
...@@ -177,7 +177,7 @@ if (dialect === 'sqlite') { ...@@ -177,7 +177,7 @@ if (dialect === 'sqlite') {
arguments: ['myTable', function (sequelize) { arguments: ['myTable', function (sequelize) {
return { return {
order: [ order: [
[sequelize.fn('f1', sequelize.col('myTable.id')), 'DESC'], [sequelize.fn('f1', sequelize.col('myTable.id')), 'DESC'],
[sequelize.fn('f2', 12, 'lalala', new Date(Date.UTC(2011, 2, 27, 10, 1, 55))), 'ASC'] [sequelize.fn('f2', 12, 'lalala', new Date(Date.UTC(2011, 2, 27, 10, 1, 55))), 'ASC']
] ]
} }
...@@ -260,6 +260,16 @@ if (dialect === 'sqlite') { ...@@ -260,6 +260,16 @@ if (dialect === 'sqlite') {
arguments: ['myTable', {where: { field: new Buffer("Sequelize")}}], arguments: ['myTable', {where: { field: new Buffer("Sequelize")}}],
expectation: "SELECT * FROM `myTable` WHERE `myTable`.`field`=X'53657175656c697a65';", expectation: "SELECT * FROM `myTable` WHERE `myTable`.`field`=X'53657175656c697a65';",
context: QueryGenerator context: QueryGenerator
}, {
title: 'use != if ne !== null',
arguments: ['myTable', {where: {field: {ne: 0}}}],
expectation: "SELECT * FROM `myTable` WHERE `myTable`.`field` != 0;",
context: QueryGenerator
}, {
title: 'use IS NOT if ne === null',
arguments: ['myTable', {where: {field: {ne: null}}}],
expectation: "SELECT * FROM `myTable` WHERE `myTable`.`field` IS NOT NULL;",
context: QueryGenerator
} }
], ],
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!