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

Allow for fixed-size binary primary key #217

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
30 changes: 26 additions & 4 deletions lib/sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,24 @@ var sql = module.exports = {
if(attribute.primaryKey) {

// If type is an integer, set auto increment
if(type === 'INT') {
if (type === 'INT') {

return attrName + ' INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY';
}


// If type is BLOB, reset to a fixed size BINARY column
if (type === 'BLOB') {

// Default 16 byte length
var size = 16;
if (validColumnSize(attr.size)) {

size = attr.size;
}

return attrName + ' BINARY('+ size +') NOT NULL PRIMARY KEY';
}

// Just set NOT NULL on other types
return attrName + ' VARCHAR(255) NOT NULL PRIMARY KEY';
}
Expand Down Expand Up @@ -358,9 +372,11 @@ function sqlTypeCast(attr) {
var size = 255; // By default.

// If attr.size is positive integer, use it as size of varchar.
if(!isNaN(attr.size) && (parseInt(attr.size) == parseFloat(attr.size)) && (parseInt(attr.size) > 0))
if(validColumnSize(attr.size)) {

size = attr.size;

}

return 'VARCHAR(' + size + ')';
}

Expand Down Expand Up @@ -428,3 +444,9 @@ function validSubAttrCriteria(c) {
!_.isUndefined(c['<=']) || !_.isUndefined(c['!']) || !_.isUndefined(c['>']) || !_.isUndefined(c['>=']) ||
!_.isUndefined(c.startsWith) || !_.isUndefined(c.endsWith) || !_.isUndefined(c.contains) || !_.isUndefined(c.like));
}

function validColumnSize(size) {

// Is definitely an integer number greater than zero
return !isNaN(size) && (parseInt(size) == parseFloat(size)) && (parseInt(size) > 0);
}