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

Commit 670c0438 by Martin Blumenstingl

Define the 'DECIMAL' data-type similar to how a FLOAT is defined.

This gives us the possibility to use "instanceof DECIMAL" when needed.
1 parent b2bc9a26
Showing with 64 additions and 17 deletions
......@@ -46,6 +46,10 @@ var BLOB = function() {
return BLOB.prototype.construct.apply(this, [BLOB].concat(Array.prototype.slice.apply(arguments)))
}
var DECIMAL = function() {
return DECIMAL.prototype.construct.apply(this, [DECIMAL].concat(Array.prototype.slice.apply(arguments)))
}
FLOAT._type = FLOAT
FLOAT._typeName = 'FLOAT'
INTEGER._type = INTEGER
......@@ -56,8 +60,11 @@ STRING._type = STRING
STRING._typeName = 'VARCHAR'
BLOB._type = BLOB
BLOB._typeName = 'BLOB'
DECIMAL._type = DECIMAL
DECIMAL._typeName = 'DECIMAL'
BLOB.toString = STRING.toString = INTEGER.toString = FLOAT.toString = BIGINT.toString = function() {
BLOB.toString = STRING.toString = INTEGER.toString = FLOAT.toString = BIGINT.toString = DECIMAL.toString = function() {
return new this._type().toString()
}
......@@ -145,6 +152,47 @@ FLOAT.prototype = BIGINT.prototype = INTEGER.prototype = {
}
}
DECIMAL.prototype = {
construct: function(RealType, precision, scale) {
if (this instanceof RealType) {
this._typeName = RealType._typeName
if (typeof precision === 'number') {
this._precision = precision
} else {
this._precision = 0
}
if (typeof scale === 'number') {
this._scale = scale
} else {
this._scale = 0
}
} else {
return new RealType(precision, scale)
}
},
get type() {
return 'DECIMAL'
},
get PRECISION() {
return this._precision
},
get SCALE() {
return this._scale
},
toString: function() {
if (this._precision || this._scale) {
return 'DECIMAL(' + this._precision + ',' + this._scale + ')'
}
return 'DECIMAL'
}
}
var unsignedDesc = {
get: function() {
return new this._type(undefined, undefined, true)
......@@ -163,11 +211,18 @@ var typeDesc = {
}
}
var decimalDesc = {
get: function() {
return new this._type(undefined, undefined, undefined)
}
}
Object.defineProperty(STRING, 'type', typeDesc)
Object.defineProperty(INTEGER, 'type', typeDesc)
Object.defineProperty(BIGINT, 'type', typeDesc)
Object.defineProperty(FLOAT, 'type', typeDesc)
Object.defineProperty(BLOB, 'type', typeDesc)
Object.defineProperty(DECIMAL, 'type', typeDesc)
Object.defineProperty(INTEGER, 'UNSIGNED', unsignedDesc)
Object.defineProperty(BIGINT, 'UNSIGNED', unsignedDesc)
......@@ -177,6 +232,9 @@ Object.defineProperty(INTEGER, 'ZEROFILL', zerofillDesc)
Object.defineProperty(BIGINT, 'ZEROFILL', zerofillDesc)
Object.defineProperty(FLOAT, 'ZEROFILL', zerofillDesc)
Object.defineProperty(DECIMAL, 'PRECISION', decimalDesc)
Object.defineProperty(DECIMAL, 'SCALE', decimalDesc)
module.exports = {
STRING: STRING,
......@@ -188,6 +246,7 @@ module.exports = {
FLOAT: FLOAT,
NOW: 'NOW',
BLOB: BLOB,
DECIMAL: DECIMAL,
UUID: 'UUID',
UUIDV1: 'UUIDV1',
UUIDV4: 'UUIDV4',
......@@ -207,16 +266,6 @@ module.exports = {
return result
},
get DECIMAL() {
var result = function(precision, scale) {
return 'DECIMAL(' + precision + ',' + scale + ')'
}
result.toString = result.valueOf = function() { return 'DECIMAL' }
return result
},
ARRAY: function(type) { return type + '[]' },
get HSTORE() {
......
......@@ -11,11 +11,6 @@ describe(Support.getTestDialectTeaser('DataTypes'), function() {
done()
})
it('should return DECIMAL(10,2) for the default decimal type with arguments', function(done) {
expect(Sequelize.DECIMAL(10, 2)).to.equal('DECIMAL(10,2)')
done()
})
var tests = [
[Sequelize.STRING, 'STRING', 'VARCHAR(255)'],
[Sequelize.STRING(1234), 'STRING(1234)', 'VARCHAR(1234)'],
......@@ -61,7 +56,10 @@ describe(Support.getTestDialectTeaser('DataTypes'), function() {
[Sequelize.FLOAT(11, 12).UNSIGNED, 'FLOAT(11,12).UNSIGNED', 'FLOAT(11,12) UNSIGNED'],
[Sequelize.FLOAT(11, 12).UNSIGNED.ZEROFILL,'FLOAT(11,12).UNSIGNED.ZEROFILL','FLOAT(11,12) UNSIGNED ZEROFILL'],
[Sequelize.FLOAT(11, 12).ZEROFILL,'FLOAT(11,12).ZEROFILL', 'FLOAT(11,12) ZEROFILL'],
[Sequelize.FLOAT(11, 12).ZEROFILL.UNSIGNED,'FLOAT(11,12).ZEROFILL.UNSIGNED', 'FLOAT(11,12) UNSIGNED ZEROFILL']
[Sequelize.FLOAT(11, 12).ZEROFILL.UNSIGNED,'FLOAT(11,12).ZEROFILL.UNSIGNED', 'FLOAT(11,12) UNSIGNED ZEROFILL'],
[Sequelize.DECIMAL, 'DECIMAL', 'DECIMAL'],
[Sequelize.DECIMAL(10,2), 'DECIMAL(10,2)','DECIMAL(10,2)']
]
tests.forEach(function(test) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!