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

Changes date parsing to return String if not a valid JS Date #430

Merged
merged 1 commit into from Mar 28, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
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));
});