From f248dbc919085d81be4bec6ff2e296a727ee17df Mon Sep 17 00:00:00 2001 From: Moises Rodriguez Date: Tue, 2 Feb 2016 14:25:11 -0600 Subject: [PATCH 1/2] Add support for size attribute for float and decimal types. --- lib/sql.js | 51 ++++++++++++++++++++++++- test/unit/adapter.define.js | 76 +++++++++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+), 2 deletions(-) diff --git a/lib/sql.js b/lib/sql.js index ce77ca78..eb95c40e 100644 --- a/lib/sql.js +++ b/lib/sql.js @@ -413,10 +413,57 @@ function sqlTypeCast(attr) { case 'float': case 'double': - return 'FLOAT'; + var type = 'FLOAT'; // default + + if(typeof attr.size === 'string' && attr.size) { + var values = attr.size.split(','); + + if(values.length === 2) { // should always be length 2 + // Validate values + var flag1 = false; + var flag2 = false; + if(!Number.isNaN(values[0]) && (parseInt(values[0]) == parseFloat(values[0])) && (parseInt(values[0]) > 0)) { + var M = values[0]; + flag1 = true; + } + if(!Number.isNaN(values[1]) && (parseInt(values[1]) == parseFloat(values[1])) && (parseInt(values[1]) >= 0)) { + var D = values[1]; + flag2 = true; + } + + if(flag1 && flag2) { + type = 'FLOAT(' + M + ',' + D + ')'; + } + } + } + + return type; case 'decimal': - return 'DECIMAL'; + var type = 'DECIMAL'; // default + + if(typeof attr.size === 'string' && attr.size) { + var values = attr.size.split(','); + + if(values.length === 2) { // should always be length 2 + // Validate values + var flag1 = false; + var flag2 = false; + if(!Number.isNaN(values[0]) && (parseInt(values[0]) == parseFloat(values[0])) && (parseInt(values[0]) > 0) && (parseInt(values[0]) <= 65)) { + var M = values[0]; + flag1 = true; + } + if(!Number.isNaN(values[1]) && (parseInt(values[1]) == parseFloat(values[1])) && (parseInt(values[1]) >= 0)) { + var D = values[1]; + flag2 = true; + } + if(flag1 && flag2) { + type = 'DECIMAL(' + M + ',' + D + ')'; + } + } + } + + return type; case 'date': return 'DATE'; diff --git a/test/unit/adapter.define.js b/test/unit/adapter.define.js index cde31852..a69be6a4 100644 --- a/test/unit/adapter.define.js +++ b/test/unit/adapter.define.js @@ -27,6 +27,22 @@ describe('adapter', function() { type: 'string', notNull: true }, + value : { + type: 'float', + size: '8,4' + }, + decimal : { + type: 'decimal', + size: '10,4' + }, + fail : { + type: 'float', + size: 32 + }, + wrong : { + type: 'decimal', + size: 32 + }, email : 'string', title : 'string', phone : 'string', @@ -75,6 +91,66 @@ describe('adapter', function() { }); }); }); + + it('should create a float with length 8,4', function(done) { + adapter.define('test', 'test_define', definition, function(err) { + support.Client(function(err, client) { + var query = "SELECT COLUMN_TYPE from information_schema.COLUMNS "+ + "WHERE TABLE_SCHEMA = '" + support.Config.database + "' AND TABLE_NAME = 'test_define' AND COLUMN_NAME = 'value'"; + + client.query(query, function(err, rows) { + rows[0].COLUMN_TYPE.should.eql("float(8,4)"); + client.end(); + done(); + }); + }); + }); + }); + + it('should create a decimal with length 10,4', function(done) { + adapter.define('test', 'test_define', definition, function(err) { + support.Client(function(err, client) { + var query = "SELECT COLUMN_TYPE from information_schema.COLUMNS "+ + "WHERE TABLE_SCHEMA = '" + support.Config.database + "' AND TABLE_NAME = 'test_define' AND COLUMN_NAME = 'decimal'"; + + client.query(query, function(err, rows) { + rows[0].COLUMN_TYPE.should.eql("decimal(10,4)"); + client.end(); + done(); + }); + }); + }); + }); + + it('should create a float with no length', function(done) { + adapter.define('test', 'test_define', definition, function(err) { + support.Client(function(err, client) { + var query = "SELECT COLUMN_TYPE from information_schema.COLUMNS "+ + "WHERE TABLE_SCHEMA = '" + support.Config.database + "' AND TABLE_NAME = 'test_define' AND COLUMN_NAME = 'fail'"; + + client.query(query, function(err, rows) { + rows[0].COLUMN_TYPE.should.eql("float"); + client.end(); + done(); + }); + }); + }); + }); + + it('should create a decimal with default length', function(done) { + adapter.define('test', 'test_define', definition, function(err) { + support.Client(function(err, client) { + var query = "SELECT COLUMN_TYPE from information_schema.COLUMNS "+ + "WHERE TABLE_SCHEMA = '" + support.Config.database + "' AND TABLE_NAME = 'test_define' AND COLUMN_NAME = 'wrong'"; + + client.query(query, function(err, rows) { + rows[0].COLUMN_TYPE.should.eql("decimal(10,0)"); + client.end(); + done(); + }); + }); + }); + }); }); From c1d6ea8a8c9c0e3515bd2162717c2d30937755bf Mon Sep 17 00:00:00 2001 From: Moises Rodriguez Date: Thu, 29 Dec 2016 23:34:08 -0600 Subject: [PATCH 2/2] Update mysql version to support node 6 & 7 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index f2833549..42fe7580 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ "dependencies": { "async": "1.5.2", "lodash": "3.10.1", - "mysql": "2.10.2", + "mysql": "2.12.0", "waterline-cursor": "0.0.6", "waterline-errors": "0.10.1", "waterline-sequel": "0.5.7"