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

fix(typeCast): ensure the same behavior for field.string() with query and execute #2820

Merged
merged 2 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
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
18 changes: 18 additions & 0 deletions lib/parsers/binary_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,24 @@ function compile(fields, options, config) {
);
}

if (
[Types.DATETIME, Types.NEWDATE, Types.TIMESTAMP, Types.DATE].includes(
field.columnType,
)
) {
return packet.readDateTimeString(parseInt(field.decimals, 10));
}

if (field.columnType === Types.TINY) {
const unsigned = field.flags & FieldFlags.UNSIGNED;

return String(unsigned ? packet.readInt8() : packet.readSInt8());
}

if (field.columnType === Types.TIME) {
return packet.readTimeString();
}

return packet.readLengthCodedString(encoding);
},
buffer: function () {
Expand Down
84 changes: 84 additions & 0 deletions test/esm/integration/parsers/typecast-field-string.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { describe, assert } from 'poku';
import { createRequire } from 'node:module';

const require = createRequire(import.meta.url);
const {
createConnection,
describeOptions,
} = require('../../../common.test.cjs');

const conn = createConnection({
typeCast: (field) => field.string(),
}).promise();

describe('typeCast field.string', describeOptions);

const query = {};
const execute = {};

await conn.query(
'CREATE TEMPORARY TABLE `tmp_tiny` (`signed` TINYINT, `unsigned` TINYINT UNSIGNED)',
);
await conn.query('CREATE TEMPORARY TABLE `tmp_date` (`timestamp` TIMESTAMP)');

await conn.query('INSERT INTO `tmp_tiny` (`signed`, `unsigned`) VALUES (0, 1)');
await conn.query(
'INSERT INTO `tmp_date` (`timestamp`) VALUES (CURRENT_TIMESTAMP())',
);

{
const [date] = await conn.query(
'SELECT STR_TO_DATE("2022-06-28", "%Y-%m-%d") AS `date`',
);
const [time] = await conn.query(
'SELECT STR_TO_DATE("12:34:56", "%H:%i:%s") AS `time`',
);
const [datetime] = await conn.query(
'SELECT STR_TO_DATE("2022-06-28 12:34:56", "%Y-%m-%d %H:%i:%s") AS `datetime`',
);
const [timestamp] = await conn.query('SELECT `timestamp` FROM `tmp_date`');
const [tiny] = await conn.query(
'SELECT `signed`, `unsigned` FROM `tmp_tiny`',
);

query.date = date[0].date;
query.time = time[0].time;
query.timestamp = timestamp[0].timestamp;
query.datetime = datetime[0].datetime;
query.tiny = tiny[0];
}

{
const [date] = await conn.execute(
'SELECT STR_TO_DATE("2022-06-28", "%Y-%m-%d") AS `date`',
);
const [time] = await conn.execute(
'SELECT STR_TO_DATE("12:34:56", "%H:%i:%s") AS `time`',
);
const [datetime] = await conn.execute(
'SELECT STR_TO_DATE("2022-06-28 12:34:56", "%Y-%m-%d %H:%i:%s") AS `datetime`',
);
const [timestamp] = await conn.execute('SELECT `timestamp` FROM `tmp_date`');
const [tiny] = await conn.execute(
'SELECT `signed`, `unsigned` FROM `tmp_tiny`',
);

execute.date = date[0].date;
execute.time = time[0].time;
execute.timestamp = timestamp[0].timestamp;
execute.datetime = datetime[0].datetime;
execute.tiny = tiny[0];
}

await conn.end();

assert.deepStrictEqual(query.date, execute.date, 'DATE');
assert.deepStrictEqual(query.time, execute.time, 'TIME');
assert.deepStrictEqual(query.datetime, execute.datetime, 'DATETIME');
assert.deepStrictEqual(query.timestamp, execute.timestamp, 'TIMESTAMP');
assert.deepStrictEqual(query.tiny.signed, execute.tiny.signed, 'TINY (signed)');
assert.deepStrictEqual(
query.tiny.unsigned,
execute.tiny.unsigned,
'TINY (unsigned)',
);
Loading