Skip to content

Commit

Permalink
Changes date parsing to return String if not a valid JS Date
Browse files Browse the repository at this point in the history
  • Loading branch information
dresende committed Mar 28, 2013
1 parent 2b75bff commit 245fb1a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/protocol/packets/RowDataPacket.js
Expand Up @@ -45,6 +45,8 @@ RowDataPacket.prototype._typeCast = function(field, parser, timeZone, supportBig
case Types.DATETIME:
case Types.NEWDATE:
var dateString = parser.parseLengthCodedString();
var dt;

if (dateString === null) {
return null;
}
Expand All @@ -57,7 +59,12 @@ RowDataPacket.prototype._typeCast = function(field, parser, timeZone, supportBig
}
}

return new Date(dateString);

This comment has been minimized.

Copy link
@chriso

chriso May 2, 2013

This is breaking lots of code for us, we now have to check if each date column actually returns a date object or not :(

This comment has been minimized.

Copy link
@dresende

dresende May 3, 2013

Author Collaborator

This should only happen on invalid dates. How did you treat invalid dates before?

dt = new Date(dateString);
if (isNaN(dt.getTime())) {
return dateString;
}

return dt;
case Types.TINY:
case Types.SHORT:
case Types.LONG:
Expand Down
42 changes: 42 additions & 0 deletions test/integration/connection/test-query-dates-as-strings.js
@@ -0,0 +1,42 @@
var common = require('../../common');
var connection = common.createConnection();
var assert = require('assert');
var util = require('util');

common.useTestDb(connection);

var table = 'dates_as_strings';
var rows;

connection.query([
'CREATE TEMPORARY TABLE `' + table + '` (',
'`id` int(11) unsigned NOT NULL AUTO_INCREMENT,',
'`dt` DATE,',
'PRIMARY KEY (`id`)',
') ENGINE=InnoDB DEFAULT CHARSET=utf8'
].join('\n'));

connection.query('INSERT INTO ' + table + ' SET ?', {dt: '0000-00-00'});
connection.query('INSERT INTO ' + table + ' SET ?', {dt: '2013-00-00'});
connection.query('INSERT INTO ' + table + ' SET ?', {dt: '2013-03-00'});
connection.query('INSERT INTO ' + table + ' SET ?', {dt: '2013-03-01'});

connection.query('SELECT * FROM ' + table, function(err, _rows) {
if (err) throw err;

rows = _rows;
});

connection.end();

process.on('exit', function() {
assert.equal(rows.length, 4);
assert.equal(rows[0].id, 1);
assert.equal(rows[0].dt, '0000-00-00');
assert.equal(rows[1].id, 2);
assert.equal(rows[1].dt, '2013-00-00');
assert.equal(rows[2].id, 3);
assert.equal(rows[2].dt, '2013-03-00');
assert.equal(rows[3].id, 4);
assert(util.isDate(rows[3].dt));
});

0 comments on commit 245fb1a

Please sign in to comment.