import { SqlExecuteResults } from "@triliumnext/commons"; import { useNoteContext, useTriliumEvent } from "./react/hooks"; import "./sql_result.css"; import { useState } from "preact/hooks"; import Alert from "./react/Alert"; import { t } from "../services/i18n"; export default function SqlResults() { const { note, ntxId } = useNoteContext(); const [ results, setResults ] = useState(); useTriliumEvent("sqlQueryResults", ({ ntxId: eventNtxId, results }) => { if (eventNtxId !== ntxId) return; setResults(results); }) const isEnabled = note?.mime === "text/x-sqlite;schema=trilium"; return (
{isEnabled && ( results?.length === 1 && Array.isArray(results[0]) && results[0].length === 0 ? ( {t("sql_result.no_rows")} ) : (
{results?.map(rows => { // inserts, updates if (typeof rows === "object" && !Array.isArray(rows)) { return
{JSON.stringify(rows, null, "\t")}
} // selects return })}
) )}
) } function SqlResultTable({ rows }: { rows: object[] }) { if (!rows.length) return; return ( {Object.keys(rows[0]).map(key => )} {rows.map(row => ( {Object.values(row).map(cell => )} ))}
{key}
{cell}
) }