Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changes date parsing to return String if not a valid JS Date
- Loading branch information
Showing
2 changed files
with
50 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
} | ||
|
@@ -57,7 +59,12 @@ RowDataPacket.prototype._typeCast = function(field, parser, timeZone, supportBig | |
} | ||
} | ||
|
||
return new Date(dateString); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
dresende
Author
Collaborator
|
||
dt = new Date(dateString); | ||
if (isNaN(dt.getTime())) { | ||
return dateString; | ||
} | ||
|
||
return dt; | ||
case Types.TINY: | ||
case Types.SHORT: | ||
case Types.LONG: | ||
|
42 changes: 42 additions & 0 deletions
42
test/integration/connection/test-query-dates-as-strings.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
}); |
This is breaking lots of code for us, we now have to check if each date column actually returns a date object or not :(