Skip to content
This repository
Browse code

Merge pull request #430 from felixge/dates_as_strings

Changes date parsing to return String if not a valid JS Date
  • Loading branch information...
commit 8ba067f2eea45772785a9a80950b1a0af695d548 2 parents b920154 + 245fb1a
Diogo Resende dresende authored
9 lib/protocol/packets/RowDataPacket.js
@@ -45,6 +45,8 @@ RowDataPacket.prototype._typeCast = function(field, parser, timeZone, supportBig
45 45 case Types.DATETIME:
46 46 case Types.NEWDATE:
47 47 var dateString = parser.parseLengthCodedString();
  48 + var dt;
  49 +
48 50 if (dateString === null) {
49 51 return null;
50 52 }
@@ -57,7 +59,12 @@ RowDataPacket.prototype._typeCast = function(field, parser, timeZone, supportBig
57 59 }
58 60 }
59 61
60   - return new Date(dateString);
  62 + dt = new Date(dateString);
  63 + if (isNaN(dt.getTime())) {
  64 + return dateString;
  65 + }
  66 +
  67 + return dt;
61 68 case Types.TINY:
62 69 case Types.SHORT:
63 70 case Types.LONG:
42 test/integration/connection/test-query-dates-as-strings.js
... ... @@ -0,0 +1,42 @@
  1 +var common = require('../../common');
  2 +var connection = common.createConnection();
  3 +var assert = require('assert');
  4 +var util = require('util');
  5 +
  6 +common.useTestDb(connection);
  7 +
  8 +var table = 'dates_as_strings';
  9 +var rows;
  10 +
  11 +connection.query([
  12 + 'CREATE TEMPORARY TABLE `' + table + '` (',
  13 + '`id` int(11) unsigned NOT NULL AUTO_INCREMENT,',
  14 + '`dt` DATE,',
  15 + 'PRIMARY KEY (`id`)',
  16 + ') ENGINE=InnoDB DEFAULT CHARSET=utf8'
  17 +].join('\n'));
  18 +
  19 +connection.query('INSERT INTO ' + table + ' SET ?', {dt: '0000-00-00'});
  20 +connection.query('INSERT INTO ' + table + ' SET ?', {dt: '2013-00-00'});
  21 +connection.query('INSERT INTO ' + table + ' SET ?', {dt: '2013-03-00'});
  22 +connection.query('INSERT INTO ' + table + ' SET ?', {dt: '2013-03-01'});
  23 +
  24 +connection.query('SELECT * FROM ' + table, function(err, _rows) {
  25 + if (err) throw err;
  26 +
  27 + rows = _rows;
  28 +});
  29 +
  30 +connection.end();
  31 +
  32 +process.on('exit', function() {
  33 + assert.equal(rows.length, 4);
  34 + assert.equal(rows[0].id, 1);
  35 + assert.equal(rows[0].dt, '0000-00-00');
  36 + assert.equal(rows[1].id, 2);
  37 + assert.equal(rows[1].dt, '2013-00-00');
  38 + assert.equal(rows[2].id, 3);
  39 + assert.equal(rows[2].dt, '2013-03-00');
  40 + assert.equal(rows[3].id, 4);
  41 + assert(util.isDate(rows[3].dt));
  42 +});

0 comments on commit 8ba067f

Please sign in to comment.
Something went wrong with that request. Please try again.