Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for size attribute for float and decimal types. #277

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 49 additions & 2 deletions lib/sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
76 changes: 76 additions & 0 deletions test/unit/adapter.define.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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();
});
});
});
});

});

Expand Down