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

feat: allow using dateStrings with specific types #1200

Merged
merged 1 commit into from
Sep 13, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat: allow using dateStrings with specific types
This aligns the `dateStrings` with mysqljs, allowing to set `dateStrings: ['DATE']` to return
the DATE type as string, but keeping other datetime types processed to Date.

Related: #99
B4nan committed Sep 12, 2020
commit 26126b1646e3b71478cd15b2e3562fabf94f5249
13 changes: 13 additions & 0 deletions lib/helpers.js
Original file line number Diff line number Diff line change
@@ -48,3 +48,16 @@ function printDebugWithCode(msg, code) {
}

exports.printDebugWithCode = printDebugWithCode;

/**
* checks whether the `type` is in the `list`
*/
function typeMatch(type, list, Types) {
if (Array.isArray(list)) {
return list.some(t => type === Types[t]);
}

return !!list;
}

exports.typeMatch = typeMatch;
3 changes: 2 additions & 1 deletion lib/parsers/binary_parser.js
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@ function readCodeFor(field, config, options, fieldNum) {
options.supportBigNumbers || config.supportBigNumbers;
const bigNumberStrings = options.bigNumberStrings || config.bigNumberStrings;
const timezone = options.timezone || config.timezone;
const dateStrings = options.dateStrings || config.dateStrings;
const unsigned = field.flags & FieldFlags.UNSIGNED;
switch (field.columnType) {
case Types.TINY:
@@ -37,7 +38,7 @@ function readCodeFor(field, config, options, fieldNum) {
case Types.DATETIME:
case Types.TIMESTAMP:
case Types.NEWDATE:
if (config.dateStrings) {
if (helpers.typeMatch(field.columnType, dateStrings, Types)) {
return `packet.readDateTimeString(${field.decimals});`;
}
return `packet.readDateTime('${timezone}');`;
5 changes: 3 additions & 2 deletions lib/parsers/text_parser.js
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@ function readCodeFor(type, charset, encodingExpr, config, options) {
options.supportBigNumbers || config.supportBigNumbers;
const bigNumberStrings = options.bigNumberStrings || config.bigNumberStrings;
const timezone = options.timezone || config.timezone;
const dateStrings = options.dateStrings || config.dateStrings;

switch (type) {
case Types.TINY:
@@ -41,13 +42,13 @@ function readCodeFor(type, charset, encodingExpr, config, options) {
}
return 'packet.readLengthCodedString("ascii")';
case Types.DATE:
if (config.dateStrings) {
if (helpers.typeMatch(type, dateStrings, Types)) {
return 'packet.readLengthCodedString("ascii")';
}
return `packet.parseDate('${timezone}')`;
case Types.DATETIME:
case Types.TIMESTAMP:
if (config.dateStrings) {
if (helpers.typeMatch(type, dateStrings, Types)) {
return 'packet.readLengthCodedString("ascii")';
}
return `packet.parseDateTime('${timezone}')`;
33 changes: 32 additions & 1 deletion test/integration/connection/test-datetime.js
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@
const common = require('../../common');
const connection = common.createConnection();
const connection1 = common.createConnection({ dateStrings: true });
const connection2 = common.createConnection({ dateStrings: ['DATE'] });
const connectionZ = common.createConnection({ timezone: 'Z' });
const connection0930 = common.createConnection({ timezone: '+09:30' });
const assert = require('assert');
@@ -18,7 +19,8 @@ let rows,
rows4,
rows5,
rows6,
rows7;
rows7,
rows8;

const date = new Date('1990-01-01 08:15:11 UTC');
const datetime = new Date('2010-12-10 14:12:09.019473');
@@ -79,6 +81,18 @@ connection1.query('INSERT INTO t set d1=?, d2=?, d3=?, d4=?, d5=?, d6=?', [
date5
]);

connection2.query(
'CREATE TEMPORARY TABLE t (d1 DATE, d2 TIMESTAMP, d3 DATETIME, d4 DATETIME, d5 DATETIME(6), d6 DATETIME(3))'
);
connection2.query('INSERT INTO t set d1=?, d2=?, d3=?, d4=?, d5=?, d6=?', [
date,
date1,
date2,
date3,
date4,
date5
]);

connectionZ.query(
'CREATE TEMPORARY TABLE t (d1 DATE, d2 DATETIME(3), d3 DATETIME(6))'
);
@@ -212,6 +226,14 @@ connection1.execute(
}
);

connection2.execute('select * from t', (err, _rows) => {
if (err) {
throw err;
}
rows8 = _rows;
connection2.end();
});

connection0930.execute(
'select *, cast(d1 as char) as d4, cast(d2 as char) as d5, cast(d3 as char) as d6 from t',
(err, _rows) => {
@@ -291,6 +313,15 @@ process.on('exit', () => {
assert.deepEqual(rows5, dateAsStringExpected);
assert.equal(rows6.length, 1);

// dateStrings as array
assert.equal(rows8[0].d1, '1990-01-01');
assert.equal(rows8[0].d1.constructor, String);
assert.equal(rows8[0].d2.constructor, Date);
assert.equal(rows8[0].d3.constructor, Date);
assert.equal(rows8[0].d4, null);
assert.equal(rows8[0].d5.constructor, Date);
assert.equal(rows8[0].d6.constructor, Date);

// +09:30
const tzOffset = -570;
assert.equal(rows7[0].d1.getTime(), toMidnight(date, tzOffset).getTime());