From 09683e13fd2581a42936c5f441c678cf53a54b70 Mon Sep 17 00:00:00 2001 From: Rebecca Turner Date: Tue, 12 Nov 2013 11:49:29 -0500 Subject: [PATCH] Fix #635: Make inflation of dates optional --- Readme.md | 2 ++ lib/ConnectionConfig.js | 1 + lib/protocol/packets/RowDataPacket.js | 9 ++++++--- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/Readme.md b/Readme.md index 376d716ff..b15d1d010 100644 --- a/Readme.md +++ b/Readme.md @@ -155,6 +155,8 @@ issue [#501](https://github.com/felixge/node-mysql/issues/501). (Default: `'fals objects only when they cannot be accurately represented with [JavaScript Number objects] (http://ecma262-5.com/ELS5_HTML.htm#Section_8.5) (which happens when they exceed the [-2^53, +2^53] range), otherwise they will be returned as Number objects. This option is ignored if `supportBigNumbers` is disabled. +* `dateStrings`: Force date types (TIMESTAMP, DATETIME, DATE) to be returned as strings rather then + inflated into JavaScript Date objects. (Default: `false`) * `debug`: Prints protocol details to stdout. (Default: `false`) * `multipleStatements`: Allow multiple mysql statements per query. Be careful with this, it exposes you to SQL injection attacks. (Default: `false`) diff --git a/lib/ConnectionConfig.js b/lib/ConnectionConfig.js index fdacfea8a..c76ffa102 100644 --- a/lib/ConnectionConfig.js +++ b/lib/ConnectionConfig.js @@ -17,6 +17,7 @@ function ConnectionConfig(options) { this.insecureAuth = options.insecureAuth || false; this.supportBigNumbers = options.supportBigNumbers || false; this.bigNumberStrings = options.bigNumberStrings || false; + this.dateStrings = options.dateStrings || false; this.debug = options.debug; this.stringifyObjects = options.stringifyObjects || false; this.timezone = options.timezone || 'local'; diff --git a/lib/protocol/packets/RowDataPacket.js b/lib/protocol/packets/RowDataPacket.js index 747f0394b..09f3fb2b9 100644 --- a/lib/protocol/packets/RowDataPacket.js +++ b/lib/protocol/packets/RowDataPacket.js @@ -10,7 +10,7 @@ function RowDataPacket() { RowDataPacket.prototype.parse = function(parser, fieldPackets, typeCast, nestTables, connection) { var self = this; var next = function () { - return self._typeCast(fieldPacket, parser, connection.config.timezone, connection.config.supportBigNumbers, connection.config.bigNumberStrings); + return self._typeCast(fieldPacket, parser, connection.config.timezone, connection.config.supportBigNumbers, connection.config.bigNumberStrings, connection.config.dateStrings); }; for (var i = 0; i < fieldPackets.length; i++) { @@ -21,7 +21,7 @@ RowDataPacket.prototype.parse = function(parser, fieldPackets, typeCast, nestTab value = typeCast.apply(connection, [ new Field({ packet: fieldPacket, parser: parser }), next ]); } else { value = (typeCast) - ? this._typeCast(fieldPacket, parser, connection.config.timezone, connection.config.supportBigNumbers, connection.config.bigNumberStrings) + ? this._typeCast(fieldPacket, parser, connection.config.timezone, connection.config.supportBigNumbers, connection.config.bigNumberStrings, connection.config.dateStrings) : ( (fieldPacket.charsetNr === Charsets.BINARY) ? parser.parseLengthCodedBuffer() : parser.parseLengthCodedString() ); @@ -38,7 +38,7 @@ RowDataPacket.prototype.parse = function(parser, fieldPackets, typeCast, nestTab } }; -RowDataPacket.prototype._typeCast = function(field, parser, timeZone, supportBigNumbers, bigNumberStrings) { +RowDataPacket.prototype._typeCast = function(field, parser, timeZone, supportBigNumbers, bigNumberStrings, dateStrings) { var numberString; switch (field.type) { @@ -47,6 +47,9 @@ RowDataPacket.prototype._typeCast = function(field, parser, timeZone, supportBig case Types.DATETIME: case Types.NEWDATE: var dateString = parser.parseLengthCodedString(); + if (dateStrings) { + return dateString; + } var dt; if (dateString === null) {