Skip to content

Commit 27e38ea

Browse files
authoredJul 1, 2024··
fix(typeCast): ensure the same behavior for field.string() with query and execute (#2820)
* fix(typeCast): ensure the same behavior for `field.string()` with `query` and `execute` * ci: add DATE check
1 parent a14f354 commit 27e38ea

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed
 

‎lib/parsers/binary_parser.js

+18
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,24 @@ function compile(fields, options, config) {
102102
);
103103
}
104104

105+
if (
106+
[Types.DATETIME, Types.NEWDATE, Types.TIMESTAMP, Types.DATE].includes(
107+
field.columnType,
108+
)
109+
) {
110+
return packet.readDateTimeString(parseInt(field.decimals, 10));
111+
}
112+
113+
if (field.columnType === Types.TINY) {
114+
const unsigned = field.flags & FieldFlags.UNSIGNED;
115+
116+
return String(unsigned ? packet.readInt8() : packet.readSInt8());
117+
}
118+
119+
if (field.columnType === Types.TIME) {
120+
return packet.readTimeString();
121+
}
122+
105123
return packet.readLengthCodedString(encoding);
106124
},
107125
buffer: function () {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { describe, assert } from 'poku';
2+
import { createRequire } from 'node:module';
3+
4+
const require = createRequire(import.meta.url);
5+
const {
6+
createConnection,
7+
describeOptions,
8+
} = require('../../../common.test.cjs');
9+
10+
const conn = createConnection({
11+
typeCast: (field) => field.string(),
12+
}).promise();
13+
14+
describe('typeCast field.string', describeOptions);
15+
16+
const query = {};
17+
const execute = {};
18+
19+
await conn.query(
20+
'CREATE TEMPORARY TABLE `tmp_tiny` (`signed` TINYINT, `unsigned` TINYINT UNSIGNED)',
21+
);
22+
await conn.query('CREATE TEMPORARY TABLE `tmp_date` (`timestamp` TIMESTAMP)');
23+
24+
await conn.query('INSERT INTO `tmp_tiny` (`signed`, `unsigned`) VALUES (0, 1)');
25+
await conn.query(
26+
'INSERT INTO `tmp_date` (`timestamp`) VALUES (CURRENT_TIMESTAMP())',
27+
);
28+
29+
{
30+
const [date] = await conn.query(
31+
'SELECT STR_TO_DATE("2022-06-28", "%Y-%m-%d") AS `date`',
32+
);
33+
const [time] = await conn.query(
34+
'SELECT STR_TO_DATE("12:34:56", "%H:%i:%s") AS `time`',
35+
);
36+
const [datetime] = await conn.query(
37+
'SELECT STR_TO_DATE("2022-06-28 12:34:56", "%Y-%m-%d %H:%i:%s") AS `datetime`',
38+
);
39+
const [timestamp] = await conn.query('SELECT `timestamp` FROM `tmp_date`');
40+
const [tiny] = await conn.query(
41+
'SELECT `signed`, `unsigned` FROM `tmp_tiny`',
42+
);
43+
44+
query.date = date[0].date;
45+
query.time = time[0].time;
46+
query.timestamp = timestamp[0].timestamp;
47+
query.datetime = datetime[0].datetime;
48+
query.tiny = tiny[0];
49+
}
50+
51+
{
52+
const [date] = await conn.execute(
53+
'SELECT STR_TO_DATE("2022-06-28", "%Y-%m-%d") AS `date`',
54+
);
55+
const [time] = await conn.execute(
56+
'SELECT STR_TO_DATE("12:34:56", "%H:%i:%s") AS `time`',
57+
);
58+
const [datetime] = await conn.execute(
59+
'SELECT STR_TO_DATE("2022-06-28 12:34:56", "%Y-%m-%d %H:%i:%s") AS `datetime`',
60+
);
61+
const [timestamp] = await conn.execute('SELECT `timestamp` FROM `tmp_date`');
62+
const [tiny] = await conn.execute(
63+
'SELECT `signed`, `unsigned` FROM `tmp_tiny`',
64+
);
65+
66+
execute.date = date[0].date;
67+
execute.time = time[0].time;
68+
execute.timestamp = timestamp[0].timestamp;
69+
execute.datetime = datetime[0].datetime;
70+
execute.tiny = tiny[0];
71+
}
72+
73+
await conn.end();
74+
75+
assert.deepStrictEqual(query.date, execute.date, 'DATE');
76+
assert.deepStrictEqual(query.time, execute.time, 'TIME');
77+
assert.deepStrictEqual(query.datetime, execute.datetime, 'DATETIME');
78+
assert.deepStrictEqual(query.timestamp, execute.timestamp, 'TIMESTAMP');
79+
assert.deepStrictEqual(query.tiny.signed, execute.tiny.signed, 'TINY (signed)');
80+
assert.deepStrictEqual(
81+
query.tiny.unsigned,
82+
execute.tiny.unsigned,
83+
'TINY (unsigned)',
84+
);

0 commit comments

Comments
 (0)
Please sign in to comment.