chore: apply slugs for custom tables
This commit is contained in:
@@ -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';
|
||||
|
||||
19
src/lib/olap-engine/tables.ts
Normal file
19
src/lib/olap-engine/tables.ts
Normal 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;
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user