Public: anyone can read this; the crew can change it.

A SQLite file's table view pages by 100 rows with the schema above it and refuses writes #13356

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/13300-a-sqlite-file-s-table-view-pages-by-100 → main 5 commits fork 4 threads · 4 unresolved

  • Merged. scout/13300-a-sqlite-file-s-table-view-pages-by-100 was merged into main as 290e978.

Review

Sign in to review this change.
8 files changed +304 −16
docs/features/sqlite-view.md +3 −1
@@ -3,7 +3,9 @@
33 A `.db`, `.sqlite` or `.sqlite3` file whose first bytes are the SQLite header opens on the blob page as its tables and views (`src/web/render/sqlite.ts`).
44 A file with one of those names but no header stays an ordinary binary file.
55 The list shows each name, its kind, its row count (tables only; a view is never counted) and its columns; a table that cannot be read says so in its row.
6 Click a name (`?table=<name>`) for its first 100 rows, columns capped at 100 and cells at 300 characters; NULL and a BLOB's size are written as words.
6 Click a name (`?table=<name>`) for its rows 100 at a time (`&page=N`, with Previous and Next links; a page past the end shows the last), columns capped at 100 and cells at 300 characters; NULL and a BLOB's size are written as words.

Both doors: nothing says how to read a table from the terminal; add a src/tui screen and document it, or explain why it is page-only.

