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

Commit 1c6255d5 by Jan Aagaard Meier

Merge pull request #879 from durango/anyarray

Fixes Postgres' ability to search within functions.
2 parents 4dbb571f 352f78db
...@@ -526,7 +526,7 @@ module.exports = (function() { ...@@ -526,7 +526,7 @@ module.exports = (function() {
if (Utils.isHash(smth)) { if (Utils.isHash(smth)) {
smth = Utils.prependTableNameToHash(tableName, smth) smth = Utils.prependTableNameToHash(tableName, smth)
result = this.hashToWhereConditions(smth) result = this.hashToWhereConditions(smth, factory)
} else if (typeof smth === 'number') { } else if (typeof smth === 'number') {
var primaryKeys = !!factory ? Object.keys(factory.primaryKeys) : [] var primaryKeys = !!factory ? Object.keys(factory.primaryKeys) : []
if (primaryKeys.length > 0) { if (primaryKeys.length > 0) {
...@@ -549,7 +549,7 @@ module.exports = (function() { ...@@ -549,7 +549,7 @@ module.exports = (function() {
return result return result
}, },
hashToWhereConditions: function(hash) { hashToWhereConditions: function(hash, factory) {
var result = [] var result = []
for (var key in hash) { for (var key in hash) {
...@@ -561,9 +561,20 @@ module.exports = (function() { ...@@ -561,9 +561,20 @@ module.exports = (function() {
if (Array.isArray(value)) { if (Array.isArray(value)) {
if (value.length === 0) { value = [null] } if (value.length === 0) { value = [null] }
_value = "(" + value.map(this.escape).join(',') + ")"
result.push([_key, _value].join(" IN ")) // Special conditions for searching within an array column type
var _realKey = key.split('.').pop()
if (!!factory && !!factory.rawAttributes[_realKey]) {
var col = factory.rawAttributes[_realKey]
if ((!!col.type && col.type.match(/\[\]$/) !== null) || (col.toString().match(/\[\]$/) !== null)) {
_value = 'ARRAY[' + value.map(this.escape).join(',') + ']::' + (!!col.type ? col.type : col.toString())
result.push([_key, _value].join(" && "))
}
} else {
_value = "(" + value.map(this.escape).join(',') + ")"
result.push([_key, _value].join(" IN "))
}
} }
else if ((value) && (typeof value === "object")) { else if ((value) && (typeof value === "object")) {
if (!!value.join) { if (!!value.join) {
......
...@@ -26,6 +26,28 @@ if (dialect.match(/^postgres/)) { ...@@ -26,6 +26,28 @@ if (dialect.match(/^postgres/)) {
done() done()
}) })
it('should be able to search within an array', function(done) {
this.User.all({where: {email: ['hello', 'world']}}).on('sql', function(sql) {
expect(sql).to.equal('SELECT * FROM "Users" WHERE "Users"."email" && ARRAY[\'hello\',\'world\']::TEXT[];')
done()
})
})
it('should be able to find a record while searching in an array', function(done) {
var self = this
this.User.bulkCreate([
{username: 'bob', email: ['myemail@email.com']},
{username: 'tony', email: ['wrongemail@email.com']}
]).success(function() {
self.User.all({where: {email: ['myemail@email.com']}}).success(function(user) {
expect(user).to.be.instanceof(Array)
expect(user).to.have.length(1)
expect(user[0].username).to.equal('bob')
done()
})
})
})
it('describeTable should tell me that a column is hstore and not USER-DEFINED', function(done) { it('describeTable should tell me that a column is hstore and not USER-DEFINED', function(done) {
this.sequelize.queryInterface.describeTable('Users').success(function(table) { this.sequelize.queryInterface.describeTable('Users').success(function(table) {
expect(table.document.type).to.equal('HSTORE') expect(table.document.type).to.equal('HSTORE')
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!