mirror of
https://github.com/stfrigerio/sqliteDB.git
synced 2026-07-22 05:38:02 +00:00
tabled results
This commit is contained in:
parent
96b6832e25
commit
08ab87653d
4 changed files with 84 additions and 4 deletions
|
|
@ -45,14 +45,41 @@ export function parseSqlParams(source: string): SqlParams | null {
|
|||
case 'orderBy':
|
||||
params.orderBy = val;
|
||||
break;
|
||||
case 'displayFormat':
|
||||
const format = val.toLowerCase();
|
||||
if (format === 'list' || format === 'table') {
|
||||
params.displayFormat = format;
|
||||
} else {
|
||||
console.warn(`Invalid displayFormat "${val}", defaulting to 'list'.`);
|
||||
params.displayFormat = 'list'; // Default if invalid value
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Validate required parameters
|
||||
// --- Validation for required 'table' ---
|
||||
if (!params.table) {
|
||||
// Indicate parsing failure specifically because 'table' is missing
|
||||
// The calling function will handle displaying the error message.
|
||||
return null;
|
||||
}
|
||||
|
||||
// --- Set Default displayFormat if not provided ---
|
||||
if (!params.displayFormat) {
|
||||
params.displayFormat = 'list';
|
||||
}
|
||||
|
||||
// Check consistency if multiple filters were provided
|
||||
if (Array.isArray(params.filterColumn) || Array.isArray(params.filterValue)) {
|
||||
if (!Array.isArray(params.filterColumn) || !Array.isArray(params.filterValue) || params.filterColumn.length !== params.filterValue.length) {
|
||||
console.error("Mismatch between number of filterColumn and filterValue entries. Filtering might be incorrect.");
|
||||
// Decide how to handle: throw error, ignore filters, return null?
|
||||
// Returning null might be safest if config is invalid
|
||||
return null; // Indicate parsing failure due to inconsistent filters
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return params as SqlParams;
|
||||
}
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
// helpers.ts (add this new function)
|
||||
|
||||
export function renderResultsAsTable(results: { columns: string[], values: any[][] }, el: HTMLElement): void {
|
||||
el.empty(); // Clear the container first
|
||||
|
||||
const table = el.createEl("table", { cls: "sql-results-table" }); // Add a class for styling
|
||||
|
||||
// --- Create Table Header ---
|
||||
const thead = table.createEl("thead", { cls: "sql-results-thead" });
|
||||
const headerRow = thead.createEl("tr", { cls: "sql-results-tr sql-results-header-row" });
|
||||
results.columns.forEach(colName => {
|
||||
headerRow.createEl("th", { text: colName, cls: "sql-results-th" });
|
||||
});
|
||||
|
||||
// --- Create Table Body ---
|
||||
const tbody = table.createEl("tbody", { cls: "sql-results-tbody" });
|
||||
results.values.forEach(valueRow => {
|
||||
const tr = tbody.createEl("tr", { cls: "sql-results-tr" });
|
||||
valueRow.forEach(cellValue => {
|
||||
// Render null/undefined as empty string or specific text
|
||||
const displayValue = (cellValue === null || cellValue === undefined) ? "" : String(cellValue);
|
||||
tr.createEl("td", { text: displayValue, cls: "sql-results-td" });
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Consider renaming the existing renderResults for clarity if needed
|
||||
// export function renderResultsAsList(...) { ... }
|
||||
// Or keep renderResults as the function for 'list' format.
|
||||
// Let's assume the existing renderResults IS the list renderer for now.
|
||||
export function renderResults(results: { columns: string[], values: any[][] }, el: HTMLElement): void {
|
||||
// This is your CURRENT rendering logic (presumably rendering as divs, list items, etc.)
|
||||
// For demonstration, let's make a simple list:
|
||||
el.empty();
|
||||
const list = el.createEl("ul", {cls: "sql-results-list"});
|
||||
results.values.forEach(valueRow => {
|
||||
const item = list.createEl("li");
|
||||
const content = results.columns.map((col, index) => `${col}: ${valueRow[index] ?? 'N/A'}`).join(" | ");
|
||||
item.createEl("pre", { text: content }); // Use pre for simple formatting
|
||||
});
|
||||
}
|
||||
|
||||
// Don't forget to export the new function if helpers.ts is a module
|
||||
// (The other helpers like parseSqlParams, validateTable, buildSqlQuery should already be exported)
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
import { Notice } from "obsidian";
|
||||
import { DBService } from "../../DBService";
|
||||
import { parseSqlParams, validateTable, buildSqlQuery, renderResults } from "./helpers";
|
||||
import { renderResultsAsTable } from "./helpers/renderResultsAsTable";
|
||||
|
||||
export async function processSqlBlock(dbService: DBService, source: string, el: HTMLElement) {
|
||||
const params = parseSqlParams(source);
|
||||
|
|
@ -75,9 +76,16 @@ orderDirection: asc`
|
|||
return;
|
||||
}
|
||||
|
||||
// Render results if found
|
||||
renderResults(resultsToRender, el);
|
||||
|
||||
// --- Choose Renderer based on displayFormat ---
|
||||
el.empty(); // Clear loading message or previous content
|
||||
if (params.displayFormat === 'table') {
|
||||
console.log("Rendering SQL block as table");
|
||||
renderResultsAsTable(resultsToRender, el);
|
||||
} else { // Default to 'list'
|
||||
console.log("Rendering SQL block as list");
|
||||
renderResults(resultsToRender, el); // Assuming renderResults is the list renderer
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
el.empty(); // Clear before showing error
|
||||
el.createEl("p", { text: "An error occurred processing the SQL block." });
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ export interface SqlParams {
|
|||
orderBy?: string;
|
||||
orderDirection?: string;
|
||||
limit?: number;
|
||||
displayFormat?: string;
|
||||
}
|
||||
|
||||
export interface ValidationError {
|
||||
|
|
|
|||
Loading…
Reference in a new issue