chore: parse schema from url

This commit is contained in:
Yann Amsellem
2025-10-16 18:05:19 +02:00
parent 2a556357fa
commit 02b478733c

View File

@@ -4,6 +4,9 @@ import type { Events, ExecOptions, OLAPEngine, OLAPResponse, Table } from './ind
import CLICKHOUSE_GET_SCHEMA from './queries/clickhouse_get_schema.sql?raw';
import CLICKHOUSE_GET_UDFS from './queries/clickhouse_get_udfs.sql?raw';
const TABLE_PATTERN =
/^(?:[a-zA-Z_]+:[a-zA-Z_]+=[a-zA-Z()0-9]+(?:,[a-zA-Z_]+=[a-zA-Z()0-9]+)*;)*[a-zA-Z_]+:[a-zA-Z_]+=[a-zA-Z()0-9]+(?:,[a-zA-Z_]+=[a-zA-Z()0-9]+)*$/;
export class RemoteEngine extends InternalEventEmitter<Events> implements OLAPEngine {
readonly isAbortable = true;
@@ -31,9 +34,10 @@ export class RemoteEngine extends InternalEventEmitter<Events> implements OLAPEn
}
async getSchema() {
const customs = this.getCustomSchemaFromUrl();
const response = await this.exec(CLICKHOUSE_GET_SCHEMA, {}, false);
if (!response) return [];
return response.data as Table[];
if (!response) return customs;
return customs.concat(response.data as Table[]);
}
async getUDFs() {
@@ -42,6 +46,30 @@ export class RemoteEngine extends InternalEventEmitter<Events> implements OLAPEn
return response.data.map((row) => row.name as string);
}
private getCustomSchemaFromUrl(): Table[] {
const schema = new URLSearchParams(window.location.search).get('schema');
if (!schema) return [];
if (!TABLE_PATTERN.test(schema)) {
console.warn('Bad schema passed');
return [];
}
return schema.split(';').map((raw) => {
const [name, _columns] = raw.split(':');
return {
engine: 'custom',
name,
short: name,
url: '',
columns: _columns.split(',').map((_column) => {
const [name, type] = _column.split('=');
return { name, type };
})
};
});
}
}
interface RemoteEngineException {