chore: apply slugs for custom tables

This commit is contained in:
Yann Amsellem
2025-10-20 16:22:44 +02:00
parent 02b478733c
commit f1de325b51
3 changed files with 27 additions and 2 deletions

View File

@@ -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';

View File

@@ -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;
}

View File

@@ -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;