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

Commit 1c562924 by durango

Merge pull request #583 from durango/master

Min/Max decimal support closes #579
2 parents a1c06dd7 92bb5fe2
...@@ -271,14 +271,14 @@ module.exports = (function() { ...@@ -271,14 +271,14 @@ module.exports = (function() {
DAOFactory.prototype.max = function(field, options) { DAOFactory.prototype.max = function(field, options) {
options = Utils._.extend({ attributes: [] }, options || {}) options = Utils._.extend({ attributes: [] }, options || {})
options.attributes.push(['max(' + field + ')', 'max']) options.attributes.push(['max(' + field + ')', 'max'])
options.parseInt = true options.parseFloat = true
return this.QueryInterface.rawSelect(this.getTableName(), options, 'max') return this.QueryInterface.rawSelect(this.getTableName(), options, 'max')
} }
DAOFactory.prototype.min = function(field, options) { DAOFactory.prototype.min = function(field, options) {
options = Utils._.extend({ attributes: [] }, options || {}) options = Utils._.extend({ attributes: [] }, options || {})
options.attributes.push(['min(' + field + ')', 'min']) options.attributes.push(['min(' + field + ')', 'min'])
options.parseInt = true options.parseFloat = true
return this.QueryInterface.rawSelect(this.getTableName(), options, 'min') return this.QueryInterface.rawSelect(this.getTableName(), options, 'min')
} }
......
...@@ -296,6 +296,10 @@ module.exports = (function() { ...@@ -296,6 +296,10 @@ module.exports = (function() {
result = parseInt(result) result = parseInt(result)
} }
if (options && options.parseFloat) {
result = parseFloat(result)
}
self.emit('rawSelect', null) self.emit('rawSelect', null)
emitter.emit('success', result) emitter.emit('success', result)
}) })
......
...@@ -1031,7 +1031,13 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() { ...@@ -1031,7 +1031,13 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
age: Sequelize.INTEGER age: Sequelize.INTEGER
}) })
this.UserWithAge.sync({ force: true }).success(done) this.UserWithDec = this.sequelize.define('UserWithDec', {
value: Sequelize.DECIMAL(10, 3)
})
this.UserWithAge.sync({ force: true }).success(function(){
this.UserWithDec.sync({ force: true }).success(done)
}.bind(this))
}) })
it("should return the min value", function(done) { it("should return the min value", function(done) {
...@@ -1052,6 +1058,17 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() { ...@@ -1052,6 +1058,17 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
done() done()
}) })
}) })
it("should allow decimals in min", function(done){
this.UserWithDec.create({value: 3.5}).success(function(){
this.UserWithDec.create({ value: 5.5 }).success(function(){
this.UserWithDec.min('value').success(function(min){
expect(min).toEqual(3.5)
done()
})
}.bind(this))
}.bind(this))
})
}) //- describe: min }) //- describe: min
describe('max', function() { describe('max', function() {
...@@ -1060,7 +1077,13 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() { ...@@ -1060,7 +1077,13 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
age: Sequelize.INTEGER age: Sequelize.INTEGER
}) })
this.UserWithAge.sync({ force: true }).success(done) this.UserWithDec = this.sequelize.define('UserWithDec', {
value: Sequelize.DECIMAL(10, 3)
})
this.UserWithAge.sync({ force: true }).success(function(){
this.UserWithDec.sync({ force: true }).success(done)
}.bind(this))
}) })
it("should return the max value", function(done) { it("should return the max value", function(done) {
...@@ -1074,6 +1097,17 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() { ...@@ -1074,6 +1097,17 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
}.bind(this)) }.bind(this))
}) })
it("should allow decimals in max", function(done){
this.UserWithDec.create({value: 3.5}).success(function(){
this.UserWithDec.create({ value: 5.5 }).success(function(){
this.UserWithDec.max('value').success(function(max){
expect(max).toEqual(5.5)
done()
})
}.bind(this))
}.bind(this))
})
it('allows sql logging', function(done) { it('allows sql logging', function(done) {
this.UserWithAge.max('age').on('sql', function(sql) { this.UserWithAge.max('age').on('sql', function(sql) {
expect(sql).toBeDefined() expect(sql).toBeDefined()
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!