Skip to content

Commit

Permalink
Merge pull request sequelize#1364 from SohumB/bugfix/use-ishash-to-av…
Browse files Browse the repository at this point in the history
…oid-buffers

Fixes sequelize#1358
  • Loading branch information
janmeier committed Feb 12, 2014
2 parents 8aafd1b + d6f936e commit 18910ff
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 11 deletions.
10 changes: 8 additions & 2 deletions lib/dao-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,10 @@ module.exports = (function() {

options = { where: parsedId }
} else if (typeof options === 'object') {
options = Utils._.clone(options)
options = Utils._.clone(options, function(thing) {
if (Buffer.isBuffer(thing)) { return thing }
return undefined;
})

if (options.hasOwnProperty('include') && options.include) {
hasJoin = true
Expand Down Expand Up @@ -1443,11 +1446,14 @@ module.exports = (function() {

var optClone = function (options) {
return Utils._.cloneDeep(options, function (elem) {
// The DAOFactories used for include are pass by ref, so don't clone them. Otherwise return undefined, meaning, 'handle this lodash'
// The DAOFactories used for include are pass by ref, so don't clone them.
if (elem instanceof DAOFactory || elem instanceof Utils.col || elem instanceof Utils.literal || elem instanceof Utils.cast || elem instanceof Utils.fn || elem instanceof Utils.and || elem instanceof Utils.or) {
return elem
}
// Unfortunately, lodash.cloneDeep doesn't preserve Buffer.isBuffer, which we have to rely on for binary data
if (Buffer.isBuffer(elem)) { return elem; }

// Otherwise return undefined, meaning, 'handle this lodash'
return undefined
})
}
Expand Down
2 changes: 2 additions & 0 deletions lib/dialects/abstract/query-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,8 @@ module.exports = (function() {
result = this.hashToWhereConditions(smth)
} else if (typeof smth === "string") {
result = smth
} else if (Buffer.isBuffer(smth)) {
result = this.escape(smth)
} else if (Array.isArray(smth)) {
var treatAsAnd = smth.reduce(function(treatAsAnd, arg) {
if (treatAsAnd) {
Expand Down
6 changes: 3 additions & 3 deletions lib/dialects/postgres/query-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,10 @@ module.exports = (function() {
}
}
if ( col && ((!!coltype && coltype.match(/\[\]$/) !== null) || (col.toString().match(/\[\]$/) !== null))) {
_value = 'ARRAY[' + value.map(this.escape).join(',') + ']::' + (!!col.type ? col.type : col.toString())
_value = 'ARRAY[' + value.map(this.escape.bind(this)).join(',') + ']::' + (!!col.type ? col.type : col.toString())
return [_key, _value].join(" && ")
} else {
_value = "(" + value.map(this.escape).join(',') + ")"
_value = "(" + value.map(this.escape.bind(this)).join(',') + ")"
return [_key, _value].join(" " + logicResult + " ")
}
},
Expand Down Expand Up @@ -789,7 +789,7 @@ module.exports = (function() {
dataType = dataType.replace(/NOT NULL/, '')
}

if (dataType.lastIndexOf('BLOB') !== -1) {
if (dataType.lastIndexOf('BLOB') !== -1 || dataType.lastIndexOf('BINARY') !== -1) {
dataType = 'bytea'
}

Expand Down
8 changes: 4 additions & 4 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ var Utils = module.exports = {
_where[i].in = _where[i].in || []
_where[i].in.concat(where[i])
}
else if (type === "object") {
else if (Utils.isHash(where[i])) {
Object.keys(where[i]).forEach(function(ii) {
logic = self.getWhereLogic(ii, where[i][ii]);

Expand Down Expand Up @@ -212,7 +212,7 @@ var Utils = module.exports = {
}
})
}
else if (type === "string" || type === "number" || type === "boolean") {
else if (type === "string" || type === "number" || type === "boolean" || Buffer.isBuffer(where[i])) {
_where[i].lazy = _where[i].lazy || {conditions: [], bindings: []}
if (type === "boolean") {
_where[i].lazy.conditions[_where[i].lazy.conditions.length] = '= ' + SqlString.escape(where[i], false, null, dialect) // sqlite is special
Expand Down Expand Up @@ -321,7 +321,7 @@ var Utils = module.exports = {
}
},
isHash: function(obj) {
return Utils._.isObject(obj) && !Array.isArray(obj);
return Utils._.isObject(obj) && !Array.isArray(obj) && !Buffer.isBuffer(obj);
},
hasChanged: function(attrValue, value) {
//If attribute value is Date, check value as a date
Expand All @@ -348,7 +348,7 @@ var Utils = module.exports = {
if (['number', 'string'].indexOf(typeof arg) !== -1) {
result = true
} else {
result = (arg instanceof Date)
result = (arg instanceof Date) || Buffer.isBuffer(arg);
}
}
})
Expand Down
75 changes: 73 additions & 2 deletions test/dao-factory/findAll.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
data: DataTypes.STRING,
intVal: DataTypes.INTEGER,
theDate: DataTypes.DATE,
aBool: DataTypes.BOOLEAN
aBool: DataTypes.BOOLEAN,
binary: DataTypes.STRING(16, true)
})

this.User.sync({ force: true }).success(function() {
Expand Down Expand Up @@ -63,9 +64,12 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
beforeEach(function(done) {
var self = this

this.buf = new Buffer(16);
this.buf.fill('\x01');

this.User.bulkCreate([
{username: 'boo', intVal: 5, theDate: '2013-01-01 12:00'},
{username: 'boo2', intVal: 10, theDate: '2013-01-10 12:00'}
{username: 'boo2', intVal: 10, theDate: '2013-01-10 12:00', binary: this.buf }
]).success(function(user2) {
done()
})
Expand All @@ -92,6 +96,19 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
});
})

it('should not break when using smart syntax on binary fields', function (done) {
this.User.findAll({
where: {
binary: [ this.buf, this.buf ]
}
}).success(function(users){
expect(users).to.have.length(1)
expect(users[0].binary).to.be.an.instanceof.string
expect(users[0].username).to.equal('boo2')
done();
});
});

it('should be able to find a row using like', function(done) {
this.User.findAll({
where: {
Expand Down Expand Up @@ -220,6 +237,60 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
})
})

it('should be able to handle binary values through associations as well...', function(done) {
var User = this.User;
var Binary = this.sequelize.define('Binary', {
id: {
type: DataTypes.STRING(16, true),
primaryKey: true
}
})

var buf1 = this.buf
var buf2 = new Buffer(16)
buf2.fill('\x02')

User.belongsTo(Binary, { foreignKey: 'binary' })

User.sync({ force: true }).success(function() {
Binary.sync({ force: true }).success(function() {
User.bulkCreate([
{username: 'boo5', aBool: false},
{username: 'boo6', aBool: true}
]).success(function() {
Binary.bulkCreate([
{id: buf1},
{id: buf2}
]).success(function() {
User.find(1).success(function(user) {
Binary.find(buf1).success(function(binary) {
user.setBinary(binary).success(function() {
User.find(2).success(function(_user) {
Binary.find(buf2).success(function(_binary) {
_user.setBinary(_binary).success(function() {
_user.getBinary().success(function(_binaryRetrieved) {
user.getBinary().success(function(binaryRetrieved) {
expect(binaryRetrieved.id).to.be.an.instanceof.string
expect(_binaryRetrieved.id).to.be.an.instanceof.string
expect(binaryRetrieved.id).to.have.length(16)
expect(_binaryRetrieved.id).to.have.length(16)
expect(binaryRetrieved.id.toString()).to.be.equal(buf1.toString())
expect(_binaryRetrieved.id.toString()).to.be.equal(buf2.toString())
done()
})
})
})
})
})
})
})
})
})
})
})
})
})

it('should be able to return a record with primaryKey being null for new inserts', function(done) {
var Session = this.sequelize.define('Session', {
token: { type: DataTypes.TEXT, allowNull: false },
Expand Down

0 comments on commit 18910ff

Please sign in to comment.