2025-01-09 12:32:24 +00:00
|
|
|
import { OmnibusRegistrator } from "@hypersphere/omnibus";
|
2025-01-10 11:03:43 +00:00
|
|
|
import { App, MarkdownPostProcessorContext, MarkdownRenderChild, TFile } from "obsidian";
|
2025-01-29 22:36:19 +00:00
|
|
|
import { SqlSealDatabase } from "../database/database";
|
|
|
|
|
import { Sync } from "../datamodel/sync";
|
|
|
|
|
import { ParserTableDefinition } from "../datamodel/syncStrategy/types";
|
|
|
|
|
import { parseLanguage } from "../grammar/newParser";
|
|
|
|
|
import { RendererRegistry, RenderReturn } from "../renderer/rendererRegistry";
|
|
|
|
|
import { transformQuery } from "../sql/sqlTransformer";
|
|
|
|
|
import { displayError, displayNotice } from "../utils/ui";
|
2025-01-30 13:16:52 +00:00
|
|
|
import SqlSealPlugin from "../main";
|
|
|
|
|
import { registerObservers } from "../utils/registerObservers";
|
2025-02-04 10:43:47 +00:00
|
|
|
import { IntermediateContent, parseIntermediateContent } from "src/grammar/parseIntermediateContent";
|
2025-01-08 19:58:14 +00:00
|
|
|
|
2025-01-09 12:32:24 +00:00
|
|
|
export class CodeblockProcessor extends MarkdownRenderChild {
|
2025-01-08 19:58:14 +00:00
|
|
|
|
|
|
|
|
registrator: OmnibusRegistrator
|
|
|
|
|
renderer: RenderReturn
|
2025-02-04 10:43:47 +00:00
|
|
|
private flags: IntermediateContent['flags']
|
|
|
|
|
private extrasEl: HTMLElement
|
|
|
|
|
private explainEl: HTMLElement
|
2024-09-02 18:40:48 +00:00
|
|
|
|
2024-11-02 15:23:44 +00:00
|
|
|
constructor(
|
2025-01-08 19:58:14 +00:00
|
|
|
private el: HTMLElement,
|
|
|
|
|
private source: string,
|
|
|
|
|
private ctx: MarkdownPostProcessorContext,
|
|
|
|
|
private rendererRegistry: RendererRegistry,
|
|
|
|
|
private db: SqlSealDatabase,
|
2025-01-30 13:16:52 +00:00
|
|
|
private plugin: SqlSealPlugin,
|
2025-01-08 19:58:14 +00:00
|
|
|
private app: App,
|
|
|
|
|
private sync: Sync) {
|
|
|
|
|
super(el)
|
2025-02-04 10:43:47 +00:00
|
|
|
|
2025-01-08 19:58:14 +00:00
|
|
|
this.registrator = this.sync.getRegistrator()
|
2024-09-02 18:40:48 +00:00
|
|
|
}
|
2024-09-01 13:01:54 +00:00
|
|
|
|
2025-01-08 19:58:14 +00:00
|
|
|
query: string;
|
2024-11-02 15:23:44 +00:00
|
|
|
|
2025-01-08 19:58:14 +00:00
|
|
|
async onload() {
|
|
|
|
|
try {
|
2025-01-16 13:50:51 +00:00
|
|
|
const results = parseLanguage(this.source, this.ctx.sourcePath)
|
2025-01-08 19:58:14 +00:00
|
|
|
if (results.tables) {
|
|
|
|
|
await this.registerTables(results.tables)
|
|
|
|
|
if (!results.queryPart) {
|
2025-01-23 13:36:50 +00:00
|
|
|
displayNotice(this.el, `Creating tables: ${results.tables.map(t => t.tableAlias).join(', ')}`)
|
2025-01-08 19:58:14 +00:00
|
|
|
return
|
|
|
|
|
}
|
2024-11-02 15:23:44 +00:00
|
|
|
}
|
|
|
|
|
|
2025-02-04 10:43:47 +00:00
|
|
|
const { intermediateContent } = results
|
|
|
|
|
const config = parseIntermediateContent(intermediateContent, {
|
|
|
|
|
flags: {
|
|
|
|
|
refresh: this.plugin.settings.enableDynamicUpdates,
|
|
|
|
|
explain: false
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
this.flags = config.flags
|
|
|
|
|
|
2025-02-04 18:26:27 +00:00
|
|
|
let rendererEl = this.el
|
|
|
|
|
|
|
|
|
|
if (this.flags.explain) {
|
|
|
|
|
|
|
|
|
|
this.extrasEl = this.el.createDiv({ cls: 'sqlseal-extras-container' })
|
|
|
|
|
if (config.flags.explain) {
|
|
|
|
|
this.explainEl = this.extrasEl.createEl('pre', { cls: 'sqlseal-extras-explain-container' })
|
|
|
|
|
}
|
|
|
|
|
rendererEl = this.el.createDiv({ cls: 'sqlseal-renderer-container' })
|
2025-02-04 10:43:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.renderer = this.rendererRegistry.prepareRender(config.renderer.toLowerCase(), config.rendererArguments)(rendererEl)
|
2025-01-08 19:58:14 +00:00
|
|
|
|
|
|
|
|
// FIXME: probably should save the one before transform and perform transform every time we execute it.
|
|
|
|
|
this.query = results.queryPart
|
|
|
|
|
await this.render()
|
|
|
|
|
} catch (e) {
|
|
|
|
|
displayError(this.el, e.toString())
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-02 15:23:44 +00:00
|
|
|
|
2025-01-08 19:58:14 +00:00
|
|
|
onunload() {
|
|
|
|
|
this.registrator.offAll()
|
2024-11-02 15:23:44 +00:00
|
|
|
}
|
|
|
|
|
|
2025-01-08 19:58:14 +00:00
|
|
|
async render() {
|
|
|
|
|
try {
|
2024-09-02 18:40:48 +00:00
|
|
|
|
2025-01-08 19:58:14 +00:00
|
|
|
const registeredTablesForContext = await this.sync.getTablesMappingForContext(this.ctx.sourcePath)
|
2024-09-02 18:40:48 +00:00
|
|
|
|
2025-01-15 17:48:33 +00:00
|
|
|
const res = transformQuery(this.query, registeredTablesForContext)
|
|
|
|
|
const transformedQuery = res.sql
|
2024-11-02 15:23:44 +00:00
|
|
|
|
2025-02-04 10:43:47 +00:00
|
|
|
if (this.flags.refresh) {
|
2025-01-30 13:16:52 +00:00
|
|
|
registerObservers({
|
|
|
|
|
bus: this.registrator,
|
|
|
|
|
callback: () => this.render(),
|
|
|
|
|
fileName: this.ctx.sourcePath,
|
|
|
|
|
tables: res.mappedTables
|
|
|
|
|
})
|
|
|
|
|
}
|
2024-11-07 18:43:15 +00:00
|
|
|
|
2025-01-08 19:58:14 +00:00
|
|
|
|
|
|
|
|
const file = this.app.vault.getFileByPath(this.ctx.sourcePath)
|
|
|
|
|
if (!file) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
const fileCache = this.app.metadataCache.getFileCache(file)
|
2025-02-04 10:43:47 +00:00
|
|
|
|
|
|
|
|
if (this.flags.explain) {
|
|
|
|
|
// Rendering explain
|
|
|
|
|
const result = await this.db.explain(transformedQuery, fileCache?.frontmatter ?? { })
|
|
|
|
|
this.explainEl.textContent = result
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-15 17:48:33 +00:00
|
|
|
const { data, columns } = await this.db.select(transformedQuery, fileCache?.frontmatter ?? {})
|
2025-01-08 19:58:14 +00:00
|
|
|
this.renderer.render({ data, columns })
|
|
|
|
|
} catch (e) {
|
|
|
|
|
this.renderer.error(e.toString())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-23 13:36:50 +00:00
|
|
|
async registerTables(tables: ParserTableDefinition[]) {
|
|
|
|
|
await Promise.all(tables.map((table) => this.sync.registerTable(table)))
|
2025-01-08 19:58:14 +00:00
|
|
|
}
|
2024-09-01 13:01:54 +00:00
|
|
|
}
|