diff --git a/src/lib/olap-engine/index.ts b/src/lib/olap-engine/index.ts index 0cd4685..f2ba2a2 100644 --- a/src/lib/olap-engine/index.ts +++ b/src/lib/olap-engine/index.ts @@ -42,3 +42,5 @@ export const engine: OLAPEngine = PLATFORM === 'WEB' || (typeof window !== 'undefined' && window.location.search.includes('proxy=')) ? new RemoteEngine() : new LocalEngine(); + +export { applySlugs } from './tables'; diff --git a/src/lib/olap-engine/tables.ts b/src/lib/olap-engine/tables.ts new file mode 100644 index 0000000..4be63ed --- /dev/null +++ b/src/lib/olap-engine/tables.ts @@ -0,0 +1,19 @@ +interface Source { + slug: string; + path: string; +} + +export function applySlugs(query: string, sources: Source[]) { + for (const source of sources) { + query = query.replace(new RegExp(`(from|FROM)[ \n\t]+(${source.slug})`, 'g'), (match) => + match.replace(source.slug, `${source.path} ${source.slug}`) + ); + + query = query.replace( + new RegExp(`(describe|DESCRIBE)([ \n\t]+(table|TABLE))?[ \n\t]+(${source.slug})`, 'g'), + (match) => match.replace(source.slug, `${source.path}`) + ); + } + + return query; +} diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index c91b7fa..6e14264 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -32,7 +32,7 @@ import Sparkles from '$lib/icons/Sparkles.svelte'; import Stop from '$lib/icons/Stop.svelte'; import type { Table } from '$lib/olap-engine'; - import { engine, type OLAPResponse } from '$lib/olap-engine'; + import { applySlugs, engine, type OLAPResponse } from '$lib/olap-engine'; import { PanelState } from '$lib/PanelState.svelte'; import { SQLiteChatsRepository, type ChatsRepository } from '$lib/repositories/chats'; import { @@ -75,6 +75,10 @@ const query = currentTab.content; if (loading || !query) return; + const sources = tables + .filter((t) => t.engine === 'custom') + .map((t) => ({ slug: t.name, path: t.url })); + loading = true; counter?.start(); try { @@ -92,7 +96,7 @@ cached = false; abortController = new AbortController(); - response = await engine.exec(query, { signal: abortController.signal }); + response = await engine.exec(applySlugs(query, sources), { signal: abortController.signal }); await cache.set(query, response); } finally { loading = false;