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

Commit 44b95392 by Sascha Depold

Add support for Sequelize.and / Sequelize.or

1 parent b49f7c0c
......@@ -807,12 +807,22 @@ module.exports = (function() {
getWhereConditions: function(smth, tableName, factory, options) {
var result = null
, where = {}
, self = this
if ((smth instanceof Utils.and) || (smth instanceof Utils.or)) {
var connector = (smth instanceof Utils.and) ? ' AND ' : ' OR '
result = smth.args.map(function(arg) {
return self.getWhereConditions(arg, tableName, factory, options)
}).join(connector)
if (Utils.isHash(smth)) {
result = "(" + result + ")"
} else if (Utils.isHash(smth)) {
smth = Utils.prependTableNameToHash(tableName, smth)
result = this.hashToWhereConditions(smth, factory, options)
} else if (typeof smth === 'number') {
var primaryKeys = !!factory ? Object.keys(factory.primaryKeys) : []
if (primaryKeys.length > 0) {
// Since we're just a number, assume only the first key
primaryKeys = primaryKeys[0]
......@@ -826,8 +836,21 @@ module.exports = (function() {
} else if (typeof smth === "string") {
result = smth
} else if (Array.isArray(smth)) {
var treatAsAnd = smth.reduce(function(treatAsAnd, arg) {
if (treatAsAnd) {
return treatAsAnd
} else {
return !(arg instanceof Date) && ((arg instanceof Utils.and) || (arg instanceof Utils.or) || Utils.isHash(arg) || Array.isArray(arg))
}
}, false)
if (treatAsAnd) {
var _smth = self.sequelize.and.apply(null, smth)
result = self.getWhereConditions(_smth, tableName, factory, options)
} else {
result = Utils.format(smth, this.dialect)
}
}
return result ? result : '1=1'
},
......
/* jshint camelcase: false */
/* jshint expr: true */
var chai = require('chai')
, Sequelize = require('../../index')
, expect = chai.expect
, Support = require(__dirname + '/../support')
, DataTypes = require(__dirname + "/../../lib/data-types")
, dialect = Support.getTestDialect()
, config = require(__dirname + "/../config/config")
, sinon = require('sinon')
, datetime = require('chai-datetime')
, _ = require('lodash')
, moment = require('moment')
, async = require('async')
chai.use(datetime)
chai.Assertion.includeStack = true
describe(Support.getTestDialectTeaser("DAOFactory"), function () {
beforeEach(function(done) {
this.User = this.sequelize.define('User', {
username: DataTypes.STRING,
secretValue: DataTypes.STRING,
data: DataTypes.STRING,
intVal: DataTypes.INTEGER,
theDate: DataTypes.DATE,
aBool: DataTypes.BOOLEAN
})
this.User.sync({ force: true }).success(function() {
done()
})
})
;(['or', 'and']).forEach(function(method) {
var word = method.toUpperCase()
describe('Sequelize.' + method, function() {
it('can handle plain strings', function(done) {
this.User.find({
where: Sequelize[method]( "1=1", "2=2" )
}).on('sql', function(sql) {
expect(sql).to.contain("WHERE (1=1 " + word + " 2=2) LIMIT 1")
done()
})
})
it('can handle arrays', function(done) {
this.User.find({
where: Sequelize[method]( ["1=?", 1], ["2=?", 2] )
}).on('sql', function(sql) {
expect(sql).to.contain("WHERE (1=1 " + word + " 2=2) LIMIT 1")
done()
})
})
it('can handle objects', function(done) {
this.User.find({
where: Sequelize[method]( { username: "foo", intVal: 2 }, { secretValue: 'bar' } )
}).on('sql', function(sql) {
var expectation = ({
mysql: "WHERE (`Users`.`username`='foo' AND `Users`.`intVal`=2 " + word + " `Users`.`secretValue`='bar')",
sqlite: "WHERE (`Users`.`username`='foo' AND `Users`.`intVal`=2 " + word + " `Users`.`secretValue`='bar')",
postgres: 'WHERE ("Users"."username"=\'foo\' AND "Users"."intVal"=2 ' + word + ' "Users"."secretValue"=\'bar\')',
mariadb: "WHERE (`Users`.`username`='foo' AND `Users`.`intVal`=2 " + word + " `Users`.`secretValue`='bar')"
})[Support.getTestDialect()]
if (!expectation) {
console.log(sql)
throw new Error('Undefined expectation for ' + Support.getTestDialect())
}
expect(sql).to.contain(expectation)
done()
})
})
it('can handle numbers', function(done) {
this.User.find({
where: Sequelize[method]( 1, 2 )
}).on('sql', function(sql) {
var expectation = ({
mysql: "WHERE (`Users`.`id`=1 " + word + " `Users`.`id`=2)",
sqlite: "WHERE (`Users`.`id`=1 " + word + " `Users`.`id`=2)",
postgres: 'WHERE ("Users"."id"=1 ' + word + ' "Users"."id"=2)',
mariadb: "WHERE (`Users`.`id`=1 " + word + " `Users`.`id`=2)"
})[Support.getTestDialect()]
if (!expectation) {
console.log(sql)
throw new Error('Undefined expectation for ' + Support.getTestDialect())
}
expect(sql).to.contain(expectation)
done()
})
})
})
})
describe('Combinations of Sequelize.and and Sequelize.or', function() {
it('allows nesting of Sequelize.or', function(done) {
this.User.find({
where: Sequelize.and( Sequelize.or("1=1", "2=2"), Sequelize.or("3=3", "4=4") )
}).on('sql', function(sql) {
expect(sql).to.contain("WHERE ((1=1 OR 2=2) AND (3=3 OR 4=4)) LIMIT 1")
done()
})
})
it('allows nesting of Sequelize.and', function(done) {
this.User.find({
where: Sequelize.or( Sequelize.and("1=1", "2=2"), Sequelize.and("3=3", "4=4") )
}).on('sql', function(sql) {
expect(sql).to.contain("WHERE ((1=1 AND 2=2) OR (3=3 AND 4=4)) LIMIT 1")
done()
})
})
it('correctly handles complex combinations', function(done) {
this.User.find({
where: [
42, "2=2", ["1=?", 1], { username: "foo" },
Sequelize.or(
42, "2=2", ["1=?", 1], { username: "foo" },
Sequelize.and( 42, "2=2", ["1=?", 1], { username: "foo" } ),
Sequelize.or( 42, "2=2", ["1=?", 1], { username: "foo" } )
),
Sequelize.and(
42, "2=2", ["1=?", 1], { username: "foo" },
Sequelize.or( 42, "2=2", ["1=?", 1], { username: "foo" } ),
Sequelize.and( 42, "2=2", ["1=?", 1], { username: "foo" } )
)
]
}).on('sql', function(sql) {
if (Support.getTestDialect() === 'postgres') {
expect(sql).to.contain(
'WHERE (' + [
'"Users"."id"=42 AND 2=2 AND 1=1 AND "Users"."username"=\'foo\' AND ',
'(',
'"Users"."id"=42 OR 2=2 OR 1=1 OR "Users"."username"=\'foo\' OR ',
'("Users"."id"=42 AND 2=2 AND 1=1 AND "Users"."username"=\'foo\') OR ',
'("Users"."id"=42 OR 2=2 OR 1=1 OR "Users"."username"=\'foo\')',
') AND ',
'(',
'"Users"."id"=42 AND 2=2 AND 1=1 AND "Users"."username"=\'foo\' AND ',
'("Users"."id"=42 OR 2=2 OR 1=1 OR "Users"."username"=\'foo\') AND ',
'("Users"."id"=42 AND 2=2 AND 1=1 AND "Users"."username"=\'foo\')',
')'
].join("") +
')'
)
} else {
expect(sql).to.contain(
"WHERE (" + [
"`Users`.`id`=42 AND 2=2 AND 1=1 AND `Users`.`username`='foo' AND ",
"(",
"`Users`.`id`=42 OR 2=2 OR 1=1 OR `Users`.`username`='foo' OR ",
"(`Users`.`id`=42 AND 2=2 AND 1=1 AND `Users`.`username`='foo') OR ",
"(`Users`.`id`=42 OR 2=2 OR 1=1 OR `Users`.`username`='foo')",
") AND ",
"(",
"`Users`.`id`=42 AND 2=2 AND 1=1 AND `Users`.`username`='foo' AND ",
"(`Users`.`id`=42 OR 2=2 OR 1=1 OR `Users`.`username`='foo') AND ",
"(`Users`.`id`=42 AND 2=2 AND 1=1 AND `Users`.`username`='foo')",
")"
].join("") +
")"
)
}
done()
})
})
})
})
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!