Convert all values to string where possible, even if not derived from a table (#18210)

## Addresses #17946
results from querying chrome extension on macOS Chrome browser:
![Screenshot 2024-04-10 at 4 39
13 PM](https://github.com/fleetdm/fleet/assets/61553566/d67901f3-6e20-4190-8dbb-26e93361555b)

- [x] Changes file added for user-visible changes in `changes/`
- [x] Updated tests
- [x] Manual QA for all new/changed functionality
- [ ] TODO - Manual QA on actual Chromebook

---------

Co-authored-by: Jacob Shandling <jacob@fleetdm.com>
This commit is contained in:
Jacob Shandling
2024-04-10 18:38:41 -07:00
committed by GitHub
co-authored by Jacob Shandling
parent 8159c2b44f
commit 02563ffef9
3 changed files with 20 additions and 4 deletions
+2
View File
@@ -0,0 +1,2 @@
- Fix a bug where values not derived from "actual" fleetd-chrome tables were not being displayed
correctly (e.g., `SELECT 1` gets its value from the query itself, not a table)
+1 -1
View File
@@ -3,5 +3,5 @@ import VirtualDatabase from "./db";
test("Simple query", async () => {
const db = await VirtualDatabase.init();
const res = await db.query("select 1");
expect(res).toEqual({"data": [{ "1": 1 }], "warnings": null});
expect(res).toEqual({ data: [{ "1": "1" }], warnings: null });
});
+17 -3
View File
@@ -1,7 +1,6 @@
import SQLiteAsyncESMFactory from "wa-sqlite/dist/wa-sqlite-async.mjs";
import * as SQLite from "wa-sqlite";
// Alphabetical order
import Table from "./tables/Table";
import TableChromeExtensions from "./tables/chrome_extensions";
import TableDiskInfo from "./tables/disk_info";
@@ -34,7 +33,6 @@ export default class VirtualDatabase {
this.sqlite3 = sqlite3;
this.db = db;
// Alphabetical order
VirtualDatabase.register(
sqlite3,
db,
@@ -81,7 +79,23 @@ export default class VirtualDatabase {
await this.sqlite3.exec(this.db, sql, (row, columns) => {
// map each row to object
rows.push(
Object.fromEntries(columns.map((_, i) => [columns[i], row[i]]))
Object.fromEntries(
columns.map((_, i) => {
let [colName, val] = [columns[i], row[i]];
if (typeof val !== "string") {
if (val.toString) {
val = val.toString();
} else {
this.warnings.push({
column: colName,
error_message: `Value is not a string and doesn't have a toString method: ${val}`,
});
val = null;
}
}
return [colName, val];
})
)
);
});
return { data: rows, warnings: this.warnings };