stfrigerio_sqliteDB/src/codeblocks/processSqlBlock/index.ts

98 lines
4.1 KiB
TypeScript
Raw Normal View History

2025-02-04 16:08:15 +00:00
import { Notice } from "obsidian";
import { DBService } from "../../DBService";
2025-02-04 16:08:15 +00:00
import { parseSqlParams, validateTable, buildSqlQuery, renderResults } from "./helpers";
2025-04-13 11:51:38 +00:00
import { renderResultsAsTable } from "./helpers/renderResultsAsTable";
2025-02-04 16:08:15 +00:00
export async function processSqlBlock(dbService: DBService, source: string, el: HTMLElement) {
const params = parseSqlParams(source);
2025-02-04 16:08:15 +00:00
if (!params) {
el.createEl("p", { text: "Missing required parameter: table" });
el.createEl("p", { text: "Example usage:" });
el.createEl("pre", {
2025-02-04 16:08:15 +00:00
text: `table: tasks
columns: title, status
filterColumn: status, priority
filterValue: active, high
dateColumn: dueDate
startDate: 2024-01-01
endDate: 2024-12-31
limit: 10
orderBy: dueDate
orderDirection: asc`
2025-02-04 16:08:15 +00:00
});
return;
}
try {
const validationError = await validateTable(dbService, params);
2025-02-04 16:08:15 +00:00
if (validationError) {
el.createEl("p", { text: validationError.message });
if (validationError.availableColumns) {
el.createEl("p", { text: `Available columns are: ${validationError.availableColumns.join(", ")}` });
}
return;
}
const { query, queryParams } = buildSqlQuery(params);
let resultsToRender: { columns: string[], values: any[][] } | null = null;
// Handle remote vs local DB
if (dbService.mode === "remote") {
const rows = await dbService.getQuery(query, queryParams);
if (!rows || rows.length === 0) {
// Keep existing no-rows message logic
} else {
// Convert remote result (array of objects) to the format renderResults expects
const columns = Object.keys(rows[0]);
const values = rows.map(obj => columns.map(col => obj[col]));
resultsToRender = { columns, values };
2025-02-04 16:08:15 +00:00
}
} else {
// Local mode logic
const db = dbService.getDB();
if (!db) {
new Notice("No DB loaded. Please open the DB first.");
el.createEl("p", { text: "Database not loaded." });
return; // Exit renderSqlBlock early
2025-02-04 16:08:15 +00:00
}
const result = db.exec(query, queryParams);
if (!result || result.length === 0 || result[0].values.length === 0) {
// Keep existing no-rows message logic
} else {
resultsToRender = result[0];
2025-02-04 16:08:15 +00:00
}
}
//? Check if we have results to render
if (!resultsToRender) {
el.createEl("p", { text: `No rows found matching the criteria.` });
// Optionally display the specific criteria used from 'params' object
let criteriaMsg = `Table: "${params.table}"`;
if(params.startDate || params.endDate) criteriaMsg += ` | Dates: ${params.startDate} to ${params.endDate}`;
if(params.filterColumn) criteriaMsg += ` | Filter: ${params.filterColumn}=${params.filterValue}`;
el.createEl("p", { text: `Criteria: ${criteriaMsg}`, cls: 'sql-block-criteria-summary'}); // Add a class for styling
2025-02-04 16:08:15 +00:00
return;
}
2025-04-13 11:51:38 +00:00
// --- Choose Renderer based on displayFormat ---
el.empty(); // Clear loading message or previous content
if (params.displayFormat === 'table') {
renderResultsAsTable(resultsToRender, el);
} else { // Default to 'list'
renderResults(resultsToRender, el); // Assuming renderResults is the list renderer
}
2025-02-04 16:08:15 +00:00
} catch (error) {
el.empty(); // Clear before showing error
el.createEl("p", { text: "An error occurred processing the SQL block." });
2025-02-04 16:08:15 +00:00
el.createEl("p", { text: String(error) });
console.error("SQL Block Error:", error);
// Optionally show query info on error
try {
const { query, queryParams } = buildSqlQuery(params); // Try building query again for error display
2025-02-04 16:08:15 +00:00
el.createEl("pre", { text: `Query: ${query}\nParams: ${JSON.stringify(queryParams)}` });
} catch (buildError) { /* Ignore if query build fails */ }
2025-02-04 16:08:15 +00:00
}
}