Skip to content

Commit e5d4428

Browse files
committed
fix: update custom tables replacement function
1 parent e6d1efd commit e5d4428

File tree

3 files changed

+56
-11
lines changed

3 files changed

+56
-11
lines changed

src/lib/olap-engine/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,4 @@ export const engine: OLAPEngine =
4343
? new RemoteEngine()
4444
: new LocalEngine();
4545

46-
export { applySlugs } from './tables';
46+
export { applyCustomTable } from './tables';

src/lib/olap-engine/tables.ts

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,60 @@ interface Source {
33
path: string;
44
}
55

6-
export function applySlugs(query: string, sources: Source[]) {
7-
for (const source of sources) {
8-
query = query.replace(new RegExp(`(from|FROM)[ \n\t]+(${source.slug})`, 'g'), (match) =>
9-
match.replace(source.slug, `${source.path} ${source.slug}`)
6+
const SQL_KEYWORDS = new Set([
7+
'WHERE',
8+
'ON',
9+
'GROUP',
10+
'ORDER',
11+
'LIMIT',
12+
'OFFSET',
13+
'HAVING',
14+
'SET',
15+
'LEFT',
16+
'RIGHT',
17+
'INNER',
18+
'OUTER',
19+
'CROSS',
20+
'JOIN',
21+
'UNION',
22+
'RETURNING',
23+
'VALUES',
24+
';'
25+
]);
26+
27+
export function applyCustomTable(query: string, sources: Source[]) {
28+
let newQuery = query;
29+
30+
for (const { slug, path } of sources) {
31+
const regex = new RegExp(
32+
`(\\bFROM\\b|\\bJOIN\\b|\\bUPDATE\\b|\\bINTO\\b)\\s+(?:(\\w+)\\.)?(${slug})\\b(\\s+(?:AS\\s+)?\\w+)?`,
33+
'gi'
34+
);
35+
36+
newQuery = newQuery.replace(
37+
regex,
38+
(_fullMatch, keyword, _schema, foundTable, potentialAliasChunk) => {
39+
let finalAliasPart = '';
40+
41+
if (potentialAliasChunk) {
42+
const rawAlias = potentialAliasChunk.trim().replace(/^AS\s+/i, '');
43+
if (SQL_KEYWORDS.has(rawAlias.toUpperCase())) {
44+
finalAliasPart = ` ${foundTable} ${potentialAliasChunk}`;
45+
} else {
46+
finalAliasPart = potentialAliasChunk;
47+
}
48+
} else {
49+
finalAliasPart = ` ${foundTable}`;
50+
}
51+
return `${keyword} ${path}${finalAliasPart}`;
52+
}
1053
);
1154

12-
query = query.replace(
13-
new RegExp(`(describe|DESCRIBE)([ \n\t]+(table|TABLE))?[ \n\t]+(${source.slug})`, 'g'),
14-
(match) => match.replace(source.slug, `${source.path}`)
55+
newQuery = newQuery.replace(
56+
new RegExp(`(DESCRIBE)\\s+(?:TABLE\\s+)?(${slug})\\b`, 'gi'),
57+
`$1 ${path}`
1558
);
1659
}
1760

18-
return query;
61+
return newQuery;
1962
}

src/routes/+page.svelte

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
import Sparkles from '$lib/icons/Sparkles.svelte';
3434
import Stop from '$lib/icons/Stop.svelte';
3535
import type { Table } from '$lib/olap-engine';
36-
import { applySlugs, engine, type OLAPResponse } from '$lib/olap-engine';
36+
import { applyCustomTable, engine, type OLAPResponse } from '$lib/olap-engine';
3737
import { SQLiteChatsRepository, type ChatsRepository } from '$lib/repositories/chats';
3838
import {
3939
SQLiteHistoryRepository,
@@ -100,7 +100,9 @@
100100
101101
cached = false;
102102
abortController = new AbortController();
103-
response = await engine.exec(applySlugs(query, sources), { signal: abortController.signal });
103+
response = await engine.exec(applyCustomTable(query, sources), {
104+
signal: abortController.signal
105+
});
104106
await cache.set(query, response);
105107
} finally {
106108
loading = false;

0 commit comments

Comments
 (0)