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

datetime fields returned as date when time is 00:00:00 #1040

Closed
lirancr opened this issue Oct 21, 2019 · 9 comments · Fixed by #3204
Closed

datetime fields returned as date when time is 00:00:00 #1040

lirancr opened this issue Oct 21, 2019 · 9 comments · Fixed by #3204

Comments

@lirancr
Copy link

lirancr commented Oct 21, 2019

Setting a DATETIME field with the value 2019-10-01 00:00:00
Then retrieve the data using the execute command:
const [result] = connection.execute('SELECT * FROM my_table', [])

result will equal to '2019-10-01' instead of '2019-10-01 00:00:00' although it's a DATETIME field and not merely a DATE

note that when using query the behavior is as expected

@sidorares
Copy link
Owner

could you post simple self contained example, including schema creation and initial data insert?

@lirancr
Copy link
Author

lirancr commented Oct 21, 2019

Sure here is a standalone snippet

    //running with node 10.16.0 using mysql2 ^1.7.0

    const mysql = require('mysql2/promise');

    async function main() {
    let db = await mysql.createConnection({
        host: 'localhost',
        port: '3306',
        user: 'root',
        password: 'root'
    });
    await db.query("SET time_zone='+00:00'");
    await db.query('DROP DATABASE IF EXISTS test_db');
    await db.query('CREATE DATABASE IF NOT EXISTS test_db');
    await db.close();

    db = await mysql.createConnection({
        host: 'localhost',
        port: '3306',
        user: 'root',
        password: 'root',
        database: 'test_db',
        dateStrings: true,
        timezone: '+00:00'
    });

    await db.query('CREATE TABLE IF NOT EXISTS mytbl (id INT AUTO_INCREMENT, created DATETIME NOT NULL, PRIMARY KEY (id)) ENGINE=INNODB');
    await db.execute('INSERT INTO mytbl (created) VALUES (?)',['2019-10-01 00:00:00']);
    let [result] = await db.execute('SELECT * FROM mytbl', []);
    console.log('execute result',result[0]);
    [result] = await db.query('SELECT * FROM mytbl', []);
    console.log('query result',result[0]);
    
    }

    main();

scratches.zip

@sidorares
Copy link
Owner

sidorares commented Oct 22, 2019

Data in db has DATETIME type, but actual value only contain 4 bytes ( 2 for year, 1 for month and 1 for day ) - this is where it gets deserialized to a string ( note that there is no intermediate conversion to JS Date when you pass dateStrings: true option:

y = this.readInt16();
m = this.readInt8();
d = this.readInt8();
str = [leftPad(4, y), leftPad(2, m), leftPad(2, d)].join('-');

I agree, result of (await db.execute|query('SELECT * FROM mytbl')[0][0]).toString() should be all the same for dateStrings: true and dateStrings: false

I guess the fix should be to append 00:00:00 when it's just 4 bytes of datetime returned

Do you want to volunteer to fix this @lirancr ?

@lirancr
Copy link
Author

lirancr commented Oct 22, 2019

Of course. Any reason why query behaves differently ?

@sidorares
Copy link
Owner

Any reason why query behaves differently ?

execute and query serialise responses very differently, for query results come as text and for execute - binary encoded

In the above example when dateStrings is set to true for query we return just whatever was in query response and with execute we construct date as string from binary data we have ( 4 bytes ). When dateStrings is not set in both execute and query we construct js Date object

lirancr added a commit to lirancr/node-mysql2 that referenced this issue Oct 23, 2019
@lirancr
Copy link
Author

lirancr commented Oct 23, 2019

Done, see associated PR

@sidorares
Copy link
Owner

Travis CI tests fail for some reason, I'll have a look tomorrow

@lirancr
Copy link
Author

lirancr commented Oct 28, 2019

any update on this ?
let me know if there's something i do to help out

sidorares pushed a commit that referenced this issue Apr 19, 2020
@deko2369
Copy link

hi, when will #1147 be merged?

w666 added a commit to w666/node-mysql2 that referenced this issue Nov 12, 2024
w666 added a commit to w666/node-mysql2 that referenced this issue Nov 12, 2024
w666 added a commit to w666/node-mysql2 that referenced this issue Nov 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants