2025-02-04 16:08:15 +00:00
|
|
|
import { Notice } from "obsidian";
|
2025-04-05 11:56:27 +00:00
|
|
|
import { DBService } from "../../DBService";
|
2025-02-04 16:08:15 +00:00
|
|
|
import { parseSqlParams, validateTable, buildSqlQuery, renderResults } from "./helpers";
|
|
|
|
|
|
|
|
|
|
export async function processSqlBlock(dbService: DBService, source: string, el: HTMLElement) {
|
2025-04-09 12:55:27 +00:00
|
|
|
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:" });
|
2025-04-09 12:55:27 +00:00
|
|
|
el.createEl("pre", {
|
2025-02-04 16:08:15 +00:00
|
|
|
text: `table: tasks
|
2025-04-09 12:55:27 +00:00
|
|
|
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 {
|
2025-04-09 12:55:27 +00:00
|
|
|
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);
|
|
|
|
|
|
2025-04-09 12:55:27 +00:00
|
|
|
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
|
|
|
}
|
2025-04-09 12:55:27 +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
|
|
|
}
|
2025-04-09 12:55:27 +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
|
|
|
}
|
2025-04-09 12:55:27 +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-09 12:55:27 +00:00
|
|
|
// Render results if found
|
|
|
|
|
renderResults(resultsToRender, el);
|
2025-02-04 16:08:15 +00:00
|
|
|
|
|
|
|
|
} catch (error) {
|
2025-04-09 12:55:27 +00:00
|
|
|
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) });
|
2025-04-09 12:55:27 +00:00
|
|
|
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)}` });
|
2025-04-09 12:55:27 +00:00
|
|
|
} catch (buildError) { /* Ignore if query build fails */ }
|
2025-02-04 16:08:15 +00:00
|
|
|
}
|
|
|
|
|
}
|