mirror of
https://github.com/Kaelio/ktx.git
synced 2026-06-28 08:49:38 +02:00
refactor(historic-sql): route dialect support through driver registry
This commit is contained in:
parent
ad2471da74
commit
65cca7bd04
2 changed files with 37 additions and 4 deletions
|
|
@ -1,5 +1,9 @@
|
||||||
|
import { getDriverRegistration } from '../../../connections/drivers.js';
|
||||||
|
import type { KtxConnectionDriver } from '../../../scan/types.js';
|
||||||
import type { HistoricSqlDialect } from './types.js';
|
import type { HistoricSqlDialect } from './types.js';
|
||||||
|
|
||||||
|
const historicSqlDialects: readonly HistoricSqlDialect[] = ['postgres', 'bigquery', 'snowflake'];
|
||||||
|
|
||||||
function recordOrNull(value: unknown): Record<string, unknown> | null {
|
function recordOrNull(value: unknown): Record<string, unknown> | null {
|
||||||
return value && typeof value === 'object' && !Array.isArray(value) ? (value as Record<string, unknown>) : null;
|
return value && typeof value === 'object' && !Array.isArray(value) ? (value as Record<string, unknown>) : null;
|
||||||
}
|
}
|
||||||
|
|
@ -10,6 +14,14 @@ function queryHistoryRecord(connection: unknown): Record<string, unknown> | null
|
||||||
return context ? recordOrNull(context.queryHistory) : null;
|
return context ? recordOrNull(context.queryHistory) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function historicSqlDialectForDriver(driver: KtxConnectionDriver): HistoricSqlDialect {
|
||||||
|
const dialect = historicSqlDialects.find((candidate) => candidate === driver);
|
||||||
|
if (!dialect) {
|
||||||
|
throw new Error(`Driver "${driver}" is marked as historic-SQL capable but has no HistoricSqlDialect mapping.`);
|
||||||
|
}
|
||||||
|
return dialect;
|
||||||
|
}
|
||||||
|
|
||||||
export function isQueryHistoryEnabled(connection: unknown): boolean {
|
export function isQueryHistoryEnabled(connection: unknown): boolean {
|
||||||
return queryHistoryRecord(connection)?.enabled === true;
|
return queryHistoryRecord(connection)?.enabled === true;
|
||||||
}
|
}
|
||||||
|
|
@ -25,8 +37,6 @@ export function queryHistoryDialectForConnection(connection: unknown): HistoricS
|
||||||
}
|
}
|
||||||
const conn = recordOrNull(connection);
|
const conn = recordOrNull(connection);
|
||||||
const driver = String(conn?.driver ?? '').toLowerCase();
|
const driver = String(conn?.driver ?? '').toLowerCase();
|
||||||
if (driver === 'postgres') return 'postgres';
|
const registration = getDriverRegistration(driver);
|
||||||
if (driver === 'bigquery') return 'bigquery';
|
return registration?.hasHistoricSqlReader ? historicSqlDialectForDriver(registration.driver) : null;
|
||||||
if (driver === 'snowflake') return 'snowflake';
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,23 @@
|
||||||
|
import { describe, expect, it } from 'vitest';
|
||||||
|
import { queryHistoryDialectForConnection } from '../../../../../src/context/ingest/adapters/historic-sql/connection-dialect.js';
|
||||||
|
|
||||||
|
describe('queryHistoryDialectForConnection', () => {
|
||||||
|
it.each([
|
||||||
|
['postgres', 'postgres'],
|
||||||
|
['bigquery', 'bigquery'],
|
||||||
|
['snowflake', 'snowflake'],
|
||||||
|
] as const)('returns %s when query history is enabled', (driver, dialect) => {
|
||||||
|
expect(queryHistoryDialectForConnection({ driver, context: { queryHistory: { enabled: true } } })).toBe(dialect);
|
||||||
|
});
|
||||||
|
|
||||||
|
it.each(['sqlite', 'mysql', 'clickhouse', 'sqlserver'] as const)(
|
||||||
|
'returns null for %s because no historic-SQL reader is registered',
|
||||||
|
(driver) => {
|
||||||
|
expect(queryHistoryDialectForConnection({ driver, context: { queryHistory: { enabled: true } } })).toBeNull();
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
it('returns null when query history is disabled', () => {
|
||||||
|
expect(queryHistoryDialectForConnection({ driver: 'postgres', context: { queryHistory: { enabled: false } } })).toBeNull();
|
||||||
|
});
|
||||||
|
});
|
||||||
Loading…
Add table
Add a link
Reference in a new issue