Skip to content

Commit 1192f52

Browse files
committed
fix: colum names display
1 parent 5a2381e commit 1192f52

File tree

4 files changed

+56
-30
lines changed

4 files changed

+56
-30
lines changed

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"preview": "vite preview"
1010
},
1111
"dependencies": {
12-
"kite_sql": "^0.1.3"
12+
"@kipdata/kite_sql": "^0.1.4"
1313
},
1414
"devDependencies": {
1515
"@types/node": "^22.9.0",

src/kite-sql-web.ts

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
// Based on the upstream glue with minimal changes for ESM + browsers.
55
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
66
// @ts-nocheck
7-
import wasmUrl from "kite_sql/kite_sql_bg.wasm?url";
7+
import wasmUrl from "@kipdata/kite_sql/kite_sql_bg.wasm?url";
88

99
const placeholder: any = {};
10-
const imports: any = { __wbindgen_placeholder__: placeholder };
10+
// The wasm expects its JS imports under the module name "./kite_sql_bg.js".
11+
const imports: any = { "./kite_sql_bg.js": placeholder };
1112
let wasm: any;
1213

1314
function addToExternrefTable0(obj) {
@@ -189,23 +190,34 @@ class WasmResultIter {
189190
free() {
190191
const ptr = this.__destroy_into_raw();
191192
wasm.__wbg_wasmresultiter_free(ptr, 0);
192-
}
193-
/**
194-
* Returns the next row as a JS object, or `undefined` when done.
195-
* @returns {any}
196-
*/
197-
next() {
198-
const ret = wasm.wasmresultiter_next(this.__wbg_ptr);
199-
if (ret[2]) {
200-
throw takeFromExternrefTable0(ret[1]);
201193
}
202-
return takeFromExternrefTable0(ret[0]);
203-
}
204-
/**
205-
* Collect all remaining rows into an array and finish the iterator.
206-
* @returns {any}
207-
*/
208-
rows() {
194+
/**
195+
* Returns the next row as a JS object, or `undefined` when done.
196+
* @returns {any}
197+
*/
198+
next() {
199+
const ret = wasm.wasmresultiter_next(this.__wbg_ptr);
200+
if (ret[2]) {
201+
throw takeFromExternrefTable0(ret[1]);
202+
}
203+
return takeFromExternrefTable0(ret[0]);
204+
}
205+
/**
206+
* Returns the output schema as an array of `{ name, datatype, nullable }`.
207+
* @returns {any}
208+
*/
209+
schema() {
210+
const ret = wasm.wasmresultiter_schema(this.__wbg_ptr);
211+
if (ret[2]) {
212+
throw takeFromExternrefTable0(ret[1]);
213+
}
214+
return takeFromExternrefTable0(ret[0]);
215+
}
216+
/**
217+
* Collect all remaining rows into an array and finish the iterator.
218+
* @returns {any}
219+
*/
220+
rows() {
209221
const ret = wasm.wasmresultiter_rows(this.__wbg_ptr);
210222
if (ret[2]) {
211223
throw takeFromExternrefTable0(ret[1]);
@@ -335,9 +347,10 @@ async function init(input = wasmUrl) {
335347
const { instance, module } = await (async () => {
336348
const res = await response;
337349
if (res instanceof Response) {
350+
const resForStreaming = res.clone();
338351
if (typeof WebAssembly.instantiateStreaming === "function") {
339352
try {
340-
return await WebAssembly.instantiateStreaming(res, imports);
353+
return await WebAssembly.instantiateStreaming(resForStreaming, imports);
341354
} catch (e) {
342355
// Fallback if incorrect MIME type.
343356
const bytes = await res.arrayBuffer();

src/main.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,21 @@ const extractValue = (v: any) => {
9393
return finalValue;
9494
};
9595

96-
function renderRows(rows: any[]) {
96+
const columnNamesFromSchema = (schema: any) => {
97+
if (!Array.isArray(schema)) return [];
98+
return schema
99+
.map((col) => (typeof col?.name === "string" ? col.name : undefined))
100+
.filter((name): name is string => Boolean(name));
101+
};
102+
103+
function renderRows(rows: any[], columnNames?: string[]) {
97104
if (!rows.length) {
98105
outputEl.textContent = "No rows returned.";
99106
return;
100107
}
101108

102109
const columns =
110+
(columnNames && columnNames.length ? columnNames : undefined) ??
103111
rows[0]?.columns ??
104112
rows[0]?.keys ??
105113
rows[0]?.names ??
@@ -153,9 +161,11 @@ async function seedDemoData(showResult = true) {
153161

154162
if (showResult) {
155163
const result = db.run("select * from t_demo order by score desc");
164+
const schema = typeof (result as any).schema === "function" ? (result as any).schema() : null;
165+
const columns = columnNamesFromSchema(schema);
156166
const rows = result.rows();
157167
result.finish();
158-
renderRows(rows);
168+
renderRows(rows, columns ?? undefined);
159169
setStatus("Demo data ready ✓", true);
160170
} else {
161171
setStatus("Demo data ready ✓", true);
@@ -185,10 +195,13 @@ async function runSql(sqlText: string) {
185195
}
186196

187197
let lastRows: any[] | null = null;
198+
let lastColumns: string[] | null = null;
188199
for (const sql of statements) {
189200
const isQuery = /^(select|with|pragma|explain)/i.test(sql.trim());
190201
if (isQuery) {
191202
const result = db!.run(sql);
203+
const schema = typeof (result as any).schema === "function" ? (result as any).schema() : null;
204+
lastColumns = columnNamesFromSchema(schema);
192205
lastRows = result.rows();
193206
result.finish();
194207
} else {
@@ -197,7 +210,7 @@ async function runSql(sqlText: string) {
197210
}
198211

199212
if (lastRows) {
200-
renderRows(lastRows);
213+
renderRows(lastRows, lastColumns ?? undefined);
201214
setStatus("Query completed ✓", true);
202215
} else {
203216
outputEl.textContent = `Executed ${statements.length} statement(s).`;

0 commit comments

Comments
 (0)