Skip to content

Commit

Permalink
Added support for bigint as distinct from regular ints
Browse files Browse the repository at this point in the history
  • Loading branch information
gmalysa committed Nov 12, 2017
1 parent 4a8cddb commit 68bf369
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
annotated/
node_modules/
lib-cov/
17 changes: 16 additions & 1 deletion lib/db-filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,14 @@ function db(table, columns, special) {
// Static/constant definition
_.extend(db, {
// Database field constants
int_t : 1, //!< Indicates that a field is one of the INT types (TINY, SMALL, etc.)
int_t : 1, //!< Indicates that a field is one of the INT types (everything but BIGINT)
date_t : 2, //!< Indicates that a field is a DATE type (*not* DATETIME, TIMESTAMP, etc.)
datetime_t : 3, //!< Indicates that a field is a DATETIME or TIMESTAMP type
timestamp_t : 3, //!< Alias for DATETIME, as both TIMESTAMP and DATETIME are implemented the same way
varchar_t : 4, //!< Should be used in an array with the length of the field, and data will be truncated
text_t : 5, //!< Long text field. Falls back to mysql.escape(). Included so that we can add all fields to the declaration
char_t : 6, //!< Should be used in an array with the length of the field
bigint_t : 7, //!< A BIGINT field is handled like a string but doesn't get quotes, so we verify it's a string containing an int only

// Logging levels
l_debug : 3, //!< Provides debugging-quality output, including verbose query information and incorrect usage info (i.e. calling limit() on an InsertQuery)
Expand Down Expand Up @@ -400,6 +401,8 @@ _.extend(db.prototype, {
if (ht) {
if (ht == db.int_t)
return parseInt(value) || 0;
else if (ht == db.bigint_t)
return this.handle_bigint(value);
else if (ht == db.date_t)
return this.handle_date(value);
else if (ht == db.datetime_t || ht == db.timestamp_t)
Expand Down Expand Up @@ -439,6 +442,18 @@ _.extend(db.prototype, {
return this.handle_date(date) + ' ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds();

return this.handle_datetime(new Date(date));
},

/**
* This verifies that a string is only numeric and thus can be converted to a bigint value by
* mysql
* @param str The string to check for being a bigint
* @return String field value
*/
handle_bigint : function(str) {
if (str.match(/^[0-9]+$/))
return str;
return '0';
}

});
Expand Down
4 changes: 3 additions & 1 deletion lib/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ function get_field_type(type) {
var ftype = match[1];
var length = match[2];

if (ftype.match(/int/i))
if (ftype.match(/bigint/i))
return 'db.bigint_t';
else if (ftype.match(/int/i))
return 'db.int_t';
else if (type.match(/varchar/i))
return '[db.varchar_t, '+length+']';
Expand Down
11 changes: 11 additions & 0 deletions test/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var users = new db('users', {
password : [db.varchar_t, 32],
registered : db.datetime_t,
salt : [db.varchar_t, 8],
longid : db.bigint_t,
status : db.int_t
}, {
salt_pw : function(key, value, terms, options) {
Expand Down Expand Up @@ -112,4 +113,14 @@ exports['where'] = function(test) {
test.done();
}

exports['bigint'] = function(test) {
var sql = users.select({longid : '12345'}).buildQuery();
test.equals(sql, 'SELECT * FROM users WHERE `longid` = 12345');

var sql = users.select({longid : 'invalid'}).buildQuery();
test.equals(sql, 'SELECT * FROM users WHERE `longid` = 0');

test.done();
}

module.exports = exports;

0 comments on commit 68bf369

Please sign in to comment.