diff --git a/lib/packets/packet.js b/lib/packets/packet.js index 42241ae950..2422a0d959 100644 --- a/lib/packets/packet.js +++ b/lib/packets/packet.js @@ -9,7 +9,7 @@ const ErrorCodeToName = require('../constants/errors.js'); const NativeBuffer = require('buffer').Buffer; const Long = require('long'); const StringParser = require('../parsers/string.js'); - +const Types = require('../constants/types.js'); const INVALID_DATE = new Date(NaN); // this is nearly duplicate of previous function so generated code is not slower @@ -292,14 +292,14 @@ class Packet { } return new Date(y, m - 1, d, H, M, S, ms); } - let str = this.readDateTimeString(6, 'T'); + let str = this.readDateTimeString(6, 'T', null); if (str.length === 10) { str += 'T00:00:00'; } return new Date(str + timezone); } - readDateTimeString(decimals, timeSep) { + readDateTimeString(decimals, timeSep, columnType) { const length = this.readInt8(); let y = 0; let m = 0; @@ -324,6 +324,8 @@ class Packet { leftPad(2, M), leftPad(2, S) ].join(':')}`; + } else if (columnType === Types.DATETIME) { + str += ' 00:00:00'; } if (length > 10) { ms = this.readInt32(); @@ -425,7 +427,7 @@ class Packet { return StringParser.decode( this.buffer, encoding, - this.offset - len, + this.offset - len, this.offset ); } @@ -915,7 +917,7 @@ class Packet { } static MockBuffer() { - const noop = function () {}; + const noop = function () { }; const res = Buffer.alloc(0); for (const op in NativeBuffer.prototype) { if (typeof res[op] === 'function') { diff --git a/lib/parsers/binary_parser.js b/lib/parsers/binary_parser.js index 4eb0d3ae69..a735873949 100644 --- a/lib/parsers/binary_parser.js +++ b/lib/parsers/binary_parser.js @@ -42,7 +42,7 @@ function readCodeFor(field, config, options, fieldNum) { case Types.TIMESTAMP: case Types.NEWDATE: if (helpers.typeMatch(field.columnType, dateStrings, Types)) { - return `packet.readDateTimeString(${parseInt(field.decimals, 10)});`; + return `packet.readDateTimeString(${parseInt(field.decimals, 10)}, ${null}, ${field.columnType});`; } return `packet.readDateTime(${helpers.srcEscape(timezone)});`; case Types.TIME: @@ -56,7 +56,7 @@ function readCodeFor(field, config, options, fieldNum) { case Types.GEOMETRY: return 'packet.parseGeometryValue();'; case Types.VECTOR: - return 'packet.parseVector()'; + return 'packet.parseVector()'; case Types.JSON: // Since for JSON columns mysql always returns charset 63 (BINARY), // we have to handle it according to JSON specs and use "utf8", diff --git a/test/integration/connection/test-datetime.test.cjs b/test/integration/connection/test-datetime.test.cjs index 3cd8e29479..1cb20ca585 100644 --- a/test/integration/connection/test-datetime.test.cjs +++ b/test/integration/connection/test-datetime.test.cjs @@ -32,6 +32,7 @@ const date2 = '2010-12-10 14:12:09.019473'; const date3 = null; const date4 = '2010-12-10 14:12:09.123456'; const date5 = '2010-12-10 14:12:09.019'; +const date6 = '2024-11-10 00:00:00'; function adjustTZ(d, offset) { if (offset === undefined) { @@ -72,28 +73,20 @@ connection.query('INSERT INTO t set d1=?, d2=?, d3=?', [ ]); connection1.query( - 'CREATE TEMPORARY TABLE t (d1 DATE, d2 TIMESTAMP, d3 DATETIME, d4 DATETIME, d5 DATETIME(6), d6 DATETIME(3))', + 'CREATE TEMPORARY TABLE t (d1 DATE, d2 TIMESTAMP, d3 DATETIME, d4 DATETIME, d5 DATETIME(6), d6 DATETIME(3), d7 DATETIME)', +); +connection1.query( + 'INSERT INTO t set d1=?, d2=?, d3=?, d4=?, d5=?, d6=?, d7=?', + [date, date1, date2, date3, date4, date5, date6], ); -connection1.query('INSERT INTO t set d1=?, d2=?, d3=?, d4=?, d5=?, d6=?', [ - date, - date1, - date2, - date3, - date4, - date5, -]); connection2.query( - 'CREATE TEMPORARY TABLE t (d1 DATE, d2 TIMESTAMP, d3 DATETIME, d4 DATETIME, d5 DATETIME(6), d6 DATETIME(3))', + 'CREATE TEMPORARY TABLE t (d1 DATE, d2 TIMESTAMP, d3 DATETIME, d4 DATETIME, d5 DATETIME(6), d6 DATETIME(3), d7 DATETIME)', +); +connection2.query( + 'INSERT INTO t set d1=?, d2=?, d3=?, d4=?, d5=?, d6=?, d7=?', + [date, date1, date2, date3, date4, date5, date6], ); -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))', @@ -123,6 +116,7 @@ const dateAsStringExpected = [ d4: date3, d5: date4, d6: date5, + d7: date6, }, ];