// mdb-reader's complex-column reader (lib/browser/data/complex/index.js), // rewritten for this site's build (-jm2). Upstream treats every complex // column as an attachment: a multi-value field (a lookup that allows several // values) has no FileData column, so reading it threw and the whole table // failed. It also read the system tables and the whole flat table again for // every row, which made a table of attachments take time growing with the // square of its rows. // // Here each complex column is resolved once per database to its flat table // and its kind: // attachment [{ name, type, data, url?, timestamp?, flags? }] as upstream // multiValue [value, ...] (or one object per value when the flat table // holds several value columns) // versionHistory [{ value, date }, ...] (the hidden column behind an // append-only long text field) // unknown [value, ...] as multiValue, when a value column is of a type // mdb-reader does not know (raw bytes) // and, while one Table.getData() runs, each flat table is read once and its // rows grouped by the foreign key the main row holds. import { getMSysObjectsTable } from "../../systemTables.js"; import { Table } from "../../Table.js"; import { ColumnTypes } from "../../types.js"; import { maskTableId } from "../../util.js"; import { decodeAttachmentFileData } from "./attachment.js"; const ATTACHMENT_COLUMNS = new Set(["FileName", "FileType", "FileData", "FileURL", "FileTimeStamp", "FileFlags"]); /** Per database: the system rows needed, and each complex column's resolved info (or the error it gave). */ const perDatabase = new WeakMap(); /** Per database, while a getData() runs: depth, and each flat table's rows by foreign key. */ const scopes = new WeakMap(); function state(database) { let s = perDatabase.get(database); if (!s) { s = { objects: null, complexColumns: null, info: new Map() }; perDatabase.set(database, s); } return s; } function systemRows(database, s) { if (!s.objects) { s.objects = getMSysObjectsTable(database).getData({ columns: ["Id", "Name"] }); } if (!s.complexColumns) { const row = s.objects.find((r) => r.Name === "MSysComplexColumns"); if (!row) { throw new Error("MSysComplexColumns table not found in MSysObjects table"); } s.complexColumns = new Table("MSysComplexColumns", database, maskTableId(row.Id)).getData(); } } function resolve(database, column) { const s = state(database); systemRows(database, s); const conceptual = maskTableId(column.complex.tableDefinitionPage); const typeId = column.complex.typeId; const rows = s.complexColumns.filter((r) => r.FlatTableID && r.ConceptualTableID === conceptual); const name = column.name.toLowerCase(); const row = rows.find((r) => r.ComplexID === typeId || r.ComplexID === (typeId & 0xffff)) ?? rows.find((r) => typeof r.ColumnName === "string" && r.ColumnName.toLowerCase() === name) ?? rows.find((r) => r.ComplexTypeObjectID === typeId); if (!row) { throw new Error(`Flat table not found for complex column ${column.name}`); } const flatPage = maskTableId(row.FlatTableID); const flatRow = s.objects.find((r) => maskTableId(r.Id) === flatPage); if (!flatRow) { throw new Error(`Flat table not found for complex column ${column.name}`); } const table = new Table(flatRow.Name, database, flatPage); const columns = table.getColumns(); // As in Jackcess: never "Value", which is a multi-value field's own value column (a Long in a lookup on IDs). const foreignKey = columns.find((c) => c.type === ColumnTypes.Long && !c.autoLong && c.name !== "Value" && !ATTACHMENT_COLUMNS.has(c.name)); if (!foreignKey) { throw new Error("Foreign key column not found"); } const values = columns.filter((c) => c !== foreignKey && !c.autoLong); let kind = "multiValue"; if (columns.some((c) => c.name === "FileData")) { kind = "attachment"; } else if (/^VersionHistory_/i.test(column.name) && values.length === 2 && values.some((c) => c.type === ColumnTypes.Memo) && values.some((c) => c.type === ColumnTypes.DateTime)) { kind = "versionHistory"; } else if (values.some((c) => c.type === "unknown")) { // Values of a type this does not know: read as raw bytes, but not called a multi-value field. kind = "unknown"; } return { kind, table, flatPage, foreignKey: foreignKey.name, values }; } /** The resolved info of a complex column, worked out once per database. Throws if it cannot be. */ export function complexInfo(database, column) { if (column.complex?.typeId === undefined || column.complex?.tableDefinitionPage === undefined) { throw new Error("Complex column is not valid"); } const s = state(database); const key = `${column.complex.tableDefinitionPage}:${column.complex.typeId}:${column.name}`; if (!s.info.has(key)) { try { s.info.set(key, resolve(database, column)); } catch (error) { s.info.set(key, error instanceof Error ? error : new Error(String(error))); } } const info = s.info.get(key); if (info instanceof Error) { throw info; } return info; } /** * What a column definition says of a complex column: its kind and the name of * its flat table (whose properties hold a multi-value lookup's settings), or * kind "unknown" when the flat table cannot be found. */ export function complexSummary(database, column) { try { const info = complexInfo(database, column); return { kind: info.kind, flatTable: info.table.name }; } catch { return { kind: "unknown" }; } } export function beginComplexScope(database) { const scope = scopes.get(database); if (scope) { scope.depth++; } else { scopes.set(database, { depth: 1, groups: new Map() }); } } export function endComplexScope(database) { const scope = scopes.get(database); if (scope && --scope.depth <= 0) { scopes.delete(database); } } function groupsOf(database, info) { const scope = scopes.get(database); let groups = scope?.groups.get(info.flatPage); if (!groups) { groups = new Map(); for (const row of info.table.getData()) { const fk = row[info.foreignKey]; let list = groups.get(fk); if (!list) { list = []; groups.set(fk, list); } list.push(row); } scope?.groups.set(info.flatPage, groups); } return groups; } function attachment(row) { let data; try { data = decodeAttachmentFileData(row.FileData); } catch { data = Buffer.alloc(0); } const out = { name: row.FileName, type: row.FileType, data }; if (row.FileURL) { out.url = row.FileURL; } if (row.FileTimeStamp) { out.timestamp = row.FileTimeStamp; } if (row.FileFlags) { out.flags = row.FileFlags; } return out; } export function readComplex(buffer, column, database) { try { const info = complexInfo(database, column); const foreignKey = buffer.readInt32LE(0); const rows = foreignKey > 0 ? (groupsOf(database, info).get(foreignKey) ?? []) : []; switch (info.kind) { case "attachment": return rows.map(attachment); case "versionHistory": { const memo = info.values.find((c) => c.type === ColumnTypes.Memo).name; const date = info.values.find((c) => c.type === ColumnTypes.DateTime).name; return rows.map((r) => ({ value: r[memo], date: r[date] })); } default: if (info.values.length === 1) { const name = info.values[0].name; return rows.map((r) => r[name]); } return rows.map((r) => Object.fromEntries(info.values.map((c) => [c.name, r[c.name]]))); } } catch (error) { throw new Error("Failed to read complex column", { cause: error }); } }