Open clovis-maniguet opened 2 years ago
Hi, thanks for reporting this!
It's a known issue, unrelated to count(*)
, but with how values are returned from MySQL/MariaDB. See the example, and the discussion in PR #9.
For some reason I'm not clear on yet, values from MySQL are always returned as an array of ASCII integers. This is why the example uses String.fromCharCode(...row.key)
to convert it into a string.
This is the case for all column types, AFAICS. :disappointed: So if you need to use the value as a number, you'd have to do parseInt(String.fromCharCode(...row.key))
.
This only happens on MySQL and not for the other RDBMSs. I tried experimenting with different character sets and collation (e.g. CREATE TABLE (...) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
), and adding charset=utf8mb4
to the connection string, but it didn't have an effect.
So to summarize, you're actually getting the correct value: [57,50,54]
are the ASCII codes for 926
, and you need to convert them in your script. E.g.:
export default function () {
const results = sql.query(db, 'SELECT count(*) c FROM filled_table;');
for (const row of results) {
console.log(`${String.fromCharCode(...row.c)}`);
}
}
I'll keep this issue open as it should be fixed specifically for MySQL. I'm not sure if something can be configured on the MySQL side to change this behavior, or if we'll have to manually inspect the column types and use reflection to return them correctly, but it needs more investigation.
Hey @clovis-maniguet, I'd prefer to keep this issue open, if you don't mind.
Even though there's a workaround for it, it should eventually be fixed. We just haven't had the bandwidth to look into it in more depth, but any help is greatly appreciated.
Versions
What
On table of 926 rows named
filled_table
;SELECT *
SELECT count
SELECT TABLE_ROWS
Observation
Logging the response of both
SELECT count(*)
andSELECT TABLE_ROWS
show us the same responses.