7 The table's columns and declared types sit above its rows; Previous and Next are links drawn as buttons.
8 In the terminal (`src/tui/screens/workspace_sqlite.ts`) the file opens in the workspace as its first table: the column names head the rows with their declared types on the line under the table's name and span; `>` and `<` page 100 rows, `}` and `{` move to the next or previous table. It reads the same `tablePage` from the same read-only in-memory copy.
79 The database is opened read-only in memory from the committed bytes and closed again; no SQL comes from the reader, and a table name is used only if `sqlite_master` lists it.
810 A name the file does not hold goes back to the list with a note, and that page is not kept in the page cache.
911 Up to 5 MB is drawn; a larger file shows Download. A truncated or damaged file shows the source with a one-line reason. Raw and Download work as for any file.
src/tui/screens/workspace.ts +26 −2
@@ -81,6 +81,7 @@ import { CARD_CAP, parseVcf } from "../../web/render/vcf-parse.ts";
8181 import { CUE_CAP, parseSubtitles } from "../../web/render/subtitles.ts";
8282 import { isJsonlPath, kindView, type KindLines } from "./workspace_kinds.ts";
8383 import { sheetView } from "./workspace_sheet.ts";
84 import { sqliteStep, sqliteView, type SqliteAt } from "./workspace_sqlite.ts";
8485 import { officeView } from "./workspace_office.ts";
8586 import { layoutTable, type Grid } from "../ui/grid.ts";
8687 import type { Ctx, Screen } from "../app/app.ts";
@@ -806,6 +807,9 @@ class Tab {
806807 grid: Grid = { header: "", rows: [] };
807808 /** An .xlsx's first sheet: the bytes are not text, so the table is all there is. */
808809 sheet: Table | null = null;
810 /** A SQLite file's bytes, kept so `>`/`<` and `}`/`{` can page it; null for any other file. */
811 db: Uint8Array | null = null;
812 dbAt: SqliteAt = { table: 0, page: 1 };
809813 /** Cues of a subtitle file that could not be read. */
810814 unread = 0;
811815 /** Why a file whose name promises a view is shown as source instead; "" when nothing failed. */
@@ -1054,6 +1058,7 @@ interface CacheEntry {
10541058 readonly raw: Uint8Array | null;
10551059 readonly picture: Uint8Array | null;
10561060 readonly sheet: Table | null;
1061 readonly db: Uint8Array | null;
10571062 }
10581063
10591064 // ---- what a workspace remembers between visits ----
@@ -1341,6 +1346,7 @@ export function workspaceScreen(
13411346 let raw: Uint8Array | null = null;
13421347 let picture: Uint8Array | null = null;
13431348 let sheet: Table | null = null;
1349 let db: Uint8Array | null = null;
13441350 if (!read.ok)
13451351 note =
13461352 read.error === "not_found"
@@ -1362,7 +1368,9 @@ export function workspaceScreen(
13621368 // A binary blob whose *name* says picture is the one binary we do
13631369 // something with. Everything else keeps the honest note: showing
13641370 // bytes as text is how a terminal ends up beeping at you.
1365 const book = looksLikeImage(t.path) ? null : (sheetView(t.path, read.value.body) ?? officeView(t.path, read.value.body));
1371 const base = looksLikeImage(t.path) ? null : sqliteView(t.path, read.value.body, { table: 0, page: 1 });
1372 if (base !== null && typeof base !== "string") db = read.value.body;
1373 const book = looksLikeImage(t.path) ? null : (base ?? sheetView(t.path, read.value.body) ?? officeView(t.path, read.value.body));
13661374 if (looksLikeImage(t.path)) picture = read.value.body;
13671375 else if (book !== null && typeof book !== "string") sheet = book;
13681376 else if (typeof book === "string") note = book;
@@ -1373,7 +1381,7 @@ export function workspaceScreen(
13731381 `Binary file, ${formatSize(read.value.size)}.`;
13741382 } else raw = read.value.body; // .html included: source here, the page preview is web-only
13751383 }
1376 entry = { note, noteTitle, noteDetail, raw, picture, sheet };
1384 entry = { note, noteTitle, noteDetail, raw, picture, sheet, db };
13771385 remember(key, entry);
13781386 }
13791387 if (stale(mine)) return;
@@ -1383,6 +1391,8 @@ export function workspaceScreen(
13831391 t.noteDetail = entry.noteDetail;
13841392 t.raw = entry.raw;
13851393 t.sheet = entry.sheet;
1394 t.db = entry.db;
1395 t.dbAt = { table: 0, page: 1 };
13861396 t.image =
13871397 entry.picture === null
13881398 ? null
@@ -2072,6 +2082,7 @@ export function workspaceScreen(
20722082 disabled: t.table != null && (t.table.error !== undefined || t.table.header.length === 0),
20732083 }),
20742084 ]),
2085 ...(t?.db == null ? [] : [binding([">", "<", "}", "{"], "next / previous 100 rows, next / previous table")]),
20752086 binding(["tab"], "focus explorer"),
20762087 binding(["e"], "edit", { disabled: !editable() }),
20772088 binding(
@@ -2849,6 +2860,18 @@ export function workspaceScreen(
28492860 return handled;
28502861 }
28512862
2863 /** `>`/`<` a SQLite table's next or previous 100 rows, `}`/`{` the next or previous table. */
2864 function pageDb(t: Tab, key: string): boolean {
2865 if (t.db === null) return false;
2866 const moved = sqliteStep(t.db, t.dbAt, key);
2867 if (moved === null) return true;
2868 t.dbAt = moved.at;
2869 t.sheet = moved.view;
2870 t.rebuild(ctx.theme.c);
2871 t.scroll.top = 0;
2872 return true;
2873 }
2874
28522875 /** Read mode: scroll, search, and the two keys that change what is open. */
28532876 function viewKey(t: Tab, e: KeyEvent): boolean {
28542877 if (e.key === "/") {
@@ -2857,6 +2880,7 @@ export function workspaceScreen(
28572880 }
28582881 if (e.key === "n" && !e.ctrl) return t.step(1);
28592882 if (e.key === "N") return t.step(-1);
2883 if (t.db !== null && t.asTable && (e.key === ">" || e.key === "<" || e.key === "}" || e.key === "{")) return pageDb(t, e.key);
28602884 // Horizontal scrolling belongs to code, which has long lines and a
28612885 // gutter; prose and pictures are already fitted to the pane.
28622886 if ((e.key === "l" || e.key === "right") && !t.reading) {
src/tui/screens/workspace_sqlite.ts added +67
@@ -0,0 +1,67 @@
1 /**
2 * A SQLite file in the terminal workspace, read the way the page reads it: the same `tablePage`
3 * from `src/web/render/sqlite.ts`, opened read-only in memory from the committed bytes. One table
4 * at a time, 100 rows a page, with its columns and declared types on top; `>`/`<` page, `}`/`{`
5 * move to the next or previous table. No SQL comes from the reader.
6 *
7 * `sqliteView(path, body, at)` answers a table, a sentence saying why not, or `null` when the
8 * file is not a SQLite database by name and bytes (the workspace keeps its binary note).
9 */
10
11 import { fileKind, HEAD_BYTES } from "../../web/render/kinds.ts";
12 import { tablePage, type TablePage } from "../../web/render/sqlite.ts";
13 import type { Table } from "../../text/csv.ts";
14
15 /** Which table (zero-based) and page (one-based) a tab shows. */
16 export interface SqliteAt {
17 readonly table: number;
18 readonly page: number;
19 }
20
21 export const isSqlite = (path: string, body: Uint8Array): boolean => fileKind(path, body.subarray(0, HEAD_BYTES)) === "sqlite";
22
23 export function sqliteView(path: string, body: Uint8Array, at: SqliteAt): Table | string | null {
24 if (!isSqlite(path, body)) return null;
25 const p = tablePage(body, at.table, at.page);
26 return typeof p === "string" ? p : draw(p);
27 }
28
29 /**
30 * One key on an open database: `>`/`<` the next or previous page, `}`/`{` the next or previous
31 * table (at its first page). Answers where it landed, clamped to what the file holds, and the
32 * table to draw; null when the key does not move it.
33 */
34 export function sqliteStep(body: Uint8Array, at: SqliteAt, key: string): { at: SqliteAt; view: Table } | null {
35 const want: SqliteAt =
36 key === ">" ? { table: at.table, page: at.page + 1 }
37 : key === "<" ? { table: at.table, page: at.page - 1 }
38 : key === "}" ? { table: at.table + 1, page: 1 }
39 : key === "{" ? { table: at.table - 1, page: 1 }
40 : at;
41 if (want === at || want.page < 1 || want.table < 0) return null;
42 const p = tablePage(body, want.table, want.page);
43 if (typeof p === "string") return null;
44 const landed = { table: p.index, page: p.page };
45 if (landed.table === at.table && landed.page === at.page) return null;
46 // A view is never counted, so its last page is the one after which nothing comes back.
47 if (key === ">" && p.rows.length === 0) return null;
48 return { at: landed, view: draw(p) };
49 }
50
51 function draw(p: TablePage): Table {
52 const width = Math.max(1, p.schema.length);
53 const header = p.schema.length === 0 ? [p.name] : p.schema.map((c) => c.name);
54 const pad = (cells: readonly string[]): string[] => [...cells, ...Array<string>(Math.max(0, width - cells.length)).fill("")];
55 const total = p.total === undefined ? "" : ` of ${p.total.toLocaleString()}`;
56 const span = p.rows.length === 0 ? (p.offset === 0 ? "no rows" : "no more rows") : `rows ${(p.offset + 1).toLocaleString()}–${(p.offset + p.rows.length).toLocaleString()}${total}`;
57 const keys = [p.page > 1 ? "< previous" : "", p.more ? "> next" : "", p.names.length > 1 ? "{ } table" : ""].filter((k) => k !== "").join(" · ");
58 const meta = [
59 // One fact a line, so a narrow first column still shows each whole.
60 pad([`${p.type === "view" ? "View" : "Table"} ${p.index + 1} of ${p.names.length}: ${p.name}`]),
61 pad([span]),
62 ...(keys === "" ? [] : [pad([keys])]),
63 ...(p.note !== undefined ? [pad([p.note])] : [pad(p.schema.map((c) => c.type))]),
64 pad([]),
65 ];
66 return { header: pad(header), rows: [...meta, ...p.rows.map((r) => pad(r))], more: 0, delimiter: ",", columns: width };
67 }
src/web/pages/repo.ts +9 −3
@@ -1192,6 +1192,8 @@ export function renderBlob(
11921192 all?: boolean;
11931193 /** `?table=` on a SQLite file. */
11941194 table?: string;
1195 /** `?page=` on a SQLite table, 1-based. */
1196 page?: number;
11951197 /** `?layer=` on a G-code file. */
11961198 layer?: number;
11971199 /** Rows a worker already built for this file. */
@@ -1218,6 +1220,7 @@ export function renderBlob(
12181220 ...(o.exists !== undefined ? { exists: o.exists } : {}),
12191221 all: o.all === true,
12201222 ...(o.table !== undefined ? { table: o.table } : {}),
1223 ...(o.page !== undefined ? { page: o.page } : {}),
12211224 ...(o.layer !== undefined ? { layer: o.layer } : {}),
12221225 ...(scope.caps.write && scope.kind === "local" ? { shift: { action: `${base}/edit`, ref: o.ref, path: o.path } } : {}),
12231226 })
@@ -1753,6 +1756,8 @@ export async function blobPage(ctx: Ctx): Promise<Response> {
17531756 const view = blobView(ctx.url.searchParams.get("view"));
17541757 const all = ctx.url.searchParams.get("all") === "1";
17551758 const table = ctx.url.searchParams.get("table")?.slice(0, 256) ?? undefined;
1759 const pageText = ctx.url.searchParams.get("page");
1760 const page = pageText !== null && /^\d{1,6}$/.test(pageText) && Number(pageText) > 1 ? Number(pageText) : undefined;
17561761 const layerText = ctx.url.searchParams.get("layer");
17571762 const layer = layerText !== null && /^\d{1,6}$/.test(layerText) ? Number(layerText) : undefined;
17581763 // The whole page as it last went out to a stranger, encoded (`kept_page.ts`).
@@ -1766,7 +1771,7 @@ export async function blobPage(ctx: Ctx): Promise<Response> {
17661771 let keepable = true;
17671772 const keepFinished = (response: Response) => (keptKey === undefined || !keepable ? response : keepPage(keptKey, response, true));
17681773 if (kept) {
1769 const hit = cachedBlobPage(repo.id, ref, path, `${view}\0${table ?? ""}\0${layer ?? ""}`, all);
1774 const hit = cachedBlobPage(repo.id, ref, path, `${view}\0${table ?? ""}\0${layer ?? ""}\0${page ?? ""}`, all);
17701775 if (hit !== undefined) {
17711776 git.done();
17721777 return immutableWhenAddressed(
@@ -1794,7 +1799,7 @@ export async function blobPage(ctx: Ctx): Promise<Response> {
17941799 let highlightMs: number | undefined;
17951800 const renderStart = performance.now();
17961801 const shown = capped(blob.value, path);
1797 if (table !== undefined) keepable = shown.kind !== "too_large" && hasTable(shown.body, table);
1802 if (table !== undefined) keepable = shown.kind !== "too_large" && hasTable(shown.body, table, page);
17981803 // A big sliced file is parsed on a worker, never on this thread; the renderer finds the parse kept.
17991804 if (shown.kind !== "too_large" && shown.body.length > INLINE_MAX && kindByName(path) === "gcode" && keptGcode(shown.body) === undefined) {
18001805 const began = performance.now();
@@ -1824,6 +1829,7 @@ export async function blobPage(ctx: Ctx): Promise<Response> {
18241829 highlighted: (ms) => (highlightMs = (highlightMs ?? 0) + ms),
18251830 all,
18261831 ...(table !== undefined ? { table } : {}),
1832 ...(page !== undefined ? { page } : {}),
18271833 ...(layer !== undefined ? { layer } : {}),
18281834 });
18291835 if (highlightMs !== undefined) notePhase(ctx.request, "highlight", highlightMs);
@@ -1855,7 +1861,7 @@ export async function blobPage(ctx: Ctx): Promise<Response> {
18551861 const frame = drawsHtml(scope, path, shown, view);
18561862 const font = drawsFont(scope, path, shown, view);
18571863 if (kept && keepable)
1858 keepBlobPage(repo.id, ref, path, `${view}\0${table ?? ""}\0${layer ?? ""}`, all, {
1864 keepBlobPage(repo.id, ref, path, `${view}\0${table ?? ""}\0${layer ?? ""}\0${page ?? ""}`, all, {
18591865 title: `${filename} · ${repo.name}`,
18601866 explorer: explorerHtml,
18611867 content: pane,
src/web/render/renderers.ts +2
@@ -81,6 +81,8 @@ export interface RenderInput {
8181 readonly all?: boolean;
8282 /** `?table=<name>`: unchecked text; a renderer matches it against its own list. */
8383 readonly table?: string;
84 /** `?page=N` (1-based) with `?table=`: the renderer clamps it to the pages the table has. */
85 readonly page?: number;
8486 /**
8587 * Set for a writer only: where a shift of this file posts (the file edit route), and what it
8688 * names.
src/web/render/sqlite.ts +95 −7
@@ -29,11 +29,17 @@ interface Entry {
2929 const plural = (n: number, one: string): string => `${n.toLocaleString()} ${one}${n === 1 ? "" : "s"}`;
3030
3131 /** True when `name` is a table or view of this database (cheap; used before a page is kept). */
32 export function hasTable(body: Uint8Array, name: string): boolean {
32 export function hasTable(body: Uint8Array, name: string, page = 1): boolean {
3333 let db: Database | undefined;
3434 try {
3535 db = Database.deserialize(body, { readonly: true });
36 return db.query("SELECT 1 FROM sqlite_master WHERE type IN ('table','view') AND name = ? AND name NOT LIKE 'sqlite\\_%' ESCAPE '\\'").get(name) !== null;
36 const found = db.query("SELECT type FROM sqlite_master WHERE type IN ('table','view') AND name = ? AND name NOT LIKE 'sqlite\\_%' ESCAPE '\\'").get(name) as { type: string } | null;
37 if (found === null) return false;
38 if (page <= 1) return true;
39 // A page past the last one is a stranger's number: it is clamped, so it is never kept.
40 if (found.type !== "table") return false;
41 const n = (db.query(`SELECT count(*) AS n FROM ${quote(name)}`).get() as { n: number }).n;
42 return page <= Math.ceil(n / ROW_LIMIT);
3743 } catch {
3844 return false;
3945 } finally {
@@ -74,12 +80,81 @@ function entries(db: Database): Entry[] {
7480 });
7581 }
7682
77 function rowsOf(db: Database, entry: Entry): string {
83 /**
84 * Pages a table has (one at least); a view is never counted, so its next link shows while a page is
85 * full.
86 */
87 const pagesOf = (entry: Entry): number => Math.max(1, Math.ceil((entry.rows ?? 0) / ROW_LIMIT));
88
89 function rowsOf(db: Database, entry: Entry, offset: number): { html: string; count: number } {
7890 const width = Math.min(entry.columns.length, COLUMN_CAP);
79 const data = db.query(`SELECT * FROM ${quote(entry.name)} LIMIT ${ROW_LIMIT}`).values() as unknown[][];
91 const data = db.query(`SELECT * FROM ${quote(entry.name)} LIMIT ${ROW_LIMIT} OFFSET ${offset}`).values() as unknown[][];
8092 const head = entry.columns.slice(0, width).map((c) => `<th>${escapeHtml(c)}</th>`).join("");
8193 const body = data.map((r) => `<tr>${r.slice(0, width).map(cell).join("")}</tr>`).join("");
82 return `<table class="table" data-dense aria-label="${escapeHtml(entry.name)} rows"><thead><tr>${head}</tr></thead><tbody>${body}</tbody></table>`;
94 return { html: `<table class="table" data-dense aria-label="${escapeHtml(entry.name)} rows"><thead><tr>${head}</tr></thead><tbody>${body}</tbody></table>`, count: data.length };
95 }
96
97 /** One cell as plain text, for the terminal: the same words and cut as the page's cell. */
98 export function cellText(v: unknown): string {
99 if (v === null || v === undefined) return "NULL";
100 if (v instanceof Uint8Array) return `BLOB · ${fileSize(v.length)}`;
101 const s = String(v);
102 return s.length > CELL_CHARS ? `${s.slice(0, CELL_CHARS)}…` : s;
103 }
104
105 /**
106 * One page of one table as plain data: what the terminal draws
107 * (`src/tui/screens/workspace_sqlite.ts`).
108 */
109 export interface TablePage {
110 /** Every table and view, in name order (at most TABLE_CAP). */
111 readonly names: readonly string[];
112 /** Zero-based index of the one shown, clamped to `names`. */
113 readonly index: number;
114 readonly name: string;
115 readonly type: "table" | "view";
116 /** Column names with their declared types ("any" when none), capped at COLUMN_CAP. */
117 readonly schema: readonly { readonly name: string; readonly type: string }[];
118 /** Rows in the table; undefined for a view, which is never counted. */
119 readonly total: number | undefined;
120 /** The 1-based page shown, clamped to the pages a table has. */
121 readonly page: number;
122 /** Whether a further page exists (for a view: while a page comes back full). */
123 readonly more: boolean;
124 readonly offset: number;
125 readonly rows: readonly (readonly string[])[];
126 readonly note?: string;
127 }
128
129 /**
outdated

Counting rows by splitting the rendered HTML on '<tr>' is fragile; have rowsOf return the row count (data.length) instead.

130 * The `index`-th table's `page`-th 100 rows, read-only, from committed bytes; a string says why
131 * not (not a database, or one with no tables). The name read is always one `sqlite_master` lists.
132 */
133 export function tablePage(body: Uint8Array, index: number, page: number): TablePage | string {
134 let db: Database | undefined;
135 try {
136 db = Database.deserialize(body, { readonly: true });
137 const list = entries(db);
138 if (list.length === 0) return "This database has no tables.";
139 const at = Math.min(list.length - 1, Math.max(0, Number.isSafeInteger(index) ? index : 0));
outdated

Principle 7/1: the table view shows no schema (columns/types) above the rows, which is the point of the title. Add it here. Also nav links are small inline text, hard to hit with a thumb; give them tappable rows.

140 const e = list[at]!;
141 const base = { names: list.map((x) => x.name), index: at, name: e.name, type: e.type, total: e.rows };
142 if (e.note !== undefined) return { ...base, schema: [], page: 1, more: false, offset: 0, rows: [], note: e.note };
143 const types = new Map((db.query("SELECT name, type FROM pragma_table_info(?)").all(e.name) as { name: string; type: string }[]).map((c) => [c.name, c.type]));
144 const width = Math.min(e.columns.length, COLUMN_CAP);
145 const schema = e.columns.slice(0, width).map((c) => ({ name: c, type: types.get(c) || "any" }));
146 const pages = e.type === "table" ? pagesOf(e) : Infinity;
147 const shown = Math.min(Math.max(1, Number.isSafeInteger(page) ? page : 1), pages);
148 const offset = (shown - 1) * ROW_LIMIT;
149 const data = db.query(`SELECT * FROM ${quote(e.name)} LIMIT ${ROW_LIMIT} OFFSET ${offset}`).values() as unknown[][];
150 const rows = data.map((r) => r.slice(0, width).map(cellText));
151 const more = e.type === "view" ? data.length === ROW_LIMIT : shown < pages;
152 return { ...base, schema, page: shown, more, offset, rows };
153 } catch {
154 return "This file could not be read as a SQLite database.";
155 } finally {
156 db?.close();
157 }
83158 }
84159
85160 export function sqlite(input: RenderInput): Rendered {
@@ -89,6 +164,7 @@ export function sqlite(input: RenderInput): Rendered {
89164 const list = entries(db);
90165 const href = input.pathHref ?? "";
91166 const wanted = input.table;
167 const asked = Number.isSafeInteger(input.page) && (input.page ?? 1) > 1 ? (input.page as number) : 1;
92168 const chosen = wanted === undefined ? undefined : list.find((e) => e.name === wanted);
93169 const notice =
94170 wanted !== undefined && chosen === undefined
@@ -110,9 +186,21 @@ export function sqlite(input: RenderInput): Rendered {
110186
111187 let content: string;
112188 if (chosen !== undefined) {
113 const total = chosen.rows === undefined ? "" : ` of ${chosen.rows.toLocaleString()}`;
114189 if (chosen.note !== undefined) return { content: html`<div class="sqlite-view"><p class="small"><a href="${href}">← All tables</a> · <strong>${chosen.name}</strong> <span class="muted">${chosen.note}</span></p></div>`, lines: 0, summary };
115 content = `<p class="small"><a href="${escapeHtml(href)}">← All tables</a> · <strong>${escapeHtml(chosen.name)}</strong> <span class="muted">first ${Math.min(ROW_LIMIT, chosen.rows ?? ROW_LIMIT).toLocaleString()}${total} rows</span></p><div class="csv-scroll">${rowsOf(db, chosen)}</div>`;
190 const pages = chosen.type === "table" ? pagesOf(chosen) : Infinity;
191 const page = Math.min(asked, pages);
192 const offset = (page - 1) * ROW_LIMIT;
193 const { html: body, count: shown } = rowsOf(db, chosen, offset);
194 const types = new Map((db.query("SELECT name, type FROM pragma_table_info(?)").all(chosen.name) as { name: string; type: string }[]).map((c) => [c.name, c.type]));
195 const schema = `<p class="small" aria-label="Columns">${chosen.columns.slice(0, COLUMN_CAP).map((c) => `<code>${escapeHtml(c)}</code> <span class="muted">${escapeHtml(types.get(c) || "any")}</span>`).join(" · ")}</p>`;
196 const from = shown === 0 ? 0 : offset + 1;
197 const total = chosen.rows === undefined ? "" : ` of ${chosen.rows.toLocaleString()}`;
198 const at = (n: number): string => `${escapeHtml(href)}?table=${encodeURIComponent(chosen.name)}${n > 1 ? `&amp;page=${n}` : ""}`;
199 const more = chosen.type === "view" ? shown === ROW_LIMIT : page < pages;
200 const prev = page > 1 ? `<a class="btn" href="${at(page - 1)}" rel="prev">← Previous 100</a>` : "";
201 const next = more ? `<a class="btn" href="${at(page + 1)}" rel="next">Next 100 →</a>` : "";
202 const nav = prev || next ? `<p>${prev}${prev && next ? " " : ""}${next}</p>` : "";
203 content = `<p class="small"><a href="${escapeHtml(href)}">← All tables</a> · <strong>${escapeHtml(chosen.name)}</strong> <span class="muted">${shown === 0 ? (offset === 0 ? "no rows" : "no more rows") : `rows ${from.toLocaleString()}–${(offset + shown).toLocaleString()}${total}`}</span></p>${schema}${nav}<div class="csv-scroll">${body}</div>${nav}`;
116204 } else {
117205 content = `${notice}${listing}${empty}${more}`;
118206 }
test/render-sqlite.test.ts +66 −3
@@ -4,7 +4,8 @@ import { describe, expect, test } from "bun:test";
44 import { renderBlob, capped } from "../src/web/pages/repo.ts";
55 import { fileKind } from "../src/web/render/kinds.ts";
66 import { RENDERERS } from "../src/web/render/renderers.ts";
7 import { ROW_LIMIT, TABLE_CAP, hasTable, sqlite } from "../src/web/render/sqlite.ts";
7 import { ROW_LIMIT, TABLE_CAP, hasTable, sqlite, tablePage } from "../src/web/render/sqlite.ts";
8 import { sqliteStep } from "../src/tui/screens/workspace_sqlite.ts";
89 import { expectWork } from "./helpers/work.ts";
910 import type { RepoScope } from "../src/web/scope.ts";
1011
@@ -21,8 +22,8 @@ const make = (): Uint8Array => {
2122 return bytes;
2223 };
2324 const DB = make();
24 const draw = (body: Uint8Array, table?: string) =>
25 sqlite({ path: "a.db", size: body.length, body, media: "/m", download: "/d", pathHref: "/o/r/blob/main/a.db", ...(table ? { table } : {}) });
25 const draw = (body: Uint8Array, table?: string, page?: number) =>
26 sqlite({ path: "a.db", size: body.length, body, media: "/m", download: "/d", pathHref: "/o/r/blob/main/a.db", ...(table ? { table } : {}), ...(page ? { page } : {}) });
2627
2728 describe("sqlite kind", () => {
2829 test("the header confirms it; a .db without it is binary", () => {
@@ -55,6 +56,38 @@ describe("sqlite page", () => {
5556 expect(big.split("<tr>").length - 1).toBe(ROW_LIMIT + 1);
5657 expect(String(draw(DB, "recent").content)).toContain("<th>body</th>");
5758 });
59 test("a table pages by 100 with next and previous links", () => {
60 const one = String(draw(DB, 'we"ird').content);
61 expect(one).toContain("rows 1–100 of 250");
62 expect(one.indexOf("<code>")).toBeGreaterThan(-1); // the schema stays above the rows
63 expect(one.indexOf("<code>")).toBeLessThan(one.indexOf("<table"));
64 expect(one).toContain('class="btn"');
65 expect(one).toContain("page=2"); expect(one).not.toContain("Previous");
66 const two = String(draw(DB, 'we"ird', 2).content);
67 expect(two).toContain("rows 101–200 of 250");
68 expect(two).toContain("Previous 100"); expect(two).toContain("Next 100");
69 expect(two).toContain("<td class=\"num\">100</td>"); expect(two).not.toContain("<td class=\"num\">99</td>");
70 const last = String(draw(DB, 'we"ird', 3).content);
71 expect(last).toContain("rows 201–250 of 250"); expect(last).not.toContain("Next 100");
72 expect(last.split("<tr>").length - 1).toBe(51);
73 // Past the end is clamped to the last page; junk is page one.
74 expect(String(draw(DB, 'we"ird', 99).content)).toContain("rows 201–250");
75 expect(String(draw(DB, 'we"ird', Number.NaN).content)).toContain("rows 1–100");
76 expect(String(draw(DB, "notes").content)).not.toContain("Next 100");
77 });
78 test("a page past the end is not kept; a hostile table name never reaches SQL", () => {
79 expect(hasTable(DB, 'we"ird', 3)).toBe(true);
80 expect(hasTable(DB, 'we"ird', 4)).toBe(false);
81 expect(hasTable(DB, 'we"ird; DROP TABLE notes', 2)).toBe(false);
82 });
83 test("the file is opened read-only: a write through it fails and the bytes stay", () => {
84 const copy = DB.slice();
85 const db = Database.deserialize(copy, { readonly: true });
86 expect(() => db.run("INSERT INTO notes (body) VALUES ('x')")).toThrow();
87 db.close();
88 draw(copy, 'notes"; DROP TABLE notes; --', 2);
89 expect(Buffer.from(copy).equals(Buffer.from(DB))).toBe(true);
90 });
5891 test("an unknown table, or an injection attempt, goes back to the list", () => {
5992 for (const t of ["nope", 'notes"; DROP TABLE notes; --', "sqlite_master"]) {
6093 const h = String(draw(DB, t).content);
@@ -154,3 +187,33 @@ describe("sqlite details", () => {
154187 expect(h).toContain("150 rows");
155188 });
156189 });
190
191 describe("the terminal's read of a table", () => {
192 test("tablePage reads one page as plain text, schema with types, clamped", () => {
193 const p = tablePage(DB, 0, 1);
194 if (typeof p === "string") throw new Error(p);
195 expect(p.names).toEqual(["notes", "recent", 'we"ird']);
196 expect(p.schema).toEqual([{ name: "id", type: "INTEGER" }, { name: "body", type: "TEXT" }, { name: "pic", type: "BLOB" }]);
197 expect(p.rows[0]).toEqual(["1", "<script>alert(1)</script>", "BLOB · 4 B"]);
198 expect(p.rows[1]).toEqual(["2", "NULL", "NULL"]);
199 const last = tablePage(DB, 2, 99);
200 if (typeof last === "string") throw new Error(last);
201 expect(last.page).toBe(3); expect(last.offset).toBe(200); expect(last.rows.length).toBe(50); expect(last.more).toBe(false);
202 expect(tablePage(new TextEncoder().encode("not a db"), 0, 1)).toContain("could not be read");
203 });
204 test("sqliteStep moves by page and table and stops at the ends", () => {
205 const start = { table: 2, page: 1 };
206 expect(sqliteStep(DB, start, "<")).toBeNull();
207 const two = sqliteStep(DB, start, ">");
208 expect(two?.at).toEqual({ table: 2, page: 2 });
209 expect(two?.view.rows[1]?.[0]).toBe("rows 101–200 of 250");
210 expect(sqliteStep(DB, { table: 2, page: 3 }, ">")).toBeNull();
211 expect(sqliteStep(DB, start, "}")).toBeNull();
212 expect(sqliteStep(DB, start, "{")?.at).toEqual({ table: 1, page: 1 });
213 expect(sqliteStep(DB, { table: 1, page: 1 }, ">")).toBeNull(); // a two-row view has no second page
214 });
215 test("a view asked past its rows says so, not rows 0–N", () => {
216 const h = String(draw(DB, "recent", 5).content);
217 expect(h).toContain("no more rows"); expect(h).not.toContain("rows 0");
218 });
219 });
test/tui-workspace-formats.test.ts +36
@@ -5,6 +5,7 @@
55 * both under the file budget).
66 */
77
8 import { Database } from "bun:sqlite";
89 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
910 import { commitToBranch, createRepo } from "../src/actions/repos.ts";
1011 import type { App as Forge } from "../src/app.ts";
@@ -756,6 +757,41 @@ describe("the terminal workspace, by file type", () => {
756757 h.stop();
757758 }
758759 });
760 test("a SQLite file opens as one table, its schema on top, paged by 100 with > < and } {", async () => {
761 const made = await createRepo(forge, alice, { name: "dbs", visibility: "public", description: "data" });
762 if (!made.ok) throw new Error("repo failed");
763 const mem = new Database(":memory:");
764 mem.run("CREATE TABLE big (n INTEGER, label TEXT)");
765 for (let i = 1; i <= 250; i++) mem.run("INSERT INTO big VALUES (?, ?)", [i, `row ${i}`]);
766 mem.run("CREATE TABLE small (word TEXT)");
767 mem.run("INSERT INTO small VALUES ('hello')");
768 const bytes = mem.serialize();
769 mem.close();
770 await commitToBranch(forge, alice, made.value, { message: "seed", files: [{ path: "data.db", content: bytes }] });
771 const h = boot();
772 try {
773 await h.settle();
774 await open(h, "dbs");
775 await openFile(h, "data.db");
776 expect(h.text()).toContain("Table 1 of 2: big");
777 expect(h.text()).toContain("rows 1–100 of 250");
778 expect(h.text()).toContain("INTEGER");
779 expect(h.text()).toContain("row 1");
780 await h.press(">");
781 expect(h.text()).toContain("rows 101–200 of 250");
782 await h.press(">");
783 await h.press(">");
784 expect(h.text()).toContain("rows 201–250 of 250");
785 await h.press("<");
786 expect(h.text()).toContain("rows 101–200 of 250");
787 await h.press("}");
788 expect(h.text()).toContain("Table 2 of 2: small");
789 expect(h.text()).toContain("rows 1–1 of 1");
790 expect(h.text()).toContain("hello");
791 } finally {
792 h.stop();
793 }
794 });
759795 test("a .pptx opens as its slide titles", async () => {
760796 const made = await createRepo(forge, alice, { name: "decks", visibility: "public", description: "slides" });
761797 if (!made.ok) throw new Error("repo failed");

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